handling of array of pointers, refactoring, fixes n shit

This commit is contained in:
mm00
2025-03-12 19:02:25 +01:00
parent 934c878878
commit 226f001c78

View File

@@ -119,7 +119,11 @@ public class Reader {
// get the fields of the object
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
// get all fields that are pointers
var list = fieldInfo.Where(fldInfo => fldInfo.GetCustomAttribute<DNAFieldAttribute>()!.IsPointer).ToList();
var list = fieldInfo.Where(fldInfo =>
fldInfo.GetCustomAttributes().OfType<DNAFieldAttribute>().FirstOrDefault()?.IsPointer ?? false).ToList();
list.AddRange(fieldInfo.Where(fldInfo =>
fldInfo.GetCustomAttributes().OfType<DNAArrayAttribute>().FirstOrDefault()?.IsPointer ?? false));
// for each pointer field
foreach (var f in list) {
@@ -166,7 +170,7 @@ public class Reader {
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private long GetFieldDataOffset(int fieldIndex, FieldInfo[] fieldMetadata) =>
fieldMetadata.First(x => x.GetCustomAttribute<DNAFieldAttribute>()!.OriginalIndex == fieldIndex)
fieldMetadata.First(x => x.GetCustomAttribute<DNAAttribute>()!.OriginalIndex == fieldIndex)
.GetCustomAttribute<DNAFieldAttribute>()!.MemoryOffset;
/// <summary>
@@ -210,12 +214,9 @@ public class Reader {
}
field.SetValue(obj, value);
//Add the freshly handled object to the database
objects.TryAdd((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
}
//Add the freshly handled object to the database
objects.TryAdd((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
objects.Add((block.MemAddr.ToPointer() + startOffset, obj!.GetType()), obj!);
}
/// <summary>
@@ -278,43 +279,106 @@ public class Reader {
return null;
}
private static int[] CalculateArrayIndices(List<int> sizes, int index)
{
int[] indexArray = new int[sizes.Count];
for (int k = 0; k < sizes.Count; k++) {
indexArray[k] = index/sizes[(k+1) ..].Aggregate(1, (a, b) => a * b) % sizes[k];
}
return indexArray;
}
private Array FillPointersArray(DNAArrayAttribute arrayAttribute, byte[] data,
Type type, List<int> sizes)
{
//Create the array
var array = Array.CreateInstance(type, sizes.ToArray());
for (int i = 0; i < arrayAttribute.Size; i += 8) {
var itemData = new byte[8];
Array.Copy(data, i, itemData, 0, 8);
object? cellData = ActivateInstance(type);
FillObject(itemData.ToPointer(), ref cellData, type.GetFields());
var indexArray = CalculateArrayIndices(sizes, i);
array.SetValue(cellData, indexArray);
}
return array;
}
private Array FillNormalTypeArray(DNAArrayAttribute arrayAttribute, byte[] data, FileBlock block, Type type, List<int> sizes, int itemLenght, int offset)
{
var array = Array.CreateInstance(type, sizes.ToArray());
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
var itemData = new byte[itemLenght];
Array.Copy(data, i, itemData, 0, itemLenght);
object? cellData = ConvertFieldData(itemData, arrayAttribute.OriginalType);
var indexArray = CalculateArrayIndices(sizes, i);
array.SetValue(cellData, indexArray);
}
return array;
}
private Array FillCustomTypeArray(DNAArrayAttribute arrayAttribute, byte[] data, FileBlock block, Type type, List<int> sizes, int itemLenght, int offset)
{
var array = Array.CreateInstance(type, sizes.ToArray());
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
var itemData = new byte[itemLenght];
Array.Copy(data, i, itemData, 0, itemLenght);
object? cellData = ActivateInstance(type);
FillObject(block, ref cellData, type.GetFields(), offset + i);
var indexArray = CalculateArrayIndices(sizes, i);
array.SetValue(cellData, indexArray);
}
return array;
}
private object? ConvertArrayField(FileBlock block, FieldInfo field, DNAArrayAttribute arrayAttribute, IntPtr startOffset) {
//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 = arrayAttribute.MemoryOffset + startOffset;
//Grab data size, create a container and copy the data from the block to the container
int size = arrayAttribute.Size;
var data = new byte[size];
Array.Copy((byte[])block.Body, offset, data, 0, size);
var sizes = arrayAttribute.OriginalName.GetArrayDimensions();
int dataLength = arrayAttribute.Size;
var data = new byte[dataLength];
Array.Copy((byte[])block.Body, offset, data, 0, dataLength);
int itemLenght = arrayAttribute.OriginalType.ParseFSize();
//Gather Array type
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType());
if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown");
//Create the array
var array = Array.CreateInstance(type, arrayAttribute.Size);
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType()) ?? dnaTypesDb[arrayAttribute.OriginalType];
if (type == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\"");
for (int i = 0; i < arrayAttribute.Size; i += itemLenght) {
var itemData = new byte[itemLenght];
Array.Copy(data, i, itemData, 0, itemLenght);
object? cellData = ConvertFieldData(itemData, arrayAttribute.OriginalType);
if (cellData == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\"");
var cellType = Type.GetType(arrayAttribute.OriginalType.ParseFType());
//check that cellData is the correct type
if (cellData.GetType() != cellType)
throw new NotSupportedException($"Array type mismatch. Expected {cellType} got {cellData.GetType()}");
array.SetValue(cellData, i / itemLenght);
if(arrayAttribute.IsPointer){
return FillPointersArray(arrayAttribute, data, type, sizes);
}
return array;
if (!dnaTypesDb.ContainsKey(arrayAttribute.OriginalType)) {
return FillNormalTypeArray(arrayAttribute, data, block, type, sizes, arrayAttribute.OriginalType.ParseFSize()!.Value, offset.ToInt32());
}
int itemLenght = type.GetCustomAttribute<DNAFieldAttribute>()?.Size ??
type.GetCustomAttribute<DNAClassAttribute>()!.Size;
return FillCustomTypeArray(arrayAttribute, data, block, type, sizes, itemLenght, offset.ToInt32());
}
private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
var countLen = attrib.CountFieldName.ParseFSize();
var countLen = attrib.CountFieldName.ParseFSize()!.Value;
var countData = new byte[countLen];
Array.Copy((byte[])block.Body, countOffset, countData, 0, countLen);
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);