2026-10-08 21:01:52 +08:00
// Copyright 2026 Code Philosophy
/ /
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
/ /
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
/ /
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2026-10-12 12:08:19 +08:00
using HybridCLR.Meta ;
2026-10-08 21:01:52 +08:00
using HybridCLR.CodeGen.MethodBridge ;
using HybridCLR.Mod ;
using HybridCLR.Utils ;
2022-09-22 08:56:07 +08:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using UnityEditor ;
using UnityEngine ;
2026-10-08 21:01:52 +08:00
namespace HybridCLR.Commands
2022-09-22 08:56:07 +08:00
{
public class MethodBridgeGeneratorCommand
{
2026-10-08 21:01:52 +08:00
public static void GenerateMethodBridgeCppFile ( IReadOnlyCollection < GenericMethod > genericMethods , List < RawMonoPInvokeCallbackMethodInfo > reversePInvokeMethods , IReadOnlyCollection < CallNativeMethodSignatureInfo > calliMethodSignatures , string outputFile )
2022-09-22 08:56:07 +08:00
{
var g = new Generator ( new Generator . Options ( )
{
OutputFile = outputFile ,
2024-05-26 11:59:54 +08:00
GenericMethods = genericMethods ,
ReversePInvokeMethods = reversePInvokeMethods ,
2025-01-02 21:58:03 +08:00
CalliMethodSignatures = calliMethodSignatures ,
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 ) ;
2026-10-08 21:01:52 +08:00
var methodBridgeAnalyzer = new MethodBridgeAnalyzer ( new MethodBridgeAnalyzer . 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 ) ) ;
2025-04-30 16:20:03 +08:00
var reversePInvokeAnalyzer = new MonoPInvokeCallbackAnalyzer ( cache , hotUpdateDlls ) ;
2024-05-26 11:59:54 +08:00
reversePInvokeAnalyzer . Run ( ) ;
2025-01-02 21:58:03 +08:00
var calliAnalyzer = new CalliAnalyzer ( cache , hotUpdateDlls ) ;
calliAnalyzer . Run ( ) ;
2024-05-26 11:59:54 +08:00
2026-10-08 21:01:52 +08:00
string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.inc" ;
2022-09-26 12:12:57 +08:00
2026-10-08 21:01:52 +08:00
var callNativeMethodSignatures = calliAnalyzer . CalliMethodSignatures . ToList ( ) ;
GenerateMethodBridgeCppFile ( methodBridgeAnalyzer . GenericMethods , reversePInvokeAnalyzer . ReversePInvokeMethods , callNativeMethodSignatures , outputFile ) ;
2022-09-22 08:56:07 +08:00
}
}
}