From 03817a4e21e9e96402aed0dd44803528c3af4a25 Mon Sep 17 00:00:00 2001
From: OMGeeky <39029799+OMGeeky@users.noreply.github.com>
Date: Sun, 31 Mar 2024 00:11:30 +0100
Subject: [PATCH] Remove UxmlTraitAttribute since it just works with unity 6
---
.../Unity/Ui/UIBackingClassGenerator.cs | 172 +-----------------
1 file changed, 3 insertions(+), 169 deletions(-)
diff --git a/ExampleGenerator/Unity/Ui/UIBackingClassGenerator.cs b/ExampleGenerator/Unity/Ui/UIBackingClassGenerator.cs
index a94cd5c..915203e 100644
--- a/ExampleGenerator/Unity/Ui/UIBackingClassGenerator.cs
+++ b/ExampleGenerator/Unity/Ui/UIBackingClassGenerator.cs
@@ -15,7 +15,6 @@ namespace ExampleGenerator.Unity.Ui
{
public static class Helpers
{
- public const string UxmlTraitAttribute = "UxmlTraitAttribute";
public const string UiElementAttribute = "UiElementAttribute";
internal static bool IsDerivedFrom( INamedTypeSymbol baseType , string targetType )
@@ -35,23 +34,6 @@ namespace ExampleGenerator.Unity.Ui
[Generator]
public class UiBackingClassGenerator : ISourceGenerator
{
- private static readonly string UxmlTraitAttributeText = $@"//
-using System;
-/// Helper attribute for UXML generation that generates the
-/// UxmlTrait definitions needed for the UIElements.
-///
-/// Works on properties and fields
-///
-/// When applied to a Property the uxml-fields only work and
-/// save if the property has a backing field. If its an auto
-/// property it won't save the changes in the UI-Builder and probably some other locations too.
-///
-[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
-internal class {Helpers.UxmlTraitAttribute} : Attribute
-{{
- public {Helpers.UxmlTraitAttribute}(string name, object defaultValue=null) {{ }}
-}}
-";
private static readonly string UiElementAttributeText = $@"//
using System;
@@ -69,9 +51,6 @@ internal class {Helpers.UiElementAttribute} : Attribute
{
context.RegisterForPostInitialization( i =>
{
- i.AddSource( $"{Helpers.UxmlTraitAttribute}_g.cs"
- , SourceText.From( UxmlTraitAttributeText , Encoding.UTF8 ) );
-
i.AddSource( $"{Helpers.UiElementAttribute}_g.cs"
, SourceText.From( UiElementAttributeText , Encoding.UTF8 ) );
} );
@@ -84,13 +63,12 @@ internal class {Helpers.UiElementAttribute} : Attribute
if ( !(context.SyntaxContextReceiver is SyntaxReceiver receiver) )
return;
- INamedTypeSymbol uxmlTraitAttributeSymbol = context.Compilation.GetTypeByMetadataName( Helpers.UxmlTraitAttribute );
INamedTypeSymbol uiElementAttributeSymbol = context.Compilation.GetTypeByMetadataName( Helpers.UiElementAttribute );
foreach ( IGrouping group in receiver.Fields
.GroupBy( f => f.ContainingType
, SymbolEqualityComparer.Default ) )
{
- var classSource = ProcessClass( group.Key , group , uxmlTraitAttributeSymbol , uiElementAttributeSymbol );
+ var classSource = ProcessClass( group.Key , group , uiElementAttributeSymbol );
if ( classSource == null )
continue;
@@ -100,7 +78,6 @@ internal class {Helpers.UiElementAttribute} : Attribute
private string ProcessClass( INamedTypeSymbol classSymbol
, IEnumerable fields
- , INamedTypeSymbol uxmlTraitAttributeSymbol
, INamedTypeSymbol uiElementAttributeSymbol )
{
var fieldsList = fields.ToList();
@@ -108,7 +85,6 @@ internal class {Helpers.UiElementAttribute} : Attribute
return null;
List elementFields = fieldsList.Where( f => GetUiElementAttributeData( f , uiElementAttributeSymbol ) != null ).ToList();
- List uxmlTraitFields = fieldsList.Where( f => GetUxmlTraitAttributeData( f , uxmlTraitAttributeSymbol ) != null ).ToList();
var source = new StringBuilder( $@"//
@@ -118,39 +94,6 @@ namespace {classSymbol.ContainingNamespace}
public partial class {classSymbol.Name}
{{" );
- if ( uxmlTraitFields.Any() )
- {
- source.Append( $@"
- public new partial class UxmlFactory : UxmlFactory<{classSymbol.Name}, UxmlTraits> {{ }}
- public new partial class UxmlTraits : VisualElement.UxmlTraits
- {{
-" );
-
- foreach ( ISymbol fieldSymbol in uxmlTraitFields )
- {
- source.AppendLine( GetAttributeDescription( fieldSymbol , uxmlTraitAttributeSymbol ) );
- }
-
- source.Append( $@"
- public override void Init(VisualElement ve , IUxmlAttributes bag , CreationContext cc )
- {{
- base.Init( ve , bag , cc );
- var self = ({classSymbol.Name}) ve;
-
-" );
-
- foreach ( ISymbol fieldSymbol in uxmlTraitFields )
- {
- source.AppendLine( GetAttributeInitialization( fieldSymbol ) );
- }
-
- source.Append( $@" }}
- }}" );
- }
- else
- {
- source.AppendLine( $" public new partial class UxmlFactory : UxmlFactory<{classSymbol.Name}> {{ }}" );
- }
source.Append( $@"
public void QueryElements()
{{
@@ -158,8 +101,6 @@ public partial class {classSymbol.Name}
foreach ( ISymbol fieldSymbol in elementFields )
{
- // source.AppendLine( $" {fieldSymbol.Name} = this.Q<{GetQualifyingTypeNameFromSymbol( fieldSymbol )}>(\"hi\");" );
-
source.AppendLine( $" {fieldSymbol.Name} = this.Q<{GetQualifyingTypeNameFromSymbol( fieldSymbol )}>(\"{GetUiElementAttributeData( fieldSymbol , uiElementAttributeSymbol )?.Name}\");" );
}
@@ -204,38 +145,6 @@ public partial class {classSymbol.Name}
};
}
- private static UxmlTraitAttributeData? GetUxmlTraitAttributeData( ISymbol fieldSymbol , INamedTypeSymbol uxmlTraitAttributeSymbol )
- {
- AttributeData attr = GetSingleAttributeData( fieldSymbol , uxmlTraitAttributeSymbol );
- if ( attr == null )
- return null;
-
- var args = attr.ConstructorArguments.ToList();
- if ( args.Count != 2 )
- {
- throw new NotImplementedException( $"Attribute did not have enough parameters: expected 2 got {args.Count} {attr}: args: {args}" );
- }
-
- var name = args[0].Value as string;
- var defaultValue = args[1].Value;
- if ( defaultValue != null )
- {
- defaultValue = defaultValue.ToString();
- if ( (string) defaultValue == "False" )
- defaultValue = "false";
-
- if ( (string) defaultValue == "True" )
- defaultValue = "true";
- }
-
- var type = GetTypeFromSymbol( fieldSymbol );
-
- return new UxmlTraitAttributeData()
- {
- Name = name , Type = type , defaultValue = defaultValue
- };
- }
-
private static AttributeData GetSingleAttributeData( ISymbol fieldSymbol , INamedTypeSymbol attributeSymbol )
{
var attr = fieldSymbol.GetAttributes()
@@ -261,84 +170,11 @@ public partial class {classSymbol.Name}
}
}
- struct UxmlTraitAttributeData
- {
- public string Name;
- public ITypeSymbol Type;
- public object defaultValue;
- }
-
struct UiElementAttributeData
{
public string Name;
}
- private string GetAttributeDescription( ISymbol fieldSymbol , INamedTypeSymbol attributeSymbol )
- {
- // private UxmlIntAttributeDescription m_PlayerHealth = new() { name = "player-health" , defaultValue = 0 };
- var name = GetAttributeDescriptionName( fieldSymbol.Name );
- var attr = GetUxmlTraitAttributeData( fieldSymbol , attributeSymbol ).Value;
- var type = ConvertTypeToUxmlAttributeDescriptionType( attr.Type );
-
- var attributeName = attr.Name;
- var defaultValue = attr.defaultValue;
-
- if ( GetTypeName( attr.Type ) == "string" )
- {
- defaultValue = $"\"{defaultValue}\"";
- }
-
- if ( defaultValue is null )
- {
- defaultValue = "default";
- }
-
- return $" private {type} {name} = new() {{ name = \"{attributeName}\" , defaultValue = {defaultValue} }};";
- }
-
- private string ConvertTypeToUxmlAttributeDescriptionType( ITypeSymbol type )
- {
- String typeString;
- string typeName = GetTypeName( type );
- switch ( typeName )
- {
- case "int":
- typeString = "Int";
- break;
-
- case "bool":
- typeString = "Bool";
- break;
-
- case "Color":
- typeString = "Color";
- break;
-
- case "string":
- typeString = "String";
- break;
-
- default:
- Debug.WriteLine( $"Could not get type-name for type: {type.Name}" );
- typeString = type.Name;
- break;
- }
-
- return $"Uxml{typeString}AttributeDescription";
- }
-
- private static string GetTypeName( ITypeSymbol type ) { return type.ToDisplayString( SymbolDisplayFormat.MinimallyQualifiedFormat ); }
-
- private static string GetQualifyingTypeName( ITypeSymbol type ) { return type.ToDisplayString( SymbolDisplayFormat.FullyQualifiedFormat ); }
-
- private string GetAttributeInitialization( ISymbol symbol )
- {
- // self.PlayerHealth = m_PlayerHealth.GetValueFromBag( bag , cc );
- return $" self.{symbol.Name} = {GetAttributeDescriptionName( symbol.Name )}.GetValueFromBag( bag , cc );";
- }
-
- private string GetAttributeDescriptionName( string name ) => $"m_{name}";
-
#endregion
}
@@ -360,8 +196,7 @@ public partial class {classSymbol.Name}
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
- .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UxmlTraitAttribute
- || ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
+ .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{
Fields.Add( symbol );
}
@@ -374,8 +209,7 @@ public partial class {classSymbol.Name}
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
- .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UxmlTraitAttribute
- || ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
+ .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{
Fields.Add( symbol );
}