Formatting and documentation pass
This commit is contained in:
@@ -14,10 +14,14 @@ public class Reader {
|
|||||||
private readonly Dictionary<string, Type> dnaTypesDb = new();
|
private readonly Dictionary<string, Type> dnaTypesDb = new();
|
||||||
|
|
||||||
private Dictionary<(IntPtr, Type), object> objects = 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, Type), object> Objects => objects;
|
||||||
|
|
||||||
public Dictionary<IntPtr, object> InstantiatedObjects = new();
|
public Dictionary<IntPtr, object> InstantiatedObjects = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A dictionary that contains pointers to pointers
|
/// A dictionary that contains pointers to pointers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -27,7 +31,15 @@ public class Reader {
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
private Dictionary<IntPtr, IntPtr> pointers = new();
|
private Dictionary<IntPtr, IntPtr> pointers = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets all the converted objects
|
||||||
|
/// </summary>
|
||||||
public List<object> GetObjects() => objects.Values.ToList();
|
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();
|
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
|
||||||
|
|
||||||
private SortedDictionary<long, FileBlock> memBlocks = new();
|
private SortedDictionary<long, FileBlock> memBlocks = new();
|
||||||
@@ -37,8 +49,7 @@ public class Reader {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="memAddr">memory address in current system endianness</param>
|
/// <param name="memAddr">memory address in current system endianness</param>
|
||||||
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
|
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
|
||||||
public FileBlock? GetBlock(long memAddr) =>
|
public FileBlock? GetBlock(long memAddr) => memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
||||||
memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="Reader"/> class
|
/// Creates a new instance of the <see cref="Reader"/> class
|
||||||
@@ -103,7 +114,8 @@ public class Reader {
|
|||||||
blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size;
|
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
|
// get the fields of the object
|
||||||
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
|
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
|
||||||
// get all fields that are pointers
|
// 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
|
else // if we haven't converted the object yet, we need to convert it now
|
||||||
{
|
{
|
||||||
newobj = ActivateInstance(f.FieldType);
|
newobj = ActivateInstance(f.FieldType);
|
||||||
if (newobj != null)
|
if (newobj != null) {
|
||||||
{
|
|
||||||
FillObject(addr, ref newobj, f.FieldType.GetFields());
|
FillObject(addr, ref newobj, f.FieldType.GetFields());
|
||||||
f.SetValue(obj.Value, newobj);
|
f.SetValue(obj.Value, newobj);
|
||||||
Objects.Add((addr, f.FieldType), 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)));
|
var obj = objects.GetValueOrDefault((ptr.Key, typeof(object)));
|
||||||
if (obj == null) continue;
|
if (obj == null) continue;
|
||||||
InstantiatedObjects.Add(ptr.Key, obj);
|
InstantiatedObjects.Add(ptr.Key, obj);
|
||||||
@@ -166,7 +177,7 @@ public class Reader {
|
|||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private object? ActivateInstance(string type) =>
|
private object? ActivateInstance(string type) =>
|
||||||
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private object? ActivateInstance(Type type) => Activator.CreateInstance(type);
|
private object? ActivateInstance(Type type) => Activator.CreateInstance(type);
|
||||||
|
|
||||||
@@ -197,9 +208,9 @@ public class Reader {
|
|||||||
default:
|
default:
|
||||||
continue; //should never happen.
|
continue; //should never happen.
|
||||||
}
|
}
|
||||||
|
|
||||||
field.SetValue(obj, value);
|
field.SetValue(obj, value);
|
||||||
|
|
||||||
//Add the freshly handled object to the database
|
//Add the freshly handled object to the database
|
||||||
objects.TryAdd((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
|
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="obj">reference to the object to be filled</param>
|
||||||
/// <param name="fieldMetadata">field metadata info for that object</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>
|
/// <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());
|
var block = GetBlock(ptr.ToInt64());
|
||||||
if (block == null) throw new($"Block for pointer {ptr.ToInt64():X} not found");
|
if (block == null) throw new($"Block for pointer {ptr.ToInt64():X} not found");
|
||||||
|
|
||||||
var blockOffset = ptr.ToInt64() - block.MemAddr.ToPointer();
|
var blockOffset = ptr.ToInt64() - block.MemAddr.ToPointer();
|
||||||
FillObject(block, ref obj, fieldMetadata, new IntPtr(blockOffset));
|
FillObject(block, ref obj, fieldMetadata, new IntPtr(blockOffset));
|
||||||
}
|
}
|
||||||
|
|
||||||
private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib, IntPtr startOffset) {
|
private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib, IntPtr startOffset) {
|
||||||
//Calculate the offset from where the data of the field starts.
|
//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
|
//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;
|
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 if it's a base type
|
//Convert the data to the correct type if it's a base type
|
||||||
object? value = ConvertFieldData(data, attrib.OriginalType);
|
object? value = ConvertFieldData(data, attrib.OriginalType);
|
||||||
if (value != null) return value;
|
if (value != null) return value;
|
||||||
|
|
||||||
//Check if the field is a pointer to another DNA structure
|
//Check if the field is a pointer to another DNA structure
|
||||||
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
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
|
//Create a new instance of the DNA structure type
|
||||||
object? newObj = ActivateInstance(attrib.OriginalType);
|
object? newObj = ActivateInstance(attrib.OriginalType);
|
||||||
//should never happen... type is missing?
|
//should never happen... type is missing?
|
||||||
@@ -250,11 +261,12 @@ public class Reader {
|
|||||||
//relative offset to this block for the structure
|
//relative offset to this block for the structure
|
||||||
IntPtr relAddr = block.MemAddr.ToPointer() + offset;
|
IntPtr relAddr = block.MemAddr.ToPointer() + offset;
|
||||||
//If the object is already created, we can just assign it
|
//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)
|
//Fill the object with the data from the block (this is recursive)
|
||||||
FillObject(block, ref newObj, fieldInfo, offset);
|
FillObject(block, ref newObj, fieldInfo, offset);
|
||||||
} else { //It's a pointer
|
} else {
|
||||||
|
//It's a pointer
|
||||||
IntPtr memAddr = data.ToPointer();
|
IntPtr memAddr = data.ToPointer();
|
||||||
if (memAddr == 0) return null; //nullPointer, no need to store the reference
|
if (memAddr == 0) return null; //nullPointer, no need to store the reference
|
||||||
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
|
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
|
||||||
@@ -275,14 +287,14 @@ public class Reader {
|
|||||||
int size = arrayAttribute.Size;
|
int size = arrayAttribute.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);
|
||||||
|
|
||||||
int itemLenght = arrayAttribute.OriginalType.ParseFSize();
|
int itemLenght = arrayAttribute.OriginalType.ParseFSize();
|
||||||
//Gather Array type
|
//Gather Array type
|
||||||
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
||||||
if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown");
|
if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown");
|
||||||
//Create the array
|
//Create the array
|
||||||
var array = Array.CreateInstance(type, arrayAttribute.Size);
|
var array = Array.CreateInstance(type, arrayAttribute.Size);
|
||||||
|
|
||||||
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
|
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
|
||||||
var itemData = new byte[itemLenght];
|
var itemData = new byte[itemLenght];
|
||||||
Array.Copy(data, i, itemData, 0, 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}\"");
|
if (cellData == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\"");
|
||||||
var cellType = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
var cellType = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
||||||
//check that cellData is the correct type
|
//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);
|
array.SetValue(cellData, i / itemLenght);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {
|
private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {
|
||||||
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
|
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
|
||||||
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
|
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
|
||||||
@@ -307,8 +320,7 @@ public class Reader {
|
|||||||
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);
|
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
switch (tmpCount)
|
switch (tmpCount) {
|
||||||
{
|
|
||||||
case int i:
|
case int i:
|
||||||
count = i;
|
count = i;
|
||||||
break;
|
break;
|
||||||
@@ -318,14 +330,14 @@ public class Reader {
|
|||||||
default:
|
default:
|
||||||
throw new NotSupportedException($"Unknown type \"{attrib.CountFieldName}\"");
|
throw new NotSupportedException($"Unknown type \"{attrib.CountFieldName}\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Object?> objList = new();
|
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)];
|
var data = new byte[sizeof(Int64)];
|
||||||
Array.Copy((byte[])block.Body, ptr, data, 0, sizeof(Int64));
|
Array.Copy((byte[])block.Body, ptr, data, 0, sizeof(Int64));
|
||||||
IntPtr memAddr = new IntPtr(BitConverter.ToInt64(data, 0));
|
IntPtr memAddr = new IntPtr(BitConverter.ToInt64(data, 0));
|
||||||
|
|
||||||
if (memAddr == IntPtr.Zero) {
|
if (memAddr == IntPtr.Zero) {
|
||||||
objList.Add(null);
|
objList.Add(null);
|
||||||
continue;
|
continue;
|
||||||
@@ -333,13 +345,13 @@ public class Reader {
|
|||||||
|
|
||||||
var obj = ActivateInstance(attrib.UnderlyingType);
|
var obj = ActivateInstance(attrib.UnderlyingType);
|
||||||
if (obj == null) throw new NotSupportedException($"Type \"{attrib.UnderlyingType}\" is unknown");
|
if (obj == null) throw new NotSupportedException($"Type \"{attrib.UnderlyingType}\" is unknown");
|
||||||
|
|
||||||
var fieldMetadata = obj.GetType().GetFields();
|
var fieldMetadata = obj.GetType().GetFields();
|
||||||
|
|
||||||
FillObject(memAddr, ref obj, fieldMetadata);
|
FillObject(memAddr, ref obj, fieldMetadata);
|
||||||
objList.Add(obj);
|
objList.Add(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
return objList;
|
return objList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user