From 23cec0bd0f417b81a976f512d42f17bd297a6675 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Tue, 25 Feb 2025 16:32:08 +0100 Subject: [PATCH] Replaced longs with built in IntPtr type. --- BlendFile/Reader.cs | 26 ++++++++++++++++---------- BlenderSharp.sln.DotSettings.user | 1 + 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/BlendFile/Reader.cs b/BlendFile/Reader.cs index 7979ec1..9906782 100644 --- a/BlendFile/Reader.cs +++ b/BlendFile/Reader.cs @@ -11,8 +11,8 @@ public class Reader { private readonly Dictionary dnaTypes = new(); private readonly Dictionary 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; /// /// 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 /// - private Dictionary pointers = new(); + private Dictionary pointers = new(); public List GetObjects() => objects.Values.ToList(); public List GetObjects() => objects.Values.OfType().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)); /// /// Gets the offset of the data of a field in a block @@ -138,7 +144,7 @@ public class Reader { /// object of same struct type as the one that is being decoded /// Array of s containing attributes /// offset in bytes from where structure starts in the block - 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) diff --git a/BlenderSharp.sln.DotSettings.user b/BlenderSharp.sln.DotSettings.user index df4eaff..0d32227 100644 --- a/BlenderSharp.sln.DotSettings.user +++ b/BlenderSharp.sln.DotSettings.user @@ -5,6 +5,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded