2022-09-22 08:56:07 +08:00
|
|
|
|
using HybridCLR.Editor;
|
2022-10-17 12:16:18 +08:00
|
|
|
|
using HybridCLR.Editor.ABI;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using HybridCLR.Editor.Meta;
|
2022-09-23 09:40:06 +08:00
|
|
|
|
using HybridCLR.Editor.MethodBridge;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace HybridCLR.Editor.Commands
|
|
|
|
|
{
|
|
|
|
|
public class MethodBridgeGeneratorCommand
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static void CleanIl2CppBuildCache()
|
|
|
|
|
{
|
|
|
|
|
string il2cppBuildCachePath = SettingsUtil.Il2CppBuildCacheDir;
|
|
|
|
|
if (!Directory.Exists(il2cppBuildCachePath))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Debug.Log($"clean il2cpp build cache:{il2cppBuildCachePath}");
|
|
|
|
|
Directory.Delete(il2cppBuildCachePath, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void GenerateMethodBridgeCppFile(Analyzer analyzer, PlatformABI platform, string templateCode, string outputFile)
|
|
|
|
|
{
|
|
|
|
|
var g = new Generator(new Generator.Options()
|
|
|
|
|
{
|
2022-10-17 12:16:18 +08:00
|
|
|
|
PlatformABI = platform,
|
2022-09-22 08:56:07 +08:00
|
|
|
|
TemplateCode = templateCode,
|
|
|
|
|
OutputFile = outputFile,
|
|
|
|
|
GenericMethods = analyzer.GenericMethods,
|
|
|
|
|
NotGenericMethods = analyzer.NotGenericMethods,
|
2023-06-07 11:47:17 +08:00
|
|
|
|
SpeicalPreserveMethods = analyzer.SpeicalPreserveMethods,
|
2022-09-22 08:56:07 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
g.PrepareMethods();
|
|
|
|
|
g.Generate();
|
|
|
|
|
Debug.LogFormat("== output:{0} ==", outputFile);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 14:26:28 +08:00
|
|
|
|
[MenuItem("HybridCLR/Generate/MethodBridge", priority = 101)]
|
2022-12-14 14:11:32 +08:00
|
|
|
|
public static void CompileAndGenerateMethodBridge()
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-12-14 14:11:32 +08:00
|
|
|
|
BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
|
|
|
|
|
CompileDllCommand.CompileDll(target);
|
|
|
|
|
GenerateMethodBridge(target);
|
2022-11-29 17:11:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 14:11:32 +08:00
|
|
|
|
public static void GenerateMethodBridge(BuildTarget target)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2023-01-11 17:41:44 +08:00
|
|
|
|
List<string> hotUpdateDllNames = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
|
2022-12-14 14:11:32 +08:00
|
|
|
|
using (AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotUpdateDllNames), hotUpdateDllNames))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
var analyzer = new Analyzer(new Analyzer.Options
|
|
|
|
|
{
|
2022-10-09 20:53:13 +08:00
|
|
|
|
MaxIterationCount = Math.Min(20, SettingsUtil.HybridCLRSettings.maxMethodBridgeGenericIteration),
|
2022-09-26 12:12:57 +08:00
|
|
|
|
Collector = collector,
|
|
|
|
|
});
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2022-09-26 12:12:57 +08:00
|
|
|
|
analyzer.Run();
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2022-09-26 12:12:57 +08:00
|
|
|
|
var tasks = new List<Task>();
|
2022-10-17 21:38:39 +08:00
|
|
|
|
string templateCode = File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/MethodBridgeStub.cpp");
|
|
|
|
|
foreach (PlatformABI platform in Enum.GetValues(typeof(PlatformABI)))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-10-17 21:38:39 +08:00
|
|
|
|
string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge_{platform}.cpp";
|
2022-09-26 12:12:57 +08:00
|
|
|
|
tasks.Add(Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
GenerateMethodBridgeCppFile(analyzer, platform, templateCode, outputFile);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
Task.WaitAll(tasks.ToArray());
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
2022-09-26 12:12:57 +08:00
|
|
|
|
|
2022-12-08 22:19:27 +08:00
|
|
|
|
CleanIl2CppBuildCache();
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|