test: add test for syncvar synchronization (#1019)

* test: add test for syncvar synchronization

* made methods internal and expose internal to the tests
This commit is contained in:
Paul Pacheco 2019-08-10 15:32:27 -07:00 committed by GitHub
parent f27fd0bdc5
commit 236afa3c7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 9 deletions

View File

@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Mirror.Tests")]

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e28d5f410e25b42e6a76a2ffc10e4675
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -677,19 +677,19 @@ bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initial
// -> returns true if something was written // -> returns true if something was written
internal bool OnSerializeAllSafely(bool initialState, NetworkWriter writer) 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"); Debug.LogError("Only 64 NetworkBehaviour components are allowed for NetworkIdentity: " + name + " because of the dirtyComponentMask");
return false; return false;
} }
ulong dirtyComponentsMask = GetDirtyMask(networkBehavioursCache, initialState); ulong dirtyComponentsMask = GetDirtyMask(NetworkBehaviours, initialState);
if (dirtyComponentsMask == 0L) if (dirtyComponentsMask == 0L)
return false; return false;
writer.WritePackedUInt64(dirtyComponentsMask); // WritePacked64 so we don't write full 8 bytes if we don't have to 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? // is this component dirty?
// -> always serialize if initialState so all components are included in spawn packet // -> 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 // read component dirty mask
ulong dirtyComponentsMask = reader.ReadPackedUInt64(); ulong dirtyComponentsMask = reader.ReadPackedUInt64();
var components = NetworkBehaviours;
// loop through all components and deserialize the dirty ones // loop through all components and deserialize the dirty ones
for (int i = 0; i < components.Length; ++i) 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) internal void OnUpdateVars(NetworkReader reader, bool initialState)
{ {
OnDeserializeAllSafely(NetworkBehaviours, reader, initialState); OnDeserializeAllSafely(reader, initialState);
} }
internal void SetLocalPlayer() internal void SetLocalPlayer()

View File

@ -16,10 +16,6 @@ public struct Guild
[SyncVar] [SyncVar]
public Guild guild; public Guild guild;
public bool IsDirty()
{
return base.syncVarDirtyBits != 0;
}
} }
@ -34,6 +30,9 @@ public void TestSettingStruct()
MockPlayer player = gameObject.AddComponent<MockPlayer>(); MockPlayer player = gameObject.AddComponent<MockPlayer>();
// synchronize immediatelly
player.syncInterval = 0f;
Assert.That(player.IsDirty(), Is.False, "First time object should not be dirty"); Assert.That(player.IsDirty(), Is.False, "First time object should not be dirty");
MockPlayer.Guild myGuild = new MockPlayer.Guild MockPlayer.Guild myGuild = new MockPlayer.Guild
@ -52,5 +51,34 @@ public void TestSettingStruct()
Assert.That(player.IsDirty(), "Clearing struct should mark object as dirty"); 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<NetworkIdentity>();
MockPlayer player1 = gameObject1.AddComponent<MockPlayer>();
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<NetworkIdentity>();
MockPlayer player2 = gameObject2.AddComponent<MockPlayer>();
// 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");
}
} }
} }