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.

45 lines
1.3 KiB

  1. using HarmonyLib;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace IPA.Loader
  5. {
  6. internal static class HarmonyProtectorProxy
  7. {
  8. public static void ProtectNull() => HarmonyProtector.Protect();
  9. }
  10. internal static class HarmonyProtector
  11. {
  12. private static Harmony instance;
  13. private static Assembly selfAssem;
  14. private static Assembly harmonyAssem;
  15. public static void Protect(Harmony inst = null)
  16. {
  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));
  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. }