[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,17 +30,20 @@ namespace HybridCLR.Editor.Commands
var gs = SettingsUtil.GlobalSettings;
var analyzer = new Analyzer(new Analyzer.Options
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
{
MaxIterationCount = Math.Min(20, gs.maxGenericReferenceIteration),
Collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames),
});
var analyzer = new Analyzer(new Analyzer.Options
{
MaxIterationCount = Math.Min(20, gs.maxGenericReferenceIteration),
Collector = collector,
});
analyzer.Run();
analyzer.Run();
var writer = new GenericReferenceWriter();
writer.Write(analyzer.AotGenericTypes.ToList(), analyzer.AotGenericMethods.ToList(), $"{Application.dataPath}/{gs.outputAOTGenericReferenceFile}");
AssetDatabase.Refresh();
var writer = new GenericReferenceWriter();
writer.Write(analyzer.AotGenericTypes.ToList(), analyzer.AotGenericMethods.ToList(), $"{Application.dataPath}/{gs.outputAOTGenericReferenceFile}");
AssetDatabase.Refresh();
}
}
}
}

View File

@ -72,32 +72,37 @@ namespace HybridCLR.Editor.Commands
{
CompileDllCommand.CompileDllActiveBuildTarget();
}
var analyzer = new Analyzer(new Analyzer.Options
{
MaxIterationCount = Math.Min(20, SettingsUtil.GlobalSettings.maxMethodBridgeGenericIteration),
Collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames),
});
analyzer.Run();
var generateJobs = new List<(PlatformABI, string)>()
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
{
(PlatformABI.Arm64, "MethodBridge_Arm64"),
(PlatformABI.Universal64, "MethodBridge_Universal64"),
(PlatformABI.Universal32, "MethodBridge_Universal32"),
};
var tasks = new List<Task>();
foreach (var (platform, stubFile) in generateJobs)
{
string templateCode = GetTemplateCode(platform);
string outputFile = $"{SettingsUtil.MethodBridgeCppDir}/{stubFile}.cpp";
tasks.Add(Task.Run(() =>
var analyzer = new Analyzer(new Analyzer.Options
{
GenerateMethodBridgeCppFile(analyzer, platform, templateCode, outputFile);
}));
MaxIterationCount = Math.Min(20, SettingsUtil.GlobalSettings.maxMethodBridgeGenericIteration),
Collector = collector,
});
analyzer.Run();
var generateJobs = new List<(PlatformABI, string)>()
{
(PlatformABI.Arm64, "MethodBridge_Arm64"),
(PlatformABI.Universal64, "MethodBridge_Universal64"),
(PlatformABI.Universal32, "MethodBridge_Universal32"),
};
var tasks = new List<Task>();
foreach (var (platform, stubFile) in generateJobs)
{
string templateCode = GetTemplateCode(platform);
string outputFile = $"{SettingsUtil.MethodBridgeCppDir}/{stubFile}.cpp";
tasks.Add(Task.Run(() =>
{
GenerateMethodBridgeCppFile(analyzer, platform, templateCode, outputFile);
}));
}
Task.WaitAll(tasks.ToArray());
}
Task.WaitAll(tasks.ToArray());
}
}
}

View File

@ -21,29 +21,32 @@ namespace HybridCLR.Editor.Link
public HashSet<TypeRef> CollectRefs(List<Assembly> rootAssemblies)
{
var assCollector = new AssemblyCache(_resolver);
var rootAssemblyName = new HashSet<string>();
foreach(var ass in rootAssemblies)
using (var assCollector = new AssemblyCache(_resolver))
{
if (!rootAssemblyName.Add(ass.GetName().Name))
var rootAssemblyName = new HashSet<string>();
foreach (var ass in rootAssemblies)
{
throw new Exception($"assembly:{ass.GetName().Name} 重复");
}
}
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
foreach (var rootAss in rootAssemblies)
{
var dnAss = assCollector.LoadModule(rootAss.GetName().Name);
foreach(var type in dnAss.GetTypeRefs())
{
if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name))
if (!rootAssemblyName.Add(ass.GetName().Name))
{
typeRefs.Add(type);
throw new Exception($"assembly:{ass.GetName().Name} 重复");
}
}
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
foreach (var rootAss in rootAssemblies)
{
var dnAss = assCollector.LoadModule(rootAss.GetName().Name);
foreach (var type in dnAss.GetTypeRefs())
{
if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name))
{
typeRefs.Add(type);
}
}
}
assCollector.Dispose();
return typeRefs;
}
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",