123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using Mono.CecilX;
- using Mono.CecilX.Cil;
- namespace Mirror.Weaver
- {
- public static class MethodProcessor
- {
- const string RpcPrefix = "UserCode_";
- // 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(TypeDefinition td, MethodDefinition md)
- {
- string newName = RpcPrefix + md.Name;
- MethodDefinition cmd = new MethodDefinition(newName, md.Attributes, md.ReturnType);
- // force the substitute method to be protected.
- // -> public would show in the Inspector for UnityEvents as
- // User_CmdUsePotion() etc. but the user shouldn't use those.
- // -> private would not allow inheriting classes to call it, see
- // OverrideVirtualWithBaseCallsBothVirtualAndBase test.
- // -> IL has no concept of 'protected', it's called IsFamily there.
- cmd.IsPublic = false;
- cmd.IsFamily = true;
- // 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);
- td.Methods.Add(cmd);
- FixRemoteCallToBaseMethod(td, cmd);
- return cmd;
- }
- /// <summary>
- /// Finds and fixes call to base methods within remote calls
- /// <para>For example, changes `base.CmdDoSomething` to `base.CallCmdDoSomething` within `this.CallCmdDoSomething`</para>
- /// </summary>
- /// <param name="type"></param>
- /// <param name="method"></param>
- public static void FixRemoteCallToBaseMethod(TypeDefinition type, MethodDefinition method)
- {
- string callName = method.Name;
- // Cmd/rpc start with Weaver.RpcPrefix
- // e.g. CallCmdDoSomething
- if (!callName.StartsWith(RpcPrefix))
- return;
- // e.g. CmdDoSomething
- string baseRemoteCallName = method.Name.Substring(RpcPrefix.Length);
- foreach (Instruction instruction in method.Body.Instructions)
- {
- // if call to base.CmdDoSomething within this.CallCmdDoSomething
- if (IsCallToMethod(instruction, out MethodDefinition calledMethod) &&
- calledMethod.Name == baseRemoteCallName)
- {
- TypeDefinition baseType = type.BaseType.Resolve();
- MethodDefinition baseMethod = baseType.GetMethodInBaseType(callName);
- if (baseMethod == null)
- {
- Weaver.Error($"Could not find base method for {callName}", method);
- return;
- }
- if (!baseMethod.IsVirtual)
- {
- Weaver.Error($"Could not find base method that was virutal {callName}", method);
- return;
- }
- instruction.Operand = baseMethod;
- Weaver.DLog(type, "Replacing call to '{0}' with '{1}' inside '{2}'", calledMethod.FullName, baseMethod.FullName, method.FullName);
- }
- }
- }
- static bool IsCallToMethod(Instruction instruction, out MethodDefinition calledMethod)
- {
- if (instruction.OpCode == OpCodes.Call &&
- instruction.Operand is MethodDefinition method)
- {
- calledMethod = method;
- return true;
- }
- else
- {
- calledMethod = null;
- return false;
- }
- }
- }
- }
|