MethodProcessor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(TypeDefinition td, MethodDefinition md)
  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(td, cmd);
  60. return cmd;
  61. }
  62. /// <summary>
  63. /// Finds and fixes call to base methods within remote calls
  64. /// <para>For example, changes `base.CmdDoSomething` to `base.CallCmdDoSomething` within `this.CallCmdDoSomething`</para>
  65. /// </summary>
  66. /// <param name="type"></param>
  67. /// <param name="method"></param>
  68. public static void FixRemoteCallToBaseMethod(TypeDefinition type, MethodDefinition method)
  69. {
  70. string callName = method.Name;
  71. // Cmd/rpc start with Weaver.RpcPrefix
  72. // e.g. CallCmdDoSomething
  73. if (!callName.StartsWith(RpcPrefix))
  74. return;
  75. // e.g. CmdDoSomething
  76. string baseRemoteCallName = method.Name.Substring(RpcPrefix.Length);
  77. foreach (Instruction instruction in method.Body.Instructions)
  78. {
  79. // if call to base.CmdDoSomething within this.CallCmdDoSomething
  80. if (IsCallToMethod(instruction, out MethodDefinition calledMethod) &&
  81. calledMethod.Name == baseRemoteCallName)
  82. {
  83. TypeDefinition baseType = type.BaseType.Resolve();
  84. MethodDefinition baseMethod = baseType.GetMethodInBaseType(callName);
  85. if (baseMethod == null)
  86. {
  87. Weaver.Error($"Could not find base method for {callName}", method);
  88. return;
  89. }
  90. if (!baseMethod.IsVirtual)
  91. {
  92. Weaver.Error($"Could not find base method that was virutal {callName}", method);
  93. return;
  94. }
  95. instruction.Operand = baseMethod;
  96. Weaver.DLog(type, "Replacing call to '{0}' with '{1}' inside '{2}'", calledMethod.FullName, baseMethod.FullName, method.FullName);
  97. }
  98. }
  99. }
  100. static bool IsCallToMethod(Instruction instruction, out MethodDefinition calledMethod)
  101. {
  102. if (instruction.OpCode == OpCodes.Call &&
  103. instruction.Operand is MethodDefinition method)
  104. {
  105. calledMethod = method;
  106. return true;
  107. }
  108. else
  109. {
  110. calledMethod = null;
  111. return false;
  112. }
  113. }
  114. }
  115. }