General code cleanup in reader class. Added stub methods for extracting data retrieval to a separate method.
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Reflection;
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Kaitai;
|
using Kaitai;
|
||||||
|
using static Kaitai.BlendFile;
|
||||||
|
|
||||||
|
|
||||||
namespace BlendFile;
|
namespace BlendFile;
|
||||||
@@ -33,7 +34,8 @@ public class Reader {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="memAddr">memory address in current system endianness</param>
|
/// <param name="memAddr">memory address in current system endianness</param>
|
||||||
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
|
/// <returns>A <see cref="Kaitai.BlendFile.FileBlock"/> object</returns>
|
||||||
public Kaitai.BlendFile.FileBlock? GetBlock(long memAddr) => memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
public FileBlock? GetBlock(long memAddr) =>
|
||||||
|
memBlocks.SkipWhile(x => x.Key < memAddr).FirstOrDefault().Value;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="Reader"/> class
|
/// Creates a new instance of the <see cref="Reader"/> class
|
||||||
@@ -103,7 +105,8 @@ public class Reader {
|
|||||||
var list = fieldInfo.Where(fldInfo => fldInfo.GetCustomAttribute<DNAFieldAttribute>()!.IsPointer).ToList();
|
var list = fieldInfo.Where(fldInfo => fldInfo.GetCustomAttribute<DNAFieldAttribute>()!.IsPointer).ToList();
|
||||||
|
|
||||||
foreach (var f in list) {
|
foreach (var f in list) {
|
||||||
var addr = GetBlockFieldDataOffset(obj.Key.Item1, f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex, fieldInfo);
|
var addr = GetBlockFieldDataOffset(obj.Key.Item1, f.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex,
|
||||||
|
fieldInfo);
|
||||||
var newobj = objects.GetValueOrDefault((addr, f.FieldType));
|
var newobj = objects.GetValueOrDefault((addr, f.FieldType));
|
||||||
if (newobj != null) f.SetValue(obj.Value, newobj);
|
if (newobj != null) f.SetValue(obj.Value, newobj);
|
||||||
}
|
}
|
||||||
@@ -135,7 +138,8 @@ public class Reader {
|
|||||||
/// <param name="type">A <see cref="string"/> containing the name of the type to create</param>
|
/// <param name="type">A <see cref="string"/> containing the name of the type to create</param>
|
||||||
/// <returns>An object of the type specified in the parameter or null</returns>
|
/// <returns>An object of the type specified in the parameter or null</returns>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private object? ActivateInstance(string type) => dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
private object? ActivateInstance(string type) =>
|
||||||
|
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Filles a given object with the data from a block, starting to read it from the specified offset
|
/// Filles a given object with the data from a block, starting to read it from the specified offset
|
||||||
@@ -144,9 +148,12 @@ 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, IntPtr startOffset = 0) {
|
private void FillObject(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) {
|
||||||
|
//object? value;
|
||||||
|
|
||||||
//Get the DNAFieldAttribute of the current field
|
//Get the DNAFieldAttribute of the current field
|
||||||
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
|
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
|
||||||
if (attrib == null) continue; //should never happen, but means a field has no metadata
|
if (attrib == null) continue; //should never happen, but means a field has no metadata
|
||||||
@@ -161,7 +168,8 @@ public class Reader {
|
|||||||
//Convert the data to the correct type
|
//Convert the data to the correct type
|
||||||
object? value = ConvertFieldData(data, attrib.OriginalType);
|
object? value = ConvertFieldData(data, attrib.OriginalType);
|
||||||
|
|
||||||
if(value == null){ //if the data could not be converted
|
if (value == null) {
|
||||||
|
//if the data could not be converted
|
||||||
//Check if the field is a pointer to another DNA structure
|
//Check if the field is a pointer to another DNA structure
|
||||||
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
|
||||||
//Create a new instance of the DNA structure type
|
//Create a new instance of the DNA structure type
|
||||||
@@ -181,24 +189,40 @@ 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
|
||||||
IntPtr memAddr = data.ToPointer();
|
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.ToPointer() + offset, data.ToPointer());
|
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue; //should never happen, but means the data could not be converted
|
if (attrib == null) {
|
||||||
|
//not a normal field
|
||||||
|
var listAttrib = field.GetCustomAttribute<DNAListAttribute>();
|
||||||
|
if (listAttrib == null) continue; //not a list field either, this should never happen...
|
||||||
|
ConvertListField(); //Convert the list field
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
value = ConvertNormalField(block, field, attrib);
|
||||||
|
|
||||||
//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.ToPointer() + startOffset, obj!.GetType()), obj!);
|
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
|
||||||
}
|
}
|
||||||
|
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
|
||||||
|
}
|
||||||
|
|
||||||
private object? ConvertFieldData(byte[] data, string type)
|
private object? ConvertNormalField(FileBlock block, FieldInfo field, DNAFieldAttribute attrib) {
|
||||||
{
|
return null;
|
||||||
return type switch
|
}
|
||||||
{
|
|
||||||
|
private object? ConvertListField(FileBlock block, FieldInfo field, DNAFieldAttribute attrib) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object? ConvertFieldData(byte[] data, string type) {
|
||||||
|
return type switch {
|
||||||
"char" => (char)data[0],
|
"char" => (char)data[0],
|
||||||
"short" => BitConverter.ToInt16(data, 0),
|
"short" => BitConverter.ToInt16(data, 0),
|
||||||
"int" => BitConverter.ToInt32(data, 0),
|
"int" => BitConverter.ToInt32(data, 0),
|
||||||
|
|||||||
Reference in New Issue
Block a user