|
@@ -2,24 +2,25 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Reflection;
|
|
|
+using Harmony;
|
|
|
using Mono.Cecil;
|
|
|
|
|
|
namespace BepInEx.Bootstrap
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public delegate void AssemblyPatcherDelegate(ref AssemblyDefinition assembly);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public static class AssemblyPatcher
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
private static bool DumpingEnabled => Utility.SafeParseBool(Config.GetEntry("dump-assemblies", "false", "Preloader"));
|
|
|
|
|
|
|
|
@@ -31,10 +32,10 @@ namespace BepInEx.Bootstrap
|
|
|
|
|
|
public static void PatchAll(string directory, IDictionary<AssemblyPatcherDelegate, IEnumerable<string>> patcherMethodDictionary, IEnumerable<Action> initializers = null, IEnumerable<Action> finalizers = null)
|
|
|
{
|
|
|
-
|
|
|
- if (initializers != null)
|
|
|
- foreach (Action init in initializers)
|
|
|
- init.Invoke();
|
|
|
+
|
|
|
+ if (initializers != null)
|
|
|
+ foreach (Action init in initializers)
|
|
|
+ init.Invoke();
|
|
|
|
|
|
|
|
|
Dictionary<string, AssemblyDefinition> assemblies = new Dictionary<string, AssemblyDefinition>();
|
|
@@ -42,7 +43,7 @@ namespace BepInEx.Bootstrap
|
|
|
foreach (string assemblyPath in Directory.GetFiles(directory, "*.dll"))
|
|
|
{
|
|
|
var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -55,75 +56,107 @@ namespace BepInEx.Bootstrap
|
|
|
}
|
|
|
|
|
|
assemblies.Add(Path.GetFileName(assemblyPath), assembly);
|
|
|
+ PatchedAssemblyResolver.AssemblyLocations.Add(assembly.FullName, Path.GetFullPath(assemblyPath));
|
|
|
}
|
|
|
|
|
|
HashSet<string> patchedAssemblies = new HashSet<string>();
|
|
|
|
|
|
|
|
|
- foreach (var patcherMethod in patcherMethodDictionary)
|
|
|
- {
|
|
|
- foreach (string assemblyFilename in patcherMethod.Value)
|
|
|
- {
|
|
|
- if (assemblies.TryGetValue(assemblyFilename, out var assembly))
|
|
|
- {
|
|
|
- Patch(ref assembly, patcherMethod.Key);
|
|
|
- assemblies[assemblyFilename] = assembly;
|
|
|
- patchedAssemblies.Add(assemblyFilename);
|
|
|
+ foreach (var patcherMethod in patcherMethodDictionary)
|
|
|
+ {
|
|
|
+ foreach (string assemblyFilename in patcherMethod.Value)
|
|
|
+ {
|
|
|
+ if (assemblies.TryGetValue(assemblyFilename, out var assembly))
|
|
|
+ {
|
|
|
+ Patch(ref assembly, patcherMethod.Key);
|
|
|
+ assemblies[assemblyFilename] = assembly;
|
|
|
+ patchedAssemblies.Add(assemblyFilename);
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- foreach (var kv in assemblies)
|
|
|
- {
|
|
|
- string filename = kv.Key;
|
|
|
- var assembly = kv.Value;
|
|
|
-
|
|
|
- if (DumpingEnabled && patchedAssemblies.Contains(filename))
|
|
|
- {
|
|
|
- using (MemoryStream mem = new MemoryStream())
|
|
|
- {
|
|
|
- string dirPath = Path.Combine(Paths.PluginPath, "DumpedAssemblies");
|
|
|
-
|
|
|
- if (!Directory.Exists(dirPath))
|
|
|
- Directory.CreateDirectory(dirPath);
|
|
|
-
|
|
|
- assembly.Write(mem);
|
|
|
- File.WriteAllBytes(Path.Combine(dirPath, filename), mem.ToArray());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- Load(assembly);
|
|
|
- assembly.Dispose();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (finalizers != null)
|
|
|
- foreach (Action finalizer in finalizers)
|
|
|
- finalizer.Invoke();
|
|
|
+ foreach (var kv in assemblies)
|
|
|
+ {
|
|
|
+ string filename = kv.Key;
|
|
|
+ var assembly = kv.Value;
|
|
|
+
|
|
|
+ if (DumpingEnabled && patchedAssemblies.Contains(filename))
|
|
|
+ {
|
|
|
+ using (MemoryStream mem = new MemoryStream())
|
|
|
+ {
|
|
|
+ string dirPath = Path.Combine(Paths.PluginPath, "DumpedAssemblies");
|
|
|
+
|
|
|
+ if (!Directory.Exists(dirPath))
|
|
|
+ Directory.CreateDirectory(dirPath);
|
|
|
+
|
|
|
+ assembly.Write(mem);
|
|
|
+ File.WriteAllBytes(Path.Combine(dirPath, filename), mem.ToArray());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Load(assembly);
|
|
|
+ assembly.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ PatchedAssemblyResolver.ApplyPatch();
|
|
|
+
|
|
|
+
|
|
|
+ if (finalizers != null)
|
|
|
+ foreach (Action finalizer in finalizers)
|
|
|
+ finalizer.Invoke();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public static void Patch(ref AssemblyDefinition assembly, AssemblyPatcherDelegate patcherMethod)
|
|
|
{
|
|
|
- patcherMethod.Invoke(ref assembly);
|
|
|
+ patcherMethod.Invoke(ref assembly);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void Load(AssemblyDefinition assembly)
|
|
|
+ {
|
|
|
+ using (MemoryStream assemblyStream = new MemoryStream())
|
|
|
+ {
|
|
|
+ assembly.Write(assemblyStream);
|
|
|
+ Assembly.Load(assemblyStream.ToArray());
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static void Load(AssemblyDefinition assembly)
|
|
|
- {
|
|
|
- using (MemoryStream assemblyStream = new MemoryStream())
|
|
|
- {
|
|
|
- assembly.Write(assemblyStream);
|
|
|
- Assembly.Load(assemblyStream.ToArray());
|
|
|
- }
|
|
|
- }
|
|
|
+ internal static class PatchedAssemblyResolver
|
|
|
+ {
|
|
|
+ public static Dictionary<string, string> AssemblyLocations { get; } = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
+
|
|
|
+ public static void ApplyPatch()
|
|
|
+ {
|
|
|
+ HarmonyInstance.Create("com.bepis.bepinex.asmlocationfix").PatchAll(typeof(PatchedAssemblyResolver));
|
|
|
+ }
|
|
|
+
|
|
|
+ [HarmonyPatch(typeof(Assembly))]
|
|
|
+ [HarmonyPatch(nameof(Assembly.Location), PropertyMethod.Getter)]
|
|
|
+ [HarmonyPostfix]
|
|
|
+ public static void GetLocation(ref string __result, Assembly __instance)
|
|
|
+ {
|
|
|
+ if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
|
|
|
+ __result = location;
|
|
|
+ }
|
|
|
+
|
|
|
+ [HarmonyPatch(typeof(Assembly))]
|
|
|
+ [HarmonyPatch(nameof(Assembly.CodeBase), PropertyMethod.Getter)]
|
|
|
+ [HarmonyPostfix]
|
|
|
+ public static void GetCodeBase(ref string __result, Assembly __instance)
|
|
|
+ {
|
|
|
+ if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
|
|
|
+ __result = $"file://{location.Replace('\\', '/')}";
|
|
|
+ }
|
|
|
}
|
|
|
}
|