public static IEnumerable <CodeInstruction> SetPublicInstanceVariableTranspiler(IEnumerable <CodeInstruction> instr)
        {
            MethodInfo             info     = typeof(DummyModClass).GetMethod("SetPublicInstanceVariable");
            List <CodeInstruction> newInstr = ILTool.MethodToILInstructions(info);

            return(newInstr);
        }
        public static IEnumerable <CodeInstruction> SetPrivateInstanceVariableTranspiler(IEnumerable <CodeInstruction> instr)
        {
            MethodInfo info   = typeof(DummyModClass).GetMethod("SetPrivateInstanceVariable");
            ILTool     parser = new ILTool(instr);

            parser.ReplaceMethod(info);
            return(parser.instructions);
        }
        public static IEnumerable <CodeInstruction> ReplaceMethodTranspiler(IEnumerable <CodeInstruction> instr)
        {
            MethodInfo info = typeof(DummyModClass).GetMethod("SomeMethod");

            ILTool tool = new ILTool(instr);

            tool.ReplaceMethod(info);

            return(tool.instructions);
        }
        public void PrintIL()
        {
            MethodInfo thingMethod   = typeof(DummyModClass).GetMethod("SearchStatement");
            MethodInfo complexMethod = typeof(DummyCompiledClass).GetMethod("ComplexMethod");

            Trace.WriteLine("--SomeMethod--");
            ILTool.MethodToILInstructions(thingMethod).Do(x => Trace.WriteLine(x));
            Trace.WriteLine("");
            Trace.WriteLine("--ComplexMethod--");
            ILTool.MethodToILInstructions(complexMethod).Do(x => Trace.WriteLine(x));
        }
        public static IEnumerable <CodeInstruction> RemoveStatementsNoReturnTranspiler(IEnumerable <CodeInstruction> instr)
        {
            List <CodeInstruction> instructions = new List <CodeInstruction>(instr);

            MethodInfo removeMethod = typeof(DummyModClass).GetMethod("SearchStatement");

            ILTool parser = new ILTool(instructions);

            parser.RemoveCodeBlock(removeMethod);

            return(parser.instructions);
        }
        public static IEnumerable <CodeInstruction> AddStatementsAfterTranspiler(IEnumerable <CodeInstruction> instr)
        {
            List <CodeInstruction> instructions = new List <CodeInstruction>(instr);

            MethodInfo searchMethod      = typeof(DummyModClass).GetMethod("ForSearchStatements");
            MethodInfo replacementMethod = typeof(DummyModClass).GetMethod("ReplacementStatement");

            ILTool parser = new ILTool(instructions);

            parser.AddCodeBlockAfter(searchMethod, replacementMethod);

            return(parser.instructions);
        }
        public static IEnumerable <CodeInstruction> ReplaceStatementsWithReturnTranspiler(IEnumerable <CodeInstruction> instr)
        {
            List <CodeInstruction> instructions = new List <CodeInstruction>(instr);

            MethodInfo searchMethod      = typeof(DummyCompiledClass).GetMethod("SomeMethod");
            MethodInfo replacementMethod = typeof(DummyModClass).GetMethod("SomeMethod");

            ILTool parser = new ILTool(instructions);

            parser.ReplaceAllCodeBlocks(searchMethod, replacementMethod);

            return(parser.instructions);
        }
        /// <summary>
        /// Used strictly and only used in CompileIL test.
        /// This method compares the IL code of Harmony's transpiler and ILHelpers transpiler
        /// </summary>
        /// <param name="instr">Harmony's transpiled IL code</param>
        /// <returns>Instructions to replace patch statement</returns>
        public static IEnumerable <CodeInstruction> ILComparison(IEnumerable <CodeInstruction> instr)
        {
            // Complex method is used
            MethodInfo methodToCompare = typeof(DummyCompiledClass).GetMethod("ComplexMethod");

            List <CodeInstruction> helperInstructions  = ILTool.MethodToILInstructions(methodToCompare);
            List <CodeInstruction> harmonyInstructions = (List <CodeInstruction>)instr;

            // Verify helper and harmony instructions are equal
            Assert.AreEqual(harmonyInstructions.Count, helperInstructions.Count);

            for (int i = 0; i < helperInstructions.Count; i++)
            {
                // Check opcodes are equal
                Assert.AreEqual(harmonyInstructions[i].opcode, helperInstructions[i].opcode);
                // Check operands are equal
                Assert.AreEqual(harmonyInstructions[i].operand, helperInstructions[i].operand);

                // Check exception blocks are equal
                Assert.AreEqual(harmonyInstructions[i].blocks.Count, helperInstructions[i].blocks.Count);

                for (int j = 0; j < harmonyInstructions[i].blocks.Count; j++)
                {
                    Assert.AreEqual(harmonyInstructions[i].blocks[j], helperInstructions[i].blocks[j]);
                }

                // Check labels are equal
                Assert.AreEqual(harmonyInstructions[i].labels.Count, helperInstructions[i].labels.Count);

                for (int j = 0; j < harmonyInstructions[i].labels.Count; j++)
                {
                    Assert.AreEqual(harmonyInstructions[i].labels[j], helperInstructions[i].labels[j]);
                }
            }

            return(instr);
        }