Ejemplo n.º 1
0
        public void OnDeserializeSafelyShouldDetectAndHandleDeSerializationMismatch()
        {
            // add components
            SerializeTest1NetworkBehaviour    comp1    = gameObject.AddComponent <SerializeTest1NetworkBehaviour>();
            SerializeMismatchNetworkBehaviour compMiss = gameObject.AddComponent <SerializeMismatchNetworkBehaviour>();
            SerializeTest2NetworkBehaviour    comp2    = gameObject.AddComponent <SerializeTest2NetworkBehaviour>();

            // set some unique values to serialize
            comp1.value = 12345;
            comp2.value = "67890";

            // serialize
            var ownerWriter     = new NetworkWriter();
            var observersWriter = new NetworkWriter();

            (int ownerWritten, int observersWritten) = identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all
            var reader = new NetworkReader(ownerWriter.ToArray());

            Assert.Throws <InvalidMessageException>(() =>
            {
                identity.OnDeserializeAllSafely(reader, true);
            });
        }
        public void OnSerializeAndDeserializeAllSafely()
        {
            // create a networkidentity with our test components
            GameObject      gameObject = new GameObject();
            NetworkIdentity identity   = gameObject.AddComponent <NetworkIdentity>();
            SerializeTest1NetworkBehaviour     comp1   = gameObject.AddComponent <SerializeTest1NetworkBehaviour>();
            SerializeExceptionNetworkBehaviour compExc = gameObject.AddComponent <SerializeExceptionNetworkBehaviour>();
            SerializeTest2NetworkBehaviour     comp2   = gameObject.AddComponent <SerializeTest2NetworkBehaviour>();

            // set some unique values to serialize
            comp1.value      = 12345;
            comp1.syncMode   = SyncMode.Observers;
            compExc.syncMode = SyncMode.Observers;
            comp2.value      = "67890";
            comp2.syncMode   = SyncMode.Owner;

            // serialize all - should work even if compExc throws an exception
            NetworkWriter ownerWriter     = new NetworkWriter();
            NetworkWriter observersWriter = new NetworkWriter();

            LogAssert.ignoreFailingMessages = true; // error log because of the exception is expected
            identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);
            LogAssert.ignoreFailingMessages = false;

            // owner should have written all components
            Assert.That(ownerWritten, Is.EqualTo(3));

            // observers should have written only the observers components
            Assert.That(observersWritten, Is.EqualTo(2));

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all for owner - should work even if compExc throws an exception
            NetworkReader reader = new NetworkReader(ownerWriter.ToArray());

            LogAssert.ignoreFailingMessages = true; // error log because of the exception is expected
            identity.OnDeserializeAllSafely(reader, true);
            LogAssert.ignoreFailingMessages = false;
            Assert.That(comp1.value, Is.EqualTo(12345));
            Assert.That(comp2.value, Is.EqualTo("67890"));

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all for observers - should work even if compExc throws an exception
            reader = new NetworkReader(observersWriter.ToArray());
            LogAssert.ignoreFailingMessages = true; // error log because of the exception is expected
            identity.OnDeserializeAllSafely(reader, true);
            LogAssert.ignoreFailingMessages = false;
            Assert.That(comp1.value, Is.EqualTo(12345)); // observers mode, should be in data
            Assert.That(comp2.value, Is.EqualTo(null));  // owner mode, should not be in data

            // clean up
            GameObject.DestroyImmediate(gameObject);
        }
Ejemplo n.º 3
0
        public void OnSerializeAndDeserializeAllSafely()
        {
            // create a networkidentity with our test components
            SerializeTest1NetworkBehaviour     comp1   = gameObject.AddComponent <SerializeTest1NetworkBehaviour>();
            SerializeExceptionNetworkBehaviour compExc = gameObject.AddComponent <SerializeExceptionNetworkBehaviour>();
            SerializeTest2NetworkBehaviour     comp2   = gameObject.AddComponent <SerializeTest2NetworkBehaviour>();

            // set some unique values to serialize
            comp1.value      = 12345;
            comp1.syncMode   = SyncMode.Observers;
            compExc.syncMode = SyncMode.Observers;
            comp2.value      = "67890";
            comp2.syncMode   = SyncMode.Owner;

            // serialize all
            var ownerWriter     = new NetworkWriter();
            var observersWriter = new NetworkWriter();

            // serialize should propagate exceptions
            Assert.Throws <Exception>(() =>
            {
                (int ownerWritten, int observersWritten) = identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
                // owner should have written all components
                Assert.That(ownerWritten, Is.EqualTo(3));
                // observers should have written only the observers components
                Assert.That(observersWritten, Is.EqualTo(2));
            });

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all for owner - should work even if compExc throws an exception
            var reader = new NetworkReader(ownerWriter.ToArray());

            Assert.Throws <Exception>(() =>
            {
                identity.OnDeserializeAllSafely(reader, true);
            });

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all for observers - should propagate exceptions
            reader = new NetworkReader(observersWriter.ToArray());
            Assert.Throws <Exception>(() =>
            {
                identity.OnDeserializeAllSafely(reader, true);
            });
        }
        public void OnDeserializeSafelyShouldDetectAndHandleDeSerializationMismatch()
        {
            // create a networkidentity with our test components
            GameObject      gameObject = new GameObject();
            NetworkIdentity identity   = gameObject.AddComponent <NetworkIdentity>();
            SerializeTest1NetworkBehaviour    comp1    = gameObject.AddComponent <SerializeTest1NetworkBehaviour>();
            SerializeMismatchNetworkBehaviour compMiss = gameObject.AddComponent <SerializeMismatchNetworkBehaviour>();
            SerializeTest2NetworkBehaviour    comp2    = gameObject.AddComponent <SerializeTest2NetworkBehaviour>();

            // set some unique values to serialize
            comp1.value = 12345;
            comp2.value = "67890";

            // serialize
            NetworkWriter ownerWriter     = new NetworkWriter();
            NetworkWriter observersWriter = new NetworkWriter();

            identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten);

            // reset component values
            comp1.value = 0;
            comp2.value = null;

            // deserialize all
            NetworkReader reader = new NetworkReader(ownerWriter.ToArray());

            LogAssert.ignoreFailingMessages = true; // warning log because of serialization mismatch
            identity.OnDeserializeAllSafely(reader, true);
            LogAssert.ignoreFailingMessages = false;

            // the mismatch component will fail, but the one before and after
            // should still work fine. that's the whole point.
            Assert.That(comp1.value, Is.EqualTo(12345));
            Assert.That(comp2.value, Is.EqualTo("67890"));

            // clean up
            GameObject.DestroyImmediate(gameObject);
        }
        public void OnSerializeAllSafely()
        {
            // create a networkidentity with our test components
            SerializeTest1NetworkBehaviour     comp1   = gameObject.AddComponent <SerializeTest1NetworkBehaviour>();
            SerializeExceptionNetworkBehaviour compExc = gameObject.AddComponent <SerializeExceptionNetworkBehaviour>();
            SerializeTest2NetworkBehaviour     comp2   = gameObject.AddComponent <SerializeTest2NetworkBehaviour>();

            // set some unique values to serialize
            comp1.value      = 12345;
            comp1.syncMode   = SyncMode.Observers;
            compExc.syncMode = SyncMode.Observers;
            comp2.value      = "67890";
            comp2.syncMode   = SyncMode.Owner;

            // serialize all
            var ownerWriter     = new NetworkWriter();
            var observersWriter = new NetworkWriter();

            // serialize should propagate exceptions
            Assert.Throws <Exception>(() =>
            {
                identity.OnSerializeAllSafely(true, ownerWriter, observersWriter);
            });
        }