From d95c81f3f29585ee8a93111645d3bbb04ac42d53 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Thu, 20 Feb 2025 17:33:55 +0100 Subject: [PATCH] Added memory address management --- BlendFile/ByteArrayExt.cs | 6 ++++++ BlendFile/Reader.cs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 BlendFile/ByteArrayExt.cs diff --git a/BlendFile/ByteArrayExt.cs b/BlendFile/ByteArrayExt.cs new file mode 100644 index 0000000..10f41aa --- /dev/null +++ b/BlendFile/ByteArrayExt.cs @@ -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); +} \ No newline at end of file diff --git a/BlendFile/Reader.cs b/BlendFile/Reader.cs index 792fabd..5cb24e0 100644 --- a/BlendFile/Reader.cs +++ b/BlendFile/Reader.cs @@ -1,3 +1,4 @@ +using System.Numerics; using System.Reflection; using System.Text; using Kaitai; @@ -12,12 +13,32 @@ public class Reader { private List objects = new(); public List Objects => objects; - public Reader(string path) { + private Dictionary memBlocks = new(); + + /// + /// Gets the block at the specified memory address + /// + /// memory address in current system endianness + /// A object + public Kaitai.BlendFile.FileBlock? GetBlock(long memAddr) => memBlocks.GetValueOrDefault(memAddr); + + /// + /// Creates a new instance of the class + /// + /// A containing a path to a blend file that will be read. + public Reader(string path) : this() { _path = path; + } + + /// + /// Creates a new instance of the class + /// + public Reader() { + _path = ""; var types = Assembly.GetExecutingAssembly().DefinedTypes; foreach (var type in types) { var attrib = type.GetCustomAttribute(); - if (attrib ==null) continue; + if (attrib == null) continue; dnaTypes.Add(attrib.OriginalIndex, type); } } @@ -25,7 +46,10 @@ public class Reader { public void Read() { 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 }; - }} \ No newline at end of file + } +} \ No newline at end of file