From 48c91f497bc17c5c17bae91801ce6071c859477e Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 18 Jul 2024 13:38:26 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8DMethodBridge/Generato?= =?UTF-8?q?r=20GetOrCalculateTypeInfoSignature=E8=AE=A1=E7=AE=97=E7=AD=89?= =?UTF-8?q?=E4=BB=B7=E7=B1=BB=E6=97=B6=E6=9C=AA=E8=80=83=E8=99=91=E5=88=B0?= =?UTF-8?q?ClassLayout=E3=80=81Layout=E5=92=8CFieldOffset=E5=9B=A0?= =?UTF-8?q?=E7=B4=A0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/MethodBridge/Generator.cs | 34 +++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index cd1b9a0..685be91 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -16,6 +16,7 @@ using UnityEngine; using TypeInfo = HybridCLR.Editor.ABI.TypeInfo; using CallingConvention = System.Runtime.InteropServices.CallingConvention; using System.Security.Cryptography; +using TypeAttributes = dnlib.DotNet.TypeAttributes; namespace HybridCLR.Editor.MethodBridge { @@ -246,11 +247,20 @@ namespace HybridCLR.Editor.MethodBridge _managed2NativeMethodList0.Count, _native2ManagedMethodList0.Count, _adjustThunkMethodList0.Count, _structTypes0.Count); } + private class AnalyzeFieldInfo + { + public FieldDef field; + + public TypeInfo type; + } + private class AnalyzeTypeInfo { public TypeInfo toSharedType; - public List fields; + public List fields; public string signature; + public ClassLayout classLayout; + public TypeAttributes layout; } private readonly Dictionary _analyzeTypeInfos = new Dictionary(); @@ -266,7 +276,11 @@ namespace HybridCLR.Editor.MethodBridge GenericArgumentContext ctx = klassInst != null ? new GenericArgumentContext(klassInst, null) : null; ClassLayout sa = typeDef.ClassLayout; - var analyzeTypeInfo = new AnalyzeTypeInfo(); + var analyzeTypeInfo = new AnalyzeTypeInfo() + { + classLayout = sa, + layout = typeDef.Layout, + }; // don't share type with explicit layout if (sa != null) @@ -277,7 +291,7 @@ namespace HybridCLR.Editor.MethodBridge return analyzeTypeInfo; } - var fields = analyzeTypeInfo.fields = new List(); + var fields = analyzeTypeInfo.fields = new List(); foreach (FieldDef field in typeDef.Fields) { @@ -286,7 +300,7 @@ namespace HybridCLR.Editor.MethodBridge continue; } TypeSig fieldType = ctx != null ? MetaUtil.Inflate(field.FieldType, ctx) : field.FieldType; - fields.Add(GetSharedTypeInfo(fieldType)); + fields.Add(new AnalyzeFieldInfo { field = field, type = GetSharedTypeInfo(fieldType) }); } return analyzeTypeInfo; } @@ -312,9 +326,19 @@ namespace HybridCLR.Editor.MethodBridge } var sigBuf = new StringBuilder(); + if (ati.classLayout != null) + { + sigBuf.Append($"[{ati.classLayout.ClassSize}|{ati.classLayout.PackingSize}|{ati.classLayout}]"); + } + if (ati.layout != 0) + { + sigBuf.Append($"[{(int)ati.layout}]"); + } + foreach (var field in ati.fields) { - sigBuf.Append("{" + GetOrCalculateTypeInfoSignature(ToIsomorphicType(field)) + "}"); + string fieldOffset = field.field.FieldOffset != null ? field.field.FieldOffset.ToString() + "|" : ""; + sigBuf.Append("{" + fieldOffset + GetOrCalculateTypeInfoSignature(ToIsomorphicType(field.type)) + "}"); } return ati.signature = sigBuf.ToString(); }