Skip to content

Commit

Permalink
fix: Nested messages (#2148)
Browse files Browse the repository at this point in the history
* test for nested messages

* fixing StackOverflow with nested types
  • Loading branch information
James-Frowen committed Aug 9, 2020
1 parent fe3bebc commit e4a5ce7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Assets/Mirror/Editor/Weaver/Weaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class WeaverLists

// amount of SyncVars per class. dict<className, amount>
public Dictionary<string, int> numSyncVars = new Dictionary<string, int>();

public HashSet<string> ProcessedMessages = new HashSet<string>();
}

internal static class Weaver
Expand Down Expand Up @@ -170,12 +172,15 @@ static bool WeaveMessage(TypeDefinition td)
if (!td.IsClass)
return false;

// already processed
if (WeaveLists.ProcessedMessages.Contains(td.FullName))
return false;

bool modified = false;

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

try
{
TypeDefinition parent = td.BaseType.Resolve();
Expand All @@ -190,10 +195,12 @@ static bool WeaveMessage(TypeDefinition td)

// process this
MessageClassProcessor.Process(td);
WeaveLists.ProcessedMessages.Add(td.FullName);
modified = true;
}

// check for embedded types
// inner classes should be processed after outter class to avoid StackOverflowException
foreach (TypeDefinition embedded in td.NestedTypes)
{
modified |= WeaveMessage(embedded);
Expand Down
5 changes: 1 addition & 4 deletions Assets/Mirror/Tests/Editor/Weaver/.WeaverTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
<Compile Include="GeneratedReaderWriter~\GivesErrorWhenUsingUnityAsset.cs" />
<Compile Include="WeaverClientRpcTests~\AbstractClientRpc.cs" />
<Compile Include="WeaverClientRpcTests~\ClientRpcCantBeStatic.cs" />
<Compile Include="WeaverClientRpcTests~\ClientRpcStartsWithRpc.cs" />
<Compile Include="WeaverClientRpcTests~\ClientRpcThatExcludesOwner.cs" />
<Compile Include="WeaverClientRpcTests~\ClientRpcValid.cs" />
<Compile Include="WeaverClientRpcTests~\OverrideAbstractClientRpc.cs" />
Expand All @@ -93,7 +92,6 @@
<Compile Include="WeaverClientServerAttributeTests~\NetworkBehaviourServer.cs" />
<Compile Include="WeaverCommandTests~\AbstractCommand.cs" />
<Compile Include="WeaverCommandTests~\CommandCantBeStatic.cs" />
<Compile Include="WeaverCommandTests~\CommandStartsWithCmd.cs" />
<Compile Include="WeaverCommandTests~\CommandThatIgnoresAuthority.cs" />
<Compile Include="WeaverCommandTests~\CommandThatIgnoresAuthorityWithSenderConnection.cs" />
<Compile Include="WeaverCommandTests~\CommandValid.cs" />
Expand All @@ -111,6 +109,7 @@
<Compile Include="WeaverGeneralTests~\TestingScriptableObjectArraySerialization.cs" />
<Compile Include="WeaverMessageTests~\MessageMemberGeneric.cs" />
<Compile Include="WeaverMessageTests~\MessageMemberInterface.cs" />
<Compile Include="WeaverMessageTests~\MessageNestedInheritance.cs" />
<Compile Include="WeaverMessageTests~\MessageSelfReferencing.cs" />
<Compile Include="WeaverMessageTests~\MessageValid.cs" />
<Compile Include="WeaverMessageTests~\MessageWithBaseClass.cs" />
Expand Down Expand Up @@ -232,7 +231,6 @@
<Compile Include="WeaverSyncVarTests~\SyncVarsSyncList.cs" />
<Compile Include="WeaverSyncVarTests~\SyncVarsValid.cs" />
<Compile Include="WeaverSyncEventTests~\ErrorWhenSyncEventUsesGenericParameter.cs" />
<Compile Include="WeaverSyncEventTests~\ErrorWhenSyncEventDoesntStartWithEvent.cs" />
<Compile Include="WeaverSyncEventTests~\SyncEventValid.cs" />
<Compile Include="WeaverTargetRpcTests~\AbstractTargetRpc.cs" />
<Compile Include="WeaverTargetRpcTests~\ErrorWhenTargetRpcIsStatic.cs" />
Expand All @@ -241,7 +239,6 @@
<Compile Include="WeaverTargetRpcTests~\TargetRpcCanHaveOtherParametersWhileSkipingNetworkConnection.cs" />
<Compile Include="WeaverTargetRpcTests~\TargetRpcCanSkipNetworkConnection.cs" />
<Compile Include="WeaverTargetRpcTests~\ErrorWhenNetworkConnectionIsNotTheFirstParameter.cs" />
<Compile Include="WeaverTargetRpcTests~\ErrorWhenMethodDoesNotStartWithTarget.cs" />
<Compile Include="WeaverTargetRpcTests~\TargetRpcValid.cs" />
<Compile Include="WeaverTargetRpcTests~\VirtualTargetRpc.cs" />
<Reference Include="UnityEngine.CoreModule">
Expand Down
7 changes: 7 additions & 0 deletions Assets/Mirror/Tests/Editor/Weaver/WeaverMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@ public void MessageMemberInterface()
Assert.That(weaverErrors, Contains.Item("Cannot generate writer for interface SuperCoolInterface. Use a supported type or provide a custom writer (at WeaverMessageTests.MessageMemberInterface.SuperCoolInterface)"));
Assert.That(weaverErrors, Contains.Item("invalidField has unsupported type (at WeaverMessageTests.MessageMemberInterface.SuperCoolInterface WeaverMessageTests.MessageMemberInterface.MessageMemberInterface::invalidField)"));
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Mirror;

namespace WeaverMessageTests.MessageNestedInheritance
{
public class Message : MessageBase
{
public class Request : Message
{

}

public class Response : Message
{
public int errorCode;
}
}
}

0 comments on commit e4a5ce7

Please sign in to comment.