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.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Kaitai;
|
using Kaitai;
|
||||||
@@ -12,12 +13,32 @@ public class Reader {
|
|||||||
private List<object> objects = new();
|
private List<object> objects = new();
|
||||||
public List<object> Objects => objects;
|
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;
|
_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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,6 +47,9 @@ public class Reader {
|
|||||||
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;
|
||||||
|
blend.Blocks.ForEach(block => memBlocks.Add(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
|
||||||
@@ -93,4 +117,5 @@ public class Reader {
|
|||||||
"uint64_t" => BitConverter.ToUInt64(data, 0),
|
"uint64_t" => BitConverter.ToUInt64(data, 0),
|
||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
}}
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user