Skip to content

Commit

Permalink
fix: Command and Rpc debugging information (#1551)
Browse files Browse the repository at this point in the history
When replacing command and rpc methods,  we now correctly move over
the debugging information.

Now you can put breakpoints in commands and Rpc

Should fix #1550
  • Loading branch information
paulpach committed Mar 16, 2020
1 parent 0b84d4c commit 658847b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 43 deletions.
17 changes: 2 additions & 15 deletions Assets/Mirror/Editor/Weaver/Processors/CommandProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mirror.Weaver
{
public static class CommandProcessor
public static class CommandProcessor
{
const string CmdPrefix = "InvokeCmd";

Expand Down Expand Up @@ -33,20 +33,7 @@ public void CallCmdThrust(float thrusting, int spin)
*/
public static MethodDefinition ProcessCommandCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca)
{
MethodDefinition cmd = new MethodDefinition("Call" + md.Name,
MethodAttributes.Public | MethodAttributes.HideBySig,
Weaver.voidType);

// add parameters
foreach (ParameterDefinition pd in md.Parameters)
{
cmd.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
}

// move the old body to the new function
MethodBody newBody = cmd.Body;
cmd.Body = md.Body;
md.Body = newBody;
MethodDefinition cmd = MethodProcessor.SubstituteMethod(md, "Call" + md.Name);

ILProcessor cmdWorker = md.Body.GetILProcessor();

Expand Down
59 changes: 59 additions & 0 deletions Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Mono.CecilX;
using Mono.CecilX.Cil;

namespace Mirror.Weaver
{
public static class MethodProcessor
{

// creates a method substitute
// For example, if we have this:
// public void CmdThrust(float thrusting, int spin)
// {
// xxxxx
// }
//
// it will substitute the method and move the code to a new method with a provided name
// for example:
//
// public void CmdTrust(float thrusting, int spin)
// {
// }
//
// public void <newName>(float thrusting, int spin)
// {
// xxxxx
// }
//
// Note that all the calls to the method remain untouched
//
// 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)
{
MethodDefinition cmd = new MethodDefinition(newName,md.Attributes, md.ReturnType);

// add parameters
foreach (ParameterDefinition pd in md.Parameters)
{
cmd.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
}

// swap bodies
(cmd.Body, md.Body) = (md.Body, cmd.Body);

// Move over all the debugging information
foreach (SequencePoint sequencePoint in md.DebugInformation.SequencePoints)
cmd.DebugInformation.SequencePoints.Add(sequencePoint);
md.DebugInformation.SequencePoints.Clear();

foreach (CustomDebugInformation customInfo in md.CustomDebugInformations)
cmd.CustomDebugInformations.Add(customInfo);
md.CustomDebugInformations.Clear();

(md.DebugInformation.Scope, cmd.DebugInformation.Scope) = (cmd.DebugInformation.Scope, md.DebugInformation.Scope);

return cmd;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirror/Editor/Weaver/Processors/MethodProcessor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions Assets/Mirror/Editor/Weaver/Processors/RpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,7 @@ public void CallRpcTest (int param)
*/
public static MethodDefinition ProcessRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca)
{
MethodDefinition rpc = new MethodDefinition("Call" + md.Name, MethodAttributes.Public |
MethodAttributes.HideBySig,
Weaver.voidType);

// add paramters
foreach (ParameterDefinition pd in md.Parameters)
{
rpc.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
}

// move the old body to the new function
MethodBody newBody = rpc.Body;
rpc.Body = md.Body;
md.Body = newBody;
MethodDefinition rpc = MethodProcessor.SubstituteMethod(md, "Call" + md.Name);

ILProcessor rpcWorker = md.Body.GetILProcessor();

Expand Down
15 changes: 1 addition & 14 deletions Assets/Mirror/Editor/Weaver/Processors/TargetRpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,7 @@ public void CallTargetTest (int param)
*/
public static MethodDefinition ProcessTargetRpcCall(TypeDefinition td, MethodDefinition md, CustomAttribute ca)
{
MethodDefinition rpc = new MethodDefinition("Call" + md.Name, MethodAttributes.Public |
MethodAttributes.HideBySig,
Weaver.voidType);

// add parameters
foreach (ParameterDefinition pd in md.Parameters)
{
rpc.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
}

// move the old body to the new function
MethodBody newBody = rpc.Body;
rpc.Body = md.Body;
md.Body = newBody;
MethodDefinition rpc = MethodProcessor.SubstituteMethod(md, "Call" + md.Name);

ILProcessor rpcWorker = md.Body.GetILProcessor();

Expand Down

0 comments on commit 658847b

Please sign in to comment.