PatchTools.cs 827 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace Harmony
  6. {
  7. public static class PatchTools
  8. {
  9. public static MethodInfo GetPatchMethod<T>(Type patchType, string name, Type[] parameters = null)
  10. {
  11. var method = patchType.GetMethods(AccessTools.all)
  12. .FirstOrDefault(m => m.GetCustomAttributes(typeof(T), true).Any());
  13. if (method == null)
  14. method = AccessTools.Method(patchType, name, parameters);
  15. return method;
  16. }
  17. public static void GetPatches(Type patchType, out MethodInfo prefix, out MethodInfo postfix, out MethodInfo transpiler)
  18. {
  19. prefix = GetPatchMethod<HarmonyPrefix>(patchType, "Prefix");
  20. postfix = GetPatchMethod<HarmonyPostfix>(patchType, "Postfix");
  21. transpiler = GetPatchMethod<HarmonyTranspiler>(patchType, "Transpiler");
  22. }
  23. }
  24. }