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);