More documentation added in the FillObject method.

This commit is contained in:
Samuele Lorefice
2025-02-20 15:09:00 +01:00
parent e618527135
commit c0533a0d69

View File

@@ -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<DNAFieldAttribute>();
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<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
.Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.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);
}
}