Integrated DnaArrayAtribute generation in the generator
This commit is contained in:
@@ -135,22 +135,13 @@ namespace CodeGenerator {
|
||||
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}");
|
||||
var cmf = CreateArrayMemberField(field);
|
||||
var attribute = new CodeAttributeDeclaration("DNAArrayAttribute");
|
||||
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))
|
||||
});
|
||||
var attribute = GenerateDnaArrayAttribute(index, field, field.M_Parent.M_Parent, totalSize, out int size);
|
||||
cmf.CustomAttributes.Add(attribute);
|
||||
ctd.Members.Add(cmf);
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
private static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, int listLenghtOffset,
|
||||
@@ -202,8 +193,10 @@ namespace CodeGenerator {
|
||||
ns.Types.Add(ctd);
|
||||
|
||||
//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, 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
|
||||
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
|
||||
if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field)) {
|
||||
//check if the field is an array
|
||||
if (field.Name.Contains("["))
|
||||
if (field.Name.Contains('['))
|
||||
totalSize += AddArrayField(ref ctd, field, index, totalSize);
|
||||
else
|
||||
totalSize += AddNormalField(field, ref ctd, index, totalSize);
|
||||
@@ -411,11 +404,9 @@ namespace CodeGenerator {
|
||||
//TODO: use AttributeBuilder inside here
|
||||
private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, DnaField field, Dna1Body body, int offset,
|
||||
out int size) {
|
||||
|
||||
string t;
|
||||
var isPointer = false;
|
||||
size = body.Lengths[field.IdxType];
|
||||
|
||||
bool isPointer = false;
|
||||
string t = field.Type;
|
||||
|
||||
if (field.Name.StartsWith('*'))
|
||||
{
|
||||
@@ -423,27 +414,6 @@ namespace CodeGenerator {
|
||||
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");
|
||||
cad.Arguments.AddRange(new CodeAttributeArgumentCollection() {
|
||||
new(new CodePrimitiveExpression(size)),
|
||||
@@ -457,6 +427,43 @@ namespace CodeGenerator {
|
||||
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,
|
||||
DnaField lenghtField, int ptrOffset, int countOffset, out int size) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user