From 24a74a6f8012fbcbbc43ccd6f081cf54c5384dc7 Mon Sep 17 00:00:00 2001 From: hzhang Date: Sat, 15 Feb 2025 08:25:01 +0000 Subject: [PATCH 1/3] draft: texture button --- src/Generators/GlobalRegistryGenerator.cs | 4 + src/Generators/ProxyNodeGenerator.cs | 44 --------- src/Generators/TextureSetGenerator.cs | 112 ++++++++++++++++++++++ 3 files changed, 116 insertions(+), 44 deletions(-) delete mode 100644 src/Generators/ProxyNodeGenerator.cs create mode 100644 src/Generators/TextureSetGenerator.cs diff --git a/src/Generators/GlobalRegistryGenerator.cs b/src/Generators/GlobalRegistryGenerator.cs index dc5ec0e..7988647 100644 --- a/src/Generators/GlobalRegistryGenerator.cs +++ b/src/Generators/GlobalRegistryGenerator.cs @@ -53,6 +53,10 @@ public class GlobalRegistryGenerator : ISourceGenerator .AppendLine(" public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;") .AppendLine(" public static bool Paused { get; set; }") .AppendLine(" public static HashSet TimeConsumers { get; } = new ();") + .AppendLine(" public static void Prepare() => PoloniumRegistry.Prepare();") + .AppendLine(" public static partial class TextureSets") + .AppendLine(" {") + .AppendLine(" }") .AppendLine("}"); context.AddSource("GlobalRegistry.g.cs", sb.ToString()); } diff --git a/src/Generators/ProxyNodeGenerator.cs b/src/Generators/ProxyNodeGenerator.cs deleted file mode 100644 index 0048ac2..0000000 --- a/src/Generators/ProxyNodeGenerator.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.CodeAnalysis; - -namespace Polonium.Generators.Generators; -[Generator] -public class ProxyNodeGenerator : AssetProcessGenerator -{ - private INamedTypeSymbol? NodeProxy { get; set; } - - private IEnumerable ProxyNodesInNamespace(INamespaceSymbol ns) - { - foreach (INamespaceOrTypeSymbol member in ns.GetMembers()) - { - if (member is INamespaceSymbol nsx) - foreach (INamedTypeSymbol nsz in ProxyNodesInNamespace(nsx)) - yield return nsz; - else if (member is INamedTypeSymbol nsu) - if (nsu.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, NodeProxy))) - yield return nsu; - } - } - - - public override void Execute(GeneratorExecutionContext context) - { - Compilation compilation = context.Compilation; - NodeProxy = compilation.GetTypeByMetadataName("Polonium.Attributes.ProxyNode"); - foreach (INamedTypeSymbol node in ProxyNodesInNamespace(compilation.GlobalNamespace)) - { - StringBuilder sb = new(); - sb - .AppendLine($"namespace Polonium.Nodes;") - .AppendLine("using Godot;") - .AppendLine($"[GlobalClass]") - .AppendLine($"[Tool]") - .AppendLine($"public partial class {node.Name} : {node.ToDisplayString()}") - .AppendLine("{") - .AppendLine("}"); - context.AddSource($"NodeProxy_{node.Name}.g.cs", sb.ToString()); - } - } -} \ No newline at end of file diff --git a/src/Generators/TextureSetGenerator.cs b/src/Generators/TextureSetGenerator.cs new file mode 100644 index 0000000..092904c --- /dev/null +++ b/src/Generators/TextureSetGenerator.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; + +namespace Polonium.Generators.Generators; +[Generator] +public class TextureSetGenerator : ISourceGenerator +{ + public void Initialize(GeneratorInitializationContext context) + { + } + + public void Execute(GeneratorExecutionContext context) + { + + AdditionalText? t = context.AdditionalFiles.FirstOrDefault(file => file.Path.EndsWith("textures_set.manifest")); + if (t is null) + return; + SourceText? ts = t.GetText(); + if (ts is null) + return; + + StringBuilder sb0 = new(); + sb0 + .AppendLine("using System;") + .AppendLine("using Polonium.DataStructures;") + .AppendLine("using System.Collections.Generic;") + .AppendLine("public static partial class GlobalRegistry") + .AppendLine("{") + .AppendLine(" public static Dictionary TextureSetMap = new();") + .AppendLine(" public enum TextureSetName") + .AppendLine(" {"); + + StringBuilder sb1 = new(); + sb1 + .AppendLine("using System;") + .AppendLine("using Polonium.Attributes;") + .AppendLine("[AutoRegister]") + .AppendLine("public class TextureSetMapRegister") + .AppendLine("{") + .AppendLine(" public static void Register()") + .AppendLine(" {"); + foreach (string line in ts.ToString().Split('\n')) + { + if(line.Trim().Length == 0) + continue; + StringBuilder sb = new(); + sb + .AppendLine("using System;") + .AppendLine("using Polonium.DataStructures;") + .AppendLine("public static partial class GlobalRegistry") + .AppendLine("{") + .AppendLine(" public static partial class TextureSets") + .AppendLine(" {"); + StringBuilder sbx = new(); + string[] paths = line.Split('/'); + string folderName = paths[paths.Length - 1].Replace(".TextureSet", ""); + + string indent = " "; + HashSet ignore = new HashSet { "res:", "", "Resources", "ButtonTextureSet", paths[paths.Length - 1]}; + string instancePath = "GlobalRegistry.TextureSets"; + foreach (string path in paths) + { + if(ignore.Contains(path)) + continue; + sb + .AppendLine($"{indent}public static partial class {path}") + .AppendLine($"{indent}{{"); + sbx.Insert(0, $"{indent}}}\n"); + instancePath += $".{path}"; + indent += " "; + } + sb + .AppendLine($"{indent}public static readonly TextureSet {folderName} = new TextureSet(\"{line}\");") + .Append(sbx) + .AppendLine(" }") + .AppendLine("}"); + instancePath += $".{folderName}"; + string lName = $"{line.Replace("res://", "").Replace("Resources/ButtonTextureSet/", "").Replace('/', '_').Replace(".TextureSet", "").Replace(".", "")}"; + sb0.AppendLine($" {lName},"); + sb1.AppendLine($" GlobalRegistry.TextureSetMap[GlobalRegistry.TextureSetName.{lName}] = {instancePath};"); + context.AddSource($"{lName}.g.cs", sb.ToString()); + + } + sb0 + .AppendLine(" }") + .AppendLine("}"); + sb1 + .AppendLine(" }") + .AppendLine("}"); + //context.AddSource("TextureSetNames.g.cs", sb0.ToString()); + context.AddSource("TextureSetMap.g.cs", sb1.ToString()); + } + private void ReportLog(string message, GeneratorExecutionContext context) + { + var diagnostic = Diagnostic.Create( + new DiagnosticDescriptor( + id: "TSG001", + title: "Source Generator Log", + messageFormat: message, + category: "SourceGenerator", + DiagnosticSeverity.Info, + isEnabledByDefault: true), + Location.None + ); + context.ReportDiagnostic(diagnostic); + } +} \ No newline at end of file From 6738080639702332f0b611d586f2f33bb03f1f26 Mon Sep 17 00:00:00 2001 From: hzhang Date: Sun, 16 Feb 2025 22:36:38 +0000 Subject: [PATCH 2/3] refactor: redesign project structure --- Polonium.Generators.csproj | 1 - src/Generators/GlobalRegistryGenerator.cs | 63 ------------ src/Generators/TextureSetGenerator.cs | 112 ---------------------- 3 files changed, 176 deletions(-) delete mode 100644 src/Generators/GlobalRegistryGenerator.cs delete mode 100644 src/Generators/TextureSetGenerator.cs diff --git a/Polonium.Generators.csproj b/Polonium.Generators.csproj index c8a8951..6fadc4d 100644 --- a/Polonium.Generators.csproj +++ b/Polonium.Generators.csproj @@ -5,7 +5,6 @@ true enable latest - true true true diff --git a/src/Generators/GlobalRegistryGenerator.cs b/src/Generators/GlobalRegistryGenerator.cs deleted file mode 100644 index 7988647..0000000 --- a/src/Generators/GlobalRegistryGenerator.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System.Text; -using Microsoft.CodeAnalysis; - -namespace Polonium.Generators.Generators; -[Generator] -public class GlobalRegistryGenerator : ISourceGenerator -{ - public void Initialize(GeneratorInitializationContext context) - { - } - - public void Execute(GeneratorExecutionContext context) - { - StringBuilder sb = new(); - sb - .AppendLine("using Godot;") - .AppendLine("using System;") - .AppendLine("using System.Collections.Generic;") - .AppendLine("using System.Linq;") - .AppendLine("using System.Reflection;") - .AppendLine("using Polonium.Attributes;") - .AppendLine("using Polonium;") - .AppendLine("using Polonium.Interfaces;") - .AppendLine("public static partial class GlobalRegistry") - .AppendLine("{") - .AppendLine(" public static void Start()") - .AppendLine(" {") - .AppendLine(" PoloniumRegistry.Prepare();") - .AppendLine(" Assembly assembly = Assembly.GetExecutingAssembly();") - .AppendLine(" IEnumerable registerTypes = Utils.GetLoadableTypes(assembly);") - .AppendLine(" registerTypes = registerTypes.Where(t => t.IsClass && t.GetCustomAttributes(typeof(AutoRegister), false).Any());") - .AppendLine(" foreach(Type t in registerTypes)") - .AppendLine(" {") - .AppendLine(" MethodInfo registerMethod = t.GetMethod(\"Register\", BindingFlags.Static | BindingFlags.Public);") - .AppendLine(" if (registerMethod != null)") - .AppendLine(" registerMethod.Invoke(null, null);") - .AppendLine(" }") - .AppendLine(" }") - .AppendLine(" public static class Asset where T : Node") - .AppendLine(" {") - .AppendLine(" private static readonly Queue Pool = new();") - .AppendLine(" public static PackedScene Scene { get; set; }") - .AppendLine(" public static T Instance => Scene.Instantiate();") - .AppendLine(" public static T Get() => Pool.Count > 0 ? Pool.Dequeue() : Instance;") - .AppendLine(" public static void Return(T obj)") - .AppendLine(" {") - .AppendLine(" if(Pool.Count < 10)") - .AppendLine(" Pool.Enqueue(obj);") - .AppendLine(" else") - .AppendLine(" obj.QueueFree();") - .AppendLine(" }") - .AppendLine(" }") - .AppendLine(" public static PoloniumRegistry PoloniumRegistry => PoloniumRegistry.Instance;") - .AppendLine(" public static bool Paused { get; set; }") - .AppendLine(" public static HashSet TimeConsumers { get; } = new ();") - .AppendLine(" public static void Prepare() => PoloniumRegistry.Prepare();") - .AppendLine(" public static partial class TextureSets") - .AppendLine(" {") - .AppendLine(" }") - .AppendLine("}"); - context.AddSource("GlobalRegistry.g.cs", sb.ToString()); - } -} \ No newline at end of file diff --git a/src/Generators/TextureSetGenerator.cs b/src/Generators/TextureSetGenerator.cs deleted file mode 100644 index 092904c..0000000 --- a/src/Generators/TextureSetGenerator.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Text; - -namespace Polonium.Generators.Generators; -[Generator] -public class TextureSetGenerator : ISourceGenerator -{ - public void Initialize(GeneratorInitializationContext context) - { - } - - public void Execute(GeneratorExecutionContext context) - { - - AdditionalText? t = context.AdditionalFiles.FirstOrDefault(file => file.Path.EndsWith("textures_set.manifest")); - if (t is null) - return; - SourceText? ts = t.GetText(); - if (ts is null) - return; - - StringBuilder sb0 = new(); - sb0 - .AppendLine("using System;") - .AppendLine("using Polonium.DataStructures;") - .AppendLine("using System.Collections.Generic;") - .AppendLine("public static partial class GlobalRegistry") - .AppendLine("{") - .AppendLine(" public static Dictionary TextureSetMap = new();") - .AppendLine(" public enum TextureSetName") - .AppendLine(" {"); - - StringBuilder sb1 = new(); - sb1 - .AppendLine("using System;") - .AppendLine("using Polonium.Attributes;") - .AppendLine("[AutoRegister]") - .AppendLine("public class TextureSetMapRegister") - .AppendLine("{") - .AppendLine(" public static void Register()") - .AppendLine(" {"); - foreach (string line in ts.ToString().Split('\n')) - { - if(line.Trim().Length == 0) - continue; - StringBuilder sb = new(); - sb - .AppendLine("using System;") - .AppendLine("using Polonium.DataStructures;") - .AppendLine("public static partial class GlobalRegistry") - .AppendLine("{") - .AppendLine(" public static partial class TextureSets") - .AppendLine(" {"); - StringBuilder sbx = new(); - string[] paths = line.Split('/'); - string folderName = paths[paths.Length - 1].Replace(".TextureSet", ""); - - string indent = " "; - HashSet ignore = new HashSet { "res:", "", "Resources", "ButtonTextureSet", paths[paths.Length - 1]}; - string instancePath = "GlobalRegistry.TextureSets"; - foreach (string path in paths) - { - if(ignore.Contains(path)) - continue; - sb - .AppendLine($"{indent}public static partial class {path}") - .AppendLine($"{indent}{{"); - sbx.Insert(0, $"{indent}}}\n"); - instancePath += $".{path}"; - indent += " "; - } - sb - .AppendLine($"{indent}public static readonly TextureSet {folderName} = new TextureSet(\"{line}\");") - .Append(sbx) - .AppendLine(" }") - .AppendLine("}"); - instancePath += $".{folderName}"; - string lName = $"{line.Replace("res://", "").Replace("Resources/ButtonTextureSet/", "").Replace('/', '_').Replace(".TextureSet", "").Replace(".", "")}"; - sb0.AppendLine($" {lName},"); - sb1.AppendLine($" GlobalRegistry.TextureSetMap[GlobalRegistry.TextureSetName.{lName}] = {instancePath};"); - context.AddSource($"{lName}.g.cs", sb.ToString()); - - } - sb0 - .AppendLine(" }") - .AppendLine("}"); - sb1 - .AppendLine(" }") - .AppendLine("}"); - //context.AddSource("TextureSetNames.g.cs", sb0.ToString()); - context.AddSource("TextureSetMap.g.cs", sb1.ToString()); - } - private void ReportLog(string message, GeneratorExecutionContext context) - { - var diagnostic = Diagnostic.Create( - new DiagnosticDescriptor( - id: "TSG001", - title: "Source Generator Log", - messageFormat: message, - category: "SourceGenerator", - DiagnosticSeverity.Info, - isEnabledByDefault: true), - Location.None - ); - context.ReportDiagnostic(diagnostic); - } -} \ No newline at end of file From e97b2f098f0daf049d316a0c728746fb72dc4cab Mon Sep 17 00:00:00 2001 From: hzhang Date: Tue, 18 Feb 2025 11:41:05 +0000 Subject: [PATCH 3/3] add: sdk --- Polonium.Generators.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Polonium.Generators.csproj b/Polonium.Generators.csproj index 6fadc4d..e8c9133 100644 --- a/Polonium.Generators.csproj +++ b/Polonium.Generators.csproj @@ -42,7 +42,7 @@ - +