AssemblyPatcher.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using BepInEx.Common;
  6. using Mono.Cecil;
  7. namespace BepInEx.Bootstrap
  8. {
  9. public delegate void AssemblyPatcherDelegate(ref AssemblyDefinition assembly);
  10. public static class AssemblyPatcher
  11. {
  12. private static bool DumpingEnabled => bool.TryParse(Config.GetEntry("preloader-dumpassemblies", "false"), out bool result) ? result : false;
  13. public static void PatchAll(string directory, Dictionary<AssemblyPatcherDelegate, IEnumerable<string>> patcherMethodDictionary)
  14. {
  15. //load all the requested assemblies
  16. List<AssemblyDefinition> assemblies = new List<AssemblyDefinition>();
  17. Dictionary<AssemblyDefinition, string> assemblyFilenames = new Dictionary<AssemblyDefinition, string>();
  18. foreach (string assemblyPath in Directory.GetFiles(directory, "*.dll"))
  19. {
  20. var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
  21. //NOTE: this is special cased here because the dependency handling for System.dll is a bit wonky
  22. //System has an assembly reference to itself, and it also has a reference to Mono.Security causing a circular dependency
  23. //It's also generally dangerous to change system.dll since so many things rely on it,
  24. // and it's already loaded into the appdomain since this loader references it, so we might as well skip it
  25. if (assembly.Name.Name == "System"
  26. || assembly.Name.Name == "mscorlib") //mscorlib is already loaded into the appdomain so it can't be patched
  27. {
  28. #if CECIL_10
  29. assembly.Dispose();
  30. #endif
  31. continue;
  32. }
  33. assemblies.Add(assembly);
  34. assemblyFilenames[assembly] = Path.GetFileName(assemblyPath);
  35. }
  36. //generate a dictionary of each assembly's dependencies
  37. Dictionary<AssemblyDefinition, IList<AssemblyDefinition>> assemblyDependencyDict = new Dictionary<AssemblyDefinition, IList<AssemblyDefinition>>();
  38. foreach (AssemblyDefinition assembly in assemblies)
  39. {
  40. assemblyDependencyDict[assembly] = new List<AssemblyDefinition>();
  41. foreach (var dependencyRef in assembly.MainModule.AssemblyReferences)
  42. {
  43. var dependencyAssembly = assemblies.FirstOrDefault(x => x.FullName == dependencyRef.FullName);
  44. if (dependencyAssembly != null)
  45. assemblyDependencyDict[assembly].Add(dependencyAssembly);
  46. }
  47. }
  48. //sort the assemblies so load the assemblies that are dependant upon first
  49. AssemblyDefinition[] sortedAssemblies = Utility.TopologicalSort(assemblies, x => assemblyDependencyDict[x]).ToArray();
  50. List<string> sortedAssemblyFilenames = sortedAssemblies.Select(x => assemblyFilenames[x]).ToList();
  51. //call the patchers on the assemblies
  52. foreach (var patcherMethod in patcherMethodDictionary)
  53. {
  54. foreach (string assemblyFilename in patcherMethod.Value)
  55. {
  56. int index = sortedAssemblyFilenames.FindIndex(x => x == assemblyFilename);
  57. if (index < 0)
  58. continue;
  59. Patch(ref sortedAssemblies[index], patcherMethod.Key);
  60. }
  61. }
  62. for (int i = 0; i < sortedAssemblies.Length; i++)
  63. {
  64. string filename = Path.GetFileName(assemblyFilenames[sortedAssemblies[i]]);
  65. if (DumpingEnabled)
  66. {
  67. using (MemoryStream mem = new MemoryStream())
  68. {
  69. string dirPath = Path.Combine(Preloader.PluginPath, "DumpedAssemblies");
  70. if (!Directory.Exists(dirPath))
  71. Directory.CreateDirectory(dirPath);
  72. sortedAssemblies[i].Write(mem);
  73. File.WriteAllBytes(Path.Combine(dirPath, filename), mem.ToArray());
  74. }
  75. }
  76. Load(sortedAssemblies[i]);
  77. #if CECIL_10
  78. sortedAssemblies[i].Dispose();
  79. #endif
  80. }
  81. }
  82. public static void Patch(ref AssemblyDefinition assembly, AssemblyPatcherDelegate patcherMethod)
  83. {
  84. patcherMethod.Invoke(ref assembly);
  85. }
  86. public static void Load(AssemblyDefinition assembly)
  87. {
  88. using (MemoryStream assemblyStream = new MemoryStream())
  89. {
  90. assembly.Write(assemblyStream);
  91. Assembly.Load(assemblyStream.ToArray());
  92. }
  93. }
  94. }
  95. }