diff --git a/Assets/Mirror/Runtime/AssemblyInfo.cs b/Assets/Mirror/Runtime/AssemblyInfo.cs new file mode 100644 index 000000000..f0c485814 --- /dev/null +++ b/Assets/Mirror/Runtime/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Mirror.Tests")] \ No newline at end of file diff --git a/Assets/Mirror/Runtime/AssemblyInfo.cs.meta b/Assets/Mirror/Runtime/AssemblyInfo.cs.meta new file mode 100644 index 000000000..cf3201c61 --- /dev/null +++ b/Assets/Mirror/Runtime/AssemblyInfo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e28d5f410e25b42e6a76a2ffc10e4675 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index aefcab778..44f48ba4a 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -677,19 +677,19 @@ bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initial // -> returns true if something was written internal bool OnSerializeAllSafely(bool initialState, NetworkWriter writer) { - if (networkBehavioursCache.Length > 64) + if (NetworkBehaviours.Length > 64) { Debug.LogError("Only 64 NetworkBehaviour components are allowed for NetworkIdentity: " + name + " because of the dirtyComponentMask"); return false; } - ulong dirtyComponentsMask = GetDirtyMask(networkBehavioursCache, initialState); + ulong dirtyComponentsMask = GetDirtyMask(NetworkBehaviours, initialState); if (dirtyComponentsMask == 0L) return false; writer.WritePackedUInt64(dirtyComponentsMask); // WritePacked64 so we don't write full 8 bytes if we don't have to - foreach (NetworkBehaviour comp in networkBehavioursCache) + foreach (NetworkBehaviour comp in NetworkBehaviours) { // is this component dirty? // -> always serialize if initialState so all components are included in spawn packet @@ -761,11 +761,12 @@ void OnDeserializeSafely(NetworkBehaviour comp, NetworkReader reader, bool initi } } - void OnDeserializeAllSafely(NetworkBehaviour[] components, NetworkReader reader, bool initialState) + internal void OnDeserializeAllSafely(NetworkReader reader, bool initialState) { // read component dirty mask ulong dirtyComponentsMask = reader.ReadPackedUInt64(); + var components = NetworkBehaviours; // loop through all components and deserialize the dirty ones for (int i = 0; i < components.Length; ++i) { @@ -834,7 +835,7 @@ internal void HandleRPC(int componentIndex, int rpcHash, NetworkReader reader) internal void OnUpdateVars(NetworkReader reader, bool initialState) { - OnDeserializeAllSafely(NetworkBehaviours, reader, initialState); + OnDeserializeAllSafely(reader, initialState); } internal void SetLocalPlayer() diff --git a/Assets/Mirror/Tests/SyncVarTest.cs b/Assets/Mirror/Tests/SyncVarTest.cs index 63e58ee9f..31d54227b 100644 --- a/Assets/Mirror/Tests/SyncVarTest.cs +++ b/Assets/Mirror/Tests/SyncVarTest.cs @@ -16,10 +16,6 @@ public struct Guild [SyncVar] public Guild guild; - public bool IsDirty() - { - return base.syncVarDirtyBits != 0; - } } @@ -34,6 +30,9 @@ public void TestSettingStruct() MockPlayer player = gameObject.AddComponent(); + // synchronize immediatelly + player.syncInterval = 0f; + Assert.That(player.IsDirty(), Is.False, "First time object should not be dirty"); MockPlayer.Guild myGuild = new MockPlayer.Guild @@ -52,5 +51,34 @@ public void TestSettingStruct() Assert.That(player.IsDirty(), "Clearing struct should mark object as dirty"); } + [Test] + public void TestSynchronizingObjects() + { + // set up a "server" object + GameObject gameObject1 = new GameObject(); + NetworkIdentity identity1 = gameObject1.AddComponent(); + MockPlayer player1 = gameObject1.AddComponent(); + MockPlayer.Guild myGuild = new MockPlayer.Guild + { + name = "Back street boys" + }; + player1.guild = myGuild; + + // serialize all the data as we would for the network + NetworkWriter writer = new NetworkWriter(); + identity1.OnSerializeAllSafely(true, writer); + + // set up a "client" object + GameObject gameObject2 = new GameObject(); + NetworkIdentity identity2 = gameObject2.AddComponent(); + MockPlayer player2 = gameObject2.AddComponent(); + + // apply all the data from the server object + NetworkReader reader = new NetworkReader(writer.ToArray()); + identity2.OnDeserializeAllSafely(reader, true); + + // check that the syncvars got updated + Assert.That(player2.guild.name, Is.EqualTo("Back street boys"), "Data should be synchronized"); + } } }