Skip to content

Commit 687d462

Browse files
committed
more YSLS changes.
- types will never be null, they will be any if it can't work it out - created json now matches the newer schema - variadics params now list their internal type and not array
1 parent 07b521d commit 687d462

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2222
- `YarnProjectImporter` now updates asset addresses and generates the C# variable storage class after the project import completes, rather than during the import.
2323
- Each assembly now gets it's own YSLS file when generating them.
2424
- renamed the test asmdef files from `YarnSpinnerTests.x` to `YarnSpinner.Unity.Tests.X` so that they are matched by the existing filters.
25+
- Generated YSLS file now matches the newer schema, see the YS core repo for details of this
2526

2627
### Removed
2728

Editor/Analysis/Action.cs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public enum AsyncType
107107

108108
static class ITypeSymbolExtension
109109
{
110-
public static string? GetYarnTypeString(this ITypeSymbol typeSymbol)
110+
public static string GetYarnTypeString(this ITypeSymbol typeSymbol)
111111
{
112112
return typeSymbol.SpecialType switch
113113
{
@@ -124,7 +124,7 @@ static class ITypeSymbolExtension
124124
SpecialType.System_Single => "number",
125125
SpecialType.System_Double => "number",
126126
SpecialType.System_String => "string",
127-
_ => null
127+
_ => "any"
128128
};
129129
}
130130
}
@@ -140,7 +140,7 @@ public struct Parameter
140140

141141
public AttributeData[]? Attributes;
142142

143-
public readonly string? YarnTypeString => Type.GetYarnTypeString();
143+
public readonly string YarnTypeString => Type.GetYarnTypeString(); // this should change to support the subtypes through the same logic we use below, for now it's fine
144144
}
145145

146146
public class Action
@@ -227,21 +227,21 @@ public Action(string name, ActionType type, IMethodSymbol methodSymbol)
227227
public List<Parameter> Parameters = new List<Parameter>();
228228

229229
public string? ReturnDescription;
230-
public string? YarnReturnTypeString => this.MethodSymbol.ReturnType.GetYarnTypeString();
230+
public string YarnReturnTypeString => this.MethodSymbol.ReturnType.GetYarnTypeString();
231231

232232
public string ToJSON()
233233
{
234234
var result = new Dictionary<string, object?>();
235235

236-
result["YarnName"] = this.Name;
237-
result["DefinitionName"] = this.MethodName;
238-
result["FileName"] = this.SourceFileName;
236+
result["yarnName"] = this.Name;
237+
result["definitionName"] = this.MethodName;
238+
result["fileName"] = this.SourceFileName;
239239
if (!string.IsNullOrEmpty(this.Description))
240240
{
241-
result["Documentation"] = this.Description;
241+
result["documentation"] = this.Description;
242242
}
243-
result["Language"] = "csharp";
244-
result["Async"] = this.AsyncType != AsyncType.Sync;
243+
result["language"] = "csharp";
244+
result["async"] = this.AsyncType != AsyncType.Sync;
245245

246246
if (this.Declaration != null)
247247
{
@@ -257,49 +257,48 @@ public string ToJSON()
257257
{"line", location.EndLinePosition.Line},
258258
{"character", location.EndLinePosition.Character},
259259
};
260-
result["Location"] = new Dictionary<string, Dictionary<string, int>>()
260+
result["location"] = new Dictionary<string, Dictionary<string, int>>()
261261
{
262262
{"start", startPosition},
263263
{"end", endPosition},
264264
};
265265
}
266266

267-
result["Parameters"] = new List<Dictionary<string, object?>>(this.Parameters.Select(p =>
267+
result["parameters"] = new List<Dictionary<string, object?>>(this.Parameters.Select(p =>
268268
{
269269
var paramObject = new Dictionary<string, object?>();
270270

271-
paramObject["Name"] = p.Name;
271+
paramObject["name"] = p.Name;
272272
if (!string.IsNullOrEmpty(p.Description))
273273
{
274-
paramObject["Documentation"] = p.Description;
274+
paramObject["documentation"] = p.Description;
275275
}
276276
if (!string.IsNullOrEmpty(p.DefaultValueString))
277277
{
278-
paramObject["DefaultValue"] = p.DefaultValueString;
278+
paramObject["defaultValue"] = p.DefaultValueString;
279279
}
280-
paramObject["IsParamsArray"] = p.IsParamsArray;
280+
paramObject["isParamsArray"] = p.IsParamsArray;
281281

282282
// there are two special cases for parameters
283283
// if it is a subclass of UnityEngine.Component or MonoBehaviour we additionally add the subtype
284284
// this is used by the editor later on to let the writer know WHERE the command will be going
285285
// otherwise we just add the Yarn type of the parameter
286286
if (p.Type.BaseType?.Name == "MonoBehaviour" || p.Type.BaseType?.Name == "Component")
287287
{
288-
paramObject["Type"] = "instance";
289-
paramObject["SubType"] = p.Type.Name;
288+
paramObject["type"] = "instance";
289+
paramObject["subtype"] = p.Type.Name;
290290
}
291291
else
292292
{
293-
// there is one special case of the regular types
294-
// if the parameter has the YarnNodeParameterAttribute on it then we want to say it's a node and not a string
293+
// there is one special case of the regular types which is if you are a string and attributed as a node parameter
295294
var isANodeType = p.Attributes?.Count(a => a.AttributeClass?.Name == "YarnNodeParameterAttribute") > 0;
296295
if (isANodeType && p.Type.SpecialType == SpecialType.System_String)
297296
{
298-
paramObject["Type"] = "node";
297+
paramObject["type"] = "node";
299298
}
300299
else
301300
{
302-
paramObject["Type"] = p.YarnTypeString;
301+
paramObject["type"] = p.YarnTypeString;
303302
}
304303
}
305304

@@ -309,13 +308,13 @@ public string ToJSON()
309308
if (this.Type == ActionType.Function)
310309
{
311310
var retvrn = new Dictionary<string, string>();
312-
retvrn["Type"] = this.YarnReturnTypeString ?? "error";
311+
retvrn["type"] = this.YarnReturnTypeString;
313312

314313
if (!string.IsNullOrWhiteSpace(this.ReturnDescription))
315314
{
316-
retvrn["Description"] = this.ReturnDescription;
315+
retvrn["description"] = this.ReturnDescription;
317316
}
318-
result["Return"] = retvrn;
317+
result["return"] = retvrn;
319318
}
320319

321320
return Yarn.Unity.Editor.Json.Serialize(result);

Editor/Analysis/ActionsRegistrationGenerator~/ActionsGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ public void Execute(GeneratorExecutionContext context)
288288
IEnumerable<string> functionJSON = actions.Where(a => a.Type == ActionType.Function).Select(a => a.ToJSON());
289289

290290
var ysls = "{" +
291-
$@"""Commands"":[{string.Join(",", commandJSON)}]," +
292-
$@"""Functions"":[{string.Join(",", functionJSON)}]" +
291+
@"""version"":2," +
292+
$@"""commands"":[{string.Join(",", commandJSON)}]," +
293+
$@"""functions"":[{string.Join(",", functionJSON)}]" +
293294
"}";
294295

295296
output.WriteLine($"Done.");

Editor/Analysis/Analyser.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,12 +896,27 @@ private static List<Parameter> GetParams(IMethodSymbol symbol, XElement? documen
896896
}
897897
}
898898

899+
// ok here need to make some changes
900+
// if p is variadic it will be array<T> and I need to get just the T
901+
ITypeSymbol parameterType = param.Type;
902+
if (param.IsParams)
903+
{
904+
if (param.Type is IArrayTypeSymbol arrayTypeSymbol)
905+
{
906+
parameterType = arrayTypeSymbol.ElementType;
907+
}
908+
else
909+
{
910+
logger?.WriteLine($"\t{param.Name} is a variadic parameter but isn't an array");
911+
}
912+
}
913+
899914
parameterDocumentation.TryGetValue(param.Name, out var paramDoc);
900915
var p = new Parameter
901916
{
902917
Name = param.Name,
903918
IsOptional = param.IsOptional,
904-
Type = param.Type,
919+
Type = parameterType,
905920
Description = paramDoc,
906921
IsParamsArray = param.IsParams,
907922
Attributes = attributes.Count() == 0 ? null : attributes.ToArray(),
512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)