Initial Commit
This commit is contained in:
commit
7eb2e750e9
9 changed files with 1081 additions and 0 deletions
64
SceneGenerator.cs
Normal file
64
SceneGenerator.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace Godot.Sharp.Extended.Generators;
|
||||
|
||||
[Generator]
|
||||
public class SceneGenerator : IIncrementalGenerator
|
||||
{
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
var scenes = context.AdditionalTextsProvider
|
||||
.Where(text => text.Path.EndsWith(".tscn"));
|
||||
|
||||
context.RegisterSourceOutput(
|
||||
context.CompilationProvider.Combine(scenes.Collect()).Combine(context.AnalyzerConfigOptionsProvider),
|
||||
(ctx, t) => GenerateCode(ctx, t.Left.Left, t.Left.Right, t.Right));
|
||||
}
|
||||
|
||||
private void GenerateCode(
|
||||
SourceProductionContext context,
|
||||
Compilation compilation,
|
||||
IEnumerable<AdditionalText> scenes,
|
||||
AnalyzerConfigOptionsProvider optionsProvider)
|
||||
{
|
||||
optionsProvider.GlobalOptions.TryGetValue("build_property.projectdir", out var projectDirectory);
|
||||
|
||||
var allMethods = new StringBuilder();
|
||||
foreach (var scene in scenes)
|
||||
{
|
||||
var directory = Path.GetDirectoryName(scene.Path);
|
||||
var name = Path.GetFileNameWithoutExtension(scene.Path);
|
||||
var relativePath = scene.Path.Replace(projectDirectory ?? "", "").Replace("\\", "/");
|
||||
allMethods.AppendLine(
|
||||
$$"""
|
||||
public static T New{{name.ToPascalCase()}}<T>() where T : Node {
|
||||
var packedScene = GD.Load<PackedScene>("res://{{relativePath}}");
|
||||
return packedScene.Instance<T>();
|
||||
}
|
||||
""");
|
||||
|
||||
var code =
|
||||
$$"""
|
||||
// <auto-generated />
|
||||
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Godot.Sharp.Extended;
|
||||
|
||||
public class Scenes
|
||||
{
|
||||
{{allMethods}}
|
||||
}
|
||||
""";
|
||||
|
||||
context.AddSource("Godot.Sharp.Extended.Scenes.g.cs", SourceText.From(code, Encoding.UTF8));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue