From 3d68dc4db32fcb594749cca6967c5cf3d0a52ab8 Mon Sep 17 00:00:00 2001 From: walon Date: Tue, 27 Jun 2023 08:57:02 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E8=A7=A3=E5=86=B3MethodBridge=E7=94=9F?= =?UTF-8?q?=E6=88=90=E4=B8=8D=E7=A8=B3=E5=AE=9A=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/MethodBridge/Generator.cs | 71 ++++++++----------- Editor/MethodBridge/PlatformGeneratorArm64.cs | 4 -- .../PlatformGeneratorUniversal32.cs | 3 - .../PlatformGeneratorUniversal64.cs | 5 -- .../MethodBridge/PlatformGeneratorWebGL32.cs | 3 - 5 files changed, 28 insertions(+), 58 deletions(-) diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index 7eee4b7..5a46263 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -35,9 +35,9 @@ namespace HybridCLR.Editor.MethodBridge private PlatformABI _platformABI; - private readonly IReadOnlyList _notGenericMethods; + private readonly List _notGenericMethods; - private readonly IReadOnlyCollection _genericMethods; + private readonly List _genericMethods; private readonly HashSet _preservedMethods; @@ -51,21 +51,22 @@ namespace HybridCLR.Editor.MethodBridge private readonly HashSet _managed2nativeMethodSet = new HashSet(); - private List _managed2nativeMethodList; - private readonly HashSet _native2managedMethodSet = new HashSet(); - private List _native2managedMethodList; - private readonly HashSet _adjustThunkMethodSet = new HashSet(); - private List _adjustThunkMethodList; - public Generator(Options options) { _platformABI = options.PlatformABI; - _notGenericMethods = options.NotGenericMethods; - _genericMethods = options.GenericMethods; + + List<(MethodDef, string)> notGenericMethodInfo = options.NotGenericMethods.Select(m => (m, m.FullName)).ToList(); + notGenericMethodInfo.Sort((a, b) => string.Compare(a.Item2, b.Item2, StringComparison.Ordinal)); + _notGenericMethods = notGenericMethodInfo.Select(m => m.Item1).ToList(); + + List<(GenericMethod, string)> genericMethodInfo = options.GenericMethods.Select(m => (m, m.ToString())).ToList(); + genericMethodInfo.Sort((a, b) => string.CompareOrdinal(a.Item2, b.Item2)); + _genericMethods = genericMethodInfo.Select(m => m.Item1).ToList(); + _preservedMethods = options.SpeicalPreserveMethods; _templateCode = options.TemplateCode; _outputFile = options.OutputFile; @@ -182,31 +183,6 @@ namespace HybridCLR.Editor.MethodBridge { ProcessMethod(method.Method, method.KlassInst, method.MethodInst); } - - { - var sortedMethods = new SortedDictionary(); - foreach (var method in _managed2nativeMethodSet) - { - sortedMethods.Add(method.CreateCallSigName(), method); - } - _managed2nativeMethodList = sortedMethods.Values.ToList(); - } - { - var sortedMethods = new SortedDictionary(); - foreach (var method in _native2managedMethodSet) - { - sortedMethods.Add(method.CreateCallSigName(), method); - } - _native2managedMethodList = sortedMethods.Values.ToList(); - } - { - var sortedMethods = new SortedDictionary(); - foreach (var method in _adjustThunkMethodSet) - { - sortedMethods.Add(method.CreateCallSigName(), method); - } - _adjustThunkMethodList = sortedMethods.Values.ToList(); - } } public void Generate() @@ -215,29 +191,38 @@ namespace HybridCLR.Editor.MethodBridge List lines = new List(20_0000); - Debug.LogFormat("== managed2native:{0} native2managed:{1} adjustThunk:{2}", - _managed2nativeMethodList.Count, _native2managedMethodList.Count, _adjustThunkMethodList.Count); + List managed2NativeMethodList = _managed2nativeMethodSet.ToList(); + managed2NativeMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig)); - foreach(var method in _managed2nativeMethodList) + List native2ManagedMethodList = _native2managedMethodSet.ToList(); + native2ManagedMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig)); + + List adjustThunkMethodList = _adjustThunkMethodSet.ToList(); + adjustThunkMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig)); + + Debug.LogFormat("== managed2native:{0} native2managed:{1} adjustThunk:{2}", + managed2NativeMethodList.Count, native2ManagedMethodList.Count, adjustThunkMethodList.Count); + + foreach(var method in managed2NativeMethodList) { _platformAdaptor.GenerateManaged2NativeMethod(method, lines); } - _platformAdaptor.GenerateManaged2NativeStub(_managed2nativeMethodList, lines); + _platformAdaptor.GenerateManaged2NativeStub(managed2NativeMethodList, lines); - foreach (var method in _native2managedMethodList) + foreach (var method in native2ManagedMethodList) { _platformAdaptor.GenerateNative2ManagedMethod(method, lines); } - _platformAdaptor.GenerateNative2ManagedStub(_native2managedMethodList, lines); + _platformAdaptor.GenerateNative2ManagedStub(native2ManagedMethodList, lines); - foreach (var method in _adjustThunkMethodList) + foreach (var method in adjustThunkMethodList) { _platformAdaptor.GenerateAdjustThunkMethod(method, lines); } - _platformAdaptor.GenerateAdjustThunkStub(_adjustThunkMethodList, lines); + _platformAdaptor.GenerateAdjustThunkStub(adjustThunkMethodList, lines); frr.Replace("CODE", string.Join("\n", lines)); diff --git a/Editor/MethodBridge/PlatformGeneratorArm64.cs b/Editor/MethodBridge/PlatformGeneratorArm64.cs index 1f15e3b..4fa5458 100644 --- a/Editor/MethodBridge/PlatformGeneratorArm64.cs +++ b/Editor/MethodBridge/PlatformGeneratorArm64.cs @@ -19,12 +19,10 @@ namespace HybridCLR.Editor.MethodBridge public override void GenerateManaged2NativeMethod(MethodDesc method, List lines) { - int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI); string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); string paramNameListStr = string.Join(", ", method.ParamInfos.Select(p => p.Managed2NativeParamValue(this.PlatformABI)).Concat(new string[] { "method" })); lines.Add($@" -// {method.MethodDef} static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_t* argVarIndexs, StackObject* localVarBase, void* ret) {{ typedef {method.ReturnInfo.Type.GetTypeName()} (*NativeMethod)({paramListStr}); @@ -39,7 +37,6 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => p.Native2ManagedParamValue(this.PlatformABI)))} }}; @@ -57,7 +54,6 @@ static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()} string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => (p.Index == 0 ? $"(uint64_t)(*(uint8_t**)&__arg{p.Index} + sizeof(Il2CppObject))" : p.Native2ManagedParamValue(this.PlatformABI))))} }}; diff --git a/Editor/MethodBridge/PlatformGeneratorUniversal32.cs b/Editor/MethodBridge/PlatformGeneratorUniversal32.cs index 7feab81..bbcc72a 100644 --- a/Editor/MethodBridge/PlatformGeneratorUniversal32.cs +++ b/Editor/MethodBridge/PlatformGeneratorUniversal32.cs @@ -20,7 +20,6 @@ namespace HybridCLR.Editor.MethodBridge string paramNameListStr = string.Join(", ", method.ParamInfos.Select(p => p.Managed2NativeParamValue(this.PlatformABI)).Concat(new string[] { "method" })); lines.Add($@" -// {method.MethodDef} static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_t* argVarIndexs, StackObject* localVarBase, void* ret) {{ typedef {method.ReturnInfo.Type.GetTypeName()} (*NativeMethod)({paramListStr}); @@ -34,7 +33,6 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => p.Native2ManagedParamValue(this.PlatformABI)))} }}; @@ -51,7 +49,6 @@ static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()} string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => (p.Index == 0 ? $"(uint64_t)(*(uint8_t**)&__arg{p.Index} + sizeof(Il2CppObject))" : p.Native2ManagedParamValue(this.PlatformABI))))} }}; diff --git a/Editor/MethodBridge/PlatformGeneratorUniversal64.cs b/Editor/MethodBridge/PlatformGeneratorUniversal64.cs index 8ed8474..f2dd87d 100644 --- a/Editor/MethodBridge/PlatformGeneratorUniversal64.cs +++ b/Editor/MethodBridge/PlatformGeneratorUniversal64.cs @@ -17,14 +17,11 @@ namespace HybridCLR.Editor.MethodBridge public override void GenerateManaged2NativeMethod(MethodDesc method, List lines) { - int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI); - string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); string paramTypeListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()}").Concat(new string[] { "const MethodInfo*" })); ; string paramNameListStr = string.Join(", ", method.ParamInfos.Select(p => p.Managed2NativeParamValue(this.PlatformABI)).Concat(new string[] { "method" })); lines.Add($@" -// {method.MethodDef} static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_t* argVarIndexs, StackObject* localVarBase, void* ret) {{ typedef {method.ReturnInfo.Type.GetTypeName()} (*NativeMethod)({paramListStr}); @@ -37,7 +34,6 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI); string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => p.Native2ManagedParamValue(this.PlatformABI)))} }}; @@ -53,7 +49,6 @@ static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()} int totalQuadWordNum = method.ParamInfos.Count + method.ReturnInfo.GetParamSlotNum(this.PlatformABI); string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => (p.Index == 0 ? $"(uint64_t)(*(uint8_t**)&__arg{p.Index} + sizeof(Il2CppObject))" : p.Native2ManagedParamValue(this.PlatformABI))))} }}; diff --git a/Editor/MethodBridge/PlatformGeneratorWebGL32.cs b/Editor/MethodBridge/PlatformGeneratorWebGL32.cs index 1269201..ccba7a5 100644 --- a/Editor/MethodBridge/PlatformGeneratorWebGL32.cs +++ b/Editor/MethodBridge/PlatformGeneratorWebGL32.cs @@ -21,7 +21,6 @@ namespace HybridCLR.Editor.MethodBridge string paramNameListStr = string.Join(", ", method.ParamInfos.Select(p => p.Managed2NativeParamValue(this.PlatformABI)).Concat(new string[] { "method" })); lines.Add($@" -// {method.MethodDef} static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_t* argVarIndexs, StackObject* localVarBase, void* ret) {{ typedef {method.ReturnInfo.Type.GetTypeName()} (*NativeMethod)({paramListStr}); @@ -35,7 +34,6 @@ static void __M2N_{method.CreateCallSigName()}(const MethodInfo* method, uint16_ string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => p.Native2ManagedParamValue(this.PlatformABI)))} }}; @@ -52,7 +50,6 @@ static {method.ReturnInfo.Type.GetTypeName()} __N2M_{method.CreateCallSigName()} string paramListStr = string.Join(", ", method.ParamInfos.Select(p => $"{p.Type.GetTypeName()} __arg{p.Index}").Concat(new string[] { "const MethodInfo* method" })); lines.Add($@" -// {method.MethodDef} static {method.ReturnInfo.Type.GetTypeName()} __N2M_AdjustorThunk_{method.CreateCallSigName()}({paramListStr}) {{ StackObject args[{Math.Max(totalQuadWordNum, 1)}] = {{{string.Join(", ", method.ParamInfos.Select(p => (p.Index == 0 ? $"(uint64_t)(*(uint8_t**)&__arg{p.Index} + sizeof(Il2CppObject))" : p.Native2ManagedParamValue(this.PlatformABI))))} }};