add: Scene
This commit is contained in:
54
AssetRegisterGenerator.cs
Normal file
54
AssetRegisterGenerator.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Hangman.SDK.Generators;
|
||||||
|
[Generator]
|
||||||
|
public class AssetRegisterGenerator : AssetProcessGenerator
|
||||||
|
{
|
||||||
|
public override void Execute(GeneratorExecutionContext context)
|
||||||
|
{
|
||||||
|
if (!(context.SyntaxReceiver is ClassSyntaxReceiver receiver))
|
||||||
|
return;
|
||||||
|
Compilation compilation = context.Compilation;
|
||||||
|
StringBuilder sb = new();
|
||||||
|
|
||||||
|
sb
|
||||||
|
.AppendLine("using System;")
|
||||||
|
.AppendLine("using Godot;")
|
||||||
|
.AppendLine("using Hangman.SDK.Attributes;")
|
||||||
|
.AppendLine("[AutoRegister]")
|
||||||
|
.AppendLine("public static class AssetRegister")
|
||||||
|
.AppendLine("{")
|
||||||
|
.AppendLine(" public static void Register()")
|
||||||
|
.AppendLine(" {");
|
||||||
|
foreach (var derivedClassDeclaration in receiver.Classes)
|
||||||
|
{
|
||||||
|
SemanticModel model = compilation.GetSemanticModel(derivedClassDeclaration.SyntaxTree);
|
||||||
|
INamedTypeSymbol? symbol = model.GetDeclaredSymbol(derivedClassDeclaration) as INamedTypeSymbol;
|
||||||
|
string filePath = derivedClassDeclaration.SyntaxTree.FilePath.Replace(".cs", ".tscn");
|
||||||
|
if(!filePath.Contains("Assets") || symbol == null)
|
||||||
|
continue;
|
||||||
|
ImmutableArray<AdditionalText> additionalFiles = context.AdditionalFiles;
|
||||||
|
if (additionalFiles.Select(y => y.Path).Contains(filePath))
|
||||||
|
{
|
||||||
|
string rs = filePath.Replace("\\", "/").Replace(" ", "%20");
|
||||||
|
rs = rs.Substring(rs.IndexOf("Assets", StringComparison.Ordinal));
|
||||||
|
string gdPath = "res://" + rs;
|
||||||
|
string className = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
sb
|
||||||
|
.AppendLine(
|
||||||
|
$" GlobalRegistry.Asset<{className}>.Scene = ResourceLoader.Load<PackedScene>(\"{gdPath}\");"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sb
|
||||||
|
.AppendLine(" }")
|
||||||
|
.AppendLine("}");
|
||||||
|
context.AddSource("AssetRegistry.g.cs", sb.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,26 +12,43 @@ public class GlobalRegistryGenerator : ISourceGenerator
|
|||||||
public void Execute(GeneratorExecutionContext context)
|
public void Execute(GeneratorExecutionContext context)
|
||||||
{
|
{
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
sb.AppendLine("using System;");
|
sb
|
||||||
sb.AppendLine("using System.Collections.Generic;");
|
.AppendLine("using Godot;")
|
||||||
sb.AppendLine("using System.Linq;");
|
.AppendLine("using System;")
|
||||||
sb.AppendLine("using System.Reflection;");
|
.AppendLine("using System.Collections.Generic;")
|
||||||
sb.AppendLine("using Hangman.SDK.Attributes;");
|
.AppendLine("using System.Linq;")
|
||||||
sb.AppendLine("public static partial class GlobalRegistry");
|
.AppendLine("using System.Reflection;")
|
||||||
sb.AppendLine("{");
|
.AppendLine("using Hangman.SDK.Attributes;")
|
||||||
sb.AppendLine(" public static void Register()");
|
.AppendLine("using Hangman.SDK;")
|
||||||
sb.AppendLine(" {");
|
.AppendLine("public static partial class GlobalRegistry")
|
||||||
sb.AppendLine(" Assembly assembly = Assembly.GetEntryAssembly();");
|
.AppendLine("{")
|
||||||
sb.AppendLine(" IEnumerable<Type> registerTypes = assembly.GetTypes()");
|
.AppendLine(" public static void Register()")
|
||||||
sb.AppendLine(" .Where(t => t.IsClass && t.GetCustomAttributes(typeof(AutoRegister), false).Any());");
|
.AppendLine(" {")
|
||||||
sb.AppendLine(" foreach(Type t in registerTypes)");
|
.AppendLine(" Assembly assembly = Assembly.GetExecutingAssembly();")
|
||||||
sb.AppendLine(" {");
|
.AppendLine(" IEnumerable<Type> registerTypes = Utils.GetLoadableTypes(assembly);")
|
||||||
sb.AppendLine(" MethodInfo registerMethod = t.GetMethod(\"Register\", BindingFlags.Static | BindingFlags.Public);");
|
.AppendLine(" registerTypes = registerTypes.Where(t => t.IsClass && t.GetCustomAttributes(typeof(AutoRegister), false).Any());")
|
||||||
sb.AppendLine(" if (registerMethod != null)");
|
.AppendLine(" foreach(Type t in registerTypes)")
|
||||||
sb.AppendLine(" registerMethod.Invoke(null, null);");
|
.AppendLine(" {")
|
||||||
sb.AppendLine(" }");
|
.AppendLine(" MethodInfo registerMethod = t.GetMethod(\"Register\", BindingFlags.Static | BindingFlags.Public);")
|
||||||
sb.AppendLine(" }");
|
.AppendLine(" if (registerMethod != null)")
|
||||||
sb.AppendLine("}");
|
.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());
|
context.AddSource("GlobalRegistry.g.cs", sb.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
|
|
||||||
@@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
<RootNamespace>Hangman.SDK.Generators</RootNamespace>
|
<RootNamespace>Hangman.SDK.Generators</RootNamespace>
|
||||||
<AssemblyName>Hangman.SDK.Generators</AssemblyName>
|
<AssemblyName>Hangman.SDK.Generators</AssemblyName>
|
||||||
|
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||||
|
<Version>0.0.1</Version>
|
||||||
|
<Authors>Hangman</Authors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user