NeighPatcher.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Mono.Cecil;
  5. using Mono.Cecil.Cil;
  6. namespace NeighPatcher
  7. {
  8. [AttributeUsage(AttributeTargets.Method)]
  9. internal class NeighPatchAttribute : Attribute
  10. {
  11. public string[] ArgTypes;
  12. public string AssemblyName;
  13. public string MethodName;
  14. public string TypeName;
  15. public NeighPatchAttribute(string assemblyName, string typeName, string methodName, params string[] argTypes)
  16. {
  17. AssemblyName = assemblyName;
  18. TypeName = typeName;
  19. MethodName = methodName;
  20. ArgTypes = argTypes;
  21. }
  22. }
  23. internal static class NeighPatcher
  24. {
  25. public static void PatchAll(AssemblyDefinition targetAssembly, string patchTypeName)
  26. {
  27. var hookAd = AssemblyDefinition.ReadAssembly(Assembly.GetExecutingAssembly().Location);
  28. var patchType = hookAd.MainModule.GetType(patchTypeName);
  29. Console.WriteLine($"[NeighPatcher] Patching {targetAssembly.Name.Name} with {patchType.FullName}");
  30. foreach (var methodDefinition in patchType.Methods.Where(m => m.HasCustomAttributes))
  31. {
  32. var patchAttributes = methodDefinition
  33. .CustomAttributes
  34. .Where(c => c.HasConstructorArguments &&
  35. c.Constructor.DeclaringType.Name == nameof(NeighPatchAttribute))
  36. .ToList();
  37. if (patchAttributes.Count == 0)
  38. continue;
  39. Console.WriteLine(
  40. $"[NeighPatcher] Applying {methodDefinition.FullName} with {patchAttributes.Count} patch attributes");
  41. var patchAttrs = patchAttributes.Select(p => new NeighPatchAttribute(
  42. p.ConstructorArguments[0].Value as string,
  43. p.ConstructorArguments[1].Value as string,
  44. p.ConstructorArguments[2].Value as string,
  45. ((CustomAttributeArgument[])p.ConstructorArguments[3].Value)
  46. .Select(c => c.Value as string).ToArray())).ToList();
  47. foreach (var neighPatchAttribute in patchAttrs)
  48. {
  49. Console.WriteLine(
  50. $"[NeighPatcher] Patching [{neighPatchAttribute.AssemblyName}]{neighPatchAttribute.TypeName}::{neighPatchAttribute.MethodName} with {neighPatchAttribute.ArgTypes.Length} arg types");
  51. if (targetAssembly.Name.Name != neighPatchAttribute.AssemblyName)
  52. continue;
  53. var t = targetAssembly.MainModule.GetType(neighPatchAttribute.TypeName);
  54. Console.WriteLine($"[NeighPatcher] Found type {t}");
  55. var method = t?.Methods.FirstOrDefault(m => m.Name == neighPatchAttribute.MethodName &&
  56. (neighPatchAttribute.ArgTypes.Length == 0 ||
  57. neighPatchAttribute.ArgTypes.Length != 0 &&
  58. neighPatchAttribute.ArgTypes.Length ==
  59. m.Parameters.Count &&
  60. m.Parameters.Select(p => p.ParameterType.FullName)
  61. .SequenceEqual(neighPatchAttribute.ArgTypes)));
  62. Console.WriteLine($"[NeighPatcher] Found method {method}");
  63. if (method == null)
  64. continue;
  65. var il = method.Body.GetILProcessor();
  66. var ins = method.Body.Instructions.First();
  67. foreach (var mParam in methodDefinition.Parameters)
  68. if (mParam.Name == "__instance")
  69. il.InsertBefore(ins, il.Create(OpCodes.Ldarg_0));
  70. else if (mParam.Name.StartsWith("___"))
  71. {
  72. string realName = mParam.Name.Substring(3);
  73. var targetField = t.Fields.FirstOrDefault(ff => ff.Name == realName);
  74. if (targetField == null)
  75. throw new Exception($"Field {realName} not found from type {t.FullName}!");
  76. if (!method.IsStatic)
  77. {
  78. il.InsertBefore(ins, il.Create(OpCodes.Ldarg_0));
  79. il.InsertBefore(ins, il.Create(OpCodes.Ldflda, targetField));
  80. }
  81. else
  82. il.InsertBefore(ins, il.Create(OpCodes.Ldsflda, targetField));
  83. }
  84. else
  85. {
  86. var pIndex = method.Parameters.FirstOrDefault(p => p.Name == mParam.Name);
  87. if (pIndex == null)
  88. throw new Exception(
  89. $"Parameter with name {mParam.Name} does not exist in {method.FullName}");
  90. il.InsertBefore(ins, il.Create(OpCodes.Ldarg, pIndex));
  91. }
  92. il.InsertBefore(
  93. ins, il.Create(OpCodes.Call, targetAssembly.MainModule.ImportReference(methodDefinition)));
  94. }
  95. }
  96. }
  97. }
  98. }