Integrated DnaArrayAtribute generation in the generator

This commit is contained in:
Samuele Lorefice
2025-03-11 19:08:47 +01:00
parent 0bf0aeab09
commit 0fee1f10d2

View File

@@ -135,22 +135,13 @@ namespace CodeGenerator {
return size; return size;
} }
private static int AddArrayField(ref CodeTypeDeclaration ctd, DnaField field, int index, int totalSize) private static int AddArrayField(ref CodeTypeDeclaration ctd, DnaField field, int index, int totalSize) {
{
Log($"Generating Array field {field.Name}"); Log($"Generating Array field {field.Name}");
var cmf = CreateArrayMemberField(field); var cmf = CreateArrayMemberField(field);
var attribute = new CodeAttributeDeclaration("DNAArrayAttribute"); var attribute = GenerateDnaArrayAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
attribute.Arguments.AddRange(new CodeAttributeArgumentCollection() {
new(new CodePrimitiveExpression(totalSize)),
new(new CodePrimitiveExpression(field.Type)),
new(new CodePrimitiveExpression(index)),
new(new CodePrimitiveExpression(field.Name)),
new(new CodePrimitiveExpression(field.Type)),
new(new CodePrimitiveExpression(0))
});
cmf.CustomAttributes.Add(attribute); cmf.CustomAttributes.Add(attribute);
ctd.Members.Add(cmf); ctd.Members.Add(cmf);
return 0; return size;
} }
private static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, int listLenghtOffset, private static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, int listLenghtOffset,
@@ -202,8 +193,10 @@ namespace CodeGenerator {
ns.Types.Add(ctd); ns.Types.Add(ctd);
//TODO: when encountering a list, run trough the fields to find a count/lenght or similar data. //TODO: when encountering a list, run trough the fields to find a count/lenght or similar data.
// ReSharper disable InlineOutVariableDeclaration
List<DnaField> normalFields; //Fields that are not lists nor lengths of lists List<DnaField> normalFields; //Fields that are not lists nor lengths of lists
List<(DnaField, DnaField)> listFields; //Fields that are lists, and their corresponding length fields List<(DnaField, DnaField)> listFields; //Fields that are lists, and their corresponding length fields
// ReSharper restore InlineOutVariableDeclaration
//filter the fields we want to include in the class minus the lists //filter the fields we want to include in the class minus the lists
FilterFields(type.Fields, out normalFields, out listFields); FilterFields(type.Fields, out normalFields, out listFields);
@@ -219,7 +212,7 @@ namespace CodeGenerator {
//Check if the field is a normal field or a list field //Check if the field is a normal field or a list field
if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field)) { if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field)) {
//check if the field is an array //check if the field is an array
if (field.Name.Contains("[")) if (field.Name.Contains('['))
totalSize += AddArrayField(ref ctd, field, index, totalSize); totalSize += AddArrayField(ref ctd, field, index, totalSize);
else else
totalSize += AddNormalField(field, ref ctd, index, totalSize); totalSize += AddNormalField(field, ref ctd, index, totalSize);
@@ -411,11 +404,9 @@ namespace CodeGenerator {
//TODO: use AttributeBuilder inside here //TODO: use AttributeBuilder inside here
private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, DnaField field, Dna1Body body, int offset, private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, DnaField field, Dna1Body body, int offset,
out int size) { out int size) {
var isPointer = false;
string t;
size = body.Lengths[field.IdxType]; size = body.Lengths[field.IdxType];
string t = field.Type;
bool isPointer = false;
if (field.Name.StartsWith('*')) if (field.Name.StartsWith('*'))
{ {
@@ -423,27 +414,6 @@ namespace CodeGenerator {
isPointer = true; isPointer = true;
} }
if (field.Name.Contains('[')) {
CodeMemberField amf = CreateArrayMemberField(field);
var sb = new StringBuilder();
sb.Append(amf.Type.BaseType);
sb.Append("[");
for(int i=1; i<amf.Type.ArrayRank; i++) {
sb.Append(",");
}
sb.Append("]");
t = sb.ToString();
var dimensions = GetArrayDimensions(field.Name);
foreach(var dim in dimensions) {
size *= dim;
}
} else
{
t = field.Type;
}
CodeAttributeDeclaration cad = new("DNAFieldAttribute"); CodeAttributeDeclaration cad = new("DNAFieldAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() { cad.Arguments.AddRange(new CodeAttributeArgumentCollection() {
new(new CodePrimitiveExpression(size)), new(new CodePrimitiveExpression(size)),
@@ -457,6 +427,43 @@ namespace CodeGenerator {
return cad; return cad;
} }
private static CodeAttributeDeclaration GenerateDnaArrayAttribute(int index, DnaField field, Dna1Body body, int offset,
out int size) {
//Grab the lenght of the single item in the array
size = body.Lengths[field.IdxType];
//Generate the array declaration again... to grab the base type
CodeMemberField amf = CreateArrayMemberField(field);
//Generate the type string
var sb = new StringBuilder();
sb.Append(amf.Type.BaseType);
sb.Append('[');
for(int i=1; i<amf.Type.ArrayRank; i++) {
sb.Append(',');
}
sb.Append(']');
var t = sb.ToString();
var dimensions = GetArrayDimensions(field.Name);
int length = 0;
foreach(int dim in dimensions) {
length += dim;
size *= dim;
}
CodeAttributeDeclaration cad = new("DNAArrayAttribute");
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() {
new(new CodePrimitiveExpression(size)),
new(new CodePrimitiveExpression(field.Type)),
new(new CodePrimitiveExpression(index)),
new(new CodePrimitiveExpression(field.Name)),
new(new CodePrimitiveExpression(t)),
new(new CodePrimitiveExpression(length)),
new(new CodePrimitiveExpression(offset))
});
return cad;
}
private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, DnaField listField, int lenghtIndex, private static CodeAttributeDeclaration GenerateDnaListAttribute(int listIndex, DnaField listField, int lenghtIndex,
DnaField lenghtField, int ptrOffset, int countOffset, out int size) { DnaField lenghtField, int ptrOffset, int countOffset, out int size) {