[refactor] 重构并且修复了Generate相关Assembly Resolve潜在的bug。

[opt] 优化 PrebuildCommand.Build 中多次编译热更新dll的问题,大幅缩短了时间。
main
walon 2022-12-14 14:11:32 +08:00
parent 947b15e7d9
commit 00f94335c3
20 changed files with 154 additions and 244 deletions

View File

@ -24,11 +24,12 @@ namespace HybridCLR.Editor.BuildProcessors
Debug.Log($"[FilterHotFixAssemblies] disabled");
return assemblies;
}
List<string> allHotUpdateDllFiles = SettingsUtil.HotUpdateAssemblyFiles;
List<string> allHotUpdateDllNames = SettingsUtil.HotUpdateAssemblyNames;
List<string> allHotupdateDllFiles = SettingsUtil.HotUpdateAssemblyFiles;
// 检查是否重复填写
var hotUpdateDllSet = new HashSet<string>();
foreach(var hotUpdateDll in allHotUpdateDllFiles)
foreach(var hotUpdateDll in allHotUpdateDllNames)
{
if (!hotUpdateDllSet.Add(hotUpdateDll))
{
@ -36,20 +37,20 @@ namespace HybridCLR.Editor.BuildProcessors
}
}
var assResolver = MetaUtil.CreateHotUpdateAssemblyResolver(EditorUserBuildSettings.activeBuildTarget);
var assResolver = MetaUtil.CreateHotUpdateAssemblyResolver(EditorUserBuildSettings.activeBuildTarget, allHotUpdateDllNames);
// 检查是否填写了正确的dll名称
foreach (var hotUpdateDll in allHotUpdateDllFiles)
foreach (var hotUpdateDllName in allHotUpdateDllNames)
{
string hotUpdateAssName = hotUpdateDll.Substring(0, hotUpdateDll.Length - 4);
if (assemblies.All(ass => !ass.EndsWith(hotUpdateDll)) && string.IsNullOrEmpty(assResolver.ResolveAssembly(hotUpdateAssName, false)))
string hotUpdateDllFile = hotUpdateDllName + ".dll";
if (assemblies.All(ass => !ass.EndsWith(hotUpdateDllFile)) && string.IsNullOrEmpty(assResolver.ResolveAssembly(hotUpdateDllName, false)))
{
throw new Exception($"热更新 assembly:{hotUpdateDll} 不存在,请检查拼写错误");
throw new Exception($"热更新 assembly:{hotUpdateDllFile} 不存在,请检查拼写错误");
}
Debug.Log($"[FilterHotFixAssemblies] 过滤热更新assembly:{hotUpdateDll}");
Debug.Log($"[FilterHotFixAssemblies] 过滤热更新assembly:{hotUpdateDllFile}");
}
// 将热更dll从打包列表中移除
return assemblies.Where(ass => allHotUpdateDllFiles.All(dll => !ass.EndsWith(dll, StringComparison.OrdinalIgnoreCase))).ToArray();
return assemblies.Where(ass => allHotupdateDllFiles.All(dll => !ass.EndsWith(dll, StringComparison.OrdinalIgnoreCase))).ToArray();
}
}
}

View File

@ -14,23 +14,19 @@ namespace HybridCLR.Editor.Commands
{
[MenuItem("HybridCLR/Generate/AOTGenericReference", priority = 102)]
public static void GenerateAOTGenericReference()
public static void CompileAndGenerateAOTGenericReference()
{
GenerateAOTGenericReference(true);
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
GenerateAOTGenericReference(target);
}
public static void GenerateAOTGenericReference(bool compileDll)
public static void GenerateAOTGenericReference(BuildTarget target)
{
// 此处理论会有点问题,打每个平台的时候,都得针对当前平台生成桥接函数
// 但影响不大,先这样吧
if (compileDll)
{
CompileDllCommand.CompileDllActiveBuildTarget();
}
var gs = SettingsUtil.HybridCLRSettings;
List<string> hotUpdateDllNames = SettingsUtil.HotUpdateAssemblyNames;
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), SettingsUtil.HotUpdateAssemblyNames))
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDllNames), hotUpdateDllNames))
{
var analyzer = new Analyzer(new Analyzer.Options
{

View File

@ -17,7 +17,7 @@ namespace HybridCLR.Editor.Commands
{
var options = new Il2CppDef.Il2CppDefGenerator.Options()
{
UnityVersion = UnityEngine.Application.unityVersion,
UnityVersion = Application.unityVersion,
OutputFile = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/il2cpp-config.h",
};

View File

@ -1,4 +1,5 @@
using HybridCLR.Editor.Link;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Reflection;
@ -14,21 +15,18 @@ namespace HybridCLR.Editor.Commands
[MenuItem("HybridCLR/Generate/LinkXml", priority = 100)]
public static void GenerateLinkXml()
{
GenerateLinkXml(true);
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
GenerateLinkXml(target);
}
public static void GenerateLinkXml(bool compileDll)
public static void GenerateLinkXml(BuildTarget target)
{
if (compileDll)
{
CompileDllCommand.CompileDllActiveBuildTarget();
}
var ls = SettingsUtil.HybridCLRSettings;
List<string> hotfixAssemblies = SettingsUtil.HotUpdateAssemblyNames;
var analyzer = new Analyzer(Meta.MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget), HybridCLRSettings.Instance.collectAssetReferenceTypes);
var analyzer = new Analyzer(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotfixAssemblies), HybridCLRSettings.Instance.collectAssetReferenceTypes);
var refTypes = analyzer.CollectRefs(hotfixAssemblies);
Debug.Log($"[LinkGeneratorCommand] hotfix assembly count:{hotfixAssemblies.Count}, ref type count:{refTypes.Count} output:{Application.dataPath}/{ls.outputLinkFile}");

View File

@ -45,27 +45,17 @@ namespace HybridCLR.Editor.Commands
}
[MenuItem("HybridCLR/Generate/MethodBridge", priority = 101)]
public static void GenerateMethodBridge()
{
GenerateMethodBridge(true);
}
static IAssemblyResolver CreateBuildTargetAssemblyResolver(BuildTarget target)
{
return new CombinedAssemblyResolver(
new PathAssemblyResolver(SettingsUtil.GetAssembliesPostIl2CppStripDir(target)),
MetaUtil.CreateHotUpdateAssemblyResolver(target)
);
}
public static void GenerateMethodBridge(bool compileDll)
public static void CompileAndGenerateMethodBridge()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
if (compileDll)
{
CompileDllCommand.CompileDllActiveBuildTarget();
}
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(CreateBuildTargetAssemblyResolver(target), SettingsUtil.HotUpdateAssemblyNames))
CompileDllCommand.CompileDll(target);
GenerateMethodBridge(target);
}
public static void GenerateMethodBridge(BuildTarget target)
{
List<string> hotUpdateDllNames = SettingsUtil.HotUpdateAssemblyNames;
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDllNames), hotUpdateDllNames))
{
var analyzer = new Analyzer(new Analyzer.Options
{

View File

@ -15,16 +15,18 @@ namespace HybridCLR.Editor.Commands
[MenuItem("HybridCLR/Generate/All", priority = 200)]
public static void GenerateAll()
{
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
Il2CppDefGeneratorCommand.GenerateIl2CppDef();
// 顺序随意
ReversePInvokeWrapperGeneratorCommand.GenerateReversePInvokeWrapper();
// AOTReferenceGeneratorCommand 涉及到代码生成必须在MethodBridgeGeneratorCommand之前
AOTReferenceGeneratorCommand.GenerateAOTGenericReference();
MethodBridgeGeneratorCommand.GenerateMethodBridge();
// 这几个生成依赖HotUpdateDlls
LinkGeneratorCommand.GenerateLinkXml(target);
ReversePInvokeWrapperGeneratorCommand.GenerateReversePInvokeWrapper(target);
AOTReferenceGeneratorCommand.GenerateAOTGenericReference(target);
// 顺序随意,只要保证 GenerateLinkXml之前有调用过CompileDll即可
LinkGeneratorCommand.GenerateLinkXml(false);
// 桥接函数生成依赖于AOT dll必须保证已经build过生成AOT dll
MethodBridgeGeneratorCommand.GenerateMethodBridge(target);
}
}
}

View File

@ -19,12 +19,20 @@ namespace HybridCLR.Editor.Commands
{
[MenuItem("HybridCLR/Generate/ReversePInvokeWrapper", priority = 103)]
public static void GenerateReversePInvokeWrapper()
public static void CompileAndGenerateReversePInvokeWrapper()
{
CompileDllCommand.CompileDllActiveBuildTarget();
using (var cache = new AssemblyCache(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget)))
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
GenerateReversePInvokeWrapper(target);
}
public static void GenerateReversePInvokeWrapper(BuildTarget target)
{
List<string> hotUpdateDlls = SettingsUtil.HotUpdateAssemblyNames;
using (var cache = new AssemblyCache(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDlls)))
{
var analyzer = new ReversePInvokeWrap.Analyzer(cache, SettingsUtil.HotUpdateAssemblyNames);
var analyzer = new ReversePInvokeWrap.Analyzer(cache, hotUpdateDlls);
analyzer.Run();

View File

@ -26,14 +26,7 @@ namespace HybridCLR.Editor.Link
{
using (var assCollector = new AssemblyCache(_resolver))
{
var rootAssemblyName = new HashSet<string>();
foreach (var ass in rootAssemblies)
{
if (!rootAssemblyName.Add(ass))
{
throw new Exception($"assembly:{ass} 重复");
}
}
var rootAssemblyNames = new HashSet<string>(rootAssemblies);
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
foreach (var rootAss in rootAssemblies)
@ -41,7 +34,7 @@ namespace HybridCLR.Editor.Link
var dnAss = assCollector.LoadModule(rootAss, _analyzeAssetType);
foreach (var type in dnAss.GetTypeRefs())
{
if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name.ToString()))
if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString()))
{
typeRefs.Add(type);
}
@ -51,7 +44,7 @@ namespace HybridCLR.Editor.Link
if (_analyzeAssetType)
{
var modsExludeRoots = assCollector.LoadedModules
.Where(e => !rootAssemblyName.Contains(e.Key))
.Where(e => !rootAssemblyNames.Contains(e.Key))
.ToDictionary(e => e.Key, e => e.Value);
CollectObjectTypeInAssets(modsExludeRoots, typeRefs);
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.Meta
{
public abstract class AssemblyResolverBase : IAssemblyResolver
{
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
{
if (TryResolveAssembly(assemblyName, out string assemblyPath))
{
return assemblyPath;
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} 失败! 如果是热更新dll找不到请先运行`HybridCLR/CompileDll/ActiveBuildTarget`编译生成热更新dll。如果是AOT dll找不到请先运行`HybridCLR/Generate/LinkXml`,接着在`Build Settings`中打包或者导出工程来生成AOT dll");
}
return null;
}
protected abstract bool TryResolveAssembly(string assemblyName, out string assemblyPath);
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 6c235be11c219a74fa5eb55ed54e7662
guid: 5f8d48774b790364cbd36f1f68fd6614
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace HybridCLR.Editor.Meta
{
public class CombinedAssemblyResolver : IAssemblyResolver
public class CombinedAssemblyResolver : AssemblyResolverBase
{
private readonly IAssemblyResolver[] _resolvers;
@ -15,21 +15,19 @@ namespace HybridCLR.Editor.Meta
_resolvers = resolvers;
}
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
protected override bool TryResolveAssembly(string assemblyName, out string assemblyPath)
{
foreach(var resolver in _resolvers)
{
var assembly = resolver.ResolveAssembly(assemblyName, false);
if (assembly != null)
{
return assembly;
assemblyPath = assembly;
return true;
}
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} fail");
}
return null;
assemblyPath = null;
return false;
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class FixedSetAssemblyResolver : AssemblyResolverBase
{
private readonly string _rootDir;
private readonly HashSet<string> _fileNames;
public FixedSetAssemblyResolver(string rootDir, IEnumerable<string> fileNameNotExts)
{
_rootDir = rootDir;
_fileNames = new HashSet<string>(fileNameNotExts);
}
protected override bool TryResolveAssembly(string assemblyName, out string assemblyPath)
{
if (_fileNames.Contains(assemblyName))
{
assemblyPath = $"{_rootDir}/{assemblyName}.dll";
if (File.Exists(assemblyPath))
{
Debug.Log($"[FixedSetAssemblyResolver] resolve:{assemblyName} path:{assemblyPath}");
return true;
}
}
assemblyPath = null;
return false;
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 87ece9687eaa17648bdc9d2c9bf30520
guid: f135accd10f42c64b9735c3aa8cb1e77
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -120,52 +120,39 @@ namespace HybridCLR.Editor.Meta
return typeSigs.Select(s => ToShareTypeSig(s)).ToList();
}
public static IAssemblyResolver CreateHotUpdateAssemblyResolver(BuildTarget target)
public static IAssemblyResolver CreateHotUpdateAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
{
var externalDirs = HybridCLRSettings.Instance.externalHotUpdateAssembliyDirs;
var defaultHotUpdateOutputDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
IAssemblyResolver defaultHotUpdateResolver = new FixedSetAssemblyResolver(defaultHotUpdateOutputDir, hotUpdateDlls);
if (externalDirs == null || externalDirs.Length == 0)
{
return new PathAssemblyResolver(defaultHotUpdateOutputDir);
return defaultHotUpdateResolver;
}
else
{
var externalDirList = new List<string>();
var resolvers = new List<IAssemblyResolver>();
foreach (var dir in externalDirs)
{
externalDirList.Add($"{dir}/{target}");
externalDirList.Add(dir);
resolvers.Add(new FixedSetAssemblyResolver($"{dir}/{target}", hotUpdateDlls));
resolvers.Add(new FixedSetAssemblyResolver(dir, hotUpdateDlls));
}
externalDirList.Add(defaultHotUpdateOutputDir);
return new PathAssemblyResolver(externalDirList.ToArray());
resolvers.Add(defaultHotUpdateResolver);
return new CombinedAssemblyResolver(resolvers.ToArray());
}
}
public static IAssemblyResolver CreateExternalAssemblyResolver(BuildTarget target)
{
var externalDirs = HybridCLRSettings.Instance.externalHotUpdateAssembliyDirs;
if (externalDirs == null || externalDirs.Length == 0)
{
return new PathAssemblyResolver();
}
else
{
var externalDirList = new List<string>();
foreach (var dir in externalDirs)
{
externalDirList.Add($"{dir}/{target}");
externalDirList.Add(dir);
}
return new PathAssemblyResolver(externalDirList.ToArray());
}
}
public static IAssemblyResolver CreateBuildTargetAssemblyResolver(BuildTarget target)
public static IAssemblyResolver CreateAOTAssemblyResolver(BuildTarget target)
{
return new CombinedAssemblyResolver(CreateHotUpdateAssemblyResolver(target),
new UnityPluginAssemblyResolver(),
new UnityDotNetAOTAssemblyResolver(),
new UnityEditorAssemblyResolver());
}
return new PathAssemblyResolver(SettingsUtil.GetAssembliesPostIl2CppStripDir(target));
}
public static IAssemblyResolver CreateHotUpdateAndAOTAssemblyResolver(BuildTarget target, List<string> hotUpdateDlls)
{
return new CombinedAssemblyResolver(
CreateHotUpdateAssemblyResolver(target, hotUpdateDlls),
CreateAOTAssemblyResolver(target)
);
}
}
}

View File

@ -8,7 +8,7 @@ using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class PathAssemblyResolver : IAssemblyResolver
public class PathAssemblyResolver : AssemblyResolverBase
{
private readonly string[] _searchPaths;
public PathAssemblyResolver(params string[] searchPaths)
@ -16,7 +16,7 @@ namespace HybridCLR.Editor.Meta
_searchPaths = searchPaths;
}
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
protected override bool TryResolveAssembly(string assemblyName, out string assemblyPath)
{
foreach(var path in _searchPaths)
{
@ -24,14 +24,12 @@ namespace HybridCLR.Editor.Meta
if (File.Exists(assPath))
{
Debug.Log($"resolve {assemblyName} at {assPath}");
return assPath;
assemblyPath = assPath;
return true;
}
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} fail");
}
return null;
assemblyPath = null;
return false;
}
}
}

View File

@ -1,43 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class UnityDotNetAOTAssemblyResolver : IAssemblyResolver
{
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
{
//
#if !UNITY_2021_1_OR_NEWER
var assemblyFile = $"{UnityEditor.EditorApplication.applicationContentsPath}/MonoBleedingEdge/lib/mono/unityaot/{assemblyName}.dll";
#else
#if UNITY_STANDALONE_WIN
var assemblyFile = $"{UnityEditor.EditorApplication.applicationContentsPath}/MonoBleedingEdge/lib/mono/unityaot-win32/{assemblyName}.dll";
#elif UNITY_STANDALONE_OSX || UNITY_IOS
var assemblyFile = $"{UnityEditor.EditorApplication.applicationContentsPath}/MonoBleedingEdge/lib/mono/unityaot-macos/{assemblyName}.dll";
#elif UNITY_STANDALONE_LINUX || UNITY_ANDROID
var assemblyFile = $"{UnityEditor.EditorApplication.applicationContentsPath}/MonoBleedingEdge/lib/mono/unityaot-linux/{assemblyName}.dll";
#else
var assemblyFile = $"{UnityEditor.EditorApplication.applicationContentsPath}/MonoBleedingEdge/lib/mono/unityaot-linux/{assemblyName}.dll";
#endif
#endif
if (File.Exists(assemblyFile))
{
Debug.Log($"UnityAOTAssemblyResolver.ResolveAssembly:{assemblyFile}");
return assemblyFile;
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} fail");
}
return null;
}
}
}

View File

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.Meta
{
public class UnityEditorAssemblyResolver : IAssemblyResolver
{
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
{
var refAss = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assemblyName);
if (refAss != null)
{
return refAss.Location;
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} fail");
}
return null;
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 78d06a3ad750c0b4b80b583188b02df3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,44 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.Meta
{
public class UnityPluginAssemblyResolver : IAssemblyResolver
{
private static readonly HashSet<string> s_ignoreDirs = new HashSet<string>
{
"Editor",
"StreamingAssets",
"Resources",
};
private bool IsPluginDll(string dllPath)
{
var fileOrDirs = new HashSet<string>(dllPath.Split('\\', '/'));
return !fileOrDirs.Any(dir => dir.EndsWith("~")) && !fileOrDirs.Intersect(s_ignoreDirs).Any();
}
public string ResolveAssembly(string assemblyName, bool throwExIfNotFind)
{
foreach(var dll in Directory.GetFiles(UnityEngine.Application.dataPath, "*.dll", SearchOption.AllDirectories))
{
if (Path.GetFileNameWithoutExtension(dll) == assemblyName && IsPluginDll(dll))
{
Debug.Log($"UnityPluginAssemblyResolver.ResolveAssembly:{dll}");
return dll;
}
}
if (throwExIfNotFind)
{
throw new Exception($"resolve assembly:{assemblyName} fail");
}
return null;
}
}
}

View File

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