From 97fe7a0a018600ef4414ab5e9231686868ee05f8 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Thu, 26 Mar 2020 07:46:15 -0500 Subject: [PATCH] Simplify adding substitute methods --- .../Weaver/Processors/CommandProcessor.cs | 4 +-- .../Weaver/Processors/MethodProcessor.cs | 3 +- .../Processors/NetworkBehaviourProcessor.cs | 32 ++----------------- .../Editor/Weaver/Processors/RpcProcessor.cs | 4 +-- .../Weaver/Processors/TargetRpcProcessor.cs | 4 +-- Assets/Mirror/Tests/Editor/WeaverTest.cs | 2 +- 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs index 3722bc346..47a1cd8bf 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs @@ -33,7 +33,7 @@ correctly in dependent assemblies */ public static MethodDefinition ProcessCommandCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { - MethodDefinition cmd = MethodProcessor.SubstituteMethod(md, "Call" + md.Name); + MethodDefinition cmd = MethodProcessor.SubstituteMethod(td, md, "Call" + md.Name); ILProcessor cmdWorker = md.Body.GetILProcessor(); @@ -112,7 +112,7 @@ public static MethodDefinition ProcessCommandInvoke(TypeDefinition td, MethodDef cmdWorker.Append(cmdWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(cmd.Parameters); - + td.Methods.Add(cmd); return cmd; } diff --git a/Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs index bbee539d5..89c6ecabb 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs @@ -29,7 +29,7 @@ public static class MethodProcessor // // the original method definition loses all code // this returns the newly created method with all the user provided code - public static MethodDefinition SubstituteMethod(MethodDefinition md, string newName) + public static MethodDefinition SubstituteMethod(TypeDefinition td, MethodDefinition md, string newName) { MethodDefinition cmd = new MethodDefinition(newName,md.Attributes, md.ReturnType); @@ -53,6 +53,7 @@ public static MethodDefinition SubstituteMethod(MethodDefinition md, string newN (md.DebugInformation.Scope, cmd.DebugInformation.Scope) = (cmd.DebugInformation.Scope, md.DebugInformation.Scope); + td.Methods.Add(cmd); return cmd; } } diff --git a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs index 59f5b32dd..4689e1533 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/NetworkBehaviourProcessor.cs @@ -841,8 +841,10 @@ void ProcessMethods() { HashSet names = new HashSet(); + // copy the list of methods because we will be adding methods in the loop + List methods = new List(netBehaviourSubclass.Methods); // find command and RPC functions - foreach (MethodDefinition md in netBehaviourSubclass.Methods) + foreach (MethodDefinition md in methods) { foreach (CustomAttribute ca in md.CustomAttributes) { @@ -865,34 +867,6 @@ void ProcessMethods() } } } - - // cmds - foreach (MethodDefinition md in commandInvocationFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } - foreach (MethodDefinition md in commandCallFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } - - // rpcs - foreach (MethodDefinition md in clientRpcInvocationFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } - foreach (MethodDefinition md in targetRpcInvocationFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } - foreach (MethodDefinition md in clientRpcCallFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } - foreach (MethodDefinition md in targetRpcCallFuncs) - { - netBehaviourSubclass.Methods.Add(md); - } } void ProcessClientRpc(HashSet names, MethodDefinition md, CustomAttribute ca) diff --git a/Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs index deee7f74c..1e986979d 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs @@ -31,7 +31,7 @@ public static MethodDefinition ProcessRpcInvoke(TypeDefinition td, MethodDefinit rpcWorker.Append(rpcWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(rpc.Parameters); - + td.Methods.Add(rpc); return rpc; } @@ -59,7 +59,7 @@ correctly in dependent assemblies */ public static MethodDefinition ProcessRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { - MethodDefinition rpc = MethodProcessor.SubstituteMethod(md, "Call" + md.Name); + MethodDefinition rpc = MethodProcessor.SubstituteMethod(td, md, "Call" + md.Name); ILProcessor rpcWorker = md.Body.GetILProcessor(); diff --git a/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs b/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs index baa84f10c..e4a71de9a 100644 --- a/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs +++ b/Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs @@ -48,7 +48,7 @@ public static MethodDefinition ProcessTargetRpcInvoke(TypeDefinition td, MethodD rpcWorker.Append(rpcWorker.Create(OpCodes.Ret)); NetworkBehaviourProcessor.AddInvokeParameters(rpc.Parameters); - + td.Methods.Add(rpc); return rpc; } @@ -87,7 +87,7 @@ correctly in dependent assemblies */ public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca) { - MethodDefinition rpc = MethodProcessor.SubstituteMethod(md, "Call" + md.Name); + MethodDefinition rpc = MethodProcessor.SubstituteMethod(td, md, "Call" + md.Name); ILProcessor rpcWorker = md.Body.GetILProcessor(); diff --git a/Assets/Mirror/Tests/Editor/WeaverTest.cs b/Assets/Mirror/Tests/Editor/WeaverTest.cs index 7fd7ce692..013b4e6ff 100644 --- a/Assets/Mirror/Tests/Editor/WeaverTest.cs +++ b/Assets/Mirror/Tests/Editor/WeaverTest.cs @@ -470,8 +470,8 @@ public void CommandCantBeStatic() [Test] public void ClientRpcValid() { - Assert.That(CompilationFinishedHook.WeaveFailed, Is.False); Assert.That(weaverErrors, Is.Empty); + Assert.That(CompilationFinishedHook.WeaveFailed, Is.False); } [Test]