handling of array of pointers, refactoring, fixes n shit
This commit is contained in:
@@ -119,7 +119,11 @@ public class Reader {
|
|||||||
// get the fields of the object
|
// get the fields of the object
|
||||||
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
|
FieldInfo[] fieldInfo = obj.Value.GetType().GetFields();
|
||||||
// get all fields that are pointers
|
// 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
|
// for each pointer field
|
||||||
foreach (var f in list) {
|
foreach (var f in list) {
|
||||||
@@ -166,7 +170,7 @@ public class Reader {
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private long GetFieldDataOffset(int fieldIndex, FieldInfo[] fieldMetadata) =>
|
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;
|
.GetCustomAttribute<DNAFieldAttribute>()!.MemoryOffset;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -210,12 +214,9 @@ public class Reader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
field.SetValue(obj, value);
|
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
|
//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>
|
/// <summary>
|
||||||
@@ -278,43 +279,106 @@ public class Reader {
|
|||||||
return null;
|
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) {
|
private object? ConvertArrayField(FileBlock block, FieldInfo field, DNAArrayAttribute arrayAttribute, IntPtr startOffset) {
|
||||||
//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 = arrayAttribute.MemoryOffset + startOffset;
|
IntPtr offset = arrayAttribute.MemoryOffset + startOffset;
|
||||||
|
|
||||||
//Grab data size, create a container and copy the data from the block to the container
|
//Grab data size, create a container and copy the data from the block to the container
|
||||||
int size = arrayAttribute.Size;
|
var sizes = arrayAttribute.OriginalName.GetArrayDimensions();
|
||||||
var data = new byte[size];
|
int dataLength = arrayAttribute.Size;
|
||||||
Array.Copy((byte[])block.Body, offset, data, 0, size);
|
var data = new byte[dataLength];
|
||||||
|
Array.Copy((byte[])block.Body, offset, data, 0, dataLength);
|
||||||
int itemLenght = arrayAttribute.OriginalType.ParseFSize();
|
|
||||||
//Gather Array type
|
//Gather Array type
|
||||||
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType());
|
var type = Type.GetType(arrayAttribute.OriginalType.ParseFType()) ?? dnaTypesDb[arrayAttribute.OriginalType];
|
||||||
if (type == null) throw new NotSupportedException($"Type \"{arrayAttribute.UnderlyingType}\" is unknown");
|
if (type == null) throw new NotSupportedException($"Unknown type \"{arrayAttribute.OriginalType}\"");
|
||||||
//Create the array
|
|
||||||
var array = Array.CreateInstance(type, arrayAttribute.Size);
|
if(arrayAttribute.IsPointer){
|
||||||
|
return FillPointersArray(arrayAttribute, data, type, sizes);
|
||||||
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 (!dnaTypesDb.ContainsKey(arrayAttribute.OriginalType)) {
|
||||||
|
|
||||||
return array;
|
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) {
|
private object? ConvertListField(FileBlock block, FieldInfo field, DNAListAttribute attrib, IntPtr startOffset) {
|
||||||
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
|
IntPtr countOffset = attrib.CountMemoryOffset + startOffset;
|
||||||
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
|
IntPtr ptrOffset = attrib.PtrMemoryOffset + startOffset;
|
||||||
|
|
||||||
var countLen = attrib.CountFieldName.ParseFSize();
|
var countLen = attrib.CountFieldName.ParseFSize()!.Value;
|
||||||
var countData = new byte[countLen];
|
var countData = new byte[countLen];
|
||||||
Array.Copy((byte[])block.Body, countOffset, countData, 0, countLen);
|
Array.Copy((byte[])block.Body, countOffset, countData, 0, countLen);
|
||||||
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);
|
var tmpCount = ConvertFieldData(countData, attrib.CountFieldName);
|
||||||
|
|||||||
Reference in New Issue
Block a user