[fix] 修复StructLayout中只指定LayoutKind参数时,struct的ClassLayout为空,计算type size和aligment错误的bug

main
walon 2022-12-14 10:21:31 +08:00
parent b40d62e16e
commit 947b15e7d9
3 changed files with 9 additions and 4 deletions

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.ABI namespace HybridCLR.Editor.ABI
{ {
@ -74,6 +75,10 @@ namespace HybridCLR.Editor.ABI
case ElementType.ValueType: case ElementType.ValueType:
{ {
TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDef(); TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDef();
if (typeDef == null)
{
throw new Exception($"type:{type} 未能找到定义。请尝试 `HybridCLR/Genergate/LinkXml`然后Build一次生成AOT dll再重新生成桥接函数");
}
if (typeDef.IsEnum) if (typeDef.IsEnum)
{ {
return CreateTypeInfo(typeDef.GetEnumUnderlyingType()); return CreateTypeInfo(typeDef.GetEnumUnderlyingType());

View File

@ -74,11 +74,11 @@ namespace HybridCLR.Editor.ABI
{ {
continue; continue;
} }
if (sa != null && typeDef.Layout.HasFlag(dnlib.DotNet.TypeAttributes.ExplicitLayout)) if (typeDef.Layout.HasFlag(dnlib.DotNet.TypeAttributes.ExplicitLayout))
{ {
int offset = (int)field.FieldOffset.Value; int offset = (int)field.FieldOffset.Value;
totalSize = Math.Max(totalSize, offset + fs); totalSize = Math.Max(totalSize, offset + fs);
if (offset > sa.ClassSize) if (sa != null && offset > sa.ClassSize)
{ {
useSLSize = false; useSLSize = false;
} }
@ -90,7 +90,7 @@ namespace HybridCLR.Editor.ABI
totalSize = (totalSize + fa - 1) / fa * fa; totalSize = (totalSize + fa - 1) / fa * fa;
} }
totalSize += fs; totalSize += fs;
if (sa != null && typeDef.Layout.HasFlag(dnlib.DotNet.TypeAttributes.SequentialLayout) && totalSize > sa.ClassSize) if (sa != null && totalSize > sa.ClassSize)
{ {
useSLSize = false; useSLSize = false;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "com.focus-creative-games.hybridclr_unity", "name": "com.focus-creative-games.hybridclr_unity",
"version": "1.1.12", "version": "1.1.13",
"displayName": "HybridCLR", "displayName": "HybridCLR",
"description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR", "description": "Unity package for HybridCLR. It includes editor and runtime scripts and assets for HybridCLR",
"category": "Runtime", "category": "Runtime",