diff --git a/CodeGenerator/AttributeBuilder.cs b/CodeGenerator/AttributeBuilder.cs index 278a9d8..2ba97bc 100644 --- a/CodeGenerator/AttributeBuilder.cs +++ b/CodeGenerator/AttributeBuilder.cs @@ -5,23 +5,29 @@ using System.Collections.Generic; namespace CodeGenerator { public class AttributeBuilder { - private static AttributeBuilder _instance; - public static AttributeBuilder Instance => _instance ??= new(); - private CodeTypeDeclaration _attrDecl = new(); private List<(Type, string)> _fields = new(); private List<(Type, string, string)> _properties = new(); - public AttributeBuilder New() { + public AttributeBuilder() { _attrDecl.IsClass = true; _attrDecl.Attributes = MemberAttributes.Public; _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; } - public AttributeBuilder New(string name) { - New(); + public AttributeBuilder SetName(string name) { _attrDecl.Name = name; return this; } diff --git a/CodeGenerator/Program.cs b/CodeGenerator/Program.cs index dc7c32d..b313957 100644 --- a/CodeGenerator/Program.cs +++ b/CodeGenerator/Program.cs @@ -54,10 +54,11 @@ namespace CodeGenerator { } private static CodeNamespace GenerateTypes(out CodeNamespace additionalNs) { + //Initialize the namespaces CodeNamespace rootNs = new CodeNamespace(Namespace); - rootNs.Types.Add(GenerateDnaFieldAttributeType()); - rootNs.Types.Add(GenerateDnaClassAttributeType()); 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)); _customTypes = new(); @@ -136,32 +137,30 @@ namespace CodeGenerator { return ns; } - private static CodeTypeDeclaration GenerateDnaFieldAttributeType() { - var attributeBuilder = AttributeBuilder.Instance; + private static CodeTypeDeclaration[] GenerateTypeDeclarations() { + var attributeBuilder = new AttributeBuilder(); - return attributeBuilder.New("DNAFieldAttribute") - .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field"))) - .AddAutoProperty(typeof(int), "Size") - .AddAutoProperty(typeof(string), "OriginalType") - .AddAutoProperty(typeof(string), "OriginalName") - .AddAutoProperty(typeof(int), "OriginalIndex") - .AddAutoProperty(typeof(string), "UnderlyingType") - .AddAutoProperty(typeof(bool), "IsPointer") - .AddAutoProperty(typeof(int), "MemoryOffset") - .AddPropertiesConstructor() - .Build(); - } - - private static CodeTypeDeclaration GenerateDnaClassAttributeType() { - var attributeBuilder = AttributeBuilder.Instance; - - return attributeBuilder.New("DNAClassAttribute") - .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct"))) - .AddAutoProperty(typeof(int), "OriginalIndex") - .AddAutoProperty(typeof(string), "OriginalName") - .AddAutoProperty(typeof(int), "Size") - .AddPropertiesConstructor() - .Build(); + var typeDeclarations = new CodeTypeDeclaration[] { + attributeBuilder.New().SetName("DNAFieldAttribute") + .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Field"))) + .AddAutoProperty(typeof(int), "Size") + .AddAutoProperty(typeof(string), "OriginalType") + .AddAutoProperty(typeof(string), "OriginalName") + .AddAutoProperty(typeof(int), "OriginalIndex") + .AddAutoProperty(typeof(string), "UnderlyingType") + .AddAutoProperty(typeof(bool), "IsPointer") + .AddAutoProperty(typeof(int), "MemoryOffset") + .AddPropertiesConstructor() + .Build(), + attributeBuilder.New().SetName("DNAClassAttribute") + .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct"))) + .AddAutoProperty(typeof(int), "OriginalIndex") + .AddAutoProperty(typeof(string), "OriginalName") + .AddAutoProperty(typeof(int), "Size") + .AddPropertiesConstructor() + .Build() + }; + return typeDeclarations; } private static CodeAttributeDeclaration GenerateDnaFieldAttribute(int index, BlendFile.DnaField field,