private void LoadDependencies(ModuleDefMD module) { if (seenModules.Contains(module.Location)) { return; } seenModules.Add(module.Location); Modules.Add(module); Resolver.AddToCache(module); foreach (var assemblyRef in module.GetAssemblyRefs()) { // There are cases, where the Resolver will not be able to resolve the assemblies // automatically, even if they are in the same directory. // (maybe this has todo with linux / specific mono versions?) // So, try to load them manually recursively first. var subModuleFile = Path.Combine(Path.GetDirectoryName(module.Location), assemblyRef.Name + ".dll"); if (File.Exists(subModuleFile)) { var subModule = ModuleDefMD.Load(subModuleFile, Resolver.DefaultModuleContext); if (subModule != null) { LoadDependencies(subModule); } } var assembly = Resolver.ResolveThrow(assemblyRef, null); foreach (var moduleRef in assembly.Modules) { LoadDependencies((ModuleDefMD)moduleRef); } } }
public void Patch() { var resolver = new Resolver(this); var context = new ModuleContext(resolver); resolver.Context = context; bool modified = false; logger.Log($"Loading {AssemblyCSharp.Name}..."); var assemblyCSharp = ModuleDefMD.Load(File.ReadAllBytes(AssemblyCSharp.FullName), context); logger.Log(" -> Loaded: {0}", assemblyCSharp.FullName); resolver.AddToCache(assemblyCSharp.Assembly); logger.Log($"Loading {AssemblyCSharpFirstpass.Name}..."); var assemblyCSharpFirstpass = ModuleDefMD.Load(File.ReadAllBytes(AssemblyCSharpFirstpass.FullName), context); logger.Log(" -> Loaded: {0}", assemblyCSharpFirstpass.FullName); resolver.AddToCache(assemblyCSharpFirstpass.Assembly); foreach (var p in patches) { logger.Log("Executing patch {0}...", p.Name); if (p.Patch(assemblyCSharp, assemblyCSharpFirstpass)) { modified = true; } else { logger.Log("{0} was not applied", p.Name); } } if (modified) { var suffix = $".{DateTime.Now.Ticks}.bak"; SaveModule(AssemblyCSharp, assemblyCSharp, suffix); SaveModule(AssemblyCSharpFirstpass, assemblyCSharpFirstpass, suffix); } else { logger.Error("The assembly was not modified"); } }
private void LoadDependencies(ModuleDefMD module) { if (seenModules.Contains(module.Location)) { return; } seenModules.Add(module.Location); Modules.Add(module); Resolver.AddToCache(module); foreach (var assemblyRef in module.GetAssemblyRefs()) { AssemblyDef assembly = Resolver.ResolveThrow(assemblyRef, null); foreach (var moduleRef in assembly.Modules) { LoadDependencies((ModuleDefMD)moduleRef); } } }