Replaced longs with built in IntPtr type.

This commit is contained in:
Samuele Lorefice
2025-02-25 16:32:08 +01:00
parent 315cd6eb44
commit 23cec0bd0f
2 changed files with 17 additions and 10 deletions

View File

@@ -11,8 +11,8 @@ public class Reader {
private readonly Dictionary<int, Type> dnaTypes = new(); private readonly Dictionary<int, Type> dnaTypes = new();
private readonly Dictionary<string, Type> dnaTypesDb = new(); private readonly Dictionary<string, Type> dnaTypesDb = new();
private Dictionary<(long, Type), object> objects = new(); private Dictionary<(IntPtr, Type), object> objects = new();
public Dictionary<(long, Type), object> Objects => objects; public Dictionary<(IntPtr, Type), object> Objects => objects;
/// <summary> /// <summary>
/// A dictionary that contains pointers to pointers /// A dictionary that contains pointers to pointers
@@ -21,7 +21,7 @@ public class Reader {
/// Key: Memory address of the pointer /// Key: Memory address of the pointer
/// Value: Memory address of the object we are pointing to /// Value: Memory address of the object we are pointing to
/// </remarks> /// </remarks>
private Dictionary<long, long> pointers = new(); private Dictionary<IntPtr, IntPtr> pointers = new();
public List<object> GetObjects() => objects.Values.ToList(); public List<object> GetObjects() => objects.Values.ToList();
public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList(); public List<T> GetObjects<T>() => objects.Values.OfType<T>().ToList();
@@ -66,6 +66,8 @@ 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);
Console.WriteLine($"Start offset: 0x{blend.Blocks[0].MemAddr.ToPointer():X}");
bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le; bool isLe = blend.Hdr.Endian == Kaitai.BlendFile.Endian.Le;
//TODO: two blocks somehow have the same mem address... this sounds wrong. //TODO: two blocks somehow have the same mem address... this sounds wrong.
blend.Blocks.ForEach(block => memBlocks.TryAdd(block.MemAddr.ToMemAddr(isLe), block)); blend.Blocks.ForEach(block => memBlocks.TryAdd(block.MemAddr.ToMemAddr(isLe), block));
@@ -112,6 +114,10 @@ public class Reader {
private long GetBlockFieldDataOffset(long blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) => private long GetBlockFieldDataOffset(long blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata); blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IntPtr GetBlockFieldDataOffset(IntPtr blockAddress, int fieldIndex, FieldInfo[] fieldMetadata) =>
new(blockAddress + GetFieldDataOffset(fieldIndex, fieldMetadata));
/// <summary> /// <summary>
/// Gets the offset of the data of a field in a block /// Gets the offset of the data of a field in a block
/// </summary> /// </summary>
@@ -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="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="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> /// <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 if(block.Code == "ENDB") return;// ENDB is a special block that does not contain any data
foreach (var field in fieldMetadata) { foreach (var field in fieldMetadata) {
//Get the DNAFieldAttribute of the current field //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. //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 //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; int size = attrib.Size;
var data = new byte[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 the field is not a pointer, we need to dereference it
if (!attrib.IsPointer) { 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 (objects.TryGetValue((relAddr, newObj.GetType()), out object? o)) {
//If the object is already created, we can just assign it //If the object is already created, we can just assign it
field.SetValue(obj, o); field.SetValue(obj, o);
@@ -177,9 +183,9 @@ public class Reader {
//Fill the object with the data from the block (this is recursive) //Fill the object with the data from the block (this is recursive)
FillObject(block, ref newObj, fieldInfo, offset); FillObject(block, ref newObj, fieldInfo, offset);
} else { // if is a pointer, make a pointer to the pointer } 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 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 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. //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); 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) private object? ConvertFieldData(byte[] data, string type)

View File

@@ -5,6 +5,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCodeGenerator_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4aa9136c27fa69d6c91e9d8679c1a1d4b7aabee06e20d4405ee3d91ee05048f0_003FCSharpCodeGenerator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AList_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fb7208b3f72528d22781d25fde9a55271bdf2b5aade4f03b1324579a25493cd8_003FList_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F5b_003F039af867_003FRuntimeType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Febdb3cec3b3875204585daa9fc42159a24cae33b2087ff4dc114d0e6a5a3e9_003FSortedDictionary_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedDictionary_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Febdb3cec3b3875204585daa9fc42159a24cae33b2087ff4dc114d0e6a5a3e9_003FSortedDictionary_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>