Moved pointer and struct conversion to a separate method
This commit is contained in:
@@ -15,6 +15,8 @@ public class Reader {
|
||||
private Dictionary<(IntPtr, Type), object> objects = new();
|
||||
public Dictionary<(IntPtr, Type), object> Objects => objects;
|
||||
|
||||
public Dictionary<IntPtr, object> InstantiatedObjects = new();
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary that contains pointers to pointers
|
||||
/// </summary>
|
||||
@@ -141,6 +143,9 @@ public class Reader {
|
||||
private object? ActivateInstance(string type) =>
|
||||
dnaTypesDb.TryGetValue(type, out Type? t) ? Activator.CreateInstance(t) : null;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private object? ActivateInstance(Type type) => Activator.CreateInstance(type);
|
||||
|
||||
/// <summary>
|
||||
/// Filles a given object with the data from a block, starting to read it from the specified offset
|
||||
/// </summary>
|
||||
@@ -152,72 +157,70 @@ public class Reader {
|
||||
IntPtr startOffset = 0) {
|
||||
if (block.Code == "ENDB") return; // ENDB is a special block that does not contain any data
|
||||
foreach (var field in fieldMetadata) {
|
||||
//object? value;
|
||||
|
||||
object? value;
|
||||
//Get the DNAFieldAttribute of the current field
|
||||
var attrib = field.GetCustomAttribute<DNAFieldAttribute>();
|
||||
if (attrib == null) continue; //should never happen, but means a field has no metadata
|
||||
|
||||
//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;
|
||||
|
||||
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
|
||||
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);
|
||||
var attrib = field.GetCustomAttribute<DNAAttribute>();
|
||||
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, FieldInfo field, DNAFieldAttribute attrib) {
|
||||
return null;
|
||||
private object? ConvertNormalField(FileBlock block, DNAFieldAttribute attrib) {
|
||||
//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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user