[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; var gs = SettingsUtil.GlobalSettings;
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
{
var analyzer = new Analyzer(new Analyzer.Options var analyzer = new Analyzer(new Analyzer.Options
{ {
MaxIterationCount = Math.Min(20, gs.maxGenericReferenceIteration), MaxIterationCount = Math.Min(20, gs.maxGenericReferenceIteration),
Collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames), Collector = collector,
}); });
analyzer.Run(); analyzer.Run();
@ -43,4 +45,5 @@ namespace HybridCLR.Editor.Commands
AssetDatabase.Refresh(); AssetDatabase.Refresh();
} }
} }
}
} }

View File

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

View File

@ -21,9 +21,10 @@ namespace HybridCLR.Editor.Link
public HashSet<TypeRef> CollectRefs(List<Assembly> rootAssemblies) public HashSet<TypeRef> CollectRefs(List<Assembly> rootAssemblies)
{ {
var assCollector = new AssemblyCache(_resolver); using (var assCollector = new AssemblyCache(_resolver))
{
var rootAssemblyName = new HashSet<string>(); var rootAssemblyName = new HashSet<string>();
foreach(var ass in rootAssemblies) foreach (var ass in rootAssemblies)
{ {
if (!rootAssemblyName.Add(ass.GetName().Name)) if (!rootAssemblyName.Add(ass.GetName().Name))
{ {
@ -35,7 +36,7 @@ namespace HybridCLR.Editor.Link
foreach (var rootAss in rootAssemblies) foreach (var rootAss in rootAssemblies)
{ {
var dnAss = assCollector.LoadModule(rootAss.GetName().Name); var dnAss = assCollector.LoadModule(rootAss.GetName().Name);
foreach(var type in dnAss.GetTypeRefs()) foreach (var type in dnAss.GetTypeRefs())
{ {
if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name)) if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name))
{ {
@ -43,7 +44,9 @@ namespace HybridCLR.Editor.Link
} }
} }
} }
assCollector.Dispose();
return typeRefs; return typeRefs;
} }
} }
}
} }

View File

@ -9,11 +9,12 @@ using UnityEngine;
namespace HybridCLR.Editor.Meta namespace HybridCLR.Editor.Meta
{ {
public class AssemblyCache public class AssemblyCache : IDisposable
{ {
private readonly IAssemblyResolver _assemblyPathResolver; private readonly IAssemblyResolver _assemblyPathResolver;
private readonly ModuleContext _modCtx; private readonly ModuleContext _modCtx;
private readonly AssemblyResolver _asmResolver; private readonly AssemblyResolver _asmResolver;
private bool disposedValue;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>(); public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
@ -51,5 +52,26 @@ namespace HybridCLR.Editor.Meta
return mod; 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 namespace HybridCLR.Editor.Meta
{ {
public class AssemblyReferenceDeepCollector public class AssemblyReferenceDeepCollector : IDisposable
{ {
private readonly IAssemblyResolver _assemblyPathResolver; private readonly IAssemblyResolver _assemblyPathResolver;
private readonly List<string> _rootAssemblies; private readonly List<string> _rootAssemblies;
private readonly ModuleContext _modCtx; private readonly ModuleContext _modCtx;
private readonly AssemblyResolver _asmResolver; private readonly AssemblyResolver _asmResolver;
private bool disposedValue;
public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>(); public Dictionary<string, ModuleDefMD> LoadedModules { get; } = new Dictionary<string, ModuleDefMD>();
@ -78,5 +79,26 @@ namespace HybridCLR.Editor.Meta
return mod; 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", "name": "com.focus-creative-games.hybridclr_unity",
"version": "0.2.1", "version": "0.3.0",
"displayName": "HybridCLR", "displayName": "HybridCLR",
"description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR",
"category": "Runtime", "category": "Runtime",