add: Scene

This commit is contained in:
h z
2025-02-05 15:01:43 +00:00
parent 148b0a0f48
commit ed1b01f052
3 changed files with 95 additions and 21 deletions

View File

@@ -12,26 +12,43 @@ public class GlobalRegistryGenerator : ISourceGenerator
public void Execute(GeneratorExecutionContext context)
{
StringBuilder sb = new();
sb.AppendLine("using System;");
sb.AppendLine("using System.Collections.Generic;");
sb.AppendLine("using System.Linq;");
sb.AppendLine("using System.Reflection;");
sb.AppendLine("using Hangman.SDK.Attributes;");
sb.AppendLine("public static partial class GlobalRegistry");
sb.AppendLine("{");
sb.AppendLine(" public static void Register()");
sb.AppendLine(" {");
sb.AppendLine(" Assembly assembly = Assembly.GetEntryAssembly();");
sb.AppendLine(" IEnumerable<Type> registerTypes = assembly.GetTypes()");
sb.AppendLine(" .Where(t => t.IsClass && t.GetCustomAttributes(typeof(AutoRegister), false).Any());");
sb.AppendLine(" foreach(Type t in registerTypes)");
sb.AppendLine(" {");
sb.AppendLine(" MethodInfo registerMethod = t.GetMethod(\"Register\", BindingFlags.Static | BindingFlags.Public);");
sb.AppendLine(" if (registerMethod != null)");
sb.AppendLine(" registerMethod.Invoke(null, null);");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("}");
sb
.AppendLine("using Godot;")
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.Linq;")
.AppendLine("using System.Reflection;")
.AppendLine("using Hangman.SDK.Attributes;")
.AppendLine("using Hangman.SDK;")
.AppendLine("public static partial class GlobalRegistry")
.AppendLine("{")
.AppendLine(" public static void Register()")
.AppendLine(" {")
.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("}");
context.AddSource("GlobalRegistry.g.cs", sb.ToString());
}
}