1- using LabExtended . Events . Mirror ;
1+ using LabExtended . Core ;
22using LabExtended . Extensions ;
33
4+ using LabExtended . Events . Mirror ;
5+
46using Mirror ;
57
68#pragma warning disable CS8604 // Possible null reference argument.
@@ -12,6 +14,22 @@ namespace LabExtended.Events;
1214/// </summary>
1315public static class MirrorEvents
1416{
17+ /// <summary>
18+ /// Represents the method that handles serialization events for a NetworkBehaviour, providing access to the
19+ /// behaviour and the serialized data writer.
20+ /// </summary>
21+ /// <param name="behaviour">The NetworkBehaviour instance being serialized. Cannot be null.</param>
22+ /// <param name="serializedData">The NetworkWriter used to write the serialized data for the behaviour. The handler should write any custom data
23+ /// to this writer as needed.</param>
24+ public delegate void BehaviourSerializingEventHandler ( NetworkBehaviour behaviour , NetworkWriter serializedData , ref bool serializeBehaviour ) ;
25+
26+ /// <summary>
27+ /// Represents the method that handles the event raised when a NetworkBehaviour is serialized.
28+ /// </summary>
29+ /// <param name="behaviour">The NetworkBehaviour instance that is being serialized.</param>
30+ /// <param name="serializedData">A NetworkWriter containing the serialized data of the behaviour.</param>
31+ public delegate void BehaviourSerializedEventHandler ( NetworkBehaviour behaviour , NetworkWriter serializedData ) ;
32+
1533 /// <summary>
1634 /// Called before a <see cref="NetworkIdentity"/> gets destroyed.
1735 /// </summary>
@@ -38,6 +56,16 @@ public static class MirrorEvents
3856 /// <inheritdoc cref="MirrorAddedObserverEventArgs"/>
3957 public static event Action < MirrorAddedObserverEventArgs > ? AddedObserver ;
4058
59+ /// <summary>
60+ /// Gets called before serialized data of a behaviour is written to the identity writer.
61+ /// </summary>
62+ public static event BehaviourSerializingEventHandler ? BehaviourSerializing ;
63+
64+ /// <summary>
65+ /// Gets called after serialized data of a behaviour is written to the identity writer.
66+ /// </summary>
67+ public static event BehaviourSerializedEventHandler ? BehaviourSerialized ;
68+
4169 /// <summary>
4270 /// Invokes the <see cref="Destroying"/> event.
4371 /// </summary>
@@ -80,4 +108,42 @@ public static bool OnAddingObserver(MirrorAddingObserverEventArgs args)
80108 /// <param name="args">The event arguments.</param>
81109 public static void OnAddedObserver ( MirrorAddedObserverEventArgs args )
82110 => AddedObserver . InvokeEvent ( args ) ;
111+
112+ /// <summary>
113+ /// Invokes the <see cref="BehaviourSerializing"/> event.
114+ /// </summary>
115+ /// <param name="behaviour">The behaviour being serialized.</param>
116+ /// <param name="writer">The target network writer.</param>
117+ public static bool OnBehaviourSerializing ( NetworkBehaviour behaviour , NetworkWriter writer )
118+ {
119+ try
120+ {
121+ var serializeBehaviour = true ;
122+
123+ BehaviourSerializing ? . Invoke ( behaviour , writer , ref serializeBehaviour ) ;
124+ return serializeBehaviour ;
125+ }
126+ catch ( Exception ex )
127+ {
128+ ApiLog . Error ( "OnBehaviourSerializing" , ex ) ;
129+ return true ;
130+ }
131+ }
132+
133+ /// <summary>
134+ /// Invokes the <see cref="BehaviourSerialized"/> event.
135+ /// </summary>
136+ /// <param name="behaviour">The behaviour being serialized.</param>
137+ /// <param name="writer">The target network writer.</param>
138+ public static void OnBehaviourSerialized ( NetworkBehaviour behaviour , NetworkWriter writer )
139+ {
140+ try
141+ {
142+ BehaviourSerialized ? . Invoke ( behaviour , writer ) ;
143+ }
144+ catch ( Exception ex )
145+ {
146+ ApiLog . Error ( "OnBehaviourSerializing" , ex ) ;
147+ }
148+ }
83149}
0 commit comments