AssemblyReferenceDeepCollector.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using dnlib.DotNet;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. namespace HybridCLR.Editor.Meta
  10. {
  11. public class AssemblyReferenceDeepCollector : IDisposable
  12. {
  13. private readonly IAssemblyResolver _assemblyPathResolver;
  14. private readonly List<string> _rootAssemblies;
  15. private readonly ModuleContext _modCtx;
  16. private readonly AssemblyResolver _asmResolver;
  17. private bool disposedValue;
  18. public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
  19. public IReadOnlyList<string> GetRootAssemblyNames()
  20. {
  21. return _rootAssemblies;
  22. }
  23. public List<ModuleDefMD> GetLoadedModulesExcludeRootAssemblies()
  24. {
  25. return LoadedModules.Where(e => !_rootAssemblies.Contains(e.Key)).Select(e => e.Value).ToList();
  26. }
  27. public List<ModuleDefMD> GetLoadedModulesOfRootAssemblies()
  28. {
  29. return _rootAssemblies.Select(ass => LoadedModules[ass]).ToList();
  30. }
  31. public AssemblyReferenceDeepCollector(IAssemblyResolver assemblyResolver, List<string> rootAssemblies)
  32. {
  33. _assemblyPathResolver = assemblyResolver;
  34. _rootAssemblies = rootAssemblies;
  35. _modCtx = ModuleDef.CreateModuleContext();
  36. _asmResolver = (AssemblyResolver)_modCtx.AssemblyResolver;
  37. _asmResolver.EnableTypeDefCache = true;
  38. _asmResolver.UseGAC = false;
  39. LoadAllAssembiles();
  40. }
  41. private void LoadAllAssembiles()
  42. {
  43. foreach (var asm in _rootAssemblies)
  44. {
  45. LoadModule(asm);
  46. }
  47. }
  48. private ModuleDefMD LoadModule(string moduleName)
  49. {
  50. // Debug.Log($"load module:{moduleName}");
  51. if (LoadedModules.TryGetValue(moduleName, out var mod))
  52. {
  53. return mod;
  54. }
  55. mod = DoLoadModule(_assemblyPathResolver.ResolveAssembly(moduleName, true));
  56. LoadedModules.Add(moduleName, mod);
  57. foreach (var refAsm in mod.GetAssemblyRefs())
  58. {
  59. LoadModule(refAsm.Name);
  60. }
  61. return mod;
  62. }
  63. private ModuleDefMD DoLoadModule(string dllPath)
  64. {
  65. //Debug.Log($"do load module:{dllPath}");
  66. ModuleDefMD mod = ModuleDefMD.Load(dllPath, _modCtx);
  67. _asmResolver.AddToCache(mod);
  68. return mod;
  69. }
  70. protected virtual void Dispose(bool disposing)
  71. {
  72. if (!disposedValue)
  73. {
  74. if (disposing)
  75. {
  76. foreach(var mod in LoadedModules.Values)
  77. {
  78. mod.Dispose();
  79. }
  80. }
  81. LoadedModules.Clear();
  82. disposedValue = true;
  83. }
  84. }
  85. public void Dispose()
  86. {
  87. Dispose(disposing: true);
  88. GC.SuppressFinalize(this);
  89. }
  90. }
  91. }