From 213a11ff2514cc92767b4f9b67a51aefcb79cf67 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 30 Apr 2025 15:05:40 +0800 Subject: [PATCH] [new] generate Managed2NativeFunction for PInvoke method --- .../Commands/MethodBridgeGeneratorCommand.cs | 8 +++- Editor/MethodBridge/PInvokeAnalyzer.cs | 46 +++++++++++++++++++ Editor/MethodBridge/PInvokeAnalyzer.cs.meta | 11 +++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 Editor/MethodBridge/PInvokeAnalyzer.cs create mode 100644 Editor/MethodBridge/PInvokeAnalyzer.cs.meta diff --git a/Editor/Commands/MethodBridgeGeneratorCommand.cs b/Editor/Commands/MethodBridgeGeneratorCommand.cs index c828264..38f36b0 100644 --- a/Editor/Commands/MethodBridgeGeneratorCommand.cs +++ b/Editor/Commands/MethodBridgeGeneratorCommand.cs @@ -31,7 +31,7 @@ namespace HybridCLR.Editor.Commands Directory.Delete(il2cppBuildCachePath, true); } - private static void GenerateMethodBridgeCppFile(IReadOnlyCollection genericMethods, List reversePInvokeMethods, IReadOnlyCollection calliMethodSignatures, string tempFile, string outputFile) + private static void GenerateMethodBridgeCppFile(IReadOnlyCollection genericMethods, List reversePInvokeMethods, IReadOnlyCollection calliMethodSignatures, string tempFile, string outputFile) { string templateCode = File.ReadAllText(tempFile, Encoding.UTF8); var g = new Generator(new Generator.Options() @@ -83,11 +83,15 @@ namespace HybridCLR.Editor.Commands var calliAnalyzer = new CalliAnalyzer(cache, hotUpdateDlls); calliAnalyzer.Run(); + var pinvokeAnalyzer = new PInvokeAnalyzer(cache, hotUpdateDlls); + pinvokeAnalyzer.Run(); + var callPInvokeMethodSignatures = pinvokeAnalyzer.PInvokeMethodSignatures; string templateFile = $"{SettingsUtil.TemplatePathInPackage}/MethodBridge.cpp.tpl"; string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge.cpp"; - GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, calliAnalyzer.CalliMethodSignatures, templateFile, outputFile); + var callNativeMethodSignatures = calliAnalyzer.CalliMethodSignatures.Concat(pinvokeAnalyzer.PInvokeMethodSignatures).ToList(); + GenerateMethodBridgeCppFile(methodBridgeAnalyzer.GenericMethods, reversePInvokeAnalyzer.ReversePInvokeMethods, callNativeMethodSignatures, templateFile, outputFile); CleanIl2CppBuildCache(); } diff --git a/Editor/MethodBridge/PInvokeAnalyzer.cs b/Editor/MethodBridge/PInvokeAnalyzer.cs new file mode 100644 index 0000000..ecca457 --- /dev/null +++ b/Editor/MethodBridge/PInvokeAnalyzer.cs @@ -0,0 +1,46 @@ +using dnlib.DotNet; +using HybridCLR.Editor.Meta; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace HybridCLR.Editor.MethodBridge +{ + + public class PInvokeAnalyzer + { + private readonly List _rootModules = new List(); + + private readonly List _pinvokeMethodSignatures = new List(); + + public List PInvokeMethodSignatures => _pinvokeMethodSignatures; + + public PInvokeAnalyzer(AssemblyCache cache, List assemblyNames) + { + foreach (var assemblyName in assemblyNames) + { + _rootModules.Add(cache.LoadModule(assemblyName)); + } + } + + public void Run() + { + foreach (var mod in _rootModules) + { + foreach (TypeDef type in mod.GetTypes()) + { + foreach (MethodDef method in type.Methods) + { + if (method.IsPinvokeImpl) + { + _pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo { MethodSig = method.MethodSig }); + } + } + } + } + } + } +} diff --git a/Editor/MethodBridge/PInvokeAnalyzer.cs.meta b/Editor/MethodBridge/PInvokeAnalyzer.cs.meta new file mode 100644 index 0000000..3d2ae8a --- /dev/null +++ b/Editor/MethodBridge/PInvokeAnalyzer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9923175c961b78849aeaf99708e294ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: