Skip to content

fix: (MLAPI.Serialization) 'specified cast is not valid.' on NetworkW… #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ namespace MLAPI.EditorTests
{
public class NetworkSerializerTests
{
[Test]
public void SerializeUnspawnedNetworkObject()
{
var gameObject = new GameObject(nameof(SerializeUnspawnedNetworkObject));
var networkObject = gameObject.AddComponent<NetworkObject>();

try
{
using (var outStream = PooledNetworkBuffer.Get())
using (var outWriter = PooledNetworkWriter.Get(outStream))
{
outWriter.WriteObjectPacked(networkObject);
}

// we expect the exception below
Assert.Fail();
}
catch(ArgumentException exception)
{
Assert.True(exception.Message.IndexOf("NetworkWriter cannot write NetworkObject types that are not spawned") != -1);
}
}

[Test]
public void SerializeBool()
{
Expand Down