Replaced longs with built in IntPtr type.
This commit is contained in:
@@ -11,8 +11,8 @@ public class Reader {
|
||||
private readonly Dictionary<int, Type> dnaTypes = new();
|
||||
private readonly Dictionary<string, Type> dnaTypesDb = new();
|
||||
|
||||
private Dictionary<(long, Type), object> objects = new();
|
||||
public Dictionary<(long, Type), object> Objects => objects;
|
||||
private Dictionary<(IntPtr, Type), object> objects = new();
|
||||
public Dictionary<(IntPtr, Type), object> Objects => objects;
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary that contains pointers to pointers
|
||||
@@ -21,7 +21,7 @@ public class Reader {
|
||||
/// Key: Memory address of the pointer
|
||||
/// Value: Memory address of the object we are pointing to
|
||||
/// </remarks>
|
||||
private Dictionary<long, long> pointers = new();
|
||||
private Dictionary<IntPtr, IntPtr> pointers = new();
|
||||
|
||||
public List<object> GetObjects() => objects.Values.ToList();
|
||||
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
|
||||
@@ -65,7 +65,9 @@ public class Reader {
|
||||
public void Read() {
|
||||
var file = new KaitaiStream(_path);
|
||||
var blend = new Kaitai.BlendFile(file);
|
||||
|
||||
|
||||
Console.WriteLine($"Start offset: 0x{blend.Blocks[0].MemAddr.ToPointer():X}");
|
||||
|
||||
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));
|
||||
@@ -111,6 +113,10 @@ public class Reader {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private long GetBlockFieldDataOffset(long blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
|
||||
blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private IntPtr GetBlockFieldDataOffset(IntPtr blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
|
||||
new(blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the offset of the data of a field in a block
|
||||
@@ -138,7 +144,7 @@ public class Reader {
|
||||
/// <param name="obj">object of same struct type as the one that is being decoded</param>
|
||||
/// <param name="fieldMetadata">Array of <see cref="FieldInfo"/>s containing <see cref="DNAFieldAttribute"/> attributes</param>
|
||||
/// <param name="startOffset">offset in bytes from where structure starts in the block</param>
|
||||
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object? obj, FieldInfo[] fieldMetadata, long startOffset = 0) {
|
||||
private void FillObject(Kaitai.BlendFile.FileBlock block, ref object? obj, FieldInfo[] fieldMetadata, IntPtr startOffset = 0) {
|
||||
if(block.Code == "ENDB") return;// ENDB is a special block that does not contain any data
|
||||
foreach (var field in fieldMetadata) {
|
||||
//Get the DNAFieldAttribute of the current field
|
||||
@@ -147,7 +153,7 @@ public class Reader {
|
||||
|
||||
//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
|
||||
long offset = attrib.MemoryOffset;
|
||||
IntPtr offset = attrib.MemoryOffset;
|
||||
|
||||
int size = attrib.Size;
|
||||
var data = new byte[size];
|
||||
@@ -168,7 +174,7 @@ public class Reader {
|
||||
|
||||
//If the field is not a pointer, we need to dereference it
|
||||
if (!attrib.IsPointer) {
|
||||
long relAddr = block.MemAddr.ToMemAddr() + offset;
|
||||
IntPtr relAddr = block.MemAddr.ToPointer() + 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);
|
||||
@@ -177,9 +183,9 @@ public class Reader {
|
||||
//Fill the object with the data from the block (this is recursive)
|
||||
FillObject(block, ref newObj, fieldInfo, offset);
|
||||
} else { // if is a pointer, make a pointer to the pointer
|
||||
long memAddr = data.ToMemAddr();
|
||||
IntPtr memAddr = data.ToPointer();
|
||||
if (memAddr == 0) continue; //nullPointer, no need to store the reference
|
||||
pointers.TryAdd(block.MemAddr.ToMemAddr() + offset, data.ToMemAddr());
|
||||
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
|
||||
}
|
||||
}
|
||||
continue; //should never happen, but means the data could not be converted
|
||||
@@ -187,7 +193,7 @@ public class Reader {
|
||||
//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);
|
||||
}
|
||||
objects.Add((block.MemAddr.ToMemAddr() + startOffset, obj!.GetType()), obj!);
|
||||
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
|
||||
}
|
||||
|
||||
private object? ConvertFieldData(byte[] data, string type)
|
||||
|
||||
Reference in New Issue
Block a user