You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.3 KiB

  1. using HarmonyLib;
  2. using System.Reflection;
  3. namespace IPA.Loader
  4. {
  5. internal static class HarmonyProtectorProxy
  6. {
  7. public static void ProtectNull() => HarmonyProtector.Protect();
  8. }
  9. internal static class HarmonyProtector
  10. {
  11. private static Harmony instance;
  12. private static Assembly selfAssem;
  13. private static Assembly harmonyAssem;
  14. public static void Protect(Harmony inst = null)
  15. {
  16. selfAssem = Assembly.GetExecutingAssembly();
  17. harmonyAssem = typeof(Harmony).Assembly;
  18. if (inst == null)
  19. {
  20. if (instance == null)
  21. instance = new Harmony("BSIPA Safeguard");
  22. inst = instance;
  23. }
  24. var target = typeof(PatchProcessor).GetMethod("Patch");
  25. var patch = typeof(HarmonyProtector).GetMethod(nameof(PatchProcessor_Patch_Prefix), BindingFlags.NonPublic | BindingFlags.Static);
  26. inst.Patch(target, new HarmonyMethod(patch));
  27. }
  28. private static bool PatchProcessor_Patch_Prefix(MethodBase ___original, out MethodInfo __result)
  29. {
  30. var asm = ___original.DeclaringType.Assembly;
  31. __result = ___original as MethodInfo;
  32. return !(asm.Equals(selfAssem) || asm.Equals(harmonyAssem));
  33. }
  34. }
  35. }