Formatting and documentation pass
This commit is contained in:
@@ -14,10 +14,14 @@ 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();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary that contains pointers to pointers
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
@@ -131,8 +142,8 @@ 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);
|
||||
@@ -166,7 +177,7 @@ public class Reader {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private object? ActivateInstance(string type) =>
|
||||
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private object? ActivateInstance(Type type) => Activator.CreateInstance(type);
|
||||
|
||||
@@ -197,9 +208,9 @@ public class Reader {
|
||||
default:
|
||||
continue; //should never happen.
|
||||
}
|
||||
|
||||
|
||||
field.SetValue(obj, value);
|
||||
|
||||
|
||||
//Add the freshly handled object to the database
|
||||
objects.TryAdd((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
|
||||
}
|
||||
@@ -214,15 +225,14 @@ 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");
|
||||
|
||||
|
||||
var blockOffset = ptr.ToInt64() - block.MemAddr.ToPointer();
|
||||
FillObject(block, ref obj, fieldMetadata, new IntPtr(blockOffset));
|
||||
}
|
||||
|
||||
|
||||
private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib, IntPtr startOffset) {
|
||||
//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
|
||||
@@ -232,15 +242,16 @@ public class Reader {
|
||||
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 if it's a base type
|
||||
object? value = ConvertFieldData(data, attrib.OriginalType);
|
||||
if (value != null) return value;
|
||||
|
||||
//Check if the field is a pointer to another DNA structure
|
||||
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
||||
if (!attrib.IsPointer) { //It's a structure
|
||||
|
||||
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
||||
if (!attrib.IsPointer) {
|
||||
//It's a structure
|
||||
|
||||
//Create a new instance of the DNA structure type
|
||||
object? newObj = ActivateInstance(attrib.OriginalType);
|
||||
//should never happen... type is missing?
|
||||
@@ -250,11 +261,12 @@ public class Reader {
|
||||
//relative offset to this block for the structure
|
||||
IntPtr relAddr = block.MemAddr.ToPointer() + offset;
|
||||
//If the object is already created, we can just assign it
|
||||
if (objects.TryGetValue((relAddr, newObj.GetType()), out object? o)) return o;
|
||||
|
||||
if (objects.TryGetValue((relAddr, newObj.GetType()), out object? o)) return o;
|
||||
|
||||
//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());
|
||||
@@ -275,14 +287,14 @@ public class Reader {
|
||||
int size = arrayAttribute.Size;
|
||||
var data = new byte[size];
|
||||
Array.Copy((byte[])block.Body, offset, data, 0, size);
|
||||
|
||||
|
||||
int itemLenght = arrayAttribute.OriginalType.ParseFSize();
|
||||
//Gather Array type
|
||||
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
||||
if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown");
|
||||
//Create the array
|
||||
var array = Array.CreateInstance(type, arrayAttribute.Size);
|
||||
|
||||
|
||||
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
|
||||
var itemData = new byte[itemLenght];
|
||||
Array.Copy(data, i, itemData, 0, itemLenght);
|
||||
@@ -290,13 +302,14 @@ 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);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
|
||||
private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {
|
||||
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
|
||||
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
|
||||
@@ -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;
|
||||
@@ -318,14 +330,14 @@ public class Reader {
|
||||
default:
|
||||
throw new NotSupportedException($"Unknown type \"{attrib.CountFieldName}\"");
|
||||
}
|
||||
|
||||
|
||||
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));
|
||||
|
||||
|
||||
if (memAddr == IntPtr.Zero) {
|
||||
objList.Add(null);
|
||||
continue;
|
||||
@@ -333,13 +345,13 @@ public class Reader {
|
||||
|
||||
var obj = ActivateInstance(attrib.UnderlyingType);
|
||||
if (obj == null) throw new NotSupportedException($"Type \"{attrib.UnderlyingType}\" is unknown");
|
||||
|
||||
|
||||
var fieldMetadata = obj.GetType().GetFields();
|
||||
|
||||
|
||||
FillObject(memAddr, ref obj, fieldMetadata);
|
||||
objList.Add(obj);
|
||||
}
|
||||
|
||||
|
||||
return objList;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user