diff --git a/Data~/Templates/MethodBridge_Arm64.cpp.txt b/Data~/Templates/MethodBridgeStub.cpp similarity index 54% rename from Data~/Templates/MethodBridge_Arm64.cpp.txt rename to Data~/Templates/MethodBridgeStub.cpp index 0270f37..fe067e0 100644 --- a/Data~/Templates/MethodBridge_Arm64.cpp.txt +++ b/Data~/Templates/MethodBridgeStub.cpp @@ -1,5 +1,3 @@ -#include "MethodBridge.h" - #include #include "vm/ClassInlines.h" #include "vm/Object.h" @@ -8,14 +6,15 @@ #include "../metadata/MetadataModule.h" #include "../metadata/MetadataUtil.h" -#include "Interpreter.h" -#include "MemoryUtil.h" -#include "InstrinctDef.h" +#include "../interpreter/MethodBridge.h" +#include "../interpreter/Interpreter.h" +#include "../interpreter/MemoryUtil.h" +#include "../interpreter/InstrinctDef.h" using namespace hybridclr::interpreter; -#if HYBRIDCLR_ABI_ARM_64 -//!!!{{INVOKE_STUB +#if {PLATFORM_ABI} +//!!!{{CODE -//!!!}}INVOKE_STUB +//!!!}}CODE #endif diff --git a/Data~/Templates/MethodBridge_Arm64.cpp.txt.meta b/Data~/Templates/MethodBridge_Arm64.cpp.txt.meta deleted file mode 100644 index 7f4a637..0000000 --- a/Data~/Templates/MethodBridge_Arm64.cpp.txt.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 1249fb131a848a14f8dd0938e3cc2558 -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Data~/Templates/MethodBridge_Universal32.cpp.txt b/Data~/Templates/MethodBridge_Universal32.cpp.txt deleted file mode 100644 index 975ca43..0000000 --- a/Data~/Templates/MethodBridge_Universal32.cpp.txt +++ /dev/null @@ -1,21 +0,0 @@ -#include "MethodBridge.h" - -#include -#include "vm/ClassInlines.h" -#include "vm/Object.h" -#include "vm/Class.h" - -#include "../metadata/MetadataModule.h" -#include "../metadata/MetadataUtil.h" - -#include "Interpreter.h" -#include "MemoryUtil.h" -#include "InstrinctDef.h" - -using namespace hybridclr::interpreter; - -#if HYBRIDCLR_ABI_UNIVERSAL_32 -//!!!{{INVOKE_STUB - -//!!!}}INVOKE_STUB -#endif diff --git a/Data~/Templates/MethodBridge_Universal32.cpp.txt.meta b/Data~/Templates/MethodBridge_Universal32.cpp.txt.meta deleted file mode 100644 index 6d489e7..0000000 --- a/Data~/Templates/MethodBridge_Universal32.cpp.txt.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: e06e14385d246aa4ab853d3fd04d6eec -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Data~/Templates/MethodBridge_Universal64.cpp.txt b/Data~/Templates/MethodBridge_Universal64.cpp.txt deleted file mode 100644 index cdc952f..0000000 --- a/Data~/Templates/MethodBridge_Universal64.cpp.txt +++ /dev/null @@ -1,21 +0,0 @@ -#include "MethodBridge.h" - -#include -#include "vm/ClassInlines.h" -#include "vm/Object.h" -#include "vm/Class.h" - -#include "../metadata/MetadataModule.h" -#include "../metadata/MetadataUtil.h" - -#include "Interpreter.h" -#include "MemoryUtil.h" -#include "InstrinctDef.h" - -using namespace hybridclr::interpreter; - -#if HYBRIDCLR_ABI_UNIVERSAL_64 -//!!!{{INVOKE_STUB - -//!!!}}INVOKE_STUB -#endif diff --git a/Data~/Templates/MethodBridge_Universal64.cpp.txt.meta b/Data~/Templates/MethodBridge_Universal64.cpp.txt.meta deleted file mode 100644 index 914ef8c..0000000 --- a/Data~/Templates/MethodBridge_Universal64.cpp.txt.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 2965fa89651ee0c4eb4c4fdcf95b76b8 -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Data~/Templates/ReversePInvokeMethodStub.cpp b/Data~/Templates/ReversePInvokeMethodStub.cpp new file mode 100644 index 0000000..96fbc81 --- /dev/null +++ b/Data~/Templates/ReversePInvokeMethodStub.cpp @@ -0,0 +1,32 @@ +#include "../metadata/ReversePInvokeMethodStub.h" +#include "../metadata/MetadataModule.h" + +namespace hybridclr +{ +namespace metadata +{ +#if {PLATFORM_ABI} + + //!!!{{CODE + + void __ReversePInvokeMethod_0(void* xState) + { + CallLuaFunction(xState, 0); + } + + void __ReversePInvokeMethod_1(void* xState) + { + CallLuaFunction(xState, 1); + } + + Il2CppMethodPointer s_ReversePInvokeMethodStub[] + { + (Il2CppMethodPointer)__ReversePInvokeMethod_0, + (Il2CppMethodPointer)__ReversePInvokeMethod_1, + nullptr, + }; + + //!!!}}CODE +#endif +} +} \ No newline at end of file diff --git a/Data~/Templates/ReversePInvokeMethodStub.cpp.txt b/Data~/Templates/ReversePInvokeMethodStub.cpp.txt deleted file mode 100644 index 736aebe..0000000 --- a/Data~/Templates/ReversePInvokeMethodStub.cpp.txt +++ /dev/null @@ -1,37 +0,0 @@ -#include "ReversePInvokeMethodStub.h" -#include "MetadataModule.h" - -namespace hybridclr -{ -namespace metadata -{ - - void CallLuaFunction(void* xState, int32_t wrapperIndex) - { - const MethodInfo* method = MetadataModule::GetMethodInfoByReversePInvokeWrapperIndex(wrapperIndex); - typedef void (*Callback)(void* xState, const MethodInfo* method); - ((Callback)(method->methodPointerCallByInterp))(xState, method); - } - - //!!!{{REVERSE_PINVOKE_METHOD_STUB - - void __ReversePInvokeMethod_0(void* xState) - { - CallLuaFunction(xState, 0); - } - - void __ReversePInvokeMethod_1(void* xState) - { - CallLuaFunction(xState, 1); - } - - Il2CppMethodPointer s_ReversePInvokeMethodStub[] - { - (Il2CppMethodPointer)__ReversePInvokeMethod_0, - (Il2CppMethodPointer)__ReversePInvokeMethod_1, - nullptr, - }; - - //!!!}}REVERSE_PINVOKE_METHOD_STUB -} -} \ No newline at end of file diff --git a/Data~/Templates/ReversePInvokeMethodStub.cpp.txt.meta b/Data~/Templates/ReversePInvokeMethodStub.cpp.txt.meta deleted file mode 100644 index 42dda1d..0000000 --- a/Data~/Templates/ReversePInvokeMethodStub.cpp.txt.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 6447cdc1825eeef49a3679545ac31d95 -TextScriptImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Editor/ABI/ABIUtil.cs b/Editor/ABI/ABIUtil.cs new file mode 100644 index 0000000..53939e0 --- /dev/null +++ b/Editor/ABI/ABIUtil.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HybridCLR.Editor.ABI +{ + public static class ABIUtil + { + public static string GetHybridCLRPlatformMacro(PlatformABI abi) + { + switch(abi) + { + case PlatformABI.Arm64: return "HYBRIDCLR_ABI_ARM_64"; + case PlatformABI.Universal64: return "HYBRIDCLR_ABI_UNIVERSAL_64"; + case PlatformABI.Universal32: return "HYBRIDCLR_ABI_UNIVERSAL_32"; + default: throw new NotSupportedException(); + } + } + } +} diff --git a/Editor/Template/TemplateUtil.cs.meta b/Editor/ABI/ABIUtil.cs.meta similarity index 83% rename from Editor/Template/TemplateUtil.cs.meta rename to Editor/ABI/ABIUtil.cs.meta index 1791b45..79a2ec0 100644 --- a/Editor/Template/TemplateUtil.cs.meta +++ b/Editor/ABI/ABIUtil.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 69e748de769c24948afd4f8a9df82b8c +guid: edeb86f4b5b13ca4cb0fe9d87ce509bb MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Editor/ABI/MethodDesc.cs b/Editor/ABI/MethodDesc.cs index 6a55466..6675f34 100644 --- a/Editor/ABI/MethodDesc.cs +++ b/Editor/ABI/MethodDesc.cs @@ -11,20 +11,21 @@ namespace HybridCLR.Editor.ABI { public class MethodDesc : IEquatable { + public string Sig { get; private set; } + public MethodDef MethodDef { get; set; } public ReturnInfo ReturnInfo { get; set; } public List ParamInfos { get; set; } - private int _hashCode; - public void Init() { for(int i = 0; i < ParamInfos.Count; i++) { ParamInfos[i].Index = i; } + Sig = CreateCallSigName(); } public void TransfromSigTypes(Func transformer) @@ -65,45 +66,12 @@ namespace HybridCLR.Editor.ABI public bool Equals(MethodDesc other) { - if (other == null) - { - return false; - } - - if (!ReturnInfo.Type.Equals(other.ReturnInfo.Type)) - { - return false; - } - if (ParamInfos.Count != other.ParamInfos.Count) - { - return false; - } - for(int i = 0; i < ParamInfos.Count; i++) - { - if (!ParamInfos[i].Type.Equals(other.ParamInfos[i].Type)) - { - return false; - } - } - return true; + return Sig == other.Sig; } public override int GetHashCode() { - if (_hashCode != 0) - { - return _hashCode; - } - int hash = 17; - - hash = hash * 23 + ReturnInfo.Type.GetHashCode(); - - foreach(var p in ParamInfos) - { - hash = hash * 23 + p.Type.GetHashCode(); - } - - return _hashCode = hash; + return Sig.GetHashCode(); } } } diff --git a/Editor/Commands/MethodBridgeGeneratorCommand.cs b/Editor/Commands/MethodBridgeGeneratorCommand.cs index 6823553..4c092b2 100644 --- a/Editor/Commands/MethodBridgeGeneratorCommand.cs +++ b/Editor/Commands/MethodBridgeGeneratorCommand.cs @@ -28,20 +28,6 @@ namespace HybridCLR.Editor.Commands Directory.Delete(il2cppBuildCachePath, true); } - private static string GetTemplateCode(PlatformABI platform) - { - string tplFile; - - switch (platform) - { - case PlatformABI.Universal32: tplFile = "Universal32"; break; - case PlatformABI.Universal64: tplFile = "Universal64"; break; - case PlatformABI.Arm64: tplFile = "Arm64"; break; - default: throw new NotSupportedException(); - }; - return File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/MethodBridge_{tplFile}.cpp.txt"); - } - private static void GenerateMethodBridgeCppFile(Analyzer analyzer, PlatformABI platform, string templateCode, string outputFile) { var g = new Generator(new Generator.Options() @@ -84,18 +70,11 @@ namespace HybridCLR.Editor.Commands analyzer.Run(); - var generateJobs = new List<(PlatformABI, string)>() - { - (PlatformABI.Arm64, "MethodBridge_Arm64"), - (PlatformABI.Universal64, "MethodBridge_Universal64"), - (PlatformABI.Universal32, "MethodBridge_Universal32"), - }; - var tasks = new List(); - foreach (var (platform, stubFile) in generateJobs) + string templateCode = File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/MethodBridgeStub.cpp"); + foreach (PlatformABI platform in Enum.GetValues(typeof(PlatformABI))) { - string templateCode = GetTemplateCode(platform); - string outputFile = $"{SettingsUtil.MethodBridgeCppDir}/{stubFile}.cpp"; + string outputFile = $"{SettingsUtil.GeneratedCppDir}/MethodBridge_{platform}.cpp"; tasks.Add(Task.Run(() => { GenerateMethodBridgeCppFile(analyzer, platform, templateCode, outputFile); diff --git a/Editor/Commands/ReversePInvokeWrapperGeneratorCommand.cs b/Editor/Commands/ReversePInvokeWrapperGeneratorCommand.cs index 73d2c46..706c019 100644 --- a/Editor/Commands/ReversePInvokeWrapperGeneratorCommand.cs +++ b/Editor/Commands/ReversePInvokeWrapperGeneratorCommand.cs @@ -1,6 +1,7 @@ using HybridCLR.Editor.ABI; using HybridCLR.Editor.Link; using HybridCLR.Editor.Meta; +using HybridCLR.Editor.ReversePInvokeWrap; using System; using System.Collections.Generic; using System.IO; @@ -24,28 +25,21 @@ namespace HybridCLR.Editor.Commands using (var cache = new AssemblyCache(MetaUtil.CreateBuildTargetAssemblyResolver(EditorUserBuildSettings.activeBuildTarget))) { var analyzer = new ReversePInvokeWrap.Analyzer(cache, SettingsUtil.HotUpdateAssemblyNames); - var methods = analyzer.CollectMonoPInvokeCallbackMethods(); - foreach (var method in methods) + analyzer.Run(); + + + string templateCode = File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/ReversePInvokeMethodStub.cpp"); + foreach (PlatformABI abi in Enum.GetValues(typeof(PlatformABI))) { - Debug.Log($"method:{method.Method}"); + string outputFile = $"{SettingsUtil.GeneratedCppDir}/ReversePInvokeMethodStub_{abi}.cpp"; + + List methods = analyzer.BuildABIMethods(abi); + Debug.Log($"GenerateReversePInvokeWrapper. abi:{abi} wraperCount:{methods.Sum(m => m.Count)} output:{outputFile}"); + var generator = new Generator(); + generator.Generate(templateCode, abi, methods, outputFile); } - - var generateJobs = new List<(PlatformABI, string)>() - { - (PlatformABI.Arm64, "HYBRIDCLR_ABI_ARM_64"), - (PlatformABI.Universal64, "HYBRIDCLR_ABI_UNIVERSAL_64"), - (PlatformABI.Universal32, "HYBRIDCLR_ABI_UNIVERSAL_32"), - }; } - return; - - //string ReversePInvokeWrapperStubFile = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/hybridclr/metadata/ReversePInvokeMethodStub.cpp"; - //string wrapperTemplateStr = File.ReadAllText($"{SettingsUtil.TemplatePathInPackage}/ReversePInvokeMethodStub.cpp.txt"); - //int wrapperCount = SettingsUtil.HybridCLRSettings.ReversePInvokeWrapperCount; - //var generator = new Generator(); - //generator.Generate(wrapperTemplateStr, wrapperCount,ReversePInvokeWrapperStubFile); - //Debug.Log($"GenerateReversePInvokeWrapper. wraperCount:{wrapperCount} output:{ReversePInvokeWrapperStubFile}"); - //MethodBridgeGeneratorCommand.CleanIl2CppBuildCache(); + MethodBridgeGeneratorCommand.CleanIl2CppBuildCache(); } } } diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index ad6f236..cc6e71f 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -30,6 +30,8 @@ namespace HybridCLR.Editor.MethodBridge public IReadOnlyCollection GenericMethods { get; set; } } + private PlatformABI _platformABI; + private readonly IReadOnlyList _notGenericMethods; private readonly IReadOnlyCollection _genericMethods; @@ -56,6 +58,7 @@ namespace HybridCLR.Editor.MethodBridge public Generator(Options options) { + _platformABI = options.PlatformABI; _notGenericMethods = options.NotGenericMethods; _genericMethods = options.GenericMethods; _templateCode = options.TemplateCode; @@ -98,26 +101,20 @@ namespace HybridCLR.Editor.MethodBridge private void AddManaged2NativeMethod(MethodDesc method) { - if (_managed2nativeMethodSet.Add(method)) - { - method.Init(); - } + method.Init(); + _managed2nativeMethodSet.Add(method); } private void AddNative2ManagedMethod(MethodDesc method) { - if (_native2managedMethodSet.Add(method)) - { - method.Init(); - } + method.Init(); + _native2managedMethodSet.Add(method); } private void AddAdjustThunkMethod(MethodDesc method) { - if (_adjustThunkMethodSet.Add(method)) - { - method.Init(); - } + method.Init(); + _adjustThunkMethodSet.Add(method); } private void ProcessMethod(MethodDef method, List klassInst, List methodInst) @@ -200,7 +197,7 @@ namespace HybridCLR.Editor.MethodBridge public void Generate() { - var frr = new FileRegionReplace(_templateCode); + var frr = new FileRegionReplace(_templateCode.Replace("{PLATFORM_ABI}", ABIUtil.GetHybridCLRPlatformMacro(_platformABI))); List lines = new List(20_0000); @@ -228,7 +225,7 @@ namespace HybridCLR.Editor.MethodBridge _platformAdaptor.GenerateAdjustThunkStub(_adjustThunkMethodList, lines); - frr.Replace("INVOKE_STUB", string.Join("\n", lines)); + frr.Replace("CODE", string.Join("\n", lines)); Directory.CreateDirectory(Path.GetDirectoryName(_outputFile)); diff --git a/Editor/MethodBridge/PlatformGeneratorBase.cs b/Editor/MethodBridge/PlatformGeneratorBase.cs index 7f13732..ac49452 100644 --- a/Editor/MethodBridge/PlatformGeneratorBase.cs +++ b/Editor/MethodBridge/PlatformGeneratorBase.cs @@ -36,7 +36,7 @@ Managed2NativeMethodInfo hybridclr::interpreter::g_managed2nativeStub[] = lines.Add($"\t{{\"{method.CreateInvokeSigName()}\", __M2N_{method.CreateInvokeSigName()}}},"); } - lines.Add($"\t{{nullptr, nullptr}},"); + lines.Add($"\t{{\"\", nullptr}},"); lines.Add("};"); } diff --git a/Editor/ReversePInvokeWrap/Analyzer.cs b/Editor/ReversePInvokeWrap/Analyzer.cs index 1323c79..9fb2a1b 100644 --- a/Editor/ReversePInvokeWrap/Analyzer.cs +++ b/Editor/ReversePInvokeWrap/Analyzer.cs @@ -1,4 +1,5 @@ using dnlib.DotNet; +using HybridCLR.Editor.ABI; using HybridCLR.Editor.Meta; using System; using System.Collections.Generic; @@ -9,18 +10,27 @@ using UnityEngine; namespace HybridCLR.Editor.ReversePInvokeWrap { - public class ReversePInvokeMethodInfo + public class RawReversePInvokeMethodInfo { public MethodDef Method { get; set; } public CustomAttribute GenerationAttribute { get; set; } } + public class ABIReversePInvokeMethodInfo + { + public MethodDesc Method { get; set; } + + public int Count { get; set; } + } + public class Analyzer { private readonly List _rootModules = new List(); + private readonly List _reversePInvokeMethods = new List(); + public Analyzer(AssemblyCache cache, List assemblyNames) { foreach(var assemblyName in assemblyNames) @@ -29,10 +39,9 @@ namespace HybridCLR.Editor.ReversePInvokeWrap } } - public List CollectMonoPInvokeCallbackMethods() + private void CollectReversePInvokeMethods() { - var wrapperMethods = new List(); - foreach(var mod in _rootModules) + foreach (var mod in _rootModules) { Debug.Log($"ass:{mod.FullName} methodcount:{mod.Metadata.TablesStream.MethodTable.Rows}"); for (uint rid = 1, n = mod.Metadata.TablesStream.MethodTable.Rows; rid <= n; rid++) @@ -52,14 +61,48 @@ namespace HybridCLR.Editor.ReversePInvokeWrap //{ // Debug.Log($"{ca.AttributeType.FullName} {ca.TypeFullName}"); //} - wrapperMethods.Add(new ReversePInvokeMethodInfo() + _reversePInvokeMethods.Add(new RawReversePInvokeMethodInfo() { Method = method, GenerationAttribute = method.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "HybridCLR.ReversePInvokeWrapperGenerationAttribute"), }); } } - return wrapperMethods; + } + + public List BuildABIMethods(PlatformABI abi) + { + var methodsBySig = new Dictionary(); + var typeCreator = TypeCreatorFactory.CreateTypeCreator(abi); + foreach(var method in _reversePInvokeMethods) + { + MethodDesc desc = new MethodDesc + { + MethodDef = method.Method, + ReturnInfo = new ReturnInfo { Type = typeCreator.CreateTypeInfo(method.Method.ReturnType)}, + ParamInfos = method.Method.Parameters.Select(p => new ParamInfo { Type = typeCreator.CreateTypeInfo(p.Type)}).ToList(), + }; + desc.Init(); + if (!methodsBySig.TryGetValue(desc.Sig, out var arm)) + { + arm = new ABIReversePInvokeMethodInfo() + { + Method = desc, + Count = 0, + }; + methodsBySig.Add(desc.Sig, arm); + } + int preserveCount = method.GenerationAttribute != null ? (int)method.GenerationAttribute.ConstructorArguments[0].Value : 1; + arm.Count += preserveCount; + } + var methods = methodsBySig.Values.ToList(); + methods.Sort((a, b) => a.Method.Sig.CompareTo(b.Method.Sig)); + return methods; + } + + public void Run() + { + CollectReversePInvokeMethods(); } } } diff --git a/Editor/ReversePInvokeWrap/Generator.cs b/Editor/ReversePInvokeWrap/Generator.cs index 78de478..7491b4f 100644 --- a/Editor/ReversePInvokeWrap/Generator.cs +++ b/Editor/ReversePInvokeWrap/Generator.cs @@ -1,45 +1,56 @@ -using HybridCLR.Editor.Template; +using HybridCLR.Editor.ABI; +using HybridCLR.Editor.Template; using System; using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Threading.Tasks; +using UnityEngine; namespace HybridCLR.Editor.ReversePInvokeWrap { public class Generator { - public void Generate(string template, int wrapperCount, string outputFile) + public void Generate(string template, PlatformABI abi, List methods, string outputFile) { - + template = template.Replace("{PLATFORM_ABI}", ABIUtil.GetHybridCLRPlatformMacro(abi)); var frr = new FileRegionReplace(template); var codes = new List(); - for(int i = 0; i < wrapperCount; i++) + int methodIndex = 0; + var stubCodes = new List(); + foreach(var methodInfo in methods) { - codes.Add($@" - void __ReversePInvokeMethod_{i}(void* xState) + MethodDesc method = methodInfo.Method; + string paramDeclaringListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}")); + string paramNameListWithoutMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"__arg{p.Index}")); + string paramTypeListWithMethodInfoStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()}").Concat(new string[] { "const MethodInfo*" })); + string methodTypeDef = $"typedef {method.ReturnInfo.Type.GetTypeName()} (*Callback)({paramTypeListWithMethodInfoStr})"; + for (int i = 0; i < methodInfo.Count; i++, methodIndex++) + { + codes.Add($@" + void __ReversePInvokeMethod_{methodIndex}({paramDeclaringListWithoutMethodInfoStr}) {{ - CallLuaFunction(xState, {i}); + const MethodInfo* method = MetadataModule::GetMethodInfoByReversePInvokeWrapperIndex({i}); + {methodTypeDef}; + ((Callback)(method->methodPointerCallByInterp))({paramNameListWithoutMethodInfoStr}, method); }} "); + stubCodes.Add($"\t\t{{\"{method.Sig}\", (Il2CppMethodPointer)__ReversePInvokeMethod_{methodIndex}}},\n"); + } + Debug.Log($"[ReversePInvokeWrap.Generator] method:{method.MethodDef} wrapperCount:{methodInfo.Count}"); } codes.Add(@" - Il2CppMethodPointer s_ReversePInvokeMethodStub[] + ReversePInvokeMethodData g_reversePInvokeMethodStub[] { "); - for(int i = 0; i < wrapperCount; i++) - { - codes.Add($"\t\t(Il2CppMethodPointer)__ReversePInvokeMethod_{i},\n"); - } + codes.AddRange(stubCodes); codes.Add(@" - nullptr, + {nullptr, nullptr}, }; "); - frr.Replace("REVERSE_PINVOKE_METHOD_STUB", string.Join("", codes)); + frr.Replace("CODE", string.Join("", codes)); frr.Commit(outputFile); } } diff --git a/Editor/Settings/HybridCLRSettingProvider.cs b/Editor/Settings/HybridCLRSettingProvider.cs index 00296eb..7752b9d 100644 --- a/Editor/Settings/HybridCLRSettingProvider.cs +++ b/Editor/Settings/HybridCLRSettingProvider.cs @@ -18,7 +18,6 @@ namespace HybridCLR.Editor private SerializedProperty m_OutputLinkFile; private SerializedProperty m_OutputAOTGenericReferenceFile; private SerializedProperty m_MaxGenericReferenceIteration; - private SerializedProperty m_ReversePInvokeWrapperCount; private SerializedProperty m_MaxMethodBridgeGenericIteration; private GUIStyle buttonStyle; public HybridCLRSettingsProvider() : base("Project/HybridCLR Settings", SettingsScope.Project) { } @@ -37,7 +36,6 @@ namespace HybridCLR.Editor m_OutputLinkFile = m_SerializedObject.FindProperty("outputLinkFile"); m_OutputAOTGenericReferenceFile = m_SerializedObject.FindProperty("outputAOTGenericReferenceFile"); m_MaxGenericReferenceIteration = m_SerializedObject.FindProperty("maxGenericReferenceIteration"); - m_ReversePInvokeWrapperCount = m_SerializedObject.FindProperty("ReversePInvokeWrapperCount"); m_MaxMethodBridgeGenericIteration = m_SerializedObject.FindProperty("maxMethodBridgeGenericIteration"); } public override void OnTitleBarGUI() @@ -110,7 +108,6 @@ namespace HybridCLR.Editor EditorGUILayout.PropertyField(m_OutputLinkFile); EditorGUILayout.PropertyField(m_OutputAOTGenericReferenceFile); EditorGUILayout.PropertyField(m_MaxGenericReferenceIteration); - EditorGUILayout.PropertyField(m_ReversePInvokeWrapperCount); EditorGUILayout.PropertyField(m_MaxMethodBridgeGenericIteration); if (EditorGUI.EndChangeCheck()) { diff --git a/Editor/Settings/HybridCLRSettings.cs b/Editor/Settings/HybridCLRSettings.cs index eb74427..c874bb9 100644 --- a/Editor/Settings/HybridCLRSettings.cs +++ b/Editor/Settings/HybridCLRSettings.cs @@ -32,8 +32,8 @@ namespace HybridCLR.Editor [Header("AOT泛型实例化搜索迭代次数")] public int maxGenericReferenceIteration = 10; - [Header("预留MonoPInvokeCallbackAttribute函数个数")] - public int ReversePInvokeWrapperCount = 10; + //[Header("预留MonoPInvokeCallbackAttribute函数个数")] + //public int ReversePInvokeWrapperCount = 10; [Header("MethodBridge泛型搜索迭代次数")] public int maxMethodBridgeGenericIteration = 10; diff --git a/Editor/SettingsUtil.cs b/Editor/SettingsUtil.cs index 34dc245..c1869f1 100644 --- a/Editor/SettingsUtil.cs +++ b/Editor/SettingsUtil.cs @@ -37,7 +37,7 @@ namespace HybridCLR.Editor public static string LocalIl2CppDir => $"{LocalUnityDataDir}/il2cpp"; - public static string MethodBridgeCppDir => $"{LocalIl2CppDir}/libil2cpp/hybridclr/interpreter"; + public static string GeneratedCppDir => $"{LocalIl2CppDir}/libil2cpp/hybridclr/generated"; public static string Il2CppBuildCacheDir { get; } = $"{ProjectDir}/Library/Il2cppBuildCache"; diff --git a/Editor/Template/FileRegionReplace.cs b/Editor/Template/FileRegionReplace.cs index a547b57..797477b 100644 --- a/Editor/Template/FileRegionReplace.cs +++ b/Editor/Template/FileRegionReplace.cs @@ -31,16 +31,44 @@ namespace HybridCLR.Editor.Template foreach (var c in _regionReplaceContents) { - resultContent = TemplateUtil.ReplaceRegion(resultContent, c.Key, c.Value); + resultContent = ReplaceRegion(resultContent, c.Key, c.Value); } return resultContent; } public void Commit(string outputFile) { + string dir = Path.GetDirectoryName(outputFile); + Directory.CreateDirectory(dir); string resultContent = GenFinalString(); var utf8WithoutBOM = new System.Text.UTF8Encoding(false); File.WriteAllText(outputFile, resultContent, utf8WithoutBOM); } + + public static string ReplaceRegion(string resultText, string region, string replaceContent) + { + int startIndex = resultText.IndexOf("//!!!{{" + region); + if (startIndex == -1) + { + throw new Exception($"region:{region} start not find"); + } + int endIndex = resultText.IndexOf("//!!!}}" + region); + if (endIndex == -1) + { + throw new Exception($"region:{region} end not find"); + } + int replaceStart = resultText.IndexOf('\n', startIndex); + int replaceEnd = resultText.LastIndexOf('\n', endIndex); + if (replaceStart == -1 || replaceEnd == -1) + { + throw new Exception($"region:{region} not find"); + } + if (resultText.Substring(replaceStart, replaceEnd - replaceStart) == replaceContent) + { + return resultText; + } + resultText = resultText.Substring(0, replaceStart) + "\n" + replaceContent + "\n" + resultText.Substring(replaceEnd); + return resultText; + } } } diff --git a/Editor/Template/TemplateUtil.cs b/Editor/Template/TemplateUtil.cs deleted file mode 100644 index 75e93d3..0000000 --- a/Editor/Template/TemplateUtil.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HybridCLR.Editor.Template -{ - public static class TemplateUtil - { - - public static string ReplaceRegion(string resultText, string region, string replaceContent) - { - int startIndex = resultText.IndexOf("//!!!{{" + region); - if (startIndex == -1) - { - throw new Exception($"region:{region} start not find"); - } - int endIndex = resultText.IndexOf("//!!!}}" + region); - if (endIndex == -1) - { - throw new Exception($"region:{region} end not find"); - } - int replaceStart = resultText.IndexOf('\n', startIndex); - int replaceEnd = resultText.LastIndexOf('\n', endIndex); - if (replaceStart == -1 || replaceEnd == -1) - { - throw new Exception($"region:{region} not find"); - } - if (resultText.Substring(replaceStart, replaceEnd - replaceStart) == replaceContent) - { - return resultText; - } - resultText = resultText.Substring(0, replaceStart) + "\n" + replaceContent + "\n" + resultText.Substring(replaceEnd); - return resultText; - } - } -} diff --git a/package.json b/package.json index 50850b0..2baa87b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.focus-creative-games.hybridclr_unity", - "version": "0.4.5", + "version": "0.5.0", "displayName": "HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "category": "Runtime",