Made AttributeBuilder not a singleton, replaced multiple methods per attribute with a single method that returns all the attributes in a single sweep.

This commit is contained in:
Samuele Lorefice
2025-02-26 17:03:15 +01:00
parent 4166482022
commit 869fd3b794
2 changed files with 38 additions and 33 deletions

View File

@@ -5,23 +5,29 @@ using System.Collections.Generic;
namespace CodeGenerator { namespace CodeGenerator {
public class AttributeBuilder { public class AttributeBuilder {
private static AttributeBuilder _instance;
public static AttributeBuilder Instance => _instance ??= new();
private CodeTypeDeclaration _attrDecl = new(); private CodeTypeDeclaration _attrDecl = new();
private List<(Type, string)> _fields = new(); private List<(Type, string)> _fields = new();
private List<(Type, string, string)> _properties = new(); private List<(Type, string, string)> _properties = new();
public AttributeBuilder New() { public AttributeBuilder() {
_attrDecl.IsClass = true; _attrDecl.IsClass = true;
_attrDecl.Attributes = MemberAttributes.Public; _attrDecl.Attributes = MemberAttributes.Public;
_attrDecl.BaseTypes.Add(typeof(Attribute)); _attrDecl.BaseTypes.Add(typeof(Attribute));
}
public AttributeBuilder(string name) : this() {
_attrDecl.Name = name;
}
public AttributeBuilder New() {
_attrDecl = new CodeTypeDeclaration();
_fields.Clear();
_properties.Clear();
return this; return this;
} }
public AttributeBuilder New(string name) { public AttributeBuilder SetName(string name) {
New();
_attrDecl.Name = name; _attrDecl.Name = name;
return this; return this;
} }

View File

@@ -54,10 +54,11 @@ namespace CodeGenerator {
} }
private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) { private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) {
//Initialize the namespaces
CodeNamespace rootNs = new CodeNamespace(Namespace); CodeNamespace rootNs = new CodeNamespace(Namespace);
rootNs.Types.Add(GenerateDnaFieldAttributeType());
rootNs.Types.Add(GenerateDnaClassAttributeType());
CodeNamespace ns = new CodeNamespace(Namespace+".DNA"); CodeNamespace ns = new CodeNamespace(Namespace+".DNA");
//Fill the attribute types then add them to the namespaces
rootNs.Types.AddRange(GenerateTypeDeclarations());
ns.Imports.Add(new(rootNs.Name)); ns.Imports.Add(new(rootNs.Name));
_customTypes = new(); _customTypes = new();
@@ -136,32 +137,30 @@ namespace CodeGenerator {
return ns; return ns;
} }
private static CodeTypeDeclaration GenerateDnaFieldAttributeType() { private static CodeTypeDeclaration[] GenerateTypeDeclarations() {
var attributeBuilder = AttributeBuilder.Instance; var attributeBuilder = new AttributeBuilder();
return attributeBuilder.New("DNAFieldAttribute") var typeDeclarations = new CodeTypeDeclaration[] {
.SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field"))) attributeBuilder.New().SetName("DNAFieldAttribute")
.AddAutoProperty(typeof(int), "Size") .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field")))
.AddAutoProperty(typeof(string), "OriginalType") .AddAutoProperty(typeof(int), "Size")
.AddAutoProperty(typeof(string), "OriginalName") .AddAutoProperty(typeof(string), "OriginalType")
.AddAutoProperty(typeof(int), "OriginalIndex") .AddAutoProperty(typeof(string), "OriginalName")
.AddAutoProperty(typeof(string), "UnderlyingType") .AddAutoProperty(typeof(int), "OriginalIndex")
.AddAutoProperty(typeof(bool), "IsPointer") .AddAutoProperty(typeof(string), "UnderlyingType")
.AddAutoProperty(typeof(int), "MemoryOffset") .AddAutoProperty(typeof(bool), "IsPointer")
.AddPropertiesConstructor() .AddAutoProperty(typeof(int), "MemoryOffset")
.Build(); .AddPropertiesConstructor()
} .Build(),
attributeBuilder.New().SetName("DNAClassAttribute")
private static CodeTypeDeclaration GenerateDnaClassAttributeType() { .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct")))
var attributeBuilder = AttributeBuilder.Instance; .AddAutoProperty(typeof(int), "OriginalIndex")
.AddAutoProperty(typeof(string), "OriginalName")
return attributeBuilder.New("DNAClassAttribute") .AddAutoProperty(typeof(int), "Size")
.SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct"))) .AddPropertiesConstructor()
.AddAutoProperty(typeof(int), "OriginalIndex") .Build()
.AddAutoProperty(typeof(string), "OriginalName") };
.AddAutoProperty(typeof(int), "Size") return typeDeclarations;
.AddPropertiesConstructor()
.Build();
} }
private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field, private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field,