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

4 years ago
  1. using Harmony;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace IPA.Loader
  5. {
  6. internal static class HarmonyProtector
  7. {
  8. private static HarmonyInstance instance;
  9. private static Assembly selfAssem;
  10. private static Assembly harmonyAssem;
  11. public static void Protect(HarmonyInstance inst = null)
  12. {
  13. selfAssem = Assembly.GetExecutingAssembly();
  14. harmonyAssem = typeof(HarmonyInstance).Assembly;
  15. if (inst == null)
  16. {
  17. if (instance == null)
  18. instance = HarmonyInstance.Create("BSIPA Safeguard");
  19. inst = instance;
  20. }
  21. var target = typeof(PatchProcessor).GetMethod("Patch");
  22. var patch = typeof(HarmonyProtector).GetMethod(nameof(PatchProcessor_Patch_Prefix));
  23. inst.Patch(target, new HarmonyMethod(patch));
  24. }
  25. private static void PatchProcessor_Patch_Prefix(ref List<MethodBase> ___originals)
  26. {
  27. for (int i = 0; i < ___originals.Count; i++)
  28. {
  29. var mi = ___originals[i];
  30. var asm = mi.DeclaringType.Assembly;
  31. if (asm.Equals(selfAssem) || asm.Equals(harmonyAssem))
  32. ___originals.RemoveAt(i--);
  33. }
  34. }
  35. }
  36. }