Added memory address management

This commit is contained in:
Samuele Lorefice
2025-02-20 17:33:55 +01:00
parent c0533a0d69
commit d95c81f3f2
2 changed files with 35 additions and 4 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

@@ -1,3 +1,4 @@
using System.Numerics;
using System.Reflection;
using System.Text;
using Kaitai;
@@ -12,8 +13,28 @@ public class Reader {
private List<object> objects = new();
public List<object> Objects => objects;
public Reader(string path) {
private Dictionary<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.GetValueOrDefault(memAddr);
/// <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>();
@@ -26,6 +47,9 @@ public class Reader {
var file = new KaitaiStream(_path);
var blend = new Kaitai.BlendFile(file);
bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le;
blend.Blocks.ForEach(block => memBlocks.Add(block.MemAddr.ToMemAddr(isLe), block));
foreach (var block in blend.Blocks)
{
//We need to read all blocks of data regardeless of the type
@@ -93,4 +117,5 @@ public class Reader {
"uint64_t" => BitConverter.ToUInt64(data, 0),
_ => null
};
}}
}
}