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>
@@ -141,6 +143,9 @@ public class Reader {
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
/// </summary> /// </summary>
@@ -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:
value = ConvertNormalField(block, fieldAttribute);
break;
case DNAListAttribute listAttribute:
value = ConvertListField(block, field, listAttribute);
break;
default:
continue; //should never happen.
}
field.SetValue(obj, value);
//Add the freshly handled object to the database
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!);
}
private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib) {
//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
IntPtr offset = attrib.MemoryOffset; IntPtr offset = attrib.MemoryOffset;
//Grab data size, create a container and copy the data from the block to the container
int size = attrib.Size; int size = attrib.Size;
var data = new byte[size]; var data = new byte[size];
Array.Copy((byte[])block.Body, offset, data, 0, size); Array.Copy((byte[])block.Body, offset, data, 0, size);
//Convert the data to the correct type
object? value = ConvertFieldData(data, attrib.OriginalType);
if (value == null) { //Convert the data to the correct type if it's a base type
//if the data could not be converted object? value = ConvertFieldData(data, attrib.OriginalType);
if (value != null) return value;
//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)) {
if (!attrib.IsPointer) { //It's a structure
//Create a new instance of the DNA structure type //Create a new instance of the DNA structure type
object? newObj = ActivateInstance(attrib.OriginalType); object? newObj = ActivateInstance(attrib.OriginalType);
if (newObj == null) continue; //should never happen... type is missing? //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 //Get the information of the fields of the new object
var fieldInfo = newObj.GetType().GetFields(); var fieldInfo = newObj.GetType().GetFields();
//relative offset to this block for the structure
//If the field is not a pointer, we need to dereference it
if (!attrib.IsPointer) {
IntPtr relAddr = block.MemAddr.ToPointer() + 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 //If the object is already created, we can just assign it
field.SetValue(obj, o); if (objects.TryGetValue((relAddr, newObj.GetType()), out object? o)) return o;
continue;
}
//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 { } else { //It's a pointer
// 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) return null; //nullPointer, no need to store the reference
pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer()); pointers.TryAdd(block.MemAddr.ToPointer() + offset, data.ToPointer());
} }
} }
if (attrib == null) { throw new NotSupportedException($"Unknown type \"{attrib.OriginalType}\"");
//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);
}
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? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib) {
return null;
}
private object? ConvertListField(FileBlock block, FieldInfo field, DNAFieldAttribute attrib) {
return null; return null;
} }