HarmonyMethod.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Reflection.Emit;
  6. namespace Harmony
  7. {
  8. public class HarmonyMethod
  9. {
  10. public MethodInfo method; // need to be called 'method'
  11. public Type declaringType;
  12. public string methodName;
  13. public MethodType? methodType;
  14. public Type[] argumentTypes;
  15. public int prioritiy = -1;
  16. public string[] before;
  17. public string[] after;
  18. public HarmonyMethod()
  19. {
  20. }
  21. void ImportMethod(MethodInfo theMethod)
  22. {
  23. method = theMethod;
  24. if (method != null)
  25. {
  26. var infos = method.GetHarmonyMethods();
  27. if (infos != null)
  28. Merge(infos).CopyTo(this);
  29. }
  30. }
  31. public HarmonyMethod(MethodInfo method)
  32. {
  33. ImportMethod(method);
  34. }
  35. public HarmonyMethod(Type type, string name, Type[] parameters = null)
  36. {
  37. var method = AccessTools.Method(type, name, parameters);
  38. ImportMethod(method);
  39. }
  40. public static List<string> HarmonyFields()
  41. {
  42. return AccessTools
  43. .GetFieldNames(typeof(HarmonyMethod))
  44. .Where(s => s != "method")
  45. .ToList();
  46. }
  47. public static HarmonyMethod Merge(List<HarmonyMethod> attributes)
  48. {
  49. var result = new HarmonyMethod();
  50. if (attributes == null) return result;
  51. var resultTrv = Traverse.Create(result);
  52. attributes.ForEach(attribute =>
  53. {
  54. var trv = Traverse.Create(attribute);
  55. HarmonyFields().ForEach(f =>
  56. {
  57. var val = trv.Field(f).GetValue();
  58. if (val != null)
  59. resultTrv.Field(f).SetValue(val);
  60. });
  61. });
  62. return result;
  63. }
  64. public override string ToString()
  65. {
  66. var result = "HarmonyMethod[";
  67. var trv = Traverse.Create(this);
  68. HarmonyFields().ForEach(f =>
  69. {
  70. result += f + '=' + trv.Field(f).GetValue();
  71. });
  72. return result + "]";
  73. }
  74. }
  75. public static class HarmonyMethodExtensions
  76. {
  77. public static void CopyTo(this HarmonyMethod from, HarmonyMethod to)
  78. {
  79. if (to == null) return;
  80. var fromTrv = Traverse.Create(from);
  81. var toTrv = Traverse.Create(to);
  82. HarmonyMethod.HarmonyFields().ForEach(f =>
  83. {
  84. var val = fromTrv.Field(f).GetValue();
  85. if (val != null) toTrv.Field(f).SetValue(val);
  86. });
  87. }
  88. public static HarmonyMethod Clone(this HarmonyMethod original)
  89. {
  90. var result = new HarmonyMethod();
  91. original.CopyTo(result);
  92. return result;
  93. }
  94. public static HarmonyMethod Merge(this HarmonyMethod master, HarmonyMethod detail)
  95. {
  96. if (detail == null) return master;
  97. var result = new HarmonyMethod();
  98. var resultTrv = Traverse.Create(result);
  99. var masterTrv = Traverse.Create(master);
  100. var detailTrv = Traverse.Create(detail);
  101. HarmonyMethod.HarmonyFields().ForEach(f =>
  102. {
  103. var baseValue = masterTrv.Field(f).GetValue();
  104. var detailValue = detailTrv.Field(f).GetValue();
  105. resultTrv.Field(f).SetValue(detailValue ?? baseValue);
  106. });
  107. return result;
  108. }
  109. public static List<HarmonyMethod> GetHarmonyMethods(this Type type)
  110. {
  111. return type.GetCustomAttributes(true)
  112. .Where(attr => attr is HarmonyAttribute)
  113. .Cast<HarmonyAttribute>()
  114. .Select(attr => attr.info)
  115. .ToList();
  116. }
  117. public static List<HarmonyMethod> GetHarmonyMethods(this MethodBase method)
  118. {
  119. if (method is DynamicMethod) return new List<HarmonyMethod>();
  120. return method.GetCustomAttributes(true)
  121. .Where(attr => attr is HarmonyAttribute)
  122. .Cast<HarmonyAttribute>()
  123. .Select(attr => attr.info)
  124. .ToList();
  125. }
  126. }
  127. }