UnityPatches.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using HarmonyLib;
  5. namespace BepInEx.Preloader.RuntimeFixes
  6. {
  7. internal static class UnityPatches
  8. {
  9. private static HarmonyLib.Harmony HarmonyInstance { get; set; }
  10. public static Dictionary<string, string> AssemblyLocations { get; } = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
  11. public static void Apply()
  12. {
  13. HarmonyInstance = Harmony.CreateAndPatchAll(typeof(UnityPatches));
  14. try
  15. {
  16. TraceFix.ApplyFix();
  17. }
  18. catch { } //ignore everything, if it's thrown an exception, we're using an assembly that has already fixed this
  19. }
  20. [HarmonyPostfix]
  21. [HarmonyPatch(typeof(Assembly), nameof(Assembly.Location), MethodType.Getter)]
  22. public static void GetLocation(ref string __result, Assembly __instance)
  23. {
  24. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  25. __result = location;
  26. }
  27. [HarmonyPostfix]
  28. [HarmonyPatch(typeof(Assembly), nameof(Assembly.CodeBase), MethodType.Getter)]
  29. public static void GetCodeBase(ref string __result, Assembly __instance)
  30. {
  31. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  32. __result = $"file://{location.Replace('\\', '/')}";
  33. }
  34. }
  35. }