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); } var idx = prefixes.Count() + postfixes.Count(); var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx); 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); 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); 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, 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(); } } }