Implemented convert array field

This commit is contained in:
Samuele Lorefice
2025-03-12 01:55:26 +01:00
parent 8202c2185b
commit 633b2b4376

View File

@@ -266,7 +266,35 @@ public class Reader {
return null; 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) { private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {