PatchFunctions.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Harmony.ILCopying;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Reflection.Emit;
  7. namespace Harmony
  8. {
  9. public static class PatchFunctions
  10. {
  11. public static void AddPrefix(PatchInfo patchInfo, string owner, HarmonyMethod info)
  12. {
  13. if (info == null || info.method == null) return;
  14. var priority = info.prioritiy == -1 ? Priority.Normal : info.prioritiy;
  15. var before = info.before ?? new string[0];
  16. var after = info.after ?? new string[0];
  17. patchInfo.AddPrefix(info.method, owner, priority, before, after);
  18. }
  19. public static void RemovePrefix(PatchInfo patchInfo, string owner)
  20. {
  21. patchInfo.RemovePrefix(owner);
  22. }
  23. public static void AddPostfix(PatchInfo patchInfo, string owner, HarmonyMethod info)
  24. {
  25. if (info == null || info.method == null) return;
  26. var priority = info.prioritiy == -1 ? Priority.Normal : info.prioritiy;
  27. var before = info.before ?? new string[0];
  28. var after = info.after ?? new string[0];
  29. patchInfo.AddPostfix(info.method, owner, priority, before, after);
  30. }
  31. public static void RemovePostfix(PatchInfo patchInfo, string owner)
  32. {
  33. patchInfo.RemovePostfix(owner);
  34. }
  35. public static void AddTranspiler(PatchInfo patchInfo, string owner, HarmonyMethod info)
  36. {
  37. if (info == null || info.method == null) return;
  38. var priority = info.prioritiy == -1 ? Priority.Normal : info.prioritiy;
  39. var before = info.before ?? new string[0];
  40. var after = info.after ?? new string[0];
  41. patchInfo.AddTranspiler(info.method, owner, priority, before, after);
  42. }
  43. public static void RemoveTranspiler(PatchInfo patchInfo, string owner)
  44. {
  45. patchInfo.RemoveTranspiler(owner);
  46. }
  47. public static void RemovePatch(PatchInfo patchInfo, MethodInfo patch)
  48. {
  49. patchInfo.RemovePatch(patch);
  50. }
  51. public static List<MethodInfo> GetSortedPatchMethods(MethodBase original, Patch[] patches)
  52. {
  53. return patches
  54. .Where(p => p.patch != null)
  55. .OrderBy(p => p)
  56. .Select(p => p.GetMethod(original))
  57. .ToList();
  58. }
  59. }
  60. }