More documentation added in the FillObject method.
This commit is contained in:
@@ -56,19 +56,21 @@ public class Reader {
|
|||||||
|
|
||||||
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
|
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
|
||||||
foreach (var field in fieldMetadata) {
|
foreach (var field in fieldMetadata) {
|
||||||
|
//Get the DNAFieldAttribute of the current field
|
||||||
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
|
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)
|
int offset = fieldMetadata.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
|
||||||
.Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.Size) + startOffset;
|
.Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.Size) + startOffset;
|
||||||
|
|
||||||
int size = attrib.Size;
|
int size = attrib.Size;
|
||||||
|
|
||||||
var data = new byte[size];
|
var data = new byte[size];
|
||||||
Array.Copy((byte[])block.Body, offset, data, 0, size);
|
Array.Copy((byte[])block.Body, offset, data, 0, size);
|
||||||
|
//Convert the data to the correct type
|
||||||
object? value = ConvertFieldData(data, attrib.OriginalType);
|
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);
|
field.SetValue(obj, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user