From 02aa7db319c1960fdd6665c57ef0ec29535e1cc9 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Thu, 6 Mar 2025 17:37:55 +0100 Subject: [PATCH] Changed attribute builder to derive from correct class, also added method to pass base class parameters using string refs. Regenerated the files --- BlendFile/DNAClassAttribute.cs | 5 ++-- BlendFile/DNAFieldAttribute.cs | 5 ++-- BlendFile/DNAListAttribute.cs | 5 ++-- BlenderSharp.sln.DotSettings.user | 2 ++ CodeGenerator/AttributeBuilder.cs | 40 +++++++++++++++++++++++-------- CodeGenerator/Program.cs | 7 ++++-- 6 files changed, 46 insertions(+), 18 deletions(-) diff --git a/BlendFile/DNAClassAttribute.cs b/BlendFile/DNAClassAttribute.cs index 3523330..12f48da 100644 --- a/BlendFile/DNAClassAttribute.cs +++ b/BlendFile/DNAClassAttribute.cs @@ -13,7 +13,7 @@ using System; namespace BlendFile { [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Struct)] - public class DNAClassAttribute : System.Attribute { + public class DNAClassAttribute : BlendFile.DNAAttribute { private int _OriginalIndex; public virtual int OriginalIndex { get { @@ -41,7 +41,8 @@ namespace BlendFile { this._Size = value; } } - public DNAClassAttribute(int OriginalIndex, string OriginalName, int Size) { + public DNAClassAttribute(int OriginalIndex, string OriginalName, int Size) : + base(OriginalIndex, OriginalName) { this._OriginalIndex = OriginalIndex; this._OriginalName = OriginalName; this._Size = Size; diff --git a/BlendFile/DNAFieldAttribute.cs b/BlendFile/DNAFieldAttribute.cs index 79c19a2..1cb07df 100644 --- a/BlendFile/DNAFieldAttribute.cs +++ b/BlendFile/DNAFieldAttribute.cs @@ -13,7 +13,7 @@ using System; namespace BlendFile { [AttributeUsageAttribute(AttributeTargets.Field)] - public class DNAFieldAttribute : System.Attribute { + public class DNAFieldAttribute : BlendFile.DNAAttribute { private int _Size; public virtual int Size { get { @@ -77,7 +77,8 @@ namespace BlendFile { this._MemoryOffset = value; } } - public DNAFieldAttribute(int Size, string OriginalType, int OriginalIndex, string OriginalName, string UnderlyingType, bool IsPointer, int MemoryOffset) { + public DNAFieldAttribute(int Size, string OriginalType, int OriginalIndex, string OriginalName, string UnderlyingType, bool IsPointer, int MemoryOffset) : + base(OriginalIndex, OriginalName) { this._Size = Size; this._OriginalType = OriginalType; this._OriginalIndex = OriginalIndex; diff --git a/BlendFile/DNAListAttribute.cs b/BlendFile/DNAListAttribute.cs index 8098f3e..422529f 100644 --- a/BlendFile/DNAListAttribute.cs +++ b/BlendFile/DNAListAttribute.cs @@ -13,7 +13,7 @@ using System; namespace BlendFile { [AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field)] - public class DNAListAttribute : System.Attribute { + public class DNAListAttribute : BlendFile.DNAAttribute { private int _Size; public virtual int Size { get { @@ -86,7 +86,8 @@ namespace BlendFile { this._MemoryOffset = value; } } - public DNAListAttribute(int Size, string OriginalType, string OriginalName, int OriginalIndex, string UnderlyingType, string CountFieldName, int CountFieldIndex, int MemoryOffset) { + public DNAListAttribute(int Size, string OriginalType, string OriginalName, int OriginalIndex, string UnderlyingType, string CountFieldName, int CountFieldIndex, int MemoryOffset) : + base(OriginalIndex, OriginalName) { this._Size = Size; this._OriginalType = OriginalType; this._OriginalName = OriginalName; diff --git a/BlenderSharp.sln.DotSettings.user b/BlenderSharp.sln.DotSettings.user index a3f3dac..aa5ab42 100644 --- a/BlenderSharp.sln.DotSettings.user +++ b/BlenderSharp.sln.DotSettings.user @@ -3,7 +3,9 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/CodeGenerator/AttributeBuilder.cs b/CodeGenerator/AttributeBuilder.cs index 3210e03..53635a0 100644 --- a/CodeGenerator/AttributeBuilder.cs +++ b/CodeGenerator/AttributeBuilder.cs @@ -1,12 +1,14 @@ using System; using System.CodeDom; using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace CodeGenerator { // ReSharper disable always BitwiseOperatorOnEnumWithoutFlags public class AttributeBuilder { - private CodeTypeDeclaration _attrDecl = new(); + private CodeTypeDeclaration _attrDecl; private List<(Type, string)> _fields = new(); @@ -28,9 +30,11 @@ namespace CodeGenerator { /// Creates a new instance of the class. /// public AttributeBuilder() { - _attrDecl.IsClass = true; - _attrDecl.Attributes = MemberAttributes.Public; - _attrDecl.BaseTypes.Add(typeof(Attribute)); + _attrDecl = new() { + IsClass = true, + Attributes = MemberAttributes.Public, + BaseTypes = { typeof(Attribute) } + }; } /// @@ -47,7 +51,11 @@ namespace CodeGenerator { /// /// clears the internal metadata for fields and properties, also reinstantiates the internal instance public AttributeBuilder New() { - _attrDecl = new CodeTypeDeclaration(); + _attrDecl = new() { + IsClass = true, + Attributes = MemberAttributes.Public, + BaseTypes = { typeof(Attribute) } + }; _fields.Clear(); _properties.Clear(); return this; @@ -57,11 +65,8 @@ namespace CodeGenerator { /// Sets the base type of the attribute. /// /// Fully qualified name from which this is going to be derived - public AttributeBuilder DeriveFromClass(string baseType) { - if (_attrDecl.BaseTypes.Count == 0) - _attrDecl.BaseTypes.Add(typeof(Attribute)); - else - _attrDecl.BaseTypes[0] = new(baseType); + public AttributeBuilder DeriveFromClass(string baseType = "System.Attribute") { + _attrDecl.BaseTypes[0] = new(baseType); return this; } @@ -199,6 +204,21 @@ namespace CodeGenerator { }); return this; } + + //TODO: tyhis should use something more robust and less error prone than just strings + /// + /// Adds parameters to be passed to the base class + /// + /// Array of strings which reference the params + /// thrown when no constructor is defined yet. + public AttributeBuilder AddBaseConstructorParams(string[] parameters) { + var ctor = _attrDecl.Members.OfType().FirstOrDefault(); + if (ctor == null) throw new InvalidOperationException("There is no constructor defined yet."); + foreach (var param in parameters) { + ctor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(param)); + } + return this; + } /// /// Builds the instance. diff --git a/CodeGenerator/Program.cs b/CodeGenerator/Program.cs index bae59d2..cf5f6a0 100644 --- a/CodeGenerator/Program.cs +++ b/CodeGenerator/Program.cs @@ -300,10 +300,10 @@ namespace CodeGenerator { private static CodeTypeDeclaration[] GenerateTypeDeclarations() { var attributeBuilder = new AttributeBuilder(); - var typeDeclarations = new CodeTypeDeclaration[] { + var typeDeclarations = new[] { attributeBuilder.New().SetName("DNAAttribute") .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.All"))) - .DeriveFromClass("System.Attribute") + .DeriveFromClass() .AddAutoProperty(typeof(int), "OriginalIndex") .AddAutoProperty(typeof(string), "OriginalName") .AddPropertiesConstructor() @@ -319,6 +319,7 @@ namespace CodeGenerator { .AddAutoProperty(typeof(bool), "IsPointer") .AddAutoProperty(typeof(int), "MemoryOffset") .AddPropertiesConstructor() + .AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) .Build(), attributeBuilder.New().SetName("DNAClassAttribute") .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Class | AttributeTargets.Struct"))) @@ -327,6 +328,7 @@ namespace CodeGenerator { .AddAutoProperty(typeof(string), "OriginalName") .AddAutoProperty(typeof(int), "Size") .AddPropertiesConstructor() + .AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) .Build(), attributeBuilder.New().SetName("DNAListAttribute") .SetAttributeUsage(new (new CodeSnippetExpression("AttributeTargets.Property | AttributeTargets.Field"))) @@ -340,6 +342,7 @@ namespace CodeGenerator { .AddAutoProperty(typeof(int), "CountFieldIndex") .AddAutoProperty(typeof(int), "MemoryOffset") .AddPropertiesConstructor() + .AddBaseConstructorParams(["OriginalIndex", "OriginalName"]) .Build() }; return typeDeclarations;