[fix] 生成完成后释放dnlib相关对象,避免出现dll文件被占用的异常

main
walon 2022-09-26 12:12:57 +08:00
parent 830f0cca7d
commit 5926b285e3
6 changed files with 105 additions and 50 deletions

View File

@ -30,10 +30,12 @@ namespace HybridCLR.Editor.Commands
var gs = SettingsUtil.GlobalSettings;
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
{
var analyzer = new Analyzer(new Analyzer.Options
{
MaxIterationCount = Math.Min(20, gs.maxGenericReferenceIteration),
Collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames),
Collector = collector,
});
analyzer.Run();
@ -44,3 +46,4 @@ namespace HybridCLR.Editor.Commands
}
}
}
}

View File

@ -72,10 +72,13 @@ namespace HybridCLR.Editor.Commands
{
CompileDllCommand.CompileDllActiveBuildTarget();
}
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
{
var analyzer = new Analyzer(new Analyzer.Options
{
MaxIterationCount = Math.Min(20, SettingsUtil.GlobalSettings.maxMethodBridgeGenericIteration),
Collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames),
Collector = collector,
});
analyzer.Run();
@ -99,5 +102,7 @@ namespace HybridCLR.Editor.Commands
}
Task.WaitAll(tasks.ToArray());
}
}
}
}

View File

@ -21,7 +21,8 @@ namespace HybridCLR.Editor.Link
public HashSet<TypeRef> CollectRefs(List<Assembly> rootAssemblies)
{
var assCollector = new AssemblyCache(_resolver);
using (var assCollector = new AssemblyCache(_resolver))
{
var rootAssemblyName = new HashSet<string>();
foreach (var ass in rootAssemblies)
{
@ -43,7 +44,9 @@ namespace HybridCLR.Editor.Link
}
}
}
assCollector.Dispose();
return typeRefs;
}
}
}
}

View File

@ -9,11 +9,12 @@ using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class AssemblyCache
public class AssemblyCache : IDisposable
{
private readonly IAssemblyResolver _assemblyPathResolver;
private readonly ModuleContext _modCtx;
private readonly AssemblyResolver _asmResolver;
private bool disposedValue;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
@ -51,5 +52,26 @@ namespace HybridCLR.Editor.Meta
return mod;
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
foreach(var mod in LoadedModules.Values)
{
mod.Dispose();
}
LoadedModules.Clear();
}
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

View File

@ -9,13 +9,14 @@ using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class AssemblyReferenceDeepCollector
public class AssemblyReferenceDeepCollector : IDisposable
{
private readonly IAssemblyResolver _assemblyPathResolver;
private readonly List<string> _rootAssemblies;
private readonly ModuleContext _modCtx;
private readonly AssemblyResolver _asmResolver;
private bool disposedValue;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
@ -78,5 +79,26 @@ namespace HybridCLR.Editor.Meta
return mod;
}
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
foreach(var mod in LoadedModules.Values)
{
mod.Dispose();
}
}
LoadedModules.Clear();
disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

View File

@ -1,6 +1,6 @@
{
"name": "com.focus-creative-games.hybridclr_unity",
"version": "0.2.1",
"version": "0.3.0",
"displayName": "HybridCLR",
"description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR",
"category": "Runtime",