UnityPatches.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using BepInEx.Harmony;
  5. using Harmony;
  6. namespace BepInEx.Preloader
  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. }
  17. [HarmonyPostfix]
  18. [HarmonyPatch(typeof(Assembly), nameof(Assembly.Location), MethodType.Getter)]
  19. public static void GetLocation(ref string __result, Assembly __instance)
  20. {
  21. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  22. __result = location;
  23. }
  24. [HarmonyPostfix]
  25. [HarmonyPatch(typeof(Assembly), nameof(Assembly.CodeBase), MethodType.Getter)]
  26. public static void GetCodeBase(ref string __result, Assembly __instance)
  27. {
  28. if (AssemblyLocations.TryGetValue(__instance.FullName, out string location))
  29. __result = $"file://{location.Replace('\\', '/')}";
  30. }
  31. #if UNITY_2018
  32. /*
  33. * DESC: Workaround for Trace class not working because of missing .config file
  34. * AFFECTS: Unity 2018+
  35. */
  36. [HarmonyPostfix, HarmonyPatch(typeof(AppDomain), nameof(AppDomain.SetupInformation), MethodType.Getter)]
  37. public static void GetExeConfigName(AppDomainSetup __result)
  38. {
  39. __result.ApplicationBase = $"file://{Paths.GameRootPath}";
  40. __result.ConfigurationFile = "app.config";
  41. }
  42. #endif
  43. }
  44. }