draft: texture button
This commit is contained in:
@@ -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<ITimeConsumer> 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());
|
||||
}
|
||||
|
||||
@@ -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<INamedTypeSymbol> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
112
src/Generators/TextureSetGenerator.cs
Normal file
112
src/Generators/TextureSetGenerator.cs
Normal file
@@ -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<TextureSetName, TextureSet> 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<string> ignore = new HashSet<string> { "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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user