Restructured
Re-Organized structure to allow for a Generator, and a Core library to be included.
This commit is contained in:
parent
c1d61d8a5c
commit
44c3d49aad
7 changed files with 18 additions and 15 deletions
70
Generators/ActionGenerator.cs
Normal file
70
Generators/ActionGenerator.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace Godot.Sharp.Extended.Generators;
|
||||
|
||||
[Generator]
|
||||
public class ActionGenerator : IIncrementalGenerator
|
||||
{
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
var projectFile = context.AdditionalTextsProvider
|
||||
.Where(file => file.Path.EndsWith("project.godot"))
|
||||
.Select((file, token) => file.GetText())
|
||||
.Collect();
|
||||
|
||||
context.RegisterSourceOutput(projectFile, GenerateCode);
|
||||
}
|
||||
|
||||
private void GenerateCode(SourceProductionContext ctx, ImmutableArray<SourceText> texts)
|
||||
{
|
||||
var memberCode = new StringBuilder();
|
||||
foreach (var text in texts)
|
||||
{
|
||||
if (text == null) continue;
|
||||
var content = text.ToString();
|
||||
var inputs = GetActions(content);
|
||||
var fields = inputs.Select(keyData => $$"""\tpublic static StringName {{keyData.ToPascalCase()}} = "{{keyData}}";""");
|
||||
|
||||
foreach (var field in fields) memberCode.AppendLine(field);
|
||||
}
|
||||
|
||||
var code = $$"""
|
||||
// <auto-generated />
|
||||
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Godot.Sharp.Extended.Generators;
|
||||
|
||||
public partial class Actions
|
||||
{
|
||||
{{memberCode}}
|
||||
}
|
||||
""";
|
||||
ctx.AddSource("Godot.Sharp.Extended.Generators.Actions.g.cs", SourceText.From(code, Encoding.UTF8));
|
||||
}
|
||||
|
||||
public List<string> GetActions(string content)
|
||||
{
|
||||
var results = new List<string>();
|
||||
var inputSection = false;
|
||||
foreach (var line in content.Split(['\n']))
|
||||
{
|
||||
if (line.Equals("[input]"))
|
||||
inputSection = true;
|
||||
else if (line.StartsWith("[") && inputSection) break;
|
||||
|
||||
if (!inputSection) continue;
|
||||
|
||||
var kv = line.Split(['='], 2);
|
||||
if (kv.Length == 2) results.Add(kv[0].Trim());
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue