From 47fbd443c17bd9d4008b028703112b40daa24ffc Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 1 Nov 2023 20:23:31 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8D=E6=A1=A5=E6=8E=A5?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=AE=A1=E7=AE=97=E6=97=B6=E6=9C=AA=E5=BD=92?= =?UTF-8?q?=E7=BB=93=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E5=87=BA=E7=8E=B0=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E5=90=8C=E5=90=8D=E7=AD=BE=E5=90=8D=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/ABI/TypeInfo.cs | 4 ++-- Editor/MethodBridge/Generator.cs | 36 ++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Editor/ABI/TypeInfo.cs b/Editor/ABI/TypeInfo.cs index 01033b9..6d0b404 100644 --- a/Editor/ABI/TypeInfo.cs +++ b/Editor/ABI/TypeInfo.cs @@ -48,7 +48,7 @@ namespace HybridCLR.Editor.ABI public bool Equals(TypeInfo other) { - return PorType == other.PorType && object.Equals(Klass, other.Klass); + return PorType == other.PorType && TypeEqualityComparer.Instance.Equals(Klass, other.Klass); } public override bool Equals(object obj) @@ -58,7 +58,7 @@ namespace HybridCLR.Editor.ABI public override int GetHashCode() { - return (int)PorType * 23 + (Klass?.GetHashCode() ?? 0); + return (int)PorType * 23 + (Klass != null ? TypeEqualityComparer.Instance.GetHashCode(Klass) : 0); } public bool NeedExpandValue() diff --git a/Editor/MethodBridge/Generator.cs b/Editor/MethodBridge/Generator.cs index f0ae4f9..a93fdc7 100644 --- a/Editor/MethodBridge/Generator.cs +++ b/Editor/MethodBridge/Generator.cs @@ -52,6 +52,23 @@ namespace HybridCLR.Editor.MethodBridge _typeCreator = new TypeCreator(); } + private readonly Dictionary _sig2Types = new Dictionary(); + + private TypeInfo GetSharedTypeInfo(TypeSig type) + { + var typeInfo = _typeCreator.CreateTypeInfo(type); + if (!typeInfo.IsStruct) + { + return typeInfo; + } + string sigName = ToFullName(typeInfo.Klass); + if (!_sig2Types.TryGetValue(sigName, out var sharedTypeInfo)) + { + sharedTypeInfo = typeInfo; + _sig2Types.Add(sigName, sharedTypeInfo); + } + return sharedTypeInfo; + } private MethodDesc CreateMethodDesc(MethodDef methodDef, bool forceRemoveThis, TypeSig returnType, List parameters) { @@ -70,12 +87,12 @@ namespace HybridCLR.Editor.MethodBridge { throw new Exception($"[PreservedMethod] method:{methodDef} has generic parameters"); } - paramInfos.Add(new ParamInfo() { Type = _typeCreator.CreateTypeInfo(paramInfo) }); + paramInfos.Add(new ParamInfo() { Type = GetSharedTypeInfo(paramInfo) }); } var mbs = new MethodDesc() { MethodDef = methodDef, - ReturnInfo = new ReturnInfo() { Type = returnType != null ? _typeCreator.CreateTypeInfo(returnType) : TypeInfo.s_void }, + ReturnInfo = new ReturnInfo() { Type = returnType != null ? GetSharedTypeInfo(returnType) : TypeInfo.s_void }, ParamInfos = paramInfos, }; return mbs; @@ -158,6 +175,18 @@ namespace HybridCLR.Editor.MethodBridge } } + static void CheckUnique(IEnumerable names) + { + var set = new HashSet(); + foreach (var name in names) + { + if (!set.Add(name)) + { + throw new Exception($"[CheckUnique] duplicate name:{name}"); + } + } + } + public void Generate() { var frr = new FileRegionReplace(_templateCode); @@ -191,6 +220,9 @@ namespace HybridCLR.Editor.MethodBridge GenerateClassInfo(type, classTypeSet, classInfos); } + CheckUnique(structTypes.Select(t => ToFullName(t.Klass))); + CheckUnique(structTypes.Select(t => t.CreateSigName())); + GenerateStructDefines(classInfos, lines); GenerateStructureSignatureStub(structTypes, lines);