Skip to content

Commit 08b70e7

Browse files
committed
basic ysls generation for godot
1 parent 7336406 commit 08b70e7

1 file changed

Lines changed: 89 additions & 4 deletions

File tree

src/YarnSpinner.Console/Commands/GenerateDefinitionsCommand.cs

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public static void GenerateDefinitions(DirectoryInfo inputDirectory, DirectoryIn
2323
System.Environment.Exit(1);
2424
break;
2525
case "Godot-csharp":
26-
Log.Warn("At this stage ysc only supports generating YSLS files for Unity but will attempt to read the Godot C# as Unity C#");
27-
GenerateYSLSFilesForUnity(inputDirectory, outputDirectory);
28-
Log.PrintLine("Processing of the files is complete, recommend double checking the YSLS for incompatibility with Godot.");
26+
GenerateYSLSFilesForGodot(inputDirectory, outputDirectory);
2927
break;
3028
case "Unreal":
3129
Log.Error("At this stage ysc only supports generating YSLS files for Unity");
@@ -119,7 +117,94 @@ private static void GenerateYSLSFilesForUnity(DirectoryInfo inputDirectory, Dire
119117
$@"""functions"":[{string.Join(",", functionJSON)}]" +
120118
"}";
121119

122-
File.WriteAllText(Path.Combine(outputDirectory.FullName, $"{compilation.AssemblyName ?? "NULL"}.ysls.json"), ysls);
120+
var outputPath = Path.Combine(outputDirectory.FullName, $"{assemblyName}.ysls.json");
121+
File.WriteAllText(outputPath, ysls);
122+
Log.Info($"Wrote {outputPath}");
123+
}
124+
else
125+
{
126+
Log.Info($"No actions found in {assemblyName}, skipping ysls.json generation");
127+
}
128+
}
129+
}
130+
131+
private static void GenerateYSLSFilesForGodot(DirectoryInfo inputDirectory, DirectoryInfo outputDirectory)
132+
{
133+
var instances = MSBuildLocator.QueryVisualStudioInstances().FirstOrDefault();
134+
if (instances == null)
135+
{
136+
Log.Error("Unable to find a working MSBuild, so cannot continue.");
137+
System.Environment.Exit(1);
138+
}
139+
140+
MSBuildLocator.RegisterInstance(instances);
141+
142+
if (inputDirectory == null)
143+
{
144+
Log.Error("The input directory is null");
145+
System.Environment.Exit(1);
146+
}
147+
148+
var projects = inputDirectory.GetFiles("*.csproj");
149+
if (projects.Length > 0)
150+
{
151+
Log.Info($"Found {projects.Length} csproj file(s) in {inputDirectory.FullName}");
152+
}
153+
else
154+
{
155+
Log.Error($"No csproj files found in {inputDirectory.FullName}");
156+
System.Environment.Exit(1);
157+
}
158+
159+
var logger = new NullLogger();
160+
foreach (var projectPath in projects)
161+
{
162+
MSBuildWorkspace workspace = MSBuildWorkspace.Create();
163+
var project = workspace.OpenProjectAsync(projectPath.FullName).Result;
164+
165+
var compilation = project.WithParseOptions(CSharpParseOptions.Default).GetCompilationAsync().Result as CSharpCompilation;
166+
var assemblyName = compilation.AssemblyName ?? "NULL";
167+
168+
var hasYarnSpinner = compilation.ReferencedAssemblyNames.Any(name =>
169+
name.Name == "YarnSpinner" || name.Name == "YarnSpinner.Compiler");
170+
171+
if (!hasYarnSpinner)
172+
{
173+
Log.Info($"Skipping {assemblyName}: does not reference YarnSpinner");
174+
continue;
175+
}
176+
177+
List<Action> actions = new List<Action>();
178+
foreach (var tree in compilation.SyntaxTrees)
179+
{
180+
actions.AddRange(Analyser.GetActions(compilation, tree, logger));
181+
}
182+
183+
if (actions.Count > 0)
184+
{
185+
foreach (var action in actions)
186+
{
187+
if (action.Validate(compilation, logger).Any(d => d.Severity == DiagnosticSeverity.Warning || d.Severity == DiagnosticSeverity.Error))
188+
{
189+
action.ContainsErrors = true;
190+
}
191+
}
192+
IEnumerable<string> commandJSON = actions.Where(a => a.Type == ActionType.Command).Select(a => a.ToJSON());
193+
IEnumerable<string> functionJSON = actions.Where(a => a.Type == ActionType.Function).Select(a => a.ToJSON());
194+
195+
var ysls = "{" +
196+
@"""version"":2," +
197+
$@"""commands"":[{string.Join(",", commandJSON)}]," +
198+
$@"""functions"":[{string.Join(",", functionJSON)}]" +
199+
"}";
200+
201+
var outputPath = Path.Combine(outputDirectory.FullName, $"{assemblyName}.ysls.json");
202+
File.WriteAllText(outputPath, ysls);
203+
Log.Info($"Wrote {outputPath}");
204+
}
205+
else
206+
{
207+
Log.Info($"No actions found in {assemblyName}, skipping ysls.json generation");
123208
}
124209
}
125210
}

0 commit comments

Comments
 (0)