Formatting and documentation pass

This commit is contained in:
Samuele Lorefice
2025-03-12 01:58:56 +01:00
parent 633b2b4376
commit c48d84bb7c

View File

@@ -14,6 +14,10 @@ public class Reader {
private readonly Dictionary<string, Type> dnaTypesDb = new();
private Dictionary<(IntPtr, Type), object> objects = new();
/// <summary>
/// The objects that have been converted from the blend file
/// </summary>
public Dictionary<(IntPtr, Type), object> Objects => objects;
public Dictionary<IntPtr, object> InstantiatedObjects = new();
@@ -27,7 +31,15 @@ public class Reader {
/// </remarks>
private Dictionary<IntPtr, IntPtr> pointers = new();
/// <summary>
/// Gets all the converted objects
/// </summary>
public List<object> GetObjects() => objects.Values.ToList();
/// <summary>
/// Gets all the converted objects of a specific type
/// </summary>
/// <typeparam name="T">The type of objects that will be retrieved</typeparam>
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
private SortedDictionary<long, FileBlock> memBlocks = new();
@@ -37,8 +49,7 @@ public class Reader {
/// </summary>
/// <param name="memAddr">memory address in current system endianness</param>
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
public FileBlock? GetBlock(long memAddr) =>
memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
public FileBlock? GetBlock(long memAddr) => memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
/// <summary>
/// Creates a new instance of the <see cref="Reader"/> class
@@ -103,7 +114,8 @@ public class Reader {
blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size;
}
}
foreach (var obj in objects) { // for each converted object
foreach (var obj in objects) {
// for each converted object
// get the fields of the object
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
// get all fields that are pointers
@@ -121,8 +133,7 @@ public class Reader {
else // if we haven't converted the object yet, we need to convert it now
{
newobj = ActivateInstance(f.FieldType);
if (newobj != null)
{
if (newobj != null) {
FillObject(addr, ref newobj, f.FieldType.GetFields());
f.SetValue(obj.Value, newobj);
Objects.Add((addr, f.FieldType), newobj);
@@ -132,7 +143,7 @@ public class Reader {
}
}
foreach(var ptr in pointers) {
foreach (var ptr in pointers) {
var obj = objects.GetValueOrDefault((ptr.Key, typeof(object)));
if (obj == null) continue;
InstantiatedObjects.Add(ptr.Key, obj);
@@ -214,8 +225,7 @@ public class Reader {
/// <param name="obj">reference to the object to be filled</param>
/// <param name="fieldMetadata">field metadata info for that object</param>
/// <exception cref="Exception">Thrown when the block the pointer is pointing to is not found.</exception>
private void FillObject(IntPtr ptr, ref object? obj, FieldInfo[] fieldMetadata)
{
private void FillObject(IntPtr ptr, ref object? obj, FieldInfo[] fieldMetadata) {
var block = GetBlock(ptr.ToInt64());
if (block == null) throw new($"Block for pointer {ptr.ToInt64():X} not found");
@@ -239,7 +249,8 @@ public class Reader {
//Check if the field is a pointer to another DNA structure
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
if (!attrib.IsPointer) { //It's a structure
if (!attrib.IsPointer) {
//It's a structure
//Create a new instance of the DNA structure type
object? newObj = ActivateInstance(attrib.OriginalType);
@@ -254,7 +265,8 @@ public class Reader {
//Fill the object with the data from the block (this is recursive)
FillObject(block, ref newObj, fieldInfo, offset);
} else { //It's a pointer
} else {
//It's a pointer
IntPtr memAddr = data.ToPointer();
if (memAddr == 0) return null; //nullPointer, no need to store the reference
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
@@ -290,7 +302,8 @@ public class Reader {
if (cellData == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\"");
var cellType = Type.GetType(arrayAttribute.OriginalType.ParseFType());
//check that cellData is the correct type
if (cellData.GetType() != cellType) throw new NotSupportedException($"Array type mismatch. Expected {cellType} got {cellData.GetType()}");
if (cellData.GetType() != cellType)
throw new NotSupportedException($"Array type mismatch. Expected {cellType} got {cellData.GetType()}");
array.SetValue(cellData, i / itemLenght);
}
@@ -307,8 +320,7 @@ public class Reader {
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);
int count;
switch (tmpCount)
{
switch (tmpCount) {
case int i:
count = i;
break;
@@ -321,7 +333,7 @@ public class Reader {
List<Object?> objList = new();
for(IntPtr ptr = ptrOffset; ptr < ptrOffset + count * sizeof(Int64); ptr += sizeof(Int64)) {
for (IntPtr ptr = ptrOffset; ptr < ptrOffset + count * sizeof(Int64); ptr += sizeof(Int64)) {
var data = new byte[sizeof(Int64)];
Array.Copy((byte[])block.Body, ptr, data, 0, sizeof(Int64));
IntPtr memAddr = new IntPtr(BitConverter.ToInt64(data, 0));