UnityPatches.cs 1.3 KB

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