[change] AssemblyCacheBase新增TryLoadModule接口

[change] AssemblyCacheBase加载Module时先File.ReadAllBytes读取dll内容,避免Dispose问题
main
walon 2024-01-22 21:59:47 +08:00
parent 22a9793b9d
commit 276cd0a4a8
1 changed files with 16 additions and 1 deletions

View File

@ -1,6 +1,7 @@
using dnlib.DotNet; using dnlib.DotNet;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -15,6 +16,9 @@ namespace HybridCLR.Editor.Meta
private bool disposedValue; private bool disposedValue;
private bool _loadedNetstandard; private bool _loadedNetstandard;
public ModuleContext ModCtx => _modCtx;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>(); public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
private readonly List<ModuleDefMD> _loadedModulesIncludeNetstandard = new List<ModuleDefMD>(); private readonly List<ModuleDefMD> _loadedModulesIncludeNetstandard = new List<ModuleDefMD>();
@ -28,6 +32,17 @@ namespace HybridCLR.Editor.Meta
_asmResolver.UseGAC = false; _asmResolver.UseGAC = false;
} }
public ModuleDefMD TryLoadModule(string moduleName, bool loadReferenceAssemblies = true)
{
string dllPath = _assemblyPathResolver.ResolveAssembly(moduleName, false);
if (string.IsNullOrEmpty(dllPath))
{
return null;
}
return LoadModule(moduleName, loadReferenceAssemblies);
}
public ModuleDefMD LoadModule(string moduleName, bool loadReferenceAssemblies = true) public ModuleDefMD LoadModule(string moduleName, bool loadReferenceAssemblies = true)
{ {
// Debug.Log($"load module:{moduleName}"); // Debug.Log($"load module:{moduleName}");
@ -75,7 +90,7 @@ namespace HybridCLR.Editor.Meta
private ModuleDefMD DoLoadModule(string dllPath) private ModuleDefMD DoLoadModule(string dllPath)
{ {
//Debug.Log($"do load module:{dllPath}"); //Debug.Log($"do load module:{dllPath}");
ModuleDefMD mod = ModuleDefMD.Load(dllPath, _modCtx); ModuleDefMD mod = ModuleDefMD.Load(File.ReadAllBytes(dllPath), _modCtx);
_asmResolver.AddToCache(mod); _asmResolver.AddToCache(mod);
_loadedModulesIncludeNetstandard.Add(mod); _loadedModulesIncludeNetstandard.Add(mod);
return mod; return mod;