static void GenerateDynamicMethod(MethodBase original, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <MethodInfo> transpilers, ILGenerator il, int idx) { var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var privateVars = new Dictionary <string, LocalBuilder>(); LocalBuilder resultVariable = null; if (idx > 0) { resultVariable = DynamicTools.DeclareLocalVariable(il, AccessTools.GetReturnedType(original)); privateVars[RESULT_VAR] = resultVariable; } prefixes.ForEach(prefix => { prefix.GetParameters() .Where(patchParam => patchParam.Name == STATE_VAR) .Do(patchParam => { var privateStateVariable = DynamicTools.DeclareLocalVariable(il, patchParam.ParameterType); privateVars[prefix.DeclaringType.FullName] = privateStateVariable; }); }); var afterOriginal1 = il.DefineLabel(); var afterOriginal2 = il.DefineLabel(); var canHaveJump = AddPrefixes(il, original, prefixes, privateVars, afterOriginal2); var copier = new MethodCopier(original, il, originalVariables); foreach (var transpiler in transpilers) { copier.AddTranspiler(transpiler); } copier.Emit(afterOriginal1); Emitter.MarkLabel(il, afterOriginal1); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } if (canHaveJump) { Emitter.MarkLabel(il, afterOriginal2); } AddPostfixes(il, original, postfixes, privateVars); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } Emitter.Emit(il, OpCodes.Ret); }
public static DynamicMethod CreatePatchedMethod(MethodBase original, MethodInfo postfix) { var patch = DynamicTools.CreateDynamicMethod(original, "_Patch"); var il = patch.GetILGenerator(); var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var privateVars = new Dictionary <string, LocalBuilder>(); LocalBuilder resultVariable = null; if (postfix != null) { resultVariable = DynamicTools.DeclareLocalVariable(il, AccessTools.GetReturnedType(original)); privateVars[RESULT_VAR] = resultVariable; } var afterOriginal1 = il.DefineLabel(); var copier = new MethodCopier(original, patch, originalVariables); copier.Emit(afterOriginal1); Emitter.MarkLabel(il, afterOriginal1); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } AddPostfix(il, original, postfix, privateVars); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } Emitter.Emit(il, OpCodes.Ret); DynamicTools.PrepareDynamicMethod(patch); return(patch); }
public static DynamicMethod CreatePatchedMethod(MethodBase original, string harmonyInstanceID, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <MethodInfo> transpilers) { try { if (HarmonyInstance.DEBUG) { FileLog.LogBuffered("### Patch " + original.DeclaringType + ", " + original); } bool isIl2Cpp = original.DeclaringType.IsSubclassOf(typeof(Il2CppObjectBase)); if (isIl2Cpp && transpilers.Count > 0) { throw new NotSupportedException("IL2CPP patches cannot use transpilers (got " + transpilers.Count + ")"); } var idx = prefixes.Count() + postfixes.Count(); var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx, isIl2Cpp); if (patch == null) { return(null); } var il = patch.GetILGenerator(); // for debugging AssemblyBuilder assemblyBuilder = null; TypeBuilder typeBuilder = null; if (DEBUG_METHOD_GENERATION_BY_DLL_CREATION) { il = DynamicTools.CreateSaveableMethod(original, "_Patch" + idx, out assemblyBuilder, out typeBuilder); } var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var privateVars = new Dictionary <string, LocalBuilder>(); LocalBuilder resultVariable = null; if (idx > 0) { resultVariable = DynamicTools.DeclareLocalVariable(il, AccessTools.GetReturnedType(original)); privateVars[RESULT_VAR] = resultVariable; } prefixes.ForEach(prefix => { prefix.GetParameters() .Where(patchParam => patchParam.Name == STATE_VAR) .Do(patchParam => { var privateStateVariable = DynamicTools.DeclareLocalVariable(il, patchParam.ParameterType); privateVars[prefix.DeclaringType.FullName] = privateStateVariable; }); }); var skipOriginalLabel = il.DefineLabel(); var canHaveJump = AddPrefixes(il, original, prefixes, privateVars, skipOriginalLabel, isIl2Cpp); var copier = new MethodCopier(original, il, originalVariables); foreach (var transpiler in transpilers) { copier.AddTranspiler(transpiler); } var endLabels = new List <Label>(); var endBlocks = new List <ExceptionBlock>(); copier.Finalize(endLabels, endBlocks); foreach (var label in endLabels) { Emitter.MarkLabel(il, label); } foreach (var block in endBlocks) { Emitter.MarkBlockAfter(il, block); } if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } if (canHaveJump) { Emitter.MarkLabel(il, skipOriginalLabel); } AddPostfixes(il, original, postfixes, privateVars, false, isIl2Cpp); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } AddPostfixes(il, original, postfixes, privateVars, true, isIl2Cpp); Emitter.Emit(il, OpCodes.Ret); if (HarmonyInstance.DEBUG) { FileLog.LogBuffered("DONE"); FileLog.LogBuffered(""); FileLog.FlushBuffer(); } // for debugging if (DEBUG_METHOD_GENERATION_BY_DLL_CREATION) { DynamicTools.SaveMethod(assemblyBuilder, typeBuilder); return(null); } DynamicTools.PrepareDynamicMethod(patch); return(patch); } catch (Exception ex) { throw new Exception("Exception from HarmonyInstance \"" + harmonyInstanceID + "\"", ex); } finally { if (HarmonyInstance.DEBUG) { FileLog.FlushBuffer(); } } }
public static DynamicMethod CreatePatchedMethod(MethodBase original, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <TranspilerImpl> transpilers, PatchFlags flags) { if (HarmonyInstance.DEBUG) { FileLog.Log("PATCHING " + original.DeclaringType + " " + original); } var idx = prefixes.Count() + postfixes.Count(); var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx); var il = patch.GetILGenerator(); var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var privateVars = new Dictionary <string, LocalBuilder>(); LocalBuilder resultVariable = null; if (idx > 0) { resultVariable = DynamicTools.DeclareLocalVariable(il, AccessTools.GetReturnedType(original)); privateVars[RESULT_VAR] = resultVariable; } prefixes.ForEach(prefix => { prefix.GetParameters() .Where(patchParam => patchParam.Name == STATE_VAR) .Do(patchParam => { var privateStateVariable = DynamicTools.DeclareLocalVariable(il, patchParam.ParameterType); privateVars[prefix.DeclaringType.FullName] = privateStateVariable; }); }); var afterOriginal1 = il.DefineLabel(); var afterOriginal2 = il.DefineLabel(); bool canHaveJump = false; if (flags.HasFlag(PatchFlags.PF_Detour) == false) { canHaveJump = AddPrefixes(il, original, prefixes, privateVars, afterOriginal2); if (flags.HasFlag(PatchFlags.PF_NoOrigin) == false) { var copier = new MethodCopier(original, patch, originalVariables); foreach (var transpiler in transpilers) { copier.AddTranspiler(transpiler); } copier.Emit(afterOriginal1); Emitter.MarkLabel(il, afterOriginal1); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } } } if (canHaveJump) { Emitter.MarkLabel(il, afterOriginal2); } if (AddPostfixes(il, original, postfixes, privateVars)) { if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } } if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } Emitter.Emit(il, OpCodes.Ret); if (HarmonyInstance.DEBUG) { FileLog.Log("DONE"); FileLog.Log(""); } DynamicTools.PrepareDynamicMethod(patch); return(patch); }
public static DynamicMethod CreatePatchedMethod(MethodBase original, string harmonyInstanceID, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <MethodInfo> transpilers) { Memory.MarkForNoInlining(original); if (original == null) { throw new ArgumentNullException(nameof(original), "Original method is null. Did you specify it correctly?"); } try { if (HarmonyInstance.DEBUG) { FileLog.LogBuffered("### Patch " + original.DeclaringType + ", " + original); FileLog.FlushBuffer(); } var idx = prefixes.Count() + postfixes.Count(); var firstArgIsReturnBuffer = NativeThisPointer.NeedsNativeThisPointerFix(original); var returnType = AccessTools.GetReturnedType(original); var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx); if (patch == null) { return(null); } var il = patch.GetILGenerator(); var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var privateVars = new Dictionary <string, LocalBuilder>(); LocalBuilder resultVariable = null; if (idx > 0) { resultVariable = DynamicTools.DeclareLocalVariable(il, returnType); privateVars[RESULT_VAR] = resultVariable; } prefixes.Union(postfixes).ToList().ForEach(fix => { if (privateVars.ContainsKey(fix.DeclaringType.FullName) == false) { fix.GetParameters() .Where(patchParam => patchParam.Name == STATE_VAR) .Do(patchParam => { var privateStateVariable = DynamicTools.DeclareLocalVariable(il, patchParam.ParameterType); privateVars[fix.DeclaringType.FullName] = privateStateVariable; }); } }); if (firstArgIsReturnBuffer) { Emitter.Emit(il, original.IsStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1); } var skipOriginalLabel = il.DefineLabel(); var canHaveJump = AddPrefixes(il, original, prefixes, privateVars, skipOriginalLabel); var copier = new MethodCopier(original, il, originalVariables); foreach (var transpiler in transpilers) { copier.AddTranspiler(transpiler); } var endLabels = new List <Label>(); var endBlocks = new List <ExceptionBlock>(); copier.Finalize(endLabels, endBlocks); foreach (var label in endLabels) { Emitter.MarkLabel(il, label); } foreach (var block in endBlocks) { Emitter.MarkBlockAfter(il, block); } if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } if (canHaveJump) { Emitter.MarkLabel(il, skipOriginalLabel); } AddPostfixes(il, original, postfixes, privateVars, false); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } AddPostfixes(il, original, postfixes, privateVars, true); if (firstArgIsReturnBuffer) { Emitter.Emit(il, OpCodes.Stobj, returnType); } Emitter.Emit(il, OpCodes.Ret); if (HarmonyInstance.DEBUG) { FileLog.LogBuffered("DONE"); FileLog.LogBuffered(""); FileLog.FlushBuffer(); } DynamicTools.PrepareDynamicMethod(patch); return(patch); } catch (Exception ex) { var exceptionString = "Exception from HarmonyInstance \"" + harmonyInstanceID + "\" patching " + original.FullDescription(); if (HarmonyInstance.DEBUG) { FileLog.Log("Exception: " + exceptionString); } throw new Exception(exceptionString, ex); } finally { if (HarmonyInstance.DEBUG) { FileLog.FlushBuffer(); } } }
public static DynamicMethod CreatePatchedMethod(MethodBase original, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <ICodeProcessor> processors) { if (MethodCopier.DEBUG_OPCODES) { FileLog.Log("PATCHING " + original.DeclaringType + " " + original); } var idx = prefixes.Count() + postfixes.Count(); var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx); var il = patch.GetILGenerator(); var originalVariables = DynamicTools.DeclareLocalVariables(original, il); var resultVariable = DynamicTools.DeclareLocalVariable(il, AccessTools.GetReturnedType(original)); var privateVars = new Dictionary <string, LocalBuilder>(); privateVars[RESULT_VAR] = resultVariable; prefixes.ForEach(prefix => { prefix.GetParameters() .Where(patchParam => patchParam.Name == STATE_VAR) .Do(patchParam => { var privateStateVariable = DynamicTools.DeclareLocalVariable(il, patchParam.ParameterType); privateVars[prefix.DeclaringType.FullName] = privateStateVariable; }); }); var afterOriginal1 = il.DefineLabel(); var afterOriginal2 = il.DefineLabel(); var canHaveJump = AddPrefixes(il, original, prefixes, privateVars, afterOriginal2); var copier = new MethodCopier(original, patch, originalVariables); foreach (var processor in processors) { copier.AddReplacement(processor); } copier.AddReplacement(new RetToBrAfterProcessor(afterOriginal1)); copier.Emit(); Emitter.MarkLabel(il, afterOriginal1); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Stloc, resultVariable); } if (canHaveJump) { Emitter.MarkLabel(il, afterOriginal2); } AddPostfixes(il, original, postfixes, privateVars); if (resultVariable != null) { Emitter.Emit(il, OpCodes.Ldloc, resultVariable); } Emitter.Emit(il, OpCodes.Ret); if (MethodCopier.DEBUG_OPCODES) { FileLog.Log("DONE"); FileLog.Log(""); } DynamicTools.PrepareDynamicMethod(patch); return(patch); }