static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo patch, Dictionary <string, LocalBuilder> variables, bool allowFirsParamPassthrough, bool isIl2Cpp) { var isInstance = original.IsStatic == false; var originalParameters = original.GetParameters(); var originalParameterNames = originalParameters.Select(p => p.Name).ToArray(); // check for passthrough using first parameter (which must have same type as return type) var parameters = patch.GetParameters().ToList(); if (allowFirsParamPassthrough && patch.ReturnType != typeof(void) && parameters.Count > 0 && parameters[0].ParameterType == patch.ReturnType) { parameters.RemoveRange(0, 1); } foreach (var patchParam in parameters) { if (patchParam.Name == ORIGINAL_METHOD_PARAM) { var constructorInfo = original as ConstructorInfo; if (constructorInfo != null) { Emitter.Emit(il, OpCodes.Ldtoken, constructorInfo); Emitter.Emit(il, OpCodes.Call, getMethodMethod); continue; } var methodInfo = original as MethodInfo; if (methodInfo != null) { Emitter.Emit(il, OpCodes.Ldtoken, methodInfo); Emitter.Emit(il, OpCodes.Call, getMethodMethod); continue; } Emitter.Emit(il, OpCodes.Ldnull); continue; } if (patchParam.Name == INSTANCE_PARAM) { if (original.IsStatic) { Emitter.Emit(il, OpCodes.Ldnull); } else if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldarga, 0); // probably won't work or will be useless } else { Emitter.Emit(il, OpCodes.Ldarg_0); } continue; } if (patchParam.Name.StartsWith(INSTANCE_FIELD_PREFIX)) { var fieldName = patchParam.Name.Substring(INSTANCE_FIELD_PREFIX.Length); if (isIl2Cpp) { if (patchParam.ParameterType.IsByRef) { throw new NotSupportedException("Ref parameters to fields are not supported in IL2CPP patches"); } var getterMethod = AccessTools.Property(original.DeclaringType, fieldName)?.GetMethod; if (getterMethod == null) { throw new ArgumentException("No such field defined in class " + original.DeclaringType.FullName, fieldName); } if (!getterMethod.IsStatic) { Emitter.Emit(il, OpCodes.Ldarg_0); } var opcode = getterMethod.IsVirtual ? OpCodes.Callvirt : OpCodes.Call; Emitter.Emit(il, opcode, getterMethod); } else { FieldInfo fieldInfo; if (fieldName.All(char.IsDigit)) { fieldInfo = AccessTools.Field(original.DeclaringType, int.Parse(fieldName)); if (fieldInfo == null) { throw new ArgumentException("No field found at given index in class " + original.DeclaringType.FullName, fieldName); } } else { fieldInfo = AccessTools.Field(original.DeclaringType, fieldName); if (fieldInfo == null) { throw new ArgumentException("No such field defined in class " + original.DeclaringType.FullName, fieldName); } } if (fieldInfo.IsStatic) { if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldsflda, fieldInfo); } else { Emitter.Emit(il, OpCodes.Ldsfld, fieldInfo); } } else { if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldarg_0); Emitter.Emit(il, OpCodes.Ldflda, fieldInfo); } else { Emitter.Emit(il, OpCodes.Ldarg_0); Emitter.Emit(il, OpCodes.Ldfld, fieldInfo); } } } continue; } if (patchParam.Name == STATE_VAR) { var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc; Emitter.Emit(il, ldlocCode, variables[patch.DeclaringType.FullName]); continue; } if (patchParam.Name == RESULT_VAR) { if (AccessTools.GetReturnedType(original) == typeof(void)) { throw new Exception("Cannot get result from void method " + original.FullDescription()); } var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc; Emitter.Emit(il, ldlocCode, variables[RESULT_VAR]); continue; } int idx; if (patchParam.Name.StartsWith(PARAM_INDEX_PREFIX)) { var val = patchParam.Name.Substring(PARAM_INDEX_PREFIX.Length); if (!int.TryParse(val, out idx)) { throw new Exception("Parameter " + patchParam.Name + " does not contain a valid index"); } if (idx < 0 || idx >= originalParameters.Length) { throw new Exception("No parameter found at index " + idx); } } else { idx = GetArgumentIndex(patch, originalParameterNames, patchParam); if (idx == -1) { throw new Exception("Parameter \"" + patchParam.Name + "\" not found in method " + original.FullDescription()); } } // original -> patch opcode // -------------------------------------- // 1 normal -> normal : LDARG // 2 normal -> ref/out : LDARGA // 3 ref/out -> normal : LDARG, LDIND_x // 4 ref/out -> ref/out : LDARG // var originalIsNormal = originalParameters[idx].IsOut == false && originalParameters[idx].ParameterType.IsByRef == false; var patchIsNormal = patchParam.IsOut == false && patchParam.ParameterType.IsByRef == false; var patchArgIndex = idx + (isInstance ? 1 : 0); // Case 1 + 4 if (originalIsNormal == patchIsNormal) { Emitter.Emit(il, OpCodes.Ldarg, patchArgIndex); continue; } // Case 2 if (originalIsNormal && patchIsNormal == false) { Emitter.Emit(il, OpCodes.Ldarga, patchArgIndex); continue; } // Case 3 Emitter.Emit(il, OpCodes.Ldarg, patchArgIndex); Emitter.Emit(il, LoadIndOpCodeFor(originalParameters[idx].ParameterType)); } }
static void EmitCallParameter(ILGenerator il, MethodBase original, MethodInfo patch, Dictionary <string, LocalBuilder> variables, bool allowFirsParamPassthrough) { var isInstance = original.IsStatic == false; var originalParameters = original.GetParameters(); var originalParameterNames = originalParameters.Select(p => p.Name).ToArray(); // check for passthrough using first parameter (which must have same type as return type) var parameters = patch.GetParameters().ToList(); if (allowFirsParamPassthrough && patch.ReturnType != typeof(void) && parameters.Count > 0 && parameters[0].ParameterType == patch.ReturnType) { parameters.RemoveRange(0, 1); } foreach (var patchParam in parameters) { if (patchParam.Name == ORIGINAL_METHOD_PARAM) { var constructorInfo = original as ConstructorInfo; if (constructorInfo != null) { Emitter.Emit(il, OpCodes.Ldtoken, constructorInfo); Emitter.Emit(il, OpCodes.Call, getMethodMethod); continue; } var methodInfo = original as MethodInfo; if (methodInfo != null) { Emitter.Emit(il, OpCodes.Ldtoken, methodInfo); Emitter.Emit(il, OpCodes.Call, getMethodMethod); continue; } Emitter.Emit(il, OpCodes.Ldnull); continue; } if (patchParam.Name == INSTANCE_PARAM) { if (original.IsStatic) { Emitter.Emit(il, OpCodes.Ldnull); } else if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldarga, 0); // probably won't work or will be useless } else { Emitter.Emit(il, OpCodes.Ldarg_0); } continue; } if (patchParam.Name.StartsWith(INSTANCE_FIELD_PREFIX)) { var fieldInfo = AccessTools.Field(original.DeclaringType, patchParam.Name.Substring(INSTANCE_FIELD_PREFIX.Length)); if (fieldInfo.IsStatic) { if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldsflda, fieldInfo); } else { Emitter.Emit(il, OpCodes.Ldsfld, fieldInfo); } } else { if (patchParam.ParameterType.IsByRef) { Emitter.Emit(il, OpCodes.Ldarg_0); Emitter.Emit(il, OpCodes.Ldflda, fieldInfo); } else { Emitter.Emit(il, OpCodes.Ldarg_0); Emitter.Emit(il, OpCodes.Ldfld, fieldInfo); } } continue; } if (patchParam.Name == STATE_VAR) { var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc; Emitter.Emit(il, ldlocCode, variables[patch.DeclaringType.FullName]); continue; } if (patchParam.Name == RESULT_VAR) { if (AccessTools.GetReturnedType(original) == typeof(void)) { throw new Exception("Cannot get result from void method " + original.FullDescription()); } var ldlocCode = patchParam.ParameterType.IsByRef ? OpCodes.Ldloca : OpCodes.Ldloc; Emitter.Emit(il, ldlocCode, variables[RESULT_VAR]); continue; } var patchParamName = patchParam.Name; var originalName = patchParam.GetParameterOverride(); if (originalName != null) { patchParamName = originalName; } else { originalName = patch.GetParameterOverride(patchParamName, true); if (originalName != null) { patchParamName = originalName; } } var idx = Array.IndexOf(originalParameterNames, patchParamName); if (idx == -1) { throw new Exception("Parameter \"" + patchParam.Name + "\" not found in method " + original.FullDescription()); } // original -> patch opcode // -------------------------------------- // 1 normal -> normal : LDARG // 2 normal -> ref/out : LDARGA // 3 ref/out -> normal : LDARG, LDIND_x // 4 ref/out -> ref/out : LDARG // var originalIsNormal = originalParameters[idx].IsOut == false && originalParameters[idx].ParameterType.IsByRef == false; var patchIsNormal = patchParam.IsOut == false && patchParam.ParameterType.IsByRef == false; var patchArgIndex = idx + (isInstance ? 1 : 0); // Case 1 + 4 if (originalIsNormal == patchIsNormal) { Emitter.Emit(il, OpCodes.Ldarg, patchArgIndex); continue; } // Case 2 if (originalIsNormal && patchIsNormal == false) { Emitter.Emit(il, OpCodes.Ldarga, patchArgIndex); continue; } // Case 3 Emitter.Emit(il, OpCodes.Ldarg, patchArgIndex); Emitter.Emit(il, LoadIndOpCodeFor(originalParameters[idx].ParameterType)); } }