Skip to content

Commit 72e89a0

Browse files
committed
BehaviourSerializing & BehaviourSerialized Mirror events
1 parent a5ed8a7 commit 72e89a0

5 files changed

Lines changed: 138 additions & 6 deletions

File tree

LabExtended/Events/MirrorEvents.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using LabExtended.Events.Mirror;
1+
using LabExtended.Core;
22
using LabExtended.Extensions;
33

4+
using LabExtended.Events.Mirror;
5+
46
using Mirror;
57

68
#pragma warning disable CS8604 // Possible null reference argument.
@@ -12,6 +14,22 @@ namespace LabExtended.Events;
1214
/// </summary>
1315
public 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
}

LabExtended/Patches/Events/Mirror/AddObserverPatch.cs renamed to LabExtended/Patches/Events/Mirror/MirrorAddObserverPatch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LabExtended.Patches.Events.Mirror;
1010
/// <summary>
1111
/// Implements the <see cref="MirrorEvents.AddingObserver"/> and <see cref="MirrorEvents.AddedObserver"/> events.
1212
/// </summary>
13-
public static class AddObserverPatch
13+
public static class MirrorAddObserverPatch
1414
{
1515
[HarmonyPatch(typeof(NetworkIdentity), nameof(NetworkIdentity.AddObserver))]
1616
private static bool Prefix(NetworkIdentity __instance, NetworkConnectionToClient conn)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using HarmonyLib;
2+
3+
using LabExtended.Events;
4+
5+
using Mirror;
6+
7+
namespace LabExtended.Patches.Events.Mirror
8+
{
9+
/// <summary>
10+
/// Implements the <see cref="MirrorEvents.BehaviourSerializing"/> and <see cref="MirrorEvents.BehaviourSerialized"/> events.
11+
/// </summary>
12+
public static class MirrorBehaviourSerializePatch
13+
{
14+
[HarmonyPatch(typeof(NetworkIdentity), nameof(NetworkIdentity.SerializeServer))]
15+
private static bool Prefix(NetworkIdentity __instance, bool initialState, NetworkWriter ownerWriter, NetworkWriter observersWriter)
16+
{
17+
__instance.ValidateComponents();
18+
19+
var behaviours = __instance.NetworkBehaviours;
20+
var dirtyBits = __instance.ServerDirtyMasks(initialState);
21+
22+
var ownerDirtyBits = dirtyBits.Item1;
23+
var observersDirtyBits = dirtyBits.Item2;
24+
25+
if (ownerDirtyBits != 0)
26+
Compression.CompressVarUInt(ownerWriter, ownerDirtyBits);
27+
28+
if (observersDirtyBits != 0)
29+
Compression.CompressVarUInt(observersWriter, observersDirtyBits);
30+
31+
if (ownerDirtyBits != 0 || observersDirtyBits != 0)
32+
{
33+
for (var i = 0; i < behaviours.Length; i++)
34+
{
35+
var behaviour = behaviours[i];
36+
37+
var isOwnerDirty = NetworkIdentity.IsDirty(ownerDirtyBits, i);
38+
var isObserversDirty = NetworkIdentity.IsDirty(observersDirtyBits, i);
39+
40+
if (isOwnerDirty || isObserversDirty)
41+
{
42+
using var writer = NetworkWriterPool.Get();
43+
44+
if (MirrorEvents.OnBehaviourSerializing(behaviour, writer))
45+
behaviour.Serialize(writer, initialState);
46+
47+
var segment = writer.ToArraySegment();
48+
49+
if (isOwnerDirty)
50+
ownerWriter.WriteBytes(segment.Array, segment.Offset, segment.Count);
51+
52+
if (isObserversDirty)
53+
observersWriter.WriteBytes(segment.Array, segment.Offset, segment.Count);
54+
55+
MirrorEvents.OnBehaviourSerialized(behaviour, writer);
56+
}
57+
58+
if (!initialState)
59+
behaviour.ClearAllDirtyBits();
60+
}
61+
}
62+
63+
return false;
64+
}
65+
}
66+
}

LabExtended/Patches/Functions/Networking/NetworkDestroyPatch.cs renamed to LabExtended/Patches/Events/Mirror/MirrorDestroyPatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
using UnityEngine;
99

10-
namespace LabExtended.Patches.Functions.Networking
10+
namespace LabExtended.Patches.Events.Mirror
1111
{
1212
/// <summary>
1313
/// Provides the <see cref="MirrorEvents.Destroying"/> and <see cref="MirrorEvents.Destroyed"/> patches.
1414
/// </summary>
15-
public static class NetworkDestroyPatch
15+
public static class MirrorDestroyPatch
1616
{
1717
[HarmonyPatch(typeof(NetworkServer), nameof(NetworkServer.DestroyObject), typeof(NetworkIdentity), typeof(NetworkServer.DestroyMode))]
1818
private static bool Prefix(NetworkIdentity identity, NetworkServer.DestroyMode mode)

LabExtended/Patches/Functions/Networking/NetworkSpawnPatch.cs renamed to LabExtended/Patches/Events/Mirror/MirrorSpawnPatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
using UnityEngine;
1010

11-
namespace LabExtended.Patches.Functions.Networking
11+
namespace LabExtended.Patches.Events.Mirror
1212
{
1313
/// <summary>
1414
/// Provides the <see cref="MirrorEvents.Spawning"/> and <see cref="MirrorEvents.Spawned"/> events.
1515
/// </summary>
16-
public static class NetworkSpawnPatch
16+
public static class MirrorSpawnPatch
1717
{
1818
[HarmonyPatch(typeof(NetworkServer), nameof(NetworkServer.SpawnObject), typeof(GameObject), typeof(NetworkConnection))]
1919
private static bool Prefix(GameObject obj, NetworkConnection ownerConnection)

0 commit comments

Comments
 (0)