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 ;
2024-05-26 11:59:54 +08:00
using HybridCLR.Editor.ReversePInvokeWrap ;
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 ;
2023-10-07 21:59:56 +08:00
using UnityEditor.Build ;
2022-09-22 08:56:07 +08:00
using UnityEngine ;
namespace HybridCLR.Editor.Commands
{
2023-06-21 11:07:00 +08:00
using Analyzer = HybridCLR . Editor . MethodBridge . Analyzer ;
2022-09-22 08:56:07 +08:00
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 ) ;
}
2024-05-26 11:59:54 +08:00
private static void GenerateMethodBridgeCppFile ( IReadOnlyCollection < GenericMethod > genericMethods , List < RawReversePInvokeMethodInfo > reversePInvokeMethods , string outputFile )
2022-09-22 08:56:07 +08:00
{
2023-11-10 13:35:53 +08:00
string templateCode = File . ReadAllText ( outputFile , Encoding . UTF8 ) ;
2022-09-22 08:56:07 +08:00
var g = new Generator ( new Generator . Options ( )
{
TemplateCode = templateCode ,
OutputFile = outputFile ,
2024-05-26 11:59:54 +08:00
GenericMethods = genericMethods ,
ReversePInvokeMethods = reversePInvokeMethods ,
2024-05-24 18:32:44 +08:00
Development = EditorUserBuildSettings . development ,
2022-09-22 08:56:07 +08:00
} ) ;
g . Generate ( ) ;
2023-11-10 13:35:53 +08:00
Debug . LogFormat ( "[MethodBridgeGeneratorCommand] output:{0}" , outputFile ) ;
2022-09-22 08:56:07 +08:00
}
2024-05-26 11:59:54 +08:00
[MenuItem("HybridCLR/Generate/MethodBridgeAndReversePInvokeWrapper", priority = 101)]
public static void GenerateMethodBridgeAndReversePInvokeWrapper ( )
2022-09-22 08:56:07 +08:00
{
2022-12-14 14:11:32 +08:00
BuildTarget target = EditorUserBuildSettings . activeBuildTarget ;
2024-05-26 11:59:54 +08:00
GenerateMethodBridgeAndReversePInvokeWrapper ( target ) ;
2022-11-29 17:11:25 +08:00
}
2024-05-26 11:59:54 +08:00
public static void GenerateMethodBridgeAndReversePInvokeWrapper ( BuildTarget target )
2022-09-22 08:56:07 +08:00
{
2023-10-07 21:59:56 +08:00
string aotDllDir = SettingsUtil . GetAssembliesPostIl2CppStripDir ( target ) ;
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`" ) ;
}
2024-05-26 11:59:54 +08:00
AssemblyReferenceDeepCollector collector = new AssemblyReferenceDeepCollector ( MetaUtil . CreateAOTAssemblyResolver ( target ) , aotAssemblyNames ) ;
var methodBridgeAnalyzer = new Analyzer ( new Analyzer . Options
2022-09-22 08:56:07 +08:00
{
2024-05-26 11:59:54 +08:00
MaxIterationCount = Math . Min ( 20 , SettingsUtil . HybridCLRSettings . maxMethodBridgeGenericIteration ) ,
Collector = collector ,
} ) ;
2022-09-22 08:56:07 +08:00
2024-05-26 11:59:54 +08:00
methodBridgeAnalyzer . Run ( ) ;
List < string > hotUpdateDlls = SettingsUtil . HotUpdateAssemblyNamesExcludePreserved ;
var cache = new AssemblyCache ( MetaUtil . CreateHotUpdateAndAOTAssemblyResolver ( target , hotUpdateDlls ) ) ;
var reversePInvokeAnalyzer = new ReversePInvokeWrap . Analyzer ( cache , hotUpdateDlls ) ;
reversePInvokeAnalyzer . Run ( ) ;
string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp" ;
GenerateMethodBridgeCppFile ( methodBridgeAnalyzer . GenericMethods , reversePInvokeAnalyzer . ReversePInvokeMethods , outputFile ) ;
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
}
}
}