Added memory address management
This commit is contained in:
6
BlendFile/ByteArrayExt.cs
Normal file
6
BlendFile/ByteArrayExt.cs
Normal 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);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using Kaitai;
|
||||
@@ -12,12 +13,32 @@ 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>();
|
||||
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
|
||||
};
|
||||
}}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user