Skip to content

Commit

Permalink
fix: Message base class not being Serialized if processed in the wron…
Browse files Browse the repository at this point in the history
…g order (#2023)

* finding all fields

* adding test for message with base

* undoing FindAllPublicFields

* adding tests for Inheritance with define order

* making sure that messages are processed in order

do not need to check if message has already been processed becuase
OnSerialize is only added/changed if it is missing or empty

* removing un-needed comments

* using recursion instead of loops

* remove white space
  • Loading branch information
James-Frowen committed Jul 5, 2020
1 parent 264f9b8 commit 3418fa2
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,21 @@ static bool WeaveMessage(TypeDefinition td)

if (td.ImplementsInterface(IMessageBaseType))
{
// process this and base classes from parent to child order

try
{
TypeDefinition parent = td.BaseType.Resolve();
// process parent
WeaveMessage(parent);
}
catch (AssemblyResolutionException)
{
// this can happen for plugins.
//Console.WriteLine("AssemblyResolutionException: "+ ex.ToString());
}

// process this
MessageClassProcessor.Process(td);
modified = true;
}
Expand Down
104 changes: 96 additions & 8 deletions Assets/Mirror/Tests/Editor/MessageInheritanceTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NUnit.Framework;

namespace Mirror.Tests
namespace Mirror.Tests.MessageInheritance
{
class ParentMessage : MessageBase
{
Expand All @@ -12,28 +12,116 @@ class ChildMessage : ParentMessage
public int childValue;
}


public abstract class RequestMessageBase : MessageBase
{
public int responseId = 0;
}
public class ResponseMessage : RequestMessageBase
{
public int state;
public string message = "";
public int errorCode = 0; // optional for error codes
}

//reverseOrder to test this https://github.com/vis2k/Mirror/issues/1925
public class ResponseMessageReverse : RequestMessageBaseReverse
{
public int state;
public string message = "";
public int errorCode = 0; // optional for error codes
}
public abstract class RequestMessageBaseReverse : MessageBase
{
public int responseId = 0;
}

[TestFixture]
public class MessageInheritanceTests
public class MessageInheritanceTest
{
[Test]
public void Roundtrip()
public void SendsVauesInParentAndChildClass()
{
NetworkWriter w = new NetworkWriter();
NetworkWriter writer = new NetworkWriter();

w.WriteMessage(new ChildMessage
writer.WriteMessage(new ChildMessage
{
parentValue = 3,
childValue = 4
});

byte[] arr = w.ToArray();
byte[] arr = writer.ToArray();

NetworkReader r = new NetworkReader(arr);
NetworkReader reader = new NetworkReader(arr);
ChildMessage received = new ChildMessage();
received.Deserialize(r);
received.Deserialize(reader);

Assert.AreEqual(3, received.parentValue);
Assert.AreEqual(4, received.childValue);

int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}

[Test]
public void SendsVauesWhenUsingAbstractClass()
{
NetworkWriter writer = new NetworkWriter();

const int state = 2;
const string message = "hello world";
const int responseId = 5;
writer.WriteMessage(new ResponseMessage
{
state = state,
message = message,
responseId = responseId,
});

byte[] arr = writer.ToArray();

NetworkReader reader = new NetworkReader(arr);
ResponseMessage received = new ResponseMessage();
received.Deserialize(reader);

Assert.AreEqual(state, received.state);
Assert.AreEqual(message, received.message);
Assert.AreEqual(responseId, received.responseId);

int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}

[Test]
public void SendsVauesWhenUsingAbstractClassReverseDefineOrder()
{
NetworkWriter writer = new NetworkWriter();

const int state = 2;
const string message = "hello world";
const int responseId = 5;
writer.WriteMessage(new ResponseMessageReverse
{
state = state,
message = message,
responseId = responseId,
});

byte[] arr = writer.ToArray();

NetworkReader reader = new NetworkReader(arr);
ResponseMessageReverse received = new ResponseMessageReverse();
received.Deserialize(reader);

Assert.AreEqual(state, received.state);
Assert.AreEqual(message, received.message);
Assert.AreEqual(responseId, received.responseId);

int writeLength = writer.Length;
int readLength = reader.Position;
Assert.That(writeLength == readLength, $"OnSerializeAll and OnDeserializeAll calls write the same amount of data\n writeLength={writeLength}\n readLength={readLength}");
}
}
}
1 change: 1 addition & 0 deletions Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="WeaverMessageTests~\MessageMemberInterface.cs" />
<Compile Include="WeaverMessageTests~\MessageSelfReferencing.cs" />
<Compile Include="WeaverMessageTests~\MessageValid.cs" />
<Compile Include="WeaverMessageTests~\MessageWithBaseClass.cs" />
<Compile Include="WeaverMonoBehaviourTests~\MonoBehaviourClient.cs" />
<Compile Include="WeaverMonoBehaviourTests~\MonoBehaviourClientCallback.cs" />
<Compile Include="WeaverMonoBehaviourTests~\MonoBehaviourClientRpc.cs" />
Expand Down
6 changes: 6 additions & 0 deletions Assets/Mirror/Tests/Editor/Weaver/WeaverMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public void MessageValid()
Assert.That(weaverErrors, Is.Empty);
}

[Test]
public void MessageWithBaseClass()
{
Assert.That(weaverErrors, Is.Empty);
}

[Test]
public void MessageSelfReferencing()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Mirror;
using UnityEngine;

namespace WeaverMessageTests.MessageWithBaseClass
{
class MessageWithBaseClass : SomeBaseMessage
{
public uint netId;
public Guid assetId;
public Vector3 position;
public Quaternion rotation;
public byte[] payload;
}

class SomeBaseMessage : MessageBase
{
public int myExtraType;
}
}

0 comments on commit 3418fa2

Please sign in to comment.