Added more documentation and started pointer handling
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Kaitai;
|
||||
@@ -7,20 +6,23 @@ using Kaitai;
|
||||
namespace BlendFile;
|
||||
|
||||
public class Reader {
|
||||
readonly string _path;
|
||||
string _path;
|
||||
private readonly Dictionary<int, Type> dnaTypes = new();
|
||||
|
||||
private List<object> objects = new();
|
||||
public List<object> Objects => objects;
|
||||
private Dictionary<(long, Type), object> objects = new();
|
||||
public Dictionary<(long, Type), object> Objects => objects;
|
||||
|
||||
private Dictionary<long, Kaitai.BlendFile.FileBlock> memBlocks = new();
|
||||
public List<object> GetObjects() => objects.Values.ToList();
|
||||
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
|
||||
|
||||
private SortedDictionary<long, Kaitai.BlendFile.FileBlock> memBlocks = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the block at the specified memory address
|
||||
/// </summary>
|
||||
/// <param name="memAddr">memory address in current system endianness</param>
|
||||
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
|
||||
public Kaitai.BlendFile.FileBlock? GetBlock(long memAddr) => memBlocks.GetValueOrDefault(memAddr);
|
||||
public Kaitai.BlendFile.FileBlock? GetBlock(long memAddr) => memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="Reader"/> class
|
||||
@@ -43,12 +45,18 @@ public class Reader {
|
||||
}
|
||||
}
|
||||
|
||||
public void Read(string path) {
|
||||
_path = path;
|
||||
Read();
|
||||
}
|
||||
|
||||
public void Read() {
|
||||
var file = new KaitaiStream(_path);
|
||||
var blend = new Kaitai.BlendFile(file);
|
||||
|
||||
bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le;
|
||||
blend.Blocks.ForEach(block => memBlocks.Add(block.MemAddr.ToMemAddr(isLe), block));
|
||||
//TODO: two blocks somehow have the same mem address... this sounds wrong.
|
||||
blend.Blocks.ForEach(block => memBlocks.TryAdd(block.MemAddr.ToMemAddr(isLe), block));
|
||||
|
||||
foreach (var block in blend.Blocks)
|
||||
{
|
||||
@@ -68,21 +76,31 @@ public class Reader {
|
||||
//create an instance of type "t" (the dna type we inferred before)
|
||||
var obj = Activator.CreateInstance(t);
|
||||
if(obj == null) continue; //should never happen
|
||||
objects.Add(obj); //add the object to the list of objects
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="block"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <param name="fieldMetadata"></param>
|
||||
/// <param name="startOffset"></param>
|
||||
/// <returns></returns>
|
||||
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object? obj, FieldInfo[] fieldMetadata, int startOffset = 0) {
|
||||
if(block.Code == "ENDB") return;
|
||||
foreach (var field in fieldMetadata) {
|
||||
//Get the DNAFieldAttribute of the current field
|
||||
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
|
||||
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)
|
||||
@@ -93,10 +111,50 @@ public class Reader {
|
||||
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; //should never happen, but means the data could not be converted
|
||||
|
||||
if(value == null){ //if the data could not be converted
|
||||
//Check if the field is a pointer to another DNA structure
|
||||
if (dnaTypes.Values.FirstOrDefault(x => x.GetCustomAttribute<DNAClassAttribute>()!.OriginalName == attrib.OriginalType) != null) {
|
||||
//Create a new instance of the DNA structure type
|
||||
object? newObj = Activator.CreateInstance(dnaTypes.Values.First(x =>
|
||||
x.GetCustomAttribute<DNAClassAttribute>()!.OriginalName == attrib.OriginalType));
|
||||
if(newObj == null) continue; //should never happen... type is missing?
|
||||
|
||||
//Get the information of the fields of the new object
|
||||
var fieldInfo = newObj.GetType().GetFields();
|
||||
|
||||
//If the field is a pointer, we need to dereference it
|
||||
if (attrib.IsPointer) {
|
||||
//Get the memory address of the data
|
||||
long addr = data.ToMemAddr();
|
||||
//Get the closest block that contains the data
|
||||
var derefBlock = GetBlock(addr);
|
||||
//Calculate the relative address of the data in the block
|
||||
long relAddr = addr - derefBlock!.MemAddr.ToMemAddr();
|
||||
|
||||
if(objects.TryGetValue((addr, newObj.GetType()), out object? o)){ //If the object is already created, we can just assign it
|
||||
field.SetValue(obj, o);
|
||||
continue;
|
||||
}
|
||||
|
||||
//Fill the object with the data from the block (this is recursive)
|
||||
FillObject(derefBlock, ref newObj, fieldInfo, (int)relAddr);
|
||||
} else {
|
||||
long relAddr = block.MemAddr.ToMemAddr() + offset;
|
||||
if(objects.TryGetValue((relAddr, newObj.GetType()), out object? o)){ //If the object is already created, we can just assign it
|
||||
field.SetValue(obj, o);
|
||||
continue;
|
||||
}
|
||||
//Fill the object with the data from the block (this is recursive)
|
||||
FillObject(block, ref newObj, fieldInfo, offset);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
objects.Add((block.MemAddr.ToMemAddr() + startOffset, obj!.GetType()), obj!);
|
||||
}
|
||||
|
||||
private object? ConvertFieldData(byte[] data, string type)
|
||||
|
||||
Reference in New Issue
Block a user