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.

46 lines
1.4 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. HarmonyGlobalSettings.DisallowLegacyGlobalUnpatchAll = true;
  17. selfAssem = Assembly.GetExecutingAssembly();
  18. harmonyAssem = typeof(Harmony).Assembly;
  19. if (inst == null)
  20. {
  21. if (instance == null)
  22. instance = new Harmony("BSIPA Safeguard");
  23. inst = instance;
  24. }
  25. var target = typeof(PatchProcessor).GetMethod("Patch");
  26. var patch = typeof(HarmonyProtector).GetMethod(nameof(PatchProcessor_Patch_Prefix), BindingFlags.NonPublic | BindingFlags.Static);
  27. inst.Patch(target, new HarmonyMethod(patch));
  28. }
  29. private static bool PatchProcessor_Patch_Prefix(MethodBase ___original, out MethodInfo __result)
  30. {
  31. var asm = ___original.DeclaringType.Assembly;
  32. __result = ___original as MethodInfo;
  33. return !(asm.Equals(selfAssem) || asm.Equals(harmonyAssem));
  34. }
  35. }
  36. }