refactor: redesign project structure
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
<IsPackable>true</IsPackable>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
<IsRoslynComponent>true</IsRoslynComponent>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
|
||||
@@ -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<Type> 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<T> where T : Node")
|
||||
.AppendLine(" {")
|
||||
.AppendLine(" private static readonly Queue<T> Pool = new();")
|
||||
.AppendLine(" public static PackedScene Scene { get; set; }")
|
||||
.AppendLine(" public static T Instance => Scene.Instantiate<T>();")
|
||||
.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<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,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<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