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.

49 lines
1.4 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 HarmonyProtectorProxy
  7. {
  8. public static void ProtectNull() => HarmonyProtector.Protect();
  9. }
  10. internal static class HarmonyProtector
  11. {
  12. private static HarmonyInstance instance;
  13. private static Assembly selfAssem;
  14. private static Assembly harmonyAssem;
  15. public static void Protect(HarmonyInstance inst = null)
  16. {
  17. selfAssem = Assembly.GetExecutingAssembly();
  18. harmonyAssem = typeof(HarmonyInstance).Assembly;
  19. if (inst == null)
  20. {
  21. if (instance == null)
  22. instance = HarmonyInstance.Create("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 void PatchProcessor_Patch_Prefix(ref List<MethodBase> ___originals)
  30. {
  31. for (int i = 0; i < ___originals.Count; i++)
  32. {
  33. var mi = ___originals[i];
  34. var asm = mi.DeclaringType.Assembly;
  35. if (asm.Equals(selfAssem) || asm.Equals(harmonyAssem))
  36. ___originals.RemoveAt(i--);
  37. }
  38. }
  39. }
  40. }