fix: Nested messages (#2148)

* test for nested messages

* fixing StackOverflow with nested types
This commit is contained in:
James Frowen 2020-08-09 19:01:14 +01:00 committed by GitHub
parent fe3bebcc63
commit e4a5ce795b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 5 deletions

View File

@ -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
@ -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();
@ -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);

View File

@ -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" />
@ -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" />
@ -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" />
@ -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" />
@ -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">

View File

@ -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);
}
}
}

View File

@ -0,0 +1,17 @@
using Mirror;
namespace WeaverMessageTests.MessageNestedInheritance
{
public class Message : MessageBase
{
public class Request : Message
{
}
public class Response : Message
{
public int errorCode;
}
}
}