public void ClearAllButLast(string typeName, string methodName)
        {
            MethodDefinition method     = CecilHelper.GetMethodDefinition(this._targetModule, typeName, methodName);
            MethodBody       methodBody = method.Body;

            this.ClearAllButLast(methodBody);
        }
        public void ReplaceByNopAt(string typeName, string methodName, int instructionIndex)
        {
            MethodDefinition method     = CecilHelper.GetMethodDefinition(this._targetModule, typeName, methodName);
            MethodBody       methodBody = method.Body;

            this.ReplaceByNop(methodBody, methodBody.Instructions[instructionIndex]);
        }
        public void InjectBefore(
            string sourceTypeName,
            string sourceMethodName,
            string targetTypeName,
            string targetMethodName,
            int instructionIndex,
            bool includeCallingObject = false,
            int includeArgumentCount  = 0,
            bool passArgumentsByRef   = false,
            bool useFullName          = false)
        {
            MethodBody targetMethodBody = CecilHelper
                                          .GetMethodDefinition(
                this._targetModule,
                targetTypeName,
                targetMethodName,
                useFullName).Body;
            Instruction instruction = targetMethodBody.Instructions[instructionIndex];

            this.InjectBefore(
                sourceTypeName,
                sourceMethodName,
                targetMethodBody,
                instruction,
                includeCallingObject,
                includeArgumentCount,
                passArgumentsByRef,
                useFullName);
        }
Example #4
0
        private void InjectOnionDebugHandler()
        {
            TypeDefinition debugHandler = this._csharpModule.Types.FirstOrDefault(type => type.Name == "DebugHandler");

            if (debugHandler != null)
            {
                PropertyDefinition debugHandlerEnabledProperty =
                    debugHandler.Properties.FirstOrDefault(property => property.Name == "enabled");

                if (debugHandlerEnabledProperty != null)
                {
                    debugHandlerEnabledProperty.SetMethod.IsPublic = true;
                }
            }
            else
            {
                Console.WriteLine("Can't find type DebugHandler");

                this.Failed = true;
            }

            MethodBody debugHandlerConstructorBody =
                CecilHelper.GetMethodDefinition(this._csharpModule, debugHandler, ".ctor").Body;

            Instruction lastInstruction = debugHandlerConstructorBody.Instructions.Last();

            this._onionToCSharpInjector.InjectBefore(
                "Hooks",
                "OnDebugHandlerCtor",
                debugHandlerConstructorBody,
                lastInstruction);
        }
Example #5
0
        // TODO: notify user when injection fails
        private void ExtendMaxActionCount()
        {
            const int MaxActionCount = 1000;

            TypeDefinition kInputController =
                this._firstPassModule.Types.FirstOrDefault(type => type.Name == "KInputController");

            if (kInputController != null)
            {
                TypeDefinition keyDef =
                    kInputController.NestedTypes.FirstOrDefault(nestedType => nestedType.Name == "KeyDef");

                if (keyDef != null)
                {
                    MethodBody keyDefConstructorBody = keyDef.Methods.First(method => method.Name == ".ctor").Body;

                    keyDefConstructorBody.Instructions.Last(instruction => instruction.OpCode == OpCodes.Ldc_I4)
                    .Operand = MaxActionCount;

                    MethodBody kInputControllerConstructorBody = CecilHelper
                                                                 .GetMethodDefinition(
                        this._firstPassModule,
                        kInputController,
                        ".ctor").Body;

                    kInputControllerConstructorBody
                    .Instructions.First(instruction => instruction.OpCode == OpCodes.Ldc_I4).Operand = MaxActionCount;
                }
                else
                {
                    Console.WriteLine("Can't find type KInputController.KeyDef");

                    this.Failed = true;
                }
            }
            else
            {
                Console.WriteLine("Can't find type KInputController");

                this.Failed = true;
            }
        }
Example #6
0
        /*
         * is disabled in:
         * FrontEndManager.LateUpdate()
         * Game.Update()
         * GraphicsOptionsScreen.Update()
         * OptionsMenuScreen.Update()
         * ReportErrorDialog.Update()
         *
         * //TODO: make it more flexible for future versions
         * // Doesn't work on Tubular Upgrade
         * private void EnableConsole()
         * {
         *  try
         *  {
         *      _csharpModule
         *          .Types.FirstOrDefault(t => t.Name == "Game")
         *              .Methods.FirstOrDefault(m => m.Name == "Update")
         *                  .Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Ldc_I4_0)
         *                     .OpCode = OpCodes.Ldc_I4_1;
         *
         *      _csharpModule
         *          .Types.FirstOrDefault(t => t.Name == "FrontEndManager")
         *              .Methods.FirstOrDefault(m => m.Name == "LateUpdate")
         *                  .Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Ldc_I4_0)
         *                     .OpCode = OpCodes.Ldc_I4_1;
         *
         *      _csharpModule
         *          .Types.FirstOrDefault(t => t.Name == "GraphicsOptionsScreen")
         *              .Methods.FirstOrDefault(m => m.Name == "Update")
         *                  .Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Ldc_I4_0)
         *                     .OpCode = OpCodes.Ldc_I4_1;
         *
         *      _csharpModule
         *          .Types.FirstOrDefault(t => t.Name == "OptionsMenuScreen")
         *              .Methods.FirstOrDefault(m => m.Name == "Update")
         *                  .Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Ldc_I4_0)
         *                     .OpCode = OpCodes.Ldc_I4_1;
         *
         *      _csharpModule
         *          .Types.FirstOrDefault(t => t.Name == "ReportErrorDialog")
         *              .Methods.FirstOrDefault(m => m.Name == "Update")
         *                  .Body.Instructions.FirstOrDefault(i => i.OpCode == OpCodes.Ldc_I4_0)
         *                     .OpCode = OpCodes.Ldc_I4_1;
         *  }
         *  catch (Exception e)
         *  {
         *      Console.WriteLine("Enable console failed");
         *      Console.WriteLine(e);
         *
         *      Failed = true;
         *  }
         * }
         */

        private void FixGameUpdateExceptionHandling()
        {
            ExceptionHandler handler    = new ExceptionHandler(ExceptionHandlerType.Finally);
            MethodBody       methodBody =
                CecilHelper.GetMethodDefinition(this._csharpModule, "Game", "Update").Body;
            Collection <Instruction> methodInstructions = methodBody.Instructions;

            handler.TryStart = methodInstructions.First(instruction => instruction.OpCode == OpCodes.Ldsfld);

            Instruction tryEndInstruction =
                methodInstructions.Last(instruction => instruction.OpCode == OpCodes.Ldloca_S);

            handler.TryEnd       = tryEndInstruction;
            handler.HandlerStart = tryEndInstruction;
            handler.HandlerEnd   = methodInstructions.Last();
            handler.CatchType    = this._csharpModule.ImportReference(typeof(Exception));

            methodBody.ExceptionHandlers.Clear();
            methodBody.ExceptionHandlers.Add(handler);
        }
        public void InjectBefore(
            string sourceTypeName,
            string sourceMethodName,
            MethodBody targetMethodBody,
            Instruction targetInstruction,
            bool includeCallingObject = false,
            int includeArgumentCount  = 0,
            bool passArgumentsByRef   = false,
            bool useFullName          = false)
        {
            MethodDefinition sourceMethod =
                CecilHelper.GetMethodDefinition(this._sourceModule, sourceTypeName, sourceMethodName, useFullName);
            MethodReference sourceMethodReference = CecilHelper.GetMethodReference(this._targetModule, sourceMethod);

            this.InjectBefore(
                sourceMethodReference,
                targetMethodBody,
                targetInstruction,
                includeCallingObject,
                includeArgumentCount,
                passArgumentsByRef);
        }
 private MethodBody GetMethodBody(string typeName, string methodName) =>
 CecilHelper.GetMethodDefinition(this._targetModule, typeName, methodName).Body;
        public void MakeMethodPublic(string typeName, string methodName)
        {
            MethodDefinition method = CecilHelper.GetMethodDefinition(this._targetModule, typeName, methodName);

            method.IsPublic = true;
        }