From 633b2b4376dc058277b21d8e24e577381bfea440 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Wed, 12 Mar 2025 01:55:26 +0100 Subject: [PATCH] Implemented convert array field --- BlendFile/Reader.cs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/BlendFile/Reader.cs b/BlendFile/Reader.cs index 8b1ad3c..1b75546 100644 --- a/BlendFile/Reader.cs +++ b/BlendFile/Reader.cs @@ -265,10 +265,38 @@ public class Reader { } return null; } - - throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\""); - } + private object? ConvertArrayField(FileBlock block, FieldInfo field, DNAArrayAttribute arrayAttribute, IntPtr startOffset) { + //Calculate the offset from where the data of the field starts. + //Because the order of the fields is not guaranteed we need to compute it each time + IntPtr offset = arrayAttribute.MemoryOffset + startOffset; + + //Grab data size, create a container and copy the data from the block to the container + int size = arrayAttribute.Size; + var data = new byte[size]; + Array.Copy((byte[])block.Body, offset, data, 0, size); + + int itemLenght = arrayAttribute.OriginalType.ParseFSize(); + //Gather Array type + var type = Type.GetType(arrayAttribute.OriginalType.ParseFType()); + if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown"); + //Create the array + var array = Array.CreateInstance(type, arrayAttribute.Size); + + for (int i = 0; i < arrayAttribute.Size; i += itemLenght) { + var itemData = new byte[itemLenght]; + Array.Copy(data, i, itemData, 0, itemLenght); + object? cellData = ConvertFieldData(itemData, arrayAttribute.OriginalType); + if (cellData == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\""); + var cellType = Type.GetType(arrayAttribute.OriginalType.ParseFType()); + //check that cellData is the correct type + if (cellData.GetType() != cellType) throw new NotSupportedException($"Array type mismatch. Expected {cellType} got {cellData.GetType()}"); + array.SetValue(cellData, i / itemLenght); + } + + return array; + } + private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) { IntPtr countOffset = attrib.CountMemoryOffset + startOffset; IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;