From d67a11b38489ba56b6738cb623f58b5001215ac8 Mon Sep 17 00:00:00 2001 From: walon Date: Thu, 9 Feb 2023 20:11:15 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E4=BF=AE=E5=A4=8DWebGL=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E6=9C=AA=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86=E7=A9=BA?= =?UTF-8?q?struct=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=A1=A5=E6=8E=A5=E5=87=BD?= =?UTF-8?q?=E6=95=B0=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/ParamOrReturnType.cs | 4 ++++ Editor/ABI/TypeCreatorBase.cs | 32 +++++++++++++++++++++++++++----- Editor/ABI/TypeCreatorWebGL32.cs | 2 +- Editor/ABI/TypeInfo.cs | 15 ++++++++++++++- package.json | 2 +- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/Editor/ABI/ParamOrReturnType.cs b/Editor/ABI/ParamOrReturnType.cs index 7e933ec..9ee3737 100644 --- a/Editor/ABI/ParamOrReturnType.cs +++ b/Editor/ABI/ParamOrReturnType.cs @@ -35,5 +35,9 @@ namespace HybridCLR.Editor.ABI STRUCTURE_ALIGN2, STRUCTURE_ALIGN4, STRUCTURE_ALIGN8, + SPECIAL_STRUCTURE_ALIGN1, + SPECIAL_STRUCTURE_ALIGN2, + SPECIAL_STRUCTURE_ALIGN4, + SPECIAL_STRUCTURE_ALIGN8, } } diff --git a/Editor/ABI/TypeCreatorBase.cs b/Editor/ABI/TypeCreatorBase.cs index 9c98490..a5485ee 100644 --- a/Editor/ABI/TypeCreatorBase.cs +++ b/Editor/ABI/TypeCreatorBase.cs @@ -15,7 +15,7 @@ namespace HybridCLR.Editor.ABI public virtual bool IsSupportHFA => false; - public virtual bool IsSupportSingletonStruct => false; + public virtual bool IsSupportWebGLSpecialValueType => false; public TypeInfo GetNativeIntTypeInfo() => IsArch32 ? TypeInfo.s_i4 : TypeInfo.s_i8; @@ -249,6 +249,21 @@ namespace HybridCLR.Editor.ABI return true; } + public static bool IsWebGLSpeicalValueType(TypeSig type) + { + TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDefThrow(); + if (typeDef.IsEnum) + { + return false; + } + var fields = typeDef.Fields;// typeDef.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + if (fields.Count == 0) + { + return true; + } + return fields.All(f => f.IsStatic); + } + protected static TypeInfo CreateGeneralValueType(TypeSig type, int size, int aligment) { System.Diagnostics.Debug.Assert(size % aligment == 0); @@ -276,10 +291,17 @@ namespace HybridCLR.Editor.ABI default: throw new NotSupportedException(); } } - //else if(IsSupportSingletonStruct && TryComputSingletonStruct(type, out var ssTypeInfo)) - //{ - // return CreateTypeInfo(ssTypeInfo.Type); - //} + if (IsSupportWebGLSpecialValueType && IsWebGLSpeicalValueType(type)) + { + switch (typeAligment) + { + case 1: return new TypeInfo(ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN1, typeSize); + case 2: return new TypeInfo(ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN2, typeSize); + case 4: return new TypeInfo(ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN4, typeSize); + case 8: return new TypeInfo(ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN8, typeSize); + default: throw new NotSupportedException(); + } + } else { // 64位下结构体内存对齐规则是一样的 diff --git a/Editor/ABI/TypeCreatorWebGL32.cs b/Editor/ABI/TypeCreatorWebGL32.cs index 0309acc..b4e18ec 100644 --- a/Editor/ABI/TypeCreatorWebGL32.cs +++ b/Editor/ABI/TypeCreatorWebGL32.cs @@ -19,7 +19,7 @@ namespace HybridCLR.Editor.ABI public override bool IsSupportHFA => false; - public override bool IsSupportSingletonStruct => true; + public override bool IsSupportWebGLSpecialValueType => true; protected override TypeInfo OptimizeSigType(TypeInfo type, bool returnType) diff --git a/Editor/ABI/TypeInfo.cs b/Editor/ABI/TypeInfo.cs index ebc6d38..647c9fc 100644 --- a/Editor/ABI/TypeInfo.cs +++ b/Editor/ABI/TypeInfo.cs @@ -90,6 +90,10 @@ namespace HybridCLR.Editor.ABI case ParamOrReturnType.STRUCTURE_ALIGN2: return "A" + Size; case ParamOrReturnType.STRUCTURE_ALIGN4: return "B" + Size; case ParamOrReturnType.STRUCTURE_ALIGN8: return "C" + Size; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN1: return "X" + Size; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN2: return "Y" + Size; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN4: return "Z" + Size; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN8: return "W" + Size; default: throw new NotSupportedException(PorType.ToString()); }; } @@ -121,6 +125,10 @@ namespace HybridCLR.Editor.ABI case ParamOrReturnType.STRUCTURE_ALIGN2: return $"ValueTypeSizeAlign2<{Size}>"; case ParamOrReturnType.STRUCTURE_ALIGN4: return $"ValueTypeSizeAlign4<{Size}>"; case ParamOrReturnType.STRUCTURE_ALIGN8: return $"ValueTypeSizeAlign8<{Size}>"; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN1: return $"WebGLSpeicalValueType<{Size}>"; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN2: return $"WebGLSpeicalValueTypeAlign2<{Size}>"; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN4: return $"WebGLSpeicalValueTypeAlign4<{Size}>"; + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN8: return $"WebGLSpeicalValueTypeAlign8<{Size}>"; default: throw new NotImplementedException(PorType.ToString()); }; } @@ -141,7 +149,12 @@ namespace HybridCLR.Editor.ABI case ParamOrReturnType.STRUCTURE_ALIGN1: case ParamOrReturnType.STRUCTURE_ALIGN2: case ParamOrReturnType.STRUCTURE_ALIGN4: - case ParamOrReturnType.STRUCTURE_ALIGN8: return (Size + 7) / 8; + case ParamOrReturnType.STRUCTURE_ALIGN8: + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN1: + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN2: + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN4: + case ParamOrReturnType.SPECIAL_STRUCTURE_ALIGN8: + return (Size + 7) / 8; default: { Debug.Assert(PorType < ParamOrReturnType.STRUCT_NOT_PASS_AS_VALUE); diff --git a/package.json b/package.json index c7f24c8..e0b71d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.focus-creative-games.hybridclr_unity", - "version": "2.0.3", + "version": "2.0.4", "displayName": "HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "category": "Runtime",