Compare commits

...

3 Commits

4 changed files with 129 additions and 51 deletions

View File

@@ -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);

View File

@@ -5,6 +5,7 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fee3c9d264411441f56a12bac52a58ad25b495ef52e59b4a86f8478158f7d_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fee3c9d264411441f56a12bac52a58ad25b495ef52e59b4a86f8478158f7d_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F51b332f64116ca1bf68931ca13adc3cd38b6dd2da04af6aef46bd08d218e58b5_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002ECoreCLR_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F51b332f64116ca1bf68931ca13adc3cd38b6dd2da04af6aef46bd08d218e58b5_003FArray_002ECoreCLR_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F96178db1709e872c391367fef1b31fca80f9f18119cdf4145a2650ac9f332ac_003FArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F96178db1709e872c391367fef1b31fca80f9f18119cdf4145a2650ac9f332ac_003FArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArray_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fdca79d6014ed19b63322d3503bc62e35795a5cb8d051f716b0141529a2964ec_003FArray_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeConstructor_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fbe433ced0c0d9b3aa171b7fbcd7df89ef13497cab9fb55640c7d49c891f_003FCodeConstructor_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeConstructor_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fbe433ced0c0d9b3aa171b7fbcd7df89ef13497cab9fb55640c7d49c891f_003FCodeConstructor_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeDomProvider_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4333c036d67eaa64623a27221cb821b663bc08410ac68797b1c10376d8379fe_003FCodeDomProvider_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeExpression_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F3127868066451f65e848b2ed6b9f84913de3cfb771a33c5f1bf5f1ba22c12e_003FCodeExpression_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACodeExpression_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F3127868066451f65e848b2ed6b9f84913de3cfb771a33c5f1bf5f1ba22c12e_003FCodeExpression_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
@@ -22,6 +23,8 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2c8e7ca976f350cba9836d5565dac56b11e0b56656fa786460eb1395857a6fa_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc7da56581ee7b20208f09e80b735961e4d5d7b9e5562bfdec94a75c57b391_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc7da56581ee7b20208f09e80b735961e4d5d7b9e5562bfdec94a75c57b391_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F3e_003F433607bb_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0beb96d31db641cf82014cb1a758a330b2dc00_003F3e_003F433607bb_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6780d13016c376c4491c5618b257d84da7eacf747ed2719783e775546b79b_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fffc196dcaa72b99bef7ac446415884fe13cbff486fc89fe87d78638ac873_003FThrowHelper_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe7dbd6fe331ff9a3c4b24dd470ec1f19a71b7c5acf258b81ae7f761cd2b319b_003FType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String> <s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AType_002Ecs_002Fl_003AC_0021_003FUsers_003Fairon_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fe7dbd6fe331ff9a3c4b24dd470ec1f19a71b7c5acf258b81ae7f761cd2b319b_003FType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:Boolean x:Key="/Default/Dpa/IsEnabledInDebug/@EntryValue">True</s:Boolean> <s:Boolean x:Key="/Default/Dpa/IsEnabledInDebug/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/Dpa/Thresholds/=AllocationClosure/@EntryIndexedValue">26214400</s:Int64> <s:Int64 x:Key="/Default/Dpa/Thresholds/=AllocationClosure/@EntryIndexedValue">26214400</s:Int64>

View File

@@ -241,7 +241,7 @@ namespace CodeGenerator {
else //Store the data for when the list attribute is made else //Store the data for when the list attribute is made
listCountOffsets.Add(fName, totalSize); listCountOffsets.Add(fName, totalSize);
totalSize += field.Type.ParseFSize(); totalSize += field.Type.ParseFSize()!.Value;
} else { } else {
Log($"Field {field.Name} is of unknown or unsupported type"); Log($"Field {field.Name} is of unknown or unsupported type");
} }
@@ -367,6 +367,7 @@ namespace CodeGenerator {
.AddAutoProperty(typeof(string), "OriginalName") .AddAutoProperty(typeof(string), "OriginalName")
.AddAutoProperty(typeof(string), "UnderlyingType") .AddAutoProperty(typeof(string), "UnderlyingType")
.AddAutoProperty(typeof(int), "ArrayLenght") .AddAutoProperty(typeof(int), "ArrayLenght")
.AddAutoProperty(typeof(bool), "IsPointer")
.AddAutoProperty(typeof(int), "MemoryOffset") .AddAutoProperty(typeof(int), "MemoryOffset")
.AddPropertiesConstructor() .AddPropertiesConstructor()
.AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) .AddBaseConstructorParams(["OriginalIndex", "OriginalName"])
@@ -427,10 +428,16 @@ namespace CodeGenerator {
private static CodeAttributeDeclaration GenerateDnaArrayAttribute(int index, DnaField field, Dna1Body body, int offset, private static CodeAttributeDeclaration GenerateDnaArrayAttribute(int index, DnaField field, Dna1Body body, int offset,
out int size) { out int size) {
var isPointer = false;
//Grab the lenght of the single item in the array //Grab the lenght of the single item in the array
size = body.Lengths[field.IdxType]; size = body.Lengths[field.IdxType];
//Generate the array declaration again... to grab the base type //Generate the array declaration again... to grab the base type
CodeMemberField amf = CreateArrayMemberField(field); CodeMemberField amf = CreateArrayMemberField(field);
if (field.Name.StartsWith('*')) {
size = 8;
isPointer = true;
}
//Generate the type string //Generate the type string
var sb = new StringBuilder(); var sb = new StringBuilder();
@@ -442,10 +449,10 @@ namespace CodeGenerator {
sb.Append(']'); sb.Append(']');
var t = sb.ToString(); var t = sb.ToString();
var dimensions = GetArrayDimensions(field.Name); var dimensions = field.Name.GetArrayDimensions();
int length = 0; int length = 1;
foreach (int dim in dimensions) { foreach (int dim in dimensions) {
length += dim; length *= dim;
size *= dim; size *= dim;
} }
@@ -457,6 +464,7 @@ namespace CodeGenerator {
new(new CodePrimitiveExpression(field.Name)), new(new CodePrimitiveExpression(field.Name)),
new(new CodePrimitiveExpression(t)), new(new CodePrimitiveExpression(t)),
new(new CodePrimitiveExpression(length)), new(new CodePrimitiveExpression(length)),
new(new CodePrimitiveExpression(isPointer)),
new(new CodePrimitiveExpression(offset)) new(new CodePrimitiveExpression(offset))
}); });
return cad; return cad;
@@ -500,7 +508,7 @@ namespace CodeGenerator {
// Parse all array dimensions // Parse all array dimensions
var name = field.Name.ParseFName(); var name = field.Name.ParseFName();
var dimensions = GetArrayDimensions(name); var dimensions = name.GetArrayDimensions();
// Get clean field name (without array brackets) // Get clean field name (without array brackets)
name = field.Name.Substring(0, field.Name.IndexOf('[')).ParseFName(); name = field.Name.Substring(0, field.Name.IndexOf('[')).ParseFName();
@@ -542,22 +550,6 @@ namespace CodeGenerator {
return cmf; return cmf;
} }
private static List<int> GetArrayDimensions(string name) {
var dimensions = new List<int>();
int startIndex = 0;
// Get all array dimensions
while ((startIndex = name.IndexOf('[', startIndex)) != -1) {
int endIndex = name.IndexOf(']', startIndex);
string sizeStr = name.Substring(startIndex + 1, endIndex - startIndex - 1);
if (int.TryParse(sizeStr, out int size)) {
dimensions.Add(size);
}
startIndex = endIndex + 1;
}
return dimensions;
}
private static CodeExpression GenerateArrayInitExpression(CodeTypeReference type, IEnumerable<int> dimensions) { private static CodeExpression GenerateArrayInitExpression(CodeTypeReference type, IEnumerable<int> dimensions) {
var dimValues = dimensions as int[] ?? dimensions.ToArray(); var dimValues = dimensions as int[] ?? dimensions.ToArray();
string dims = string.Concat(dimValues.Take(dimValues.Count() - 1).Select(d => $"{d},")); string dims = string.Concat(dimValues.Take(dimValues.Count() - 1).Select(d => $"{d},"));

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace CodeGenerator { namespace CodeGenerator {
@@ -28,7 +29,7 @@ namespace CodeGenerator {
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ParseFSize(this string str) { public static int? ParseFSize(this string str) {
return str switch { return str switch {
"char" => sizeof(sbyte), "char" => sizeof(sbyte),
"short" => sizeof(short), "short" => sizeof(short),
@@ -40,8 +41,26 @@ namespace CodeGenerator {
"int64_t" => sizeof(long), "int64_t" => sizeof(long),
"int8_t" => sizeof(sbyte), "int8_t" => sizeof(sbyte),
"uint64_t" => sizeof(ulong), "uint64_t" => sizeof(ulong),
_ => throw new("Unknown type") _ => null
}; };
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static List<int> GetArrayDimensions(this string name) {
var dimensions = new List<int>();
int startIndex = 0;
// Get all array dimensions
while ((startIndex = name.IndexOf('[', startIndex)) != -1) {
int endIndex = name.IndexOf(']', startIndex);
string sizeStr = name.Substring(startIndex + 1, endIndex - startIndex - 1);
if (int.TryParse(sizeStr, out int size)) {
dimensions.Add(size);
}
startIndex = endIndex + 1;
}
return dimensions;
}
} }
} }