HarmonyFixes.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Diagnostics;
  3. using HarmonyLib;
  4. namespace BepInEx.Preloader.RuntimeFixes
  5. {
  6. internal static class HarmonyFixes
  7. {
  8. public static void Apply()
  9. {
  10. try
  11. {
  12. var harmony = new HarmonyLib.Harmony("BepInEx.Preloader.RuntimeFixes.HarmonyFixes");
  13. harmony.Patch(AccessTools.Method(typeof(Traverse), nameof(Traverse.GetValue), new Type[0]), null, new HarmonyMethod(typeof(HarmonyFixes), nameof(GetValue)));
  14. harmony.Patch(AccessTools.Method(typeof(Traverse), nameof(Traverse.SetValue), new []{ typeof(object) }), null, new HarmonyMethod(typeof(HarmonyFixes), nameof(SetValue)));
  15. }
  16. catch (Exception e)
  17. {
  18. Logging.Logger.LogError(e);
  19. }
  20. }
  21. private static void GetValue(Traverse __instance)
  22. {
  23. if (!__instance.FieldExists() && !__instance.MethodExists() && !__instance.TypeExists())
  24. Logging.Logger.LogWarning("Traverse.GetValue was called while not pointing at an existing Field, Property, Method or Type. The return value can be unexpected.\n" + new StackTrace());
  25. }
  26. private static void SetValue(Traverse __instance)
  27. {
  28. // If method exists it will crash inside traverse so only need to mention the field missing
  29. if (!__instance.FieldExists() && !__instance.MethodExists())
  30. Logging.Logger.LogWarning("Traverse.SetValue was called while not pointing at an existing Field or Property. The call will have no effect.\n" + new StackTrace());
  31. }
  32. }
  33. }