Moved pointer and struct conversion to a separate method

This commit is contained in:
Samuele Lorefice
2025-03-06 18:32:21 +01:00
parent 02aa7db319
commit 0bc7f73aee

View File

@@ -15,6 +15,8 @@ public class Reader {
private Dictionary<(IntPtr, Type), object> objects = new(); private Dictionary<(IntPtr, Type), object> objects = new();
public Dictionary<(IntPtr, Type), object> Objects => objects; public Dictionary<(IntPtr, Type), object> Objects => objects;
public Dictionary<IntPtr, object> InstantiatedObjects = new();
/// <summary> /// <summary>
/// A dictionary that contains pointers to pointers /// A dictionary that contains pointers to pointers
/// </summary> /// </summary>
@@ -140,6 +142,9 @@ public class Reader {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private object? ActivateInstance(string type) => private object? ActivateInstance(string type) =>
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null; dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private object? ActivateInstance(Type type) => Activator.CreateInstance(type);
/// <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
@@ -152,72 +157,70 @@ public class Reader {
IntPtr startOffset = 0) { 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; 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<DNAAttribute>();
if (attrib == null) continue; //should never happen, but means a field has no metadata switch (attrib) {
case DNAFieldAttribute fieldAttribute:
//Calculate the offset from where the data of the field starts. value = ConvertNormalField(block, fieldAttribute);
//Because the order of the fields is not guaranteed we need to compute it each time break;
IntPtr offset = attrib.MemoryOffset; case DNAListAttribute listAttribute:
value = ConvertListField(block, field, listAttribute);
int size = attrib.Size; break;
var data = new byte[size]; default:
Array.Copy((byte[])block.Body, offset, data, 0, size); continue; //should never happen.
//Convert the data to the correct type
object? value = ConvertFieldData(data, attrib.OriginalType);
if (value == null) {
//if the data could not be converted
//Check if the field is a pointer to another DNA structure
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
//Create a new instance of the DNA structure type
object? newObj = ActivateInstance(attrib.OriginalType);
if (newObj == null) continue; //should never happen... type is missing?
//Get the information of the fields of the new object
var fieldInfo = newObj.GetType().GetFields();
//If the field is not a pointer, we need to dereference it
if (!attrib.IsPointer) {
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);
continue;
}
//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
IntPtr memAddr = data.ToPointer();
if (memAddr == 0) continue; //nullPointer, no need to store the reference
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
}
}
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.
field.SetValue(obj, value);
} }
field.SetValue(obj, value);
//Add the freshly handled object to the database
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!); objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
} }
//Add the freshly handled object to the database
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!); objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
} }
private object? ConvertNormalField(FileBlock block, FieldInfo field, DNAFieldAttribute attrib) { private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib) {
return null; //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
IntPtr offset = attrib.MemoryOffset;
//Grab data size, create a container and copy the data from the block to the container
int size = attrib.Size;
var data = new byte[size];
Array.Copy((byte[])block.Body, offset, data, 0, size);
//Convert the data to the correct type if it's a base type
object? value = ConvertFieldData(data, attrib.OriginalType);
if (value != null) return value;
//Check if the field is a pointer to another DNA structure
if (dnaTypesDb.ContainsKey(attrib.OriginalType)) {
if (!attrib.IsPointer) { //It's a structure
//Create a new instance of the DNA structure type
object? newObj = ActivateInstance(attrib.OriginalType);
//should never happen... type is missing?
if (newObj == null) throw new NotSupportedException($"Type \"{attrib.OriginalType}\" is unknown");
//Get the information of the fields of the new object
var fieldInfo = newObj.GetType().GetFields();
//relative offset to this block for the structure
IntPtr relAddr = block.MemAddr.ToPointer() + offset;
//If the object is already created, we can just assign it
if (objects.TryGetValue((relAddr, newObj.GetType()), out object? o)) return o;
//Fill the object with the data from the block (this is recursive)
FillObject(block, ref newObj, fieldInfo, offset);
} else { //It's a pointer
IntPtr memAddr = data.ToPointer();
if (memAddr == 0) return null; //nullPointer, no need to store the reference
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
}
}
throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\"");
} }
private object? ConvertListField(FileBlock block, FieldInfo field, DNAFieldAttribute attrib) { private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib) {
return null; return null;
} }