ioncodes / dnpatch

.NET Patcher library using dnlib
MIT License
313 stars 48 forks source link

Add instruction inserting #56

Open loco-choco opened 3 years ago

loco-choco commented 3 years ago

Added so targets can now be inserted while patching.

To insert instructions you have to follow a similar path of replacing them at a given index:

  1. Add Instruction(s) to the Target;
  2. Add the Indices/Index to the Target
  3. Set the bool var InsertInstructions to true (which defaults to false so it is compatible with codes from before this change)
Instruction[] insertingOpCodes = {
                Instruction.Create(OpCodes.Ldstr, "Hello Sir"), // String to print
                Instruction.Create(OpCodes.Call, p.BuildCall(typeof(Console), "WriteLine", typeof(void), new[] { typeof(string) })), // Console.WriteLine call

            };
            Target insertingTarget = new Target()
            {
                Namespace = "Test",
                Class = "Program",
                Method = "Print",
                Instructions = insertingOpCodes,
                Indices = new[] { 3, 4 }, // Insert instructions at given index
                InsertInstructions = true // Tells the patcher to insert and not replace
            };

            p.Patch(insertingTarget);
            p.Save("Test15.exe");

Before the patch: image image

After the patch: image image