|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Reflection; |
| 3 | +using System.Xml.Linq; |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using Mono.Cecil; |
| 7 | + |
| 8 | +namespace RemapDllTarget |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + var filename = args[0]; |
| 15 | + Console.WriteLine($"Loading existing DLL {filename}..."); |
| 16 | + var def = AssemblyDefinition.ReadAssembly(filename, new ReaderParameters { |
| 17 | + ReadingMode = ReadingMode.Immediate, |
| 18 | + ReadWrite = true, |
| 19 | + InMemory = true |
| 20 | + }); |
| 21 | + |
| 22 | + var mappings = args.Skip(1) |
| 23 | + .Select(x => x.Split('=', 2).ToArray()) |
| 24 | + .ToDictionary(x => x[0], x => new ModuleReference(x[1])); |
| 25 | + |
| 26 | + Console.WriteLine($"Remapping DllImport paths in {filename}..."); |
| 27 | + foreach (var kvp in mappings) { |
| 28 | + Console.WriteLine($"{kvp.Key} => {kvp.Value}"); |
| 29 | + } |
| 30 | + |
| 31 | + var module = def.MainModule; |
| 32 | + foreach (var mref in mappings.Values) |
| 33 | + module.ModuleReferences.Add(mref); |
| 34 | + |
| 35 | + foreach (var type in def.MainModule.Types) { |
| 36 | + foreach (var func in type.Methods) { |
| 37 | + if (func.HasPInvokeInfo) { |
| 38 | + var info = func.PInvokeInfo; |
| 39 | + if (mappings.TryGetValue(info.Module.Name, out ModuleReference newRef)) { |
| 40 | + Console.WriteLine($"Remapping {info.EntryPoint}: {info.Module} => {newRef}"); |
| 41 | + info.Module = newRef; |
| 42 | + } |
| 43 | + |
| 44 | + func.PInvokeInfo = info; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Console.WriteLine($"Writing result"); |
| 50 | + def.Write(filename); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments