HarmonyInteropFix: load shimmed assembly from file instead of memory

This ensures mono is properly aware of the DLL path
This commit is contained in:
ghorsington committed 2021-07-16 13:31:22 +03:00
1 parent d5ab110c4d
commit 10bf9cfd24
1 file changed
+7 -16
@@ -11,33 +11,24 @@ namespace BepInEx.Preloader.RuntimeFixes
{
internal static class HarmonyInteropFix
{
private static Dictionary<string, string> AssemblyLocations { get; } = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
public static void Apply()
{
HarmonyInterop.Initialize(Paths.CachePath);
Harmony.CreateAndPatchAll(typeof(HarmonyInteropFix), "org.bepinex.fixes.harmonyinterop");
}
[HarmonyReversePatch]
[HarmonyPatch(typeof(Assembly), nameof(Assembly.LoadFile), typeof(string))]
private static Assembly LoadFile(string path) => null;
[HarmonyPatch(typeof(Assembly), nameof(Assembly.LoadFile), typeof(string))]
[HarmonyPatch(typeof(Assembly), nameof(Assembly.LoadFrom), typeof(string))]
[HarmonyPrefix]
private static bool OnAssemblyLoad(ref Assembly __result, string __0)
{
var bytes = HarmonyInterop.TryShim(__0, Paths.GameRootPath, Logger.LogWarning, TypeLoader.ReaderParameters);
if (bytes == null)
return true;
__result = Assembly.Load(bytes);
AssemblyLocations[__result.FullName] = Path.GetFullPath(__0);
return false;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Assembly), nameof(Assembly.Location), MethodType.Getter)]
public static void GetLocation(ref string __result, Assembly __instance)
{
if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
__result = location;
HarmonyInterop.TryShim(__0, Paths.GameRootPath, Logger.LogWarning, TypeLoader.ReaderParameters);
__result = LoadFile(__0);
return true;
}
}
}