From 8678ee6c85a35390651cde25021e80c54c3687fb Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Tue, 11 Mar 2025 18:44:09 +0100 Subject: [PATCH] Added DNAArrayAttribute to the generation and AddArrayField method. Disabled Array checking in AddNormalField and done Array checking at the main logic --- CodeGenerator/Program.cs | 50 +++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/CodeGenerator/Program.cs b/CodeGenerator/Program.cs index 4b9c264..db59f98 100644 --- a/CodeGenerator/Program.cs +++ b/CodeGenerator/Program.cs @@ -120,10 +120,10 @@ namespace CodeGenerator { CodeMemberField cmf; string name = field.Name; if (name.Contains("()")) return 0; - if (name.Contains("[")) { + /*if (name.Contains("[")) { Log($"Generating array field {field.Name}"); cmf = CreateArrayMemberField(field); - } + }*/ else { Log($"Generating field {field.Name}"); cmf = CreateMemberField(field); @@ -134,10 +134,27 @@ namespace CodeGenerator { ctd.Members.Add(cmf); return size; } - - public static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, - int listLenghtOffset, BlendFile.DnaField listPointer, BlendFile.DnaField listLength, int sizeIndex) + + 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)) + }); + cmf.CustomAttributes.Add(attribute); + ctd.Members.Add(cmf); + return 0; + } + + private static int AddListField(ref CodeTypeDeclaration ctd, int totalSize, int index, int listLenghtOffset, + DnaField listPointer, DnaField listLength, int sizeIndex) { var cmf = CreateListMemberField(listPointer, listLength); var attribute = GenerateDnaListAttribute(index, listPointer, sizeIndex, listLength, totalSize, @@ -199,9 +216,13 @@ namespace CodeGenerator { Log($"Fields: {type.Fields.Count}"); for (var index = 0; index < type.Fields.Count; index++) { var field = type.Fields[index]; - if (normalFields.Contains(field) && !listFields.Select(f => f.Item2).Contains(field)) - { - totalSize += AddNormalField(field, ref ctd, index, totalSize); + //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("[")) + totalSize += AddArrayField(ref ctd, field, index, totalSize); + else + totalSize += AddNormalField(field, ref ctd, index, totalSize); } else if (listFields.Select(f => f.Item1).Contains(field)) { //Retrieve the list pointer and the list length fields var (listPointer, listLength) = listFields.FirstOrDefault(x => x.Item1 == field); @@ -345,6 +366,19 @@ namespace CodeGenerator { .AddPropertiesConstructor() .AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) .Build(), + attributeBuilder.New().SetName("DNAArrayAttribute") + .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field"))) + .DeriveFromClass($"{Namespace}.DNAAttribute") + .AddAutoProperty(typeof(int), "Size") + .AddAutoProperty(typeof(string), "OriginalType") + .AddAutoProperty(typeof(int), "OriginalIndex") + .AddAutoProperty(typeof(string), "OriginalName") + .AddAutoProperty(typeof(string), "UnderlyingType") + .AddAutoProperty(typeof(int), "ArrayLenght") + .AddAutoProperty(typeof(int), "MemoryOffset") + .AddPropertiesConstructor() + .AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) + .Build(), attributeBuilder.New().SetName("DNAClassAttribute") .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct"))) .DeriveFromClass($"{Namespace}.DNAAttribute")