From 4706d037e2373e502fc394e4779231589dc2ab84 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Thu, 8 Jul 2021 14:33:28 -0400 Subject: [PATCH 1/2] fix: (MLAPI.Serialization) 'specified cast is not valid.' on NetworkWriter. This source code has Illegal cast some network components To GameObject. So, fix it to get the GameObject from the component. Contribution from https://github.com/0vjm3wfw9gqm4j --- .../Runtime/Serialization/NetworkWriter.cs | 4 ++-- .../Tests/Editor/NetworkSerializerTests.cs | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/com.unity.multiplayer.mlapi/Runtime/Serialization/NetworkWriter.cs b/com.unity.multiplayer.mlapi/Runtime/Serialization/NetworkWriter.cs index 591b7b1940..ef160b1e8f 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Serialization/NetworkWriter.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Serialization/NetworkWriter.cs @@ -210,7 +210,7 @@ public void WriteObjectPacked(object value) { if (!((NetworkObject)value).IsSpawned) { - throw new ArgumentException($"{nameof(NetworkWriter)} cannot write {nameof(NetworkObject)} types that are not spawned. {nameof(GameObject)}: {((GameObject)value).name}"); + throw new ArgumentException($"{nameof(NetworkWriter)} cannot write {nameof(NetworkObject)} types that are not spawned. {nameof(GameObject)}: {((NetworkObject)value).gameObject.name}"); } WriteUInt64Packed(((NetworkObject)value).NetworkObjectId); @@ -220,7 +220,7 @@ public void WriteObjectPacked(object value) { if (!((NetworkBehaviour)value).HasNetworkObject || !((NetworkBehaviour)value).NetworkObject.IsSpawned) { - throw new ArgumentException($"{nameof(NetworkWriter)} cannot write {nameof(NetworkBehaviour)} types that are not spawned. {nameof(GameObject)}: {((GameObject)value).name}"); + throw new ArgumentException($"{nameof(NetworkWriter)} cannot write {nameof(NetworkBehaviour)} types that are not spawned. {nameof(GameObject)}: {((NetworkBehaviour)value).gameObject.name}"); } WriteUInt64Packed(((NetworkBehaviour)value).NetworkObjectId); diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs index d514ffbce4..2594260647 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs @@ -8,6 +8,29 @@ namespace MLAPI.EditorTests { public class NetworkSerializerTests { + [Test] + public void SerializeUnspawnedNetworkObject() + { + var gameObject = new GameObject(nameof(SerializeUnspawnedNetworkObject)); + var networkObject = gameObject.AddComponent(); + + try + { + using (var outStream = PooledNetworkBuffer.Get()) + using (var outWriter = PooledNetworkWriter.Get(outStream)) + { + outWriter.WriteObjectPacked(networkObject); + } + + // we expect the exception below + Assert.Fail(); + } + catch(System.ArgumentException exception) + { + Assert.True(exception.Message.IndexOf("NetworkWriter cannot write NetworkObject types that are not spawned") != -1); + } + } + [Test] public void SerializeBool() { From daa11f844199196e54ee3ee798715f6294826fa3 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Thu, 8 Jul 2021 14:53:39 -0400 Subject: [PATCH 2/2] style: tests, simplifying name of exception --- .../Tests/Editor/NetworkSerializerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs index 2594260647..88a14c153b 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkSerializerTests.cs @@ -25,7 +25,7 @@ public void SerializeUnspawnedNetworkObject() // we expect the exception below Assert.Fail(); } - catch(System.ArgumentException exception) + catch(ArgumentException exception) { Assert.True(exception.Message.IndexOf("NetworkWriter cannot write NetworkObject types that are not spawned") != -1); }