MethodProcessor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Mono.CecilX;
  2. using Mono.CecilX.Cil;
  3. namespace Mirror.Weaver
  4. {
  5. public static class MethodProcessor
  6. {
  7. const string RpcPrefix = "UserCode_";
  8. // creates a method substitute
  9. // For example, if we have this:
  10. // public void CmdThrust(float thrusting, int spin)
  11. // {
  12. // xxxxx
  13. // }
  14. //
  15. // it will substitute the method and move the code to a new method with a provided name
  16. // for example:
  17. //
  18. // public void CmdTrust(float thrusting, int spin)
  19. // {
  20. // }
  21. //
  22. // public void <newName>(float thrusting, int spin)
  23. // {
  24. // xxxxx
  25. // }
  26. //
  27. // Note that all the calls to the method remain untouched
  28. //
  29. // the original method definition loses all code
  30. // this returns the newly created method with all the user provided code
  31. public static MethodDefinition SubstituteMethod(Logger Log, TypeDefinition td, MethodDefinition md, ref bool WeavingFailed)
  32. {
  33. string newName = RpcPrefix + md.Name;
  34. MethodDefinition cmd = new MethodDefinition(newName, md.Attributes, md.ReturnType);
  35. // force the substitute method to be protected.
  36. // -> public would show in the Inspector for UnityEvents as
  37. // User_CmdUsePotion() etc. but the user shouldn't use those.
  38. // -> private would not allow inheriting classes to call it, see
  39. // OverrideVirtualWithBaseCallsBothVirtualAndBase test.
  40. // -> IL has no concept of 'protected', it's called IsFamily there.
  41. cmd.IsPublic = false;
  42. cmd.IsFamily = true;
  43. // add parameters
  44. foreach (ParameterDefinition pd in md.Parameters)
  45. {
  46. cmd.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
  47. }
  48. // swap bodies
  49. (cmd.Body, md.Body) = (md.Body, cmd.Body);
  50. // Move over all the debugging information
  51. foreach (SequencePoint sequencePoint in md.DebugInformation.SequencePoints)
  52. cmd.DebugInformation.SequencePoints.Add(sequencePoint);
  53. md.DebugInformation.SequencePoints.Clear();
  54. foreach (CustomDebugInformation customInfo in md.CustomDebugInformations)
  55. cmd.CustomDebugInformations.Add(customInfo);
  56. md.CustomDebugInformations.Clear();
  57. (md.DebugInformation.Scope, cmd.DebugInformation.Scope) = (cmd.DebugInformation.Scope, md.DebugInformation.Scope);
  58. td.Methods.Add(cmd);
  59. FixRemoteCallToBaseMethod(Log, td, cmd, ref WeavingFailed);
  60. return cmd;
  61. }
  62. // Finds and fixes call to base methods within remote calls
  63. //For example, changes `base.CmdDoSomething` to `base.CallCmdDoSomething` within `this.CallCmdDoSomething`
  64. public static void FixRemoteCallToBaseMethod(Logger Log, TypeDefinition type, MethodDefinition method, ref bool WeavingFailed)
  65. {
  66. string callName = method.Name;
  67. // Cmd/rpc start with Weaver.RpcPrefix
  68. // e.g. CallCmdDoSomething
  69. if (!callName.StartsWith(RpcPrefix))
  70. return;
  71. // e.g. CmdDoSomething
  72. string baseRemoteCallName = method.Name.Substring(RpcPrefix.Length);
  73. foreach (Instruction instruction in method.Body.Instructions)
  74. {
  75. // if call to base.CmdDoSomething within this.CallCmdDoSomething
  76. if (IsCallToMethod(instruction, out MethodDefinition calledMethod) &&
  77. calledMethod.Name == baseRemoteCallName)
  78. {
  79. TypeDefinition baseType = type.BaseType.Resolve();
  80. MethodDefinition baseMethod = baseType.GetMethodInBaseType(callName);
  81. if (baseMethod == null)
  82. {
  83. Log.Error($"Could not find base method for {callName}", method);
  84. WeavingFailed = true;
  85. return;
  86. }
  87. if (!baseMethod.IsVirtual)
  88. {
  89. Log.Error($"Could not find base method that was virtual {callName}", method);
  90. WeavingFailed = true;
  91. return;
  92. }
  93. instruction.Operand = baseMethod;
  94. }
  95. }
  96. }
  97. static bool IsCallToMethod(Instruction instruction, out MethodDefinition calledMethod)
  98. {
  99. if (instruction.OpCode == OpCodes.Call &&
  100. instruction.Operand is MethodDefinition method)
  101. {
  102. calledMethod = method;
  103. return true;
  104. }
  105. else
  106. {
  107. calledMethod = null;
  108. return false;
  109. }
  110. }
  111. }
  112. }