Exemple #1
0
        internal static List <CodeInstruction> GetInstructions(ILGenerator generator, MethodBase method, int maxTranspilers)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            var originalVariables     = MethodPatcher.DeclareLocalVariables(generator, method);
            var useStructReturnBuffer = StructReturnBuffer.NeedsFix(method);
            var copier = new MethodCopier(method, generator, originalVariables);

            copier.SetArgumentShift(useStructReturnBuffer);

            var info = Harmony.GetPatchInfo(method);

            if (info != null)
            {
                var sortedTranspilers = PatchFunctions.GetSortedPatchMethods(method, info.Transpilers.ToArray(), false);
                for (var i = 0; i < maxTranspilers && i < sortedTranspilers.Count; i++)
                {
                    copier.AddTranspiler(sortedTranspilers[i]);
                }
            }

            return(copier.Finalize(null, null, out var _));
        }
Exemple #2
0
        internal static List <CodeInstruction> GetInstructions(ILGenerator generator, MethodBase method, int maxTranspilers)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            var originalVariables     = MethodPatcher.DeclareLocalVariables(generator, method);
            var useStructReturnBuffer = StructReturnBuffer.NeedsFix(method);
            var copier = new MethodCopier(method, generator, originalVariables);

            copier.SetArgumentShift(useStructReturnBuffer);

            var info = Harmony.GetPatchInfo(method);

            if (info != null)
            {
                var sortedTranspilers = PatchFunctions.GetSortedPatchMethods(method, info.Transpilers.ToArray(), false);
                for (var i = 0; i < maxTranspilers && i < sortedTranspilers.Count; i++)
                {
                    copier.AddTranspiler(sortedTranspilers[i]);
                }
            }

            var endLabels = new List <Label>();
            var emitter   = new Emitter(generator, false);

            copier.Finalize(emitter, endLabels, out var hasReturnCode);
            return(emitter.GetInstructions().OrderBy(pair => pair.Key).Select(pair => pair.Value).ToList());
        }
Exemple #3
0
        internal MethodInfo CreateReplacement(out Dictionary <int, CodeInstruction> finalInstructions)
        {
            var originalVariables = DeclareLocalVariables(il, source ?? original);
            var privateVars       = new Dictionary <string, LocalBuilder>();

            LocalBuilder resultVariable = null;

            if (idx > 0)
            {
                resultVariable          = DeclareLocalVariable(returnType, true);
                privateVars[RESULT_VAR] = resultVariable;
            }

            Label?       skipOriginalLabel   = null;
            LocalBuilder runOriginalVariable = null;

            if (prefixes.Any(fix => PrefixAffectsOriginal(fix)))
            {
                runOriginalVariable = DeclareLocalVariable(typeof(bool));
                emitter.Emit(OpCodes.Ldc_I4_1);
                emitter.Emit(OpCodes.Stloc, runOriginalVariable);

                skipOriginalLabel = il.DefineLabel();
            }

            prefixes.Union(postfixes).Union(finalizers).ToList().ForEach(fix =>
            {
                if (fix.DeclaringType is object && privateVars.ContainsKey(fix.DeclaringType.FullName) is false)
                {
                    fix.GetParameters()
                    .Where(patchParam => patchParam.Name == STATE_VAR)
                    .Do(patchParam =>
                    {
                        var privateStateVariable = DeclareLocalVariable(patchParam.ParameterType);
                        privateVars[fix.DeclaringType.FullName] = privateStateVariable;
                    });
                }
            });

            LocalBuilder finalizedVariable = null;

            if (finalizers.Any())
            {
                finalizedVariable = DeclareLocalVariable(typeof(bool));

                privateVars[EXCEPTION_VAR] = DeclareLocalVariable(typeof(Exception));

                // begin try
                emitter.MarkBlockBefore(new ExceptionBlock(ExceptionBlockType.BeginExceptionBlock), out _);
            }

            AddPrefixes(privateVars, runOriginalVariable);
            if (skipOriginalLabel.HasValue)
            {
                emitter.Emit(OpCodes.Ldloc, runOriginalVariable);
                emitter.Emit(OpCodes.Brfalse, skipOriginalLabel.Value);
            }

            var copier = new MethodCopier(source ?? original, il, originalVariables);

            copier.SetArgumentShift(useStructReturnBuffer);
            copier.SetDebugging(debug);

            foreach (var transpiler in transpilers)
            {
                copier.AddTranspiler(transpiler);
            }

            var endLabels = new List <Label>();

            _ = copier.Finalize(emitter, endLabels, out var hasReturnCode);

            foreach (var label in endLabels)
            {
                emitter.MarkLabel(label);
            }
            if (resultVariable is object)
            {
                emitter.Emit(OpCodes.Stloc, resultVariable);
            }
            if (skipOriginalLabel.HasValue)
            {
                emitter.MarkLabel(skipOriginalLabel.Value);
            }

            _ = AddPostfixes(privateVars, false);

            if (resultVariable is object)
            {
                emitter.Emit(OpCodes.Ldloc, resultVariable);
            }

            var needsToStorePassthroughResult = AddPostfixes(privateVars, true);

            var hasFinalizers = finalizers.Any();

            if (hasFinalizers)
            {
                if (needsToStorePassthroughResult)
                {
                    emitter.Emit(OpCodes.Stloc, resultVariable);
                    emitter.Emit(OpCodes.Ldloc, resultVariable);
                }

                _ = AddFinalizers(privateVars, false);
                emitter.Emit(OpCodes.Ldc_I4_1);
                emitter.Emit(OpCodes.Stloc, finalizedVariable);
                var noExceptionLabel1 = il.DefineLabel();
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Brfalse, noExceptionLabel1);
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Throw);
                emitter.MarkLabel(noExceptionLabel1);

                // end try, begin catch
                emitter.MarkBlockBefore(new ExceptionBlock(ExceptionBlockType.BeginCatchBlock), out var label);
                emitter.Emit(OpCodes.Stloc, privateVars[EXCEPTION_VAR]);

                emitter.Emit(OpCodes.Ldloc, finalizedVariable);
                var endFinalizerLabel = il.DefineLabel();
                emitter.Emit(OpCodes.Brtrue, endFinalizerLabel);

                var rethrowPossible = AddFinalizers(privateVars, true);

                emitter.MarkLabel(endFinalizerLabel);

                var noExceptionLabel2 = il.DefineLabel();
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Brfalse, noExceptionLabel2);
                if (rethrowPossible)
                {
                    emitter.Emit(OpCodes.Rethrow);
                }
                else
                {
                    emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                    emitter.Emit(OpCodes.Throw);
                }
                emitter.MarkLabel(noExceptionLabel2);

                // end catch
                emitter.MarkBlockAfter(new ExceptionBlock(ExceptionBlockType.EndExceptionBlock));

                if (resultVariable is object)
                {
                    emitter.Emit(OpCodes.Ldloc, resultVariable);
                }
            }

            if (useStructReturnBuffer)
            {
                var tmpVar = DeclareLocalVariable(returnType);
                emitter.Emit(OpCodes.Stloc, tmpVar);
                emitter.Emit(original.IsStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1);
                emitter.Emit(OpCodes.Ldloc, tmpVar);
                emitter.Emit(OpCodes.Stobj, returnType);                 // store result into ref
            }

            if (hasFinalizers || hasReturnCode)
            {
                emitter.Emit(OpCodes.Ret);
            }

            finalInstructions = emitter.GetInstructions();

            if (debug)
            {
                FileLog.LogBuffered("DONE");
                FileLog.LogBuffered("");
                FileLog.FlushBuffer();
            }

            return(patch.Generate().Pin());
        }
Exemple #4
0
        public static DynamicMethod CreatePatchedMethod(MethodBase original, MethodBase source, string harmonyInstanceID, List <MethodInfo> prefixes, List <MethodInfo> postfixes, List <MethodInfo> transpilers, List <MethodInfo> finalizers)
        {
            try
            {
                if (original == null)
                {
                    throw new ArgumentNullException(nameof(original));
                }

                Memory.MarkForNoInlining(original);

                if (Harmony.DEBUG)
                {
                    FileLog.LogBuffered("### Patch " + original.FullDescription());
                    FileLog.FlushBuffer();
                }

                var idx = prefixes.Count() + postfixes.Count() + finalizers.Count();
                var firstArgIsReturnBuffer = NativeThisPointer.NeedsNativeThisPointerFix(original);
                var returnType             = AccessTools.GetReturnedType(original);
                var hasFinalizers          = finalizers.Any();
                var patch = DynamicTools.CreateDynamicMethod(original, "_Patch" + idx);
                if (patch == null)
                {
                    return(null);
                }

                var il = patch.GetILGenerator();

                var originalVariables = DynamicTools.DeclareLocalVariables(source ?? 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).Union(finalizers).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;
                        });
                    }
                });

                LocalBuilder finalizedVariable = null;
                if (hasFinalizers)
                {
                    finalizedVariable = DynamicTools.DeclareLocalVariable(il, typeof(bool));

                    privateVars[EXCEPTION_VAR] = DynamicTools.DeclareLocalVariable(il, typeof(Exception));

                    // begin try
                    Emitter.MarkBlockBefore(il, new ExceptionBlock(ExceptionBlockType.BeginExceptionBlock), out _);
                }

                if (firstArgIsReturnBuffer)
                {
                    Emitter.Emit(il, OpCodes.Ldarg_1);                     // load ref to return value
                }
                var skipOriginalLabel = il.DefineLabel();
                var canHaveJump       = AddPrefixes(il, original, prefixes, privateVars, skipOriginalLabel);

                var copier = new MethodCopier(source ?? original, il, originalVariables);
                foreach (var transpiler in transpilers)
                {
                    copier.AddTranspiler(transpiler);
                }
                if (firstArgIsReturnBuffer)
                {
                    copier.AddTranspiler(NativeThisPointer.m_ArgumentShiftTranspiler);
                }

                var endLabels = new List <Label>();
                copier.Finalize(endLabels);

                foreach (var label in endLabels)
                {
                    Emitter.MarkLabel(il, label);
                }
                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 (hasFinalizers)
                {
                    AddFinalizers(il, original, finalizers, privateVars, false);
                    Emitter.Emit(il, OpCodes.Ldc_I4_1);
                    Emitter.Emit(il, OpCodes.Stloc, finalizedVariable);
                    var noExceptionLabel1 = il.DefineLabel();
                    Emitter.Emit(il, OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                    Emitter.Emit(il, OpCodes.Brfalse, noExceptionLabel1);
                    Emitter.Emit(il, OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                    Emitter.Emit(il, OpCodes.Throw);
                    Emitter.MarkLabel(il, noExceptionLabel1);

                    // end try, begin catch
                    Emitter.MarkBlockBefore(il, new ExceptionBlock(ExceptionBlockType.BeginCatchBlock), out var label);
                    Emitter.Emit(il, OpCodes.Stloc, privateVars[EXCEPTION_VAR]);

                    Emitter.Emit(il, OpCodes.Ldloc, finalizedVariable);
                    var endFinalizerLabel = il.DefineLabel();
                    Emitter.Emit(il, OpCodes.Brtrue, endFinalizerLabel);

                    var rethrowPossible = AddFinalizers(il, original, finalizers, privateVars, true);

                    Emitter.MarkLabel(il, endFinalizerLabel);

                    var noExceptionLabel2 = il.DefineLabel();
                    Emitter.Emit(il, OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                    Emitter.Emit(il, OpCodes.Brfalse, noExceptionLabel2);
                    if (rethrowPossible)
                    {
                        Emitter.Emit(il, OpCodes.Rethrow);
                    }
                    else
                    {
                        Emitter.Emit(il, OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                        Emitter.Emit(il, OpCodes.Throw);
                    }
                    Emitter.MarkLabel(il, noExceptionLabel2);

                    // end catch
                    Emitter.MarkBlockAfter(il, new ExceptionBlock(ExceptionBlockType.EndExceptionBlock));

                    if (resultVariable != null)
                    {
                        Emitter.Emit(il, OpCodes.Ldloc, resultVariable);
                    }
                }

                if (firstArgIsReturnBuffer)
                {
                    Emitter.Emit(il, OpCodes.Stobj, returnType);                     // store result into ref
                }
                Emitter.Emit(il, OpCodes.Ret);

                if (Harmony.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() + ": " + ex;
                if (Harmony.DEBUG)
                {
                    var savedIndentLevel = FileLog.indentLevel;
                    FileLog.indentLevel = 0;
                    FileLog.Log(exceptionString);
                    FileLog.indentLevel = savedIndentLevel;
                }

                throw new Exception(exceptionString, ex);
            }
            finally
            {
                if (Harmony.DEBUG)
                {
                    FileLog.FlushBuffer();
                }
            }
        }
Exemple #5
0
        internal MethodInfo CreateReplacement(out Dictionary <int, CodeInstruction> finalInstructions)
        {
            var originalVariables = DeclareLocalVariables(source ?? original);
            var privateVars       = new Dictionary <string, LocalBuilder>();

            LocalBuilder resultVariable = null;

            if (idx > 0)
            {
                resultVariable          = DeclareLocalVariable(returnType);
                privateVars[RESULT_VAR] = resultVariable;
            }

            prefixes.Union(postfixes).Union(finalizers).ToList().ForEach(fix =>
            {
                if (fix.DeclaringType != null && privateVars.ContainsKey(fix.DeclaringType.FullName) == false)
                {
                    fix.GetParameters()
                    .Where(patchParam => patchParam.Name == STATE_VAR)
                    .Do(patchParam =>
                    {
                        var privateStateVariable = DeclareLocalVariable(patchParam.ParameterType);
                        privateVars[fix.DeclaringType.FullName] = privateStateVariable;
                    });
                }
            });

            LocalBuilder finalizedVariable = null;

            if (finalizers.Any())
            {
                finalizedVariable = DeclareLocalVariable(typeof(bool));

                privateVars[EXCEPTION_VAR] = DeclareLocalVariable(typeof(Exception));

                // begin try
                emitter.MarkBlockBefore(new ExceptionBlock(ExceptionBlockType.BeginExceptionBlock), out _);
            }

            if (firstArgIsReturnBuffer)
            {
                emitter.Emit(original.IsStatic ? OpCodes.Ldarg_0 : OpCodes.Ldarg_1);                 // load ref to return value
            }
            var skipOriginalLabel = il.DefineLabel();
            var canHaveJump       = AddPrefixes(privateVars, skipOriginalLabel);

            var copier = new MethodCopier(source ?? original, il, originalVariables);

            foreach (var transpiler in transpilers)
            {
                copier.AddTranspiler(transpiler);
            }
            if (firstArgIsReturnBuffer)
            {
                if (original.IsStatic)
                {
                    copier.AddTranspiler(NativeThisPointer.m_ArgumentShiftTranspilerStatic);
                }
                else
                {
                    copier.AddTranspiler(NativeThisPointer.m_ArgumentShiftTranspilerInstance);
                }
            }

            var endLabels = new List <Label>();

            copier.Finalize(emitter, endLabels, out var endingReturn);

            foreach (var label in endLabels)
            {
                emitter.MarkLabel(label);
            }
            if (resultVariable != null)
            {
                emitter.Emit(OpCodes.Stloc, resultVariable);
            }
            if (canHaveJump)
            {
                emitter.MarkLabel(skipOriginalLabel);
            }

            AddPostfixes(privateVars, false);

            if (resultVariable != null)
            {
                emitter.Emit(OpCodes.Ldloc, resultVariable);
            }

            AddPostfixes(privateVars, true);

            var hasFinalizers = finalizers.Any();

            if (hasFinalizers)
            {
                _ = AddFinalizers(privateVars, false);
                emitter.Emit(OpCodes.Ldc_I4_1);
                emitter.Emit(OpCodes.Stloc, finalizedVariable);
                var noExceptionLabel1 = il.DefineLabel();
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Brfalse, noExceptionLabel1);
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Throw);
                emitter.MarkLabel(noExceptionLabel1);

                // end try, begin catch
                emitter.MarkBlockBefore(new ExceptionBlock(ExceptionBlockType.BeginCatchBlock), out var label);
                emitter.Emit(OpCodes.Stloc, privateVars[EXCEPTION_VAR]);

                emitter.Emit(OpCodes.Ldloc, finalizedVariable);
                var endFinalizerLabel = il.DefineLabel();
                emitter.Emit(OpCodes.Brtrue, endFinalizerLabel);

                var rethrowPossible = AddFinalizers(privateVars, true);

                emitter.MarkLabel(endFinalizerLabel);

                var noExceptionLabel2 = il.DefineLabel();
                emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                emitter.Emit(OpCodes.Brfalse, noExceptionLabel2);
                if (rethrowPossible)
                {
                    emitter.Emit(OpCodes.Rethrow);
                }
                else
                {
                    emitter.Emit(OpCodes.Ldloc, privateVars[EXCEPTION_VAR]);
                    emitter.Emit(OpCodes.Throw);
                }
                emitter.MarkLabel(noExceptionLabel2);

                // end catch
                emitter.MarkBlockAfter(new ExceptionBlock(ExceptionBlockType.EndExceptionBlock));

                if (resultVariable != null)
                {
                    emitter.Emit(OpCodes.Ldloc, resultVariable);
                }
            }

            if (firstArgIsReturnBuffer)
            {
                emitter.Emit(OpCodes.Stobj, returnType);                 // store result into ref
            }
            if (hasFinalizers || endingReturn)
            {
                emitter.Emit(OpCodes.Ret);
            }

            finalInstructions = emitter.GetInstructions();

            if (debug)
            {
                FileLog.LogBuffered("DONE");
                FileLog.LogBuffered("");
                FileLog.FlushBuffer();
            }

            return(patch.Generate().Pin());
        }
Exemple #6
0
 /// <summary>Returns the methods current list of code instructions after all existing transpilers have been applied</summary>
 /// <param name="original">The original method/constructor</param>
 /// <param name="generator">A new generator that now contains all local variables and labels contained in the result</param>
 /// <param name="maxTranspilers">Apply only the first count of transpilers</param>
 /// <returns>A list of <see cref="CodeInstruction"/></returns>
 ///
 public static List <CodeInstruction> GetCurrentInstructions(MethodBase original, out ILGenerator generator, int maxTranspilers = int.MaxValue)
 {
     generator = CreateILGenerator(original);
     return(MethodCopier.GetInstructions(generator, original, maxTranspilers));
 }
Exemple #7
0
 /// <summary>Returns the methods unmodified list of code instructions</summary>
 /// <param name="original">The original method/constructor</param>
 /// <param name="generator">A new generator that now contains all local variables and labels contained in the result</param>
 /// <returns>A list containing all the original <see cref="CodeInstruction"/></returns>
 ///
 public static List <CodeInstruction> GetOriginalInstructions(MethodBase original, out ILGenerator generator)
 {
     generator = CreateILGenerator(original);
     return(MethodCopier.GetInstructions(generator, original, 0));
 }
Exemple #8
0
 /// <summary>Returns the methods unmodified list of code instructions</summary>
 /// <param name="original">The original method/constructor</param>
 /// <param name="generator">Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used)</param>
 /// <returns>A list containing all the original <see cref="CodeInstruction"/></returns>
 ///
 public static List <CodeInstruction> GetOriginalInstructions(MethodBase original, ILGenerator generator = null)
 {
     return(MethodCopier.GetInstructions(generator ?? CreateILGenerator(original), original, 0));
 }
Exemple #9
0
 /// <summary>Returns the methods current list of code instructions after all existing transpilers have been applied</summary>
 /// <param name="original">The original method/constructor</param>
 /// <param name="maxTranspilers">Apply only the first count of transpilers</param>
 /// <param name="generator">Optionally an existing generator that will be used to create all local variables and labels contained in the result (if not specified, an internal generator is used)</param>
 /// <returns>A list of <see cref="CodeInstruction"/></returns>
 ///
 public static List <CodeInstruction> GetCurrentInstructions(MethodBase original, int maxTranspilers = int.MaxValue, ILGenerator generator = null)
 {
     return(MethodCopier.GetInstructions(generator ?? CreateILGenerator(), original, maxTranspilers));
 }