Resolvers.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // all the resolve functions for the weaver
  2. // NOTE: these functions should be made extensions, but right now they still
  3. // make heavy use of Weaver.fail and we'd have to check each one's return
  4. // value for null otherwise.
  5. // (original FieldType.Resolve returns null if not found too, so
  6. // exceptions would be a bit inconsistent here)
  7. using Mono.CecilX;
  8. namespace Mirror.Weaver
  9. {
  10. public static class Resolvers
  11. {
  12. public static MethodReference ResolveMethod(TypeReference tr, AssemblyDefinition assembly, Logger Log, string name, ref bool WeavingFailed)
  13. {
  14. if (tr == null)
  15. {
  16. Log.Error($"Cannot resolve method {name} without a class");
  17. WeavingFailed = true;
  18. return null;
  19. }
  20. MethodReference method = ResolveMethod(tr, assembly, Log, m => m.Name == name, ref WeavingFailed);
  21. if (method == null)
  22. {
  23. Log.Error($"Method not found with name {name} in type {tr.Name}", tr);
  24. WeavingFailed = true;
  25. }
  26. return method;
  27. }
  28. public static MethodReference ResolveMethod(TypeReference t, AssemblyDefinition assembly, Logger Log, System.Func<MethodDefinition, bool> predicate, ref bool WeavingFailed)
  29. {
  30. foreach (MethodDefinition methodRef in t.Resolve().Methods)
  31. {
  32. if (predicate(methodRef))
  33. {
  34. return assembly.MainModule.ImportReference(methodRef);
  35. }
  36. }
  37. Log.Error($"Method not found in type {t.Name}", t);
  38. WeavingFailed = true;
  39. return null;
  40. }
  41. public static MethodReference TryResolveMethodInParents(TypeReference tr, AssemblyDefinition assembly, string name)
  42. {
  43. if (tr == null)
  44. {
  45. return null;
  46. }
  47. foreach (MethodDefinition methodDef in tr.Resolve().Methods)
  48. {
  49. if (methodDef.Name == name)
  50. {
  51. MethodReference methodRef = methodDef;
  52. if (tr.IsGenericInstance)
  53. {
  54. methodRef = methodRef.MakeHostInstanceGeneric(tr.Module, (GenericInstanceType)tr);
  55. }
  56. return assembly.MainModule.ImportReference(methodRef);
  57. }
  58. }
  59. // Could not find the method in this class, try the parent
  60. return TryResolveMethodInParents(tr.Resolve().BaseType.ApplyGenericParameters(tr), assembly, name);
  61. }
  62. public static MethodDefinition ResolveDefaultPublicCtor(TypeReference variable)
  63. {
  64. foreach (MethodDefinition methodRef in variable.Resolve().Methods)
  65. {
  66. if (methodRef.Name == ".ctor" &&
  67. methodRef.Resolve().IsPublic &&
  68. methodRef.Parameters.Count == 0)
  69. {
  70. return methodRef;
  71. }
  72. }
  73. return null;
  74. }
  75. public static MethodReference ResolveProperty(TypeReference tr, AssemblyDefinition assembly, string name)
  76. {
  77. foreach (PropertyDefinition pd in tr.Resolve().Properties)
  78. {
  79. if (pd.Name == name)
  80. {
  81. return assembly.MainModule.ImportReference(pd.GetMethod);
  82. }
  83. }
  84. return null;
  85. }
  86. }
  87. }