diff options
author | aszlig <aszlig@nix.build> | 2018-07-18 11:47:03 +0200 |
---|---|---|
committer | aszlig <aszlig@nix.build> | 2018-07-19 06:35:24 +0200 |
commit | ec65da1fbc93bd4fcdbdf20cb41c085ece53473c (patch) | |
tree | c73242ef0680067486de97f5ddca30fdd06bcc16 /pkgs/games/build-support/monogame-patcher/src/patcher.cs | |
parent | be80103bd659e922569deb9c52982a8c081fb698 (diff) |
monogame-patcher: Allow specifying extra assembly
If we want to replace a call with one that's not in a module which is currently referenced by the target file we now have another command line flag for replace-call (-a) where we can specify the additional DLL file. Signed-off-by: aszlig <aszlig@nix.build>
Diffstat (limited to 'pkgs/games/build-support/monogame-patcher/src/patcher.cs')
-rw-r--r-- | pkgs/games/build-support/monogame-patcher/src/patcher.cs | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/pkgs/games/build-support/monogame-patcher/src/patcher.cs b/pkgs/games/build-support/monogame-patcher/src/patcher.cs index 6aeaa324..961a6b7e 100644 --- a/pkgs/games/build-support/monogame-patcher/src/patcher.cs +++ b/pkgs/games/build-support/monogame-patcher/src/patcher.cs @@ -21,28 +21,36 @@ class Command { this.outfile = options.outputFile; this.infile = options.inputFile; + var readOnly = this.infile != this.outfile; + this.module = this.read_module(this.infile, readOnly); + } + + protected ModuleDefinition read_module(string path, bool readOnly) { var resolver = new DefaultAssemblyResolver(); - resolver.AddSearchDirectory(Path.GetDirectoryName(this.infile)); + resolver.AddSearchDirectory(Path.GetDirectoryName(path)); var rp = new ReaderParameters { - ReadWrite = this.infile == this.outfile, + ReadWrite = !readOnly, AssemblyResolver = resolver }; - this.module = ModuleDefinition.ReadModule(this.infile, rp); + return ModuleDefinition.ReadModule(path, rp); + } + + protected virtual IEnumerable<TypeDefinition> get_assembly_types() { + return this.module.AssemblyReferences + .Select(a => this.module.AssemblyResolver.Resolve(a)) + .SelectMany(r => r.MainModule.Types); } protected MethodReference find_method_ref(string fullSig) { - foreach (var aref in this.module.AssemblyReferences) { - var resolved = this.module.AssemblyResolver.Resolve(aref); - foreach (var type in resolved.MainModule.Types) { - foreach (var ctor in type.GetConstructors()) { - if (ctor.ToString() != fullSig) continue; - return this.module.ImportReference(ctor); - } - foreach (var meth in type.GetMethods()) { - if (meth.ToString() != fullSig) continue; - return this.module.ImportReference(meth); - } + foreach (var type in this.get_assembly_types()) { + foreach (var ctor in type.GetConstructors()) { + if (ctor.ToString() != fullSig) continue; + return this.module.ImportReference(ctor); + } + foreach (var meth in type.GetMethods()) { + if (meth.ToString() != fullSig) continue; + return this.module.ImportReference(meth); } } @@ -99,8 +107,12 @@ class FixFileStreams : Command { class ReplaceCall : Command { private string search; private MethodReference replace; + private ModuleDefinition targetModule; public ReplaceCall(ReplaceCallCmd options) : base(options) { + if (options.assemblyFile != null) + this.targetModule = this.read_module(options.assemblyFile, true); + this.search = options.replaceMethod; this.replace = this.find_method_ref(options.replacementMethod); @@ -113,6 +125,13 @@ class ReplaceCall : Command { this.save(); } + protected override IEnumerable<TypeDefinition> get_assembly_types() { + if (this.targetModule != null) + return this.targetModule.Types; + else + return base.get_assembly_types(); + } + private void patch_method(MethodDefinition md) { var il = md.Body.GetILProcessor(); |