From 00bf1b438e3b8f29be45ef8669034533f82da24c Mon Sep 17 00:00:00 2001 From: hzhang Date: Thu, 11 Jul 2024 11:21:29 +0100 Subject: [PATCH] Data Type --- .gitignore | 5 ++++ DataTypes/DataTypeGenerator.cs | 52 ++++++++++++++++++++++++++++++++++ Nocturnis.Generators.csproj | 26 +++++++++++++++++ Nocturnis.Generators.sln | 16 +++++++++++ Properties/launchSettings.json | 9 ++++++ 5 files changed, 108 insertions(+) create mode 100644 .gitignore create mode 100644 DataTypes/DataTypeGenerator.cs create mode 100644 Nocturnis.Generators.csproj create mode 100644 Nocturnis.Generators.sln create mode 100644 Properties/launchSettings.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/DataTypes/DataTypeGenerator.cs b/DataTypes/DataTypeGenerator.cs new file mode 100644 index 0000000..40dfd47 --- /dev/null +++ b/DataTypes/DataTypeGenerator.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Text; + +namespace Nocturnis.Generators.DataTypes; +[Generator] +public class DataTypeGenerator : ISourceGenerator +{ + public void Initialize(GeneratorInitializationContext context) + { + } + + public void Execute(GeneratorExecutionContext context) + { + var classesTxt = context.AdditionalFiles + .FirstOrDefault(f => Path.GetFileName(f.Path) == "BaseTypes"); + if (classesTxt != null) + { + IEnumerable? types = + classesTxt.GetText(context.CancellationToken)?.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); + if (types == null) + return; + StringBuilder sb = new(); + sb.AppendLine("using Godot;"); + sb.AppendLine("using Nocturnis.DataStructures.DataTypes;"); + sb.AppendLine("namespace Nocturnis.GlobalManagement.Constants;"); + sb.AppendLine("public static partial class DataTypeConstant"); + sb.AppendLine("{"); + sb.AppendLine(" public static class BaseDataTypeNames"); + sb.AppendLine(" {"); + StringBuilder sxa = new(); + foreach (string? type in types) + { + sb.AppendLine($" public static StringName {type} = \"{type}\";"); + sxa.AppendLine($" public static DataType {type} = new DataType(BaseDataTypeNames.{type});"); + sxa.AppendLine($" public static DataType {type}Array = new DataType({type})"); + } + + sb.AppendLine(" }"); + sb.AppendLine(" public static class BaseDataTypes"); + sb.AppendLine(" {"); + sb.Append(sxa); + sb.AppendLine(" }"); + sb.AppendLine("}"); + context.AddSource("DataTypeConstant.g.cs", SourceText.From(sb.ToString(), Encoding.UTF8)); + } + } +} \ No newline at end of file diff --git a/Nocturnis.Generators.csproj b/Nocturnis.Generators.csproj new file mode 100644 index 0000000..bcc0534 --- /dev/null +++ b/Nocturnis.Generators.csproj @@ -0,0 +1,26 @@ + + + + netstandard2.0 + false + enable + latest + + true + true + + Nocturnis.Generators + Nocturnis.Generators + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + diff --git a/Nocturnis.Generators.sln b/Nocturnis.Generators.sln new file mode 100644 index 0000000..9b33582 --- /dev/null +++ b/Nocturnis.Generators.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocturnis.Generators", "Nocturnis.Generators.csproj", "{8CEDE314-85A0-40D8-80D5-8927E21133B8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8CEDE314-85A0-40D8-80D5-8927E21133B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8CEDE314-85A0-40D8-80D5-8927E21133B8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8CEDE314-85A0-40D8-80D5-8927E21133B8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8CEDE314-85A0-40D8-80D5-8927E21133B8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..6d73c59 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "DebugRoslynSourceGenerator": { + "commandName": "DebugRoslynComponent", + "targetProject": "../Nocturnis/Nocturnis.csproj" + } + } +} \ No newline at end of file