[fix] 修复生成桥接函数时,如果热更新程序集未包含任何代码直接引用了某个aot程序集,则没有为该aot程序集生成桥接函数,导致出现NotSupportNative2Managed异常的bug

main
walon 2023-10-07 21:59:56 +08:00
parent 4da7c1a524
commit 1bd27a3e22
3 changed files with 16 additions and 4 deletions

View File

@ -10,6 +10,7 @@ using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEditor; using UnityEditor;
using UnityEditor.Build;
using UnityEngine; using UnityEngine;
namespace HybridCLR.Editor.Commands namespace HybridCLR.Editor.Commands
@ -47,14 +48,20 @@ namespace HybridCLR.Editor.Commands
public static void CompileAndGenerateMethodBridge() public static void CompileAndGenerateMethodBridge()
{ {
BuildTarget target = EditorUserBuildSettings.activeBuildTarget; BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
CompileDllCommand.CompileDll(target);
GenerateMethodBridge(target); GenerateMethodBridge(target);
} }
public static void GenerateMethodBridge(BuildTarget target) public static void GenerateMethodBridge(BuildTarget target)
{ {
List<string> hotUpdateDllNames = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved; string aotDllDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDllNames), hotUpdateDllNames)) List<string> aotAssemblyNames = Directory.Exists(aotDllDir) ?
Directory.GetFiles(aotDllDir, "*.dll", SearchOption.TopDirectoryOnly).Select(Path.GetFileNameWithoutExtension).ToList()
: new List<string>();
if (aotAssemblyNames.Count == 0)
{
throw new Exception($"no aot assembly found. please run `HybridCLR/Generate/All` or `HybridCLR/Generate/AotDlls` to generate aot dlls before runing `HybridCLR/Generate/MethodBridge`");
}
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateAOTAssemblyResolver(target), aotAssemblyNames))
{ {
var analyzer = new Analyzer(new Analyzer.Options var analyzer = new Analyzer(new Analyzer.Options
{ {

View File

@ -30,6 +30,11 @@ namespace HybridCLR.Editor.Meta
return LoadedModules.Where(e => !_rootAssemblies.Contains(e.Key)).Select(e => e.Value).ToList(); return LoadedModules.Where(e => !_rootAssemblies.Contains(e.Key)).Select(e => e.Value).ToList();
} }
public List<ModuleDefMD> GetLoadedModules()
{
return LoadedModules.Select(e => e.Value).ToList();
}
public List<ModuleDefMD> GetLoadedModulesOfRootAssemblies() public List<ModuleDefMD> GetLoadedModulesOfRootAssemblies()
{ {
return _rootAssemblies.Select(ass => LoadedModules[ass]).ToList(); return _rootAssemblies.Select(ass => LoadedModules[ass]).ToList();

View File

@ -143,7 +143,7 @@ namespace HybridCLR.Editor.MethodBridge
{ {
// 将所有非泛型函数全部加入函数列表同时立马walk这些method。 // 将所有非泛型函数全部加入函数列表同时立马walk这些method。
// 后续迭代中将只遍历MethodSpec // 后续迭代中将只遍历MethodSpec
foreach (var ass in _assemblyCollector.GetLoadedModulesExcludeRootAssemblies()) foreach (var ass in _assemblyCollector.GetLoadedModules())
{ {
foreach (TypeDef typeDef in ass.GetTypes()) foreach (TypeDef typeDef in ass.GetTypes())
{ {