Added documentation to Reader class in BlendFile. Extracted method for Filling an object with data from a block, given an offset

This commit is contained in:
Samuele Lorefice
2025-02-20 15:04:12 +01:00
parent b66247488e
commit e618527135

View File

@@ -28,57 +28,51 @@ public class Reader {
foreach (var block in blend.Blocks) foreach (var block in blend.Blocks)
{ {
//We need to read all blocks of data regardeless of the type
//if (block.Code != "DATA") continue; //if (block.Code != "DATA") continue;
//Checks if the block has a known SDNA type, meaning that we know how it is structured
if(!dnaTypes.ContainsKey((int)block.SdnaIndex)) continue; if(!dnaTypes.ContainsKey((int)block.SdnaIndex)) continue;
Type t = dnaTypes.Values.First(x => Type t = dnaTypes.Values.First(
x.GetCustomAttribute<DNAClassAttribute>()!.OriginalName == block.SdnaStruct.Type); x => x.GetCustomAttribute<DNAClassAttribute>()!.OriginalName == block.SdnaStruct.Type);
// How many objects are in the block
var count = block.Count; var count = block.Count;
//offset for the next object in the block
var blockOffset = 0; var blockOffset = 0;
//for each expected object in the block
for(var i=0; i<count; i++) { for(var i=0; i<count; i++) {
//create an instance of type "t" (the dna type we inferred before)
var obj = Activator.CreateInstance(t); var obj = Activator.CreateInstance(t);
if(obj == null) continue; if(obj == null) continue; //should never happen
objects.Add(obj); objects.Add(obj); //add the object to the list of objects
var fields = t.GetFields();
foreach (var field in fields) {
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
if (attrib == null) continue;
var offset = fields.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
.Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.Size) + blockOffset;
var size = attrib.Size;
byte[] data = new byte[size];
Array.Copy((byte[])block.Body, offset, data, 0, size);
var value = ConvertFieldData(data, attrib.OriginalType);
if(value == null) continue;
field.SetValue(obj, value);
}
//fill the object with the data from the block
FillObject(block, ref obj, t.GetFields(), blockOffset);
//move the offset to the next object in the block
blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size; blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size;
} }
} }
} }
/* private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
* "char" => typeof(char).AssemblyQualifiedName, foreach (var field in fieldMetadata) {
"short" => typeof(short).AssemblyQualifiedName, var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
"int" => typeof(int).AssemblyQualifiedName, if (attrib == null) continue;
"float" => typeof(float).AssemblyQualifiedName,
"double" => typeof(double).AssemblyQualifiedName, int offset = fieldMetadata.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
"string" => typeof(string).AssemblyQualifiedName, .Sum(f => f.GetCustomAttribute<DNAFieldAttribute>()!.Size) + startOffset;
"void" => typeof(object).AssemblyQualifiedName, int size = attrib.Size;
"ushort" => typeof(ushort).AssemblyQualifiedName,
"uchar" => typeof(byte).AssemblyQualifiedName, var data = new byte[size];
"int64_t" => typeof(long).AssemblyQualifiedName, Array.Copy((byte[])block.Body, offset, data, 0, size);
"int8_t" => typeof(sbyte).AssemblyQualifiedName,
"uint64_t" => typeof(ulong).AssemblyQualifiedName, object? value = ConvertFieldData(data, attrib.OriginalType);
*/ if(value == null) continue;
field.SetValue(obj, value);
}
}
private object? ConvertFieldData(byte[] data, string type) private object? ConvertFieldData(byte[] data, string type)
{ {
return type switch return type switch