UnityPatches.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. public static HarmonyLib.Harmony HarmonyInstance { get; } = new HarmonyLib.Harmony("com.bepinex.unitypatches");
  11. public static Dictionary<string, string> AssemblyLocations { get; } =
  12. new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
  13. public static void Apply()
  14. {
  15. HarmonyWrapper.PatchAll(typeof(UnityPatches), HarmonyInstance);
  16. try
  17. {
  18. TraceFix.ApplyFix();
  19. }
  20. catch { } //ignore everything, if it's thrown an exception, we're using an assembly that has already fixed this
  21. }
  22. [HarmonyPostfix]
  23. [HarmonyPatch(typeof(Assembly), nameof(Assembly.Location), MethodType.Getter)]
  24. public static void GetLocation(ref string __result, Assembly __instance)
  25. {
  26. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  27. __result = location;
  28. }
  29. [HarmonyPostfix]
  30. [HarmonyPatch(typeof(Assembly), nameof(Assembly.CodeBase), MethodType.Getter)]
  31. public static void GetCodeBase(ref string __result, Assembly __instance)
  32. {
  33. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  34. __result = $"file://{location.Replace('\\', '/')}";
  35. }
  36. }
  37. }