Compare commits

..

2 Commits

Author SHA1 Message Date
Samuele Lorefice
6d565377a4 Added more documentation and started pointer handling 2025-02-20 18:41:27 +01:00
Samuele Lorefice
d95c81f3f2 Added memory address management 2025-02-20 17:33:55 +01:00
4 changed files with 103 additions and 11 deletions

View File

@@ -0,0 +1,6 @@
namespace BlendFile;
public static class ByteArrayExt{
public static long ToMemAddr(this Byte[] bytes, bool isLittleEndian = true) =>
BitConverter.ToInt64(isLittleEndian == BitConverter.IsLittleEndian ? bytes : bytes.Reverse().ToArray(), 0);
}

View File

@@ -6,26 +6,58 @@ using Kaitai;
namespace BlendFile; namespace BlendFile;
public class Reader { public class Reader {
readonly string _path; string _path;
private readonly Dictionary<int, Type> dnaTypes = new(); private readonly Dictionary<int, Type> dnaTypes = new();
private List<object> objects = new(); private Dictionary<(long, Type), object> objects = new();
public List<object> Objects => objects; public Dictionary<(long, Type), object> Objects => objects;
public Reader(string path) { 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; _path = path;
}
/// <summary>
/// Creates a new instance of the <see cref="Reader"/> class
/// </summary>
public Reader() {
_path = "";
var types = Assembly.GetExecutingAssembly().DefinedTypes; var types = Assembly.GetExecutingAssembly().DefinedTypes;
foreach (var type in types) { foreach (var type in types) {
var attrib = type.GetCustomAttribute<DNAClassAttribute>(); var attrib = type.GetCustomAttribute<DNAClassAttribute>();
if (attrib ==null) continue; if (attrib == null) continue;
dnaTypes.Add(attrib.OriginalIndex, type); dnaTypes.Add(attrib.OriginalIndex, type);
} }
} }
public void Read(string path) {
_path = path;
Read();
}
public void Read() { public void Read() {
var file = new KaitaiStream(_path); var file = new KaitaiStream(_path);
var blend = new Kaitai.BlendFile(file); var blend = new Kaitai.BlendFile(file);
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) foreach (var block in blend.Blocks)
{ {
//We need to read all blocks of data regardeless of the type //We need to read all blocks of data regardeless of the type
@@ -44,21 +76,31 @@ public class Reader {
//create an instance of type "t" (the dna type we inferred before) //create an instance of type "t" (the dna type we inferred before)
var obj = Activator.CreateInstance(t); var obj = Activator.CreateInstance(t);
if(obj == null) continue; //should never happen 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 //fill the object with the data from the block
FillObject(block, ref obj, t.GetFields(), blockOffset); FillObject(block, ref obj, t.GetFields(), blockOffset);
//move the offset to the next object in the block //move the offset to the next object in the block
blockOffset += t.GetCustomAttribute<DNAClassAttribute>()!.Size; 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) { foreach (var field in fieldMetadata) {
//Get the DNAFieldAttribute of the current field //Get the DNAFieldAttribute of the current field
var attrib = field.GetCustomAttribute<DNAFieldAttribute>(); var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
if (attrib == null) continue; //should never happen, but means a field has no metadata 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. //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
int offset = fieldMetadata.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex) int offset = fieldMetadata.Where(f => f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex < attrib.OriginalIndex)
@@ -69,10 +111,50 @@ public class Reader {
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 //Convert the data to the correct type
object? value = ConvertFieldData(data, attrib.OriginalType); 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. //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); field.SetValue(obj, value);
} }
objects.Add((block.MemAddr.ToMemAddr() + startOffset, obj!.GetType()), obj!);
} }
private object? ConvertFieldData(byte[] data, string type) private object? ConvertFieldData(byte[] data, string type)
@@ -93,4 +175,5 @@ public class Reader {
"uint64_t" => BitConverter.ToUInt64(data, 0), "uint64_t" => BitConverter.ToUInt64(data, 0),
_ => null _ => null
}; };
}} }
}

View File

@@ -4,7 +4,7 @@ using BlendFile;
var reader = new Reader("cube.blend"); var reader = new Reader("cube.blend");
reader.Read(); reader.Read();
foreach (var obj in reader.Objects) foreach (var obj in reader.GetObjects())
{ {
Console.WriteLine(obj.GetType()); Console.WriteLine(obj.GetType());
Console.WriteLine(obj.ToString()); Console.WriteLine(obj.ToString());

View File

@@ -1,9 +1,12 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArgumentException_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003Fea_003F056794d0_003FArgumentException_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArgumentException_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003Fea_003F056794d0_003FArgumentException_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fee3c9d264411441f56a12bac52a58ad25b495ef52e59b4a86f8478158f7d_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F51b332f64116ca1bf68931ca13adc3cd38b6dd2da04af6aef46bd08d218e58b5_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F51b332f64116ca1bf68931ca13adc3cd38b6dd2da04af6aef46bd08d218e58b5_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Febdb3cec3b3875204585daa9fc42159a24cae33b2087ff4dc114d0e6a5a3e9_003FSortedDictionary_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F3e_003F433607bb_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F3e_003F433607bb_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe7dbd6fe331ff9a3c4b24dd470ec1f19a71b7c5acf258b81ae7f761cd2b319b_003FType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe7dbd6fe331ff9a3c4b24dd470ec1f19a71b7c5acf258b81ae7f761cd2b319b_003FType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>