123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using BepInEx.Configuration;
- using BepInEx.Logging;
- using BepInEx.Preloader.RuntimeFixes;
- using Mono.Cecil;
- namespace BepInEx.Preloader.Patching
- {
-
-
-
-
- internal delegate void AssemblyPatcherDelegate(ref AssemblyDefinition assembly);
-
-
-
-
- internal static class AssemblyPatcher
- {
- public static List<PatcherPlugin> PatcherPlugins { get; } = new List<PatcherPlugin>();
- private static readonly string DumpedAssembliesPath = Path.Combine(Paths.BepInExRootPath, "DumpedAssemblies");
-
-
-
-
- public static void AddPatcher(PatcherPlugin patcher)
- {
- PatcherPlugins.Add(patcher);
- }
-
-
-
-
-
- public static void AddPatchersFromDirectory(string directory,
- Func<Assembly, List<PatcherPlugin>> patcherLocator)
- {
- if (!Directory.Exists(directory))
- return;
- var sortedPatchers = new SortedDictionary<string, PatcherPlugin>();
- foreach (string assemblyPath in Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories))
- try
- {
- var assembly = Assembly.LoadFrom(assemblyPath);
- foreach (var patcher in patcherLocator(assembly))
- sortedPatchers.Add(patcher.Name, patcher);
- }
- catch (BadImageFormatException) { }
- catch (ReflectionTypeLoadException) { }
- foreach (KeyValuePair<string, PatcherPlugin> patcher in sortedPatchers)
- AddPatcher(patcher.Value);
- }
- private static void InitializePatchers()
- {
- foreach (var assemblyPatcher in PatcherPlugins)
- assemblyPatcher.Initializer?.Invoke();
- }
- private static void FinalizePatching()
- {
- foreach (var assemblyPatcher in PatcherPlugins)
- assemblyPatcher.Finalizer?.Invoke();
- }
-
-
-
- public static void DisposePatchers()
- {
- PatcherPlugins.Clear();
- }
-
-
-
-
- public static void PatchAndLoad(string directory)
- {
-
- var assemblies = new Dictionary<string, AssemblyDefinition>();
- foreach (string assemblyPath in Directory.GetFiles(directory, "*.dll"))
- {
- var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
-
-
-
-
- if (assembly.Name.Name == "System" || assembly.Name.Name == "mscorlib")
- {
- assembly.Dispose();
- continue;
- }
- if (UnityPatches.AssemblyLocations.ContainsKey(assembly.FullName))
- {
- Logger.LogWarning($"Tried to load duplicate assembly {Path.GetFileName(assemblyPath)} from Managed folder! Skipping...");
- continue;
- }
- assemblies.Add(Path.GetFileName(assemblyPath), assembly);
- UnityPatches.AssemblyLocations.Add(assembly.FullName, Path.GetFullPath(assemblyPath));
- }
-
- InitializePatchers();
-
- var patchedAssemblies = new HashSet<string>();
- foreach (var assemblyPatcher in PatcherPlugins)
- foreach (string targetDll in assemblyPatcher.TargetDLLs)
- if (assemblies.TryGetValue(targetDll, out var assembly))
- {
- Logger.LogInfo($"Patching [{assembly.Name.Name}] with [{assemblyPatcher.Name}]");
- assemblyPatcher.Patcher?.Invoke(ref assembly);
- assemblies[targetDll] = assembly;
- patchedAssemblies.Add(targetDll);
- }
-
- if (ConfigDumpAssemblies.Value || ConfigLoadDumpedAssemblies.Value)
- {
- if (!Directory.Exists(DumpedAssembliesPath))
- Directory.CreateDirectory(DumpedAssembliesPath);
- foreach (KeyValuePair<string, AssemblyDefinition> kv in assemblies)
- {
- string filename = kv.Key;
- var assembly = kv.Value;
- if (patchedAssemblies.Contains(filename))
- assembly.Write(Path.Combine(DumpedAssembliesPath, filename));
- }
- }
- if (ConfigBreakBeforeLoadAssemblies.Value)
- {
- Logger.LogInfo($"BepInEx is about load the following assemblies:\n{string.Join("\n", patchedAssemblies.ToArray())}");
- Logger.LogInfo($"The assemblies were dumped into {DumpedAssembliesPath}");
- Logger.LogInfo("Load any assemblies into the debugger, set breakpoints and continue execution.");
- Debugger.Break();
- }
- foreach (var kv in assemblies)
- {
- string filename = kv.Key;
- var assembly = kv.Value;
-
-
-
- if (patchedAssemblies.Contains(filename))
- Load(assembly, filename);
-
- assembly.Dispose();
- }
-
- FinalizePatching();
- }
-
-
-
-
- public static void Load(AssemblyDefinition assembly, string filename)
- {
- if (ConfigLoadDumpedAssemblies.Value)
- Assembly.LoadFile(Path.Combine(DumpedAssembliesPath, filename));
- else
- using (var assemblyStream = new MemoryStream())
- {
- assembly.Write(assemblyStream);
- Assembly.Load(assemblyStream.ToArray());
- }
- }
- #region Config
- private static readonly ConfigWrapper<bool> ConfigDumpAssemblies = ConfigFile.CoreConfig.Wrap(
- "Preloader",
- "DumpAssemblies",
- "If enabled, BepInEx will save patched assemblies into BepInEx/DumpedAssemblies.\nThis can be used by developers to inspect and debug preloader patchers.",
- false);
- private static readonly ConfigWrapper<bool> ConfigLoadDumpedAssemblies = ConfigFile.CoreConfig.Wrap(
- "Preloader",
- "LoadDumpedAssemblies",
- "If enabled, BepInEx will load patched assemblies from BepInEx/DumpedAssemblies instead of memory.\nThis can be used to be able to load patched assemblies into debuggers like dnSpy.\nIf set to true, will override DumpAssemblies.",
- false);
- private static readonly ConfigWrapper<bool> ConfigBreakBeforeLoadAssemblies = ConfigFile.CoreConfig.Wrap(
- "Preloader",
- "BreakBeforeLoadAssemblies",
- "If enabled, BepInEx will call Debugger.Break() once before loading patched assemblies.\nThis can be used with debuggers like dnSpy to install breakpoints into patched assemblies before they are loaded.",
- false);
- #endregion
- }
- }
|