UnityPatches.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using BepInEx.Harmony;
  5. using Harmony;
  6. namespace BepInEx.Preloader.RuntimeFixes
  7. {
  8. internal static class UnityPatches
  9. {
  10. public static HarmonyInstance HarmonyInstance { get; } = HarmonyInstance.Create("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. #if UNITY_2018
  37. /*
  38. * DESC: Workaround for Trace class not working because of missing .config file
  39. * AFFECTS: Unity 2018+
  40. */
  41. [HarmonyPostfix, HarmonyPatch(typeof(AppDomain), nameof(AppDomain.SetupInformation), MethodType.Getter)]
  42. public static void GetExeConfigName(AppDomainSetup __result)
  43. {
  44. __result.ApplicationBase = $"file://{Paths.GameRootPath}";
  45. __result.ConfigurationFile = "app.config";
  46. }
  47. #endif
  48. }
  49. }