diff --git a/BlendFile/Reader.cs b/BlendFile/Reader.cs index f4f50a6..792fabd 100644 --- a/BlendFile/Reader.cs +++ b/BlendFile/Reader.cs @@ -56,19 +56,21 @@ public class Reader { private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) { foreach (var field in fieldMetadata) { + //Get the DNAFieldAttribute of the current field var attrib = field.GetCustomAttribute(); - if (attrib == null) continue; - + if (attrib == null) continue; //should never happen, but means a field has no metadata + //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 int offset = fieldMetadata.Where(f => f.GetCustomAttribute()!.OriginalIndex < attrib.OriginalIndex) .Sum(f => f.GetCustomAttribute()!.Size) + startOffset; + int size = attrib.Size; - var data = new byte[size]; Array.Copy((byte[])block.Body, offset, data, 0, size); - + //Convert the data to the correct type object? value = ConvertFieldData(data, attrib.OriginalType); - if(value == null) continue; - + if(value == null) continue; //should never happen, but means the data could not be converted + //Additionally... some fields might not be nullable so it's better to not assign the value and leave the default one. field.SetValue(obj, value); } }