AssemblyCacheBase.cs 3.4 KB

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