217 lines
10 KiB
C#
217 lines
10 KiB
C#
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using Kaitai;
|
|
|
|
|
|
namespace BlendFile;
|
|
|
|
public class Reader {
|
|
string _path;
|
|
private readonly Dictionary<int, Type> dnaTypes = new();
|
|
private readonly Dictionary<string, Type> dnaTypesDb = new();
|
|
|
|
private Dictionary<(IntPtr, Type), object> objects = new();
|
|
public Dictionary<(IntPtr, Type), object> Objects => objects;
|
|
|
|
/// <summary>
|
|
/// A dictionary that contains pointers to pointers
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Key: Memory address of the pointer
|
|
/// Value: Memory address of the object we are pointing to
|
|
/// </remarks>
|
|
private Dictionary<IntPtr, IntPtr> pointers = 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.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="Reader"/> class
|
|
/// </summary>
|
|
/// <param name="path">A <see cref="string"/> containing a path to a blend file that will be read.</param>
|
|
public Reader(string path) : this() {
|
|
_path = path;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="Reader"/> class
|
|
/// </summary>
|
|
public Reader() {
|
|
_path = "";
|
|
var types = Assembly.GetExecutingAssembly().DefinedTypes;
|
|
foreach (var type in types) {
|
|
var attrib = type.GetCustomAttribute<DNAClassAttribute>();
|
|
if (attrib == null) continue;
|
|
dnaTypes.Add(attrib.OriginalIndex, type);
|
|
dnaTypesDb.Add(attrib.OriginalName, type);
|
|
}
|
|
}
|
|
|
|
public void Read(string path) {
|
|
_path = path;
|
|
Read();
|
|
}
|
|
|
|
public void Read() {
|
|
var file = new KaitaiStream(_path);
|
|
var blend = new Kaitai.BlendFile(file);
|
|
|
|
Console.WriteLine($"Start offset: 0x{blend.Blocks[0].MemAddr.ToPointer():X}");
|
|
|
|
bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le;
|
|
//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) {
|
|
//We need to read all blocks of data regardless of the type
|
|
//Checks if the block has a known SDNA type, meaning that we know how it is structured
|
|
if (!dnaTypes.ContainsKey((int)block.SdnaIndex)) continue;
|
|
|
|
//Get the type of the block
|
|
dnaTypesDb.TryGetValue(block.SdnaStruct.Type, out Type? t);
|
|
if (t == null) continue; //should never happen
|
|
|
|
// How many objects are in the block
|
|
var count = block.Count;
|
|
//offset for the next object in the block
|
|
var blockOffset = 0;
|
|
//for each expected object in the block
|
|
for (var i = 0; i < count; i++) {
|
|
//create an instance of type "t" (the dna type we inferred before)
|
|
var obj = ActivateInstance(block.SdnaStruct.Type);
|
|
if (obj == null) continue; //should never happen
|
|
|
|
//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;
|
|
}
|
|
}
|
|
foreach (var obj in objects) {
|
|
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
|
|
var list = fieldInfo.Where(fldInfo => fldInfo.GetCustomAttribute<DNAFieldAttribute>()!.IsPointer).ToList();
|
|
|
|
foreach(var f in list) {
|
|
var addr = GetBlockFieldDataOffset(obj.Key.Item1, f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex, fieldInfo);
|
|
var newobj = objects.GetValueOrDefault((addr, f.FieldType));
|
|
if (newobj != null) f.SetValue(obj.Value, newobj);
|
|
}
|
|
}
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private long GetBlockFieldDataOffset(long blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
|
|
blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private IntPtr GetBlockFieldDataOffset(IntPtr blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
|
|
new(blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata));
|
|
|
|
/// <summary>
|
|
/// Gets the offset of the data of a field in a block
|
|
/// </summary>
|
|
/// <param name="fieldIndex">index of the field in the structure</param>
|
|
/// <param name="fieldMetadata"><see cref="FieldInfo"/> array of metadata</param>
|
|
/// <returns></returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private long GetFieldDataOffset(int fieldIndex, FieldInfo[] fieldMetadata) =>
|
|
fieldMetadata.First(x => x.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex == fieldIndex)
|
|
.GetCustomAttribute<DNAFieldAttribute>()!.MemoryOffset;
|
|
|
|
/// <summary>
|
|
/// Creates an instance of a given type
|
|
/// </summary>
|
|
/// <param name="type">A <see cref="string"/> containing the name of the type to create</param>
|
|
/// <returns>An object of the type specified in the parameter or null</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private object? ActivateInstance(string type) => dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
|
|
|
/// <summary>
|
|
/// Filles a given object with the data from a block, starting to read it from the specified offset
|
|
/// </summary>
|
|
/// <param name="block">data block from where to read the values</param>
|
|
/// <param name="obj">object of same struct type as the one that is being decoded</param>
|
|
/// <param name="fieldMetadata">Array of <see cref="FieldInfo"/>s containing <see cref="DNAFieldAttribute"/> attributes</param>
|
|
/// <param name="startOffset">offset in bytes from where structure starts in the block</param>
|
|
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object? obj, FieldInfo[] fieldMetadata, IntPtr startOffset = 0) {
|
|
if(block.Code == "ENDB") return;// ENDB is a special block that does not contain any data
|
|
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
|
|
IntPtr offset = attrib.MemoryOffset;
|
|
|
|
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
|
|
object? value = ConvertFieldData(data, attrib.OriginalType);
|
|
|
|
if(value == null){ //if the data could not be converted
|
|
//Check if the field is a pointer to another DNA structure
|
|
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
|
//Create a new instance of the DNA structure type
|
|
object? newObj = ActivateInstance(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 not a pointer, we need to dereference it
|
|
if (!attrib.IsPointer) {
|
|
IntPtr relAddr = block.MemAddr.ToPointer() + 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);
|
|
} else { // if is a pointer, make a pointer to the pointer
|
|
IntPtr memAddr = data.ToPointer();
|
|
if (memAddr == 0) continue; //nullPointer, no need to store the reference
|
|
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
|
|
}
|
|
}
|
|
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.ToPointer() + startOffset, obj!.GetType()), obj!);
|
|
}
|
|
|
|
private object? ConvertFieldData(byte[] data, string type)
|
|
{
|
|
return type switch
|
|
{
|
|
"char" => (char)data[0],
|
|
"short" => BitConverter.ToInt16(data, 0),
|
|
"int" => BitConverter.ToInt32(data, 0),
|
|
"float" => BitConverter.ToSingle(data, 0),
|
|
"double" => BitConverter.ToDouble(data, 0),
|
|
"string" => Encoding.UTF8.GetString(data), // utf8?
|
|
"void" => null, // object?
|
|
"ushort" => BitConverter.ToUInt16(data, 0),
|
|
"uchar" => data[0],
|
|
"int64_t" => BitConverter.ToInt64(data, 0),
|
|
"int8_t" => (sbyte)data[0],
|
|
"uint64_t" => BitConverter.ToUInt64(data, 0),
|
|
_ => null
|
|
};
|
|
}
|
|
} |