MethodProcessor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // For a function like
  9. // [ClientRpc] void RpcTest(int value),
  10. // Weaver substitutes the method and moves the code to a new method:
  11. // UserCode_RpcTest(int value) <- contains original code
  12. // RpcTest(int value) <- serializes parameters, sends the message
  13. //
  14. // Note that all the calls to the method remain untouched.
  15. // FixRemoteCallToBaseMethod replaces them afterwards.
  16. public static MethodDefinition SubstituteMethod(Logger Log, TypeDefinition td, MethodDefinition md, ref bool WeavingFailed)
  17. {
  18. string newName = Weaver.GenerateMethodName(RpcPrefix, md);
  19. MethodDefinition cmd = new MethodDefinition(newName, md.Attributes, md.ReturnType);
  20. // force the substitute method to be protected.
  21. // -> public would show in the Inspector for UnityEvents as
  22. // User_CmdUsePotion() etc. but the user shouldn't use those.
  23. // -> private would not allow inheriting classes to call it, see
  24. // OverrideVirtualWithBaseCallsBothVirtualAndBase test.
  25. // -> IL has no concept of 'protected', it's called IsFamily there.
  26. cmd.IsPublic = false;
  27. cmd.IsFamily = true;
  28. // add parameters
  29. foreach (ParameterDefinition pd in md.Parameters)
  30. {
  31. cmd.Parameters.Add(new ParameterDefinition(pd.Name, ParameterAttributes.None, pd.ParameterType));
  32. }
  33. // swap bodies
  34. (cmd.Body, md.Body) = (md.Body, cmd.Body);
  35. // Move over all the debugging information
  36. foreach (SequencePoint sequencePoint in md.DebugInformation.SequencePoints)
  37. cmd.DebugInformation.SequencePoints.Add(sequencePoint);
  38. md.DebugInformation.SequencePoints.Clear();
  39. foreach (CustomDebugInformation customInfo in md.CustomDebugInformations)
  40. cmd.CustomDebugInformations.Add(customInfo);
  41. md.CustomDebugInformations.Clear();
  42. (md.DebugInformation.Scope, cmd.DebugInformation.Scope) = (cmd.DebugInformation.Scope, md.DebugInformation.Scope);
  43. td.Methods.Add(cmd);
  44. FixRemoteCallToBaseMethod(Log, td, cmd, ref WeavingFailed);
  45. return cmd;
  46. }
  47. // For a function like
  48. // [ClientRpc] void RpcTest(int value),
  49. // Weaver substitutes the method and moves the code to a new method:
  50. // UserCode_RpcTest(int value) <- contains original code
  51. // RpcTest(int value) <- serializes parameters, sends the message
  52. //
  53. // FixRemoteCallToBaseMethod replaces all calls to
  54. // RpcTest(value)
  55. // with
  56. // UserCode_RpcTest(value)
  57. public static void FixRemoteCallToBaseMethod(Logger Log, TypeDefinition type, MethodDefinition method, ref bool WeavingFailed)
  58. {
  59. string callName = method.Name;
  60. // Cmd/rpc start with Weaver.RpcPrefix
  61. // e.g. CallCmdDoSomething
  62. if (!callName.StartsWith(RpcPrefix))
  63. return;
  64. // e.g. CmdDoSomething
  65. string baseRemoteCallName = method.Name.Substring(RpcPrefix.Length);
  66. foreach (Instruction instruction in method.Body.Instructions)
  67. {
  68. // is this instruction a Call to a method?
  69. // if yes, output the method so we can check it.
  70. if (IsCallToMethod(instruction, out MethodDefinition calledMethod))
  71. {
  72. // when considering if 'calledMethod' is a call to 'method',
  73. // we originally compared .Name.
  74. //
  75. // to fix IL2CPP build bugs with overloaded Rpcs, we need to
  76. // generated rpc names like
  77. // RpcTest(string value) => RpcTestString(strig value)
  78. // RpcTest(int value) => RpcTestInt(int value)
  79. // to make them unique.
  80. //
  81. // calledMethod.Name is still "RpcTest", so we need to
  82. // convert this to the generated name as well before comparing.
  83. string calledMethodName_Generated = Weaver.GenerateMethodName("", calledMethod);
  84. if (calledMethodName_Generated == baseRemoteCallName)
  85. {
  86. TypeDefinition baseType = type.BaseType.Resolve();
  87. MethodDefinition baseMethod = baseType.GetMethodInBaseType(callName);
  88. if (baseMethod == null)
  89. {
  90. Log.Error($"Could not find base method for {callName}", method);
  91. WeavingFailed = true;
  92. return;
  93. }
  94. if (!baseMethod.IsVirtual)
  95. {
  96. Log.Error($"Could not find base method that was virtual {callName}", method);
  97. WeavingFailed = true;
  98. return;
  99. }
  100. instruction.Operand = baseMethod;
  101. }
  102. }
  103. }
  104. }
  105. static bool IsCallToMethod(Instruction instruction, out MethodDefinition calledMethod)
  106. {
  107. if (instruction.OpCode == OpCodes.Call &&
  108. instruction.Operand is MethodDefinition method)
  109. {
  110. calledMethod = method;
  111. return true;
  112. }
  113. else
  114. {
  115. calledMethod = null;
  116. return false;
  117. }
  118. }
  119. }
  120. }