MissingMetadataChecker.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using dnlib.DotNet;
  2. using HybridCLR.Editor.Meta;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HybridCLR.Editor.HotUpdate
  10. {
  11. public class MissingMetadataChecker
  12. {
  13. private readonly HashSet<string> _aotAssNames;
  14. private readonly AssemblyCache _assCache;
  15. public MissingMetadataChecker(string aotDllDir, IEnumerable<string> excludeDllNames)
  16. {
  17. var excludeDllNameSet = new HashSet<string>(excludeDllNames ?? new List<string>());
  18. _aotAssNames = new HashSet<string>();
  19. foreach (var aotFile in Directory.GetFiles(aotDllDir, "*.dll"))
  20. {
  21. string aotAssName = Path.GetFileNameWithoutExtension(aotFile);
  22. if (excludeDllNameSet.Contains(aotAssName))
  23. {
  24. continue;
  25. }
  26. _aotAssNames.Add(aotAssName);
  27. }
  28. _assCache = new AssemblyCache(new PathAssemblyResolver(aotDllDir));
  29. }
  30. public bool Check(string hotUpdateDllPath)
  31. {
  32. ModuleDef mod = ModuleDefMD.Load(File.ReadAllBytes(hotUpdateDllPath), _assCache.ModCtx);
  33. foreach (var refass in mod.GetAssemblyRefs())
  34. {
  35. string refAssName = refass.Name;
  36. if (_aotAssNames.Contains(refAssName))
  37. {
  38. _assCache.LoadModule(refass.Name, true);
  39. }
  40. }
  41. bool anyMissing = false;
  42. foreach (TypeRef typeRef in mod.GetTypeRefs())
  43. {
  44. string defAssName = typeRef.DefinitionAssembly.Name;
  45. if (!_aotAssNames.Contains(defAssName))
  46. {
  47. continue;
  48. }
  49. if (typeRef.ResolveTypeDef() == null)
  50. {
  51. UnityEngine.Debug.LogError($"Missing Type: {typeRef.FullName}");
  52. anyMissing = true;
  53. }
  54. }
  55. foreach (IMethodDefOrRef methodRef in mod.GetMemberRefs())
  56. {
  57. if (methodRef.DeclaringType.DefinitionAssembly == null)
  58. {
  59. continue;
  60. }
  61. string defAssName = methodRef.DeclaringType.DefinitionAssembly.Name;
  62. if (!_aotAssNames.Contains(defAssName))
  63. {
  64. continue;
  65. }
  66. if (methodRef.IsField)
  67. {
  68. }
  69. else if (methodRef.IsMethod)
  70. {
  71. TypeSig declaringTypeSig = methodRef.DeclaringType.ToTypeSig();
  72. if (methodRef.ResolveMethodDef() == null)
  73. {
  74. if (declaringTypeSig.ElementType == ElementType.Array || declaringTypeSig.ElementType == ElementType.SZArray)
  75. {
  76. continue;
  77. }
  78. UnityEngine.Debug.LogError($"Missing Method: {methodRef.FullName}");
  79. anyMissing = true;
  80. }
  81. }
  82. }
  83. return !anyMissing;
  84. }
  85. }
  86. }