ioncodes / dnpatch

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

Jump helper function #29

Open DenJur opened 7 years ago

DenJur commented 7 years ago

Currently building jump instructions is a bit annoying since you have to initialize them outside the instruction array. It becomes worse when you need to change it later. I would like to see an option similar to method call builder that would allow to create those jumps while initializing instruction array. One idea I had was to extend Instruction with an optional string id field and create instruction child type (ResolveLaterInstruction?) that can be replaced by a processor before patching. Ideally I would like instruction array initialization to look something like this.

Instruction[] opCodes = {
                Instruction.Create(OpCodes.Ldarg_0),
                Instruction.Create(OpCodes.Call, p.BuildCall(typeof(Mod), "get_Assembly", typeof(System.Reflection.Assembly), new Type[]{})),
                Instruction.BuildJump(OpCodes.Brtrue_S, "assemblyIsNull"),
                Instruction.Create(OpCodes.Ldc_I4_1),
                Instruction.Create(OpCodes.Ret),
                Instruction.Create(OpCodes.Ldc_I4_0, null, "assemblyIsNull"),
                Instruction.Create(OpCodes.Ret)
            };

After method Patch is called it goes through the list of instructions and resolves those jumps. This way some errors can be thrown before dnlib tries to save the assembly, it is easier to keep track what jumps where and it is easier to modify whole thing later.

This is from the top of the head solution and I have not looked at the code so sorry if there are some obvious problems with it.

ioncodes commented 7 years ago

Thanks for this great idea! I will add this to the v1 pool :)