Skip to content

Commit 67bacd9

Browse files
committed
generated ysls file now includes game object subclasses for instance commands.
1 parent 804c28c commit 67bacd9

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
### Added
1010

1111
- When using the Unity Localization package Yarn Spinner can now sort the localisation tables based on a lines position in the file.
12-
- This resolves an issue where lines added into the middle of a Yarn file however this does require sorting your localisation table every time you edit your Yarn files
13-
- Defaults to off, can be changed in Yarn Spinners settings in `Edit -> Project Settings -> Yarn Spinner`
14-
- Will log any values found in the localisation table that didn't come from your Yarn files and sort them to the top of the table
12+
- This resolves an issue where lines added into the middle of a Yarn file however this does require sorting your localisation table every time you edit your Yarn files
13+
- Defaults to off, can be changed in Yarn Spinners settings in `Edit -> Project Settings -> Yarn Spinner`
14+
- Will log any values found in the localisation table that didn't come from your Yarn files and sort them to the top of the table
1515
- `YarnSpinnerAssemblyGeneratedYSLSPath` function to the `YarnSpinnerProjectSettings` so that each assembly can have a generated and consistent path.
16+
- Generated YSLS file now includes subtype information for instance commands
17+
- This means a command like `<<move gary left>>` now knows that the `"gary"` is the name of a game object and the specific game object subclass
18+
- Only works at a single level of monobehaviour depth, subclasses of a monobehaviour subclass will not be recognised
1619

1720
### Changed
1821

Editor/Analysis/Action.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,20 @@ public string ToJSON()
275275
paramObject["DefaultValue"] = p.DefaultValueString;
276276
}
277277
paramObject["IsParamsArray"] = p.IsParamsArray;
278-
paramObject["Type"] = p.YarnTypeString;
278+
279+
// there are two special cases for parameters
280+
// if it is a subclass of UnityEngine.Component or MonoBehaviour we additionally add the subtype
281+
// this is used by the editor later on to let the writer know WHERE the command will be going
282+
// otherwise we just add the Yarn type of the parameter
283+
if (p.Type.BaseType?.Name == "MonoBehaviour" || p.Type.BaseType?.Name == "Component")
284+
{
285+
paramObject["Type"] = "instance";
286+
paramObject["SubType"] = p.Type.Name;
287+
}
288+
else
289+
{
290+
paramObject["Type"] = p.YarnTypeString;
291+
}
279292

280293
return paramObject;
281294
}).ToArray());

Editor/Analysis/Analyser.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ private static IEnumerable<Action> GetRuntimeDefinedActions(CompilationUnitSynta
574574
MethodDeclarationSyntax = declaringSyntax,
575575
Declaration = declaringSyntax,
576576
Description = summary,
577-
Parameters = new List<Parameter>(GetParameters(targetSymbol, documentationXML)),
577+
Parameters = GetParams(targetSymbol, documentationXML, logger),
578578
SourceFileName = root.SyntaxTree.FilePath,
579579
DeclarationType = DeclarationType.DirectRegistration,
580580
};
@@ -755,7 +755,7 @@ private static IEnumerable<Action> GetAttributeActions(CompilationUnitSyntax roo
755755
MethodDeclarationSyntax = methodInfo.MethodDeclaration,
756756
IsStatic = methodSymbol.IsStatic,
757757
Declaration = methodInfo.MethodDeclaration,
758-
Parameters = new List<Parameter>(GetParameters(methodSymbol, documentationXML)),
758+
Parameters = GetParams(methodSymbol, documentationXML, logger),
759759
AsyncType = GetAsyncType(methodSymbol),
760760
SemanticModel = model,
761761
Description = summary,
@@ -800,16 +800,46 @@ private static AsyncType GetAsyncType(IMethodSymbol symbol)
800800
{
801801
case "global::Yarn.Unity.YarnTask":
802802
case "global::System.Threading.Tasks.Task":
803-
case "global::Cysharp.Threading.Tasks.UniTask": // double check this one
803+
case "global::Cysharp.Threading.Tasks.UniTask":
804804
case "global::UnityEngine.Awaitable":
805805
return AsyncType.AsyncTask;
806806
default:
807807
return default;
808808
};
809809
}
810810

811-
private static IEnumerable<Parameter> GetParameters(IMethodSymbol symbol, XElement? documentationXML)
811+
private static List<Parameter> GetParams(IMethodSymbol symbol, XElement? documentationXML, ILogger? logger)
812812
{
813+
List<Parameter> parameters = new List<Parameter>();
814+
815+
// if this is an instance command registered via the yarn attribute we need to do an extra step
816+
// we will need to create and add in a new parameter to the front of the parameters list
817+
// to represent the game object that we will do a lookup for
818+
var isAttributeRegistered = symbol.GetAttributes().Where(a => a.AttributeClass?.Name == "YarnCommandAttribute").Count() > 0;
819+
if (isAttributeRegistered && !symbol.IsStatic)
820+
{
821+
logger?.WriteLine("Command has been registered via the attribute, will be adding a target parameter.");
822+
var p = new Parameter
823+
{
824+
Name = "target",
825+
IsOptional = false,
826+
Type = symbol.ContainingType,
827+
Description = "The name of the Game Object the runner will search for to run this command upon. This will be done through a normal GameObject.Find Unity call.",
828+
IsParamsArray = false,
829+
};
830+
parameters.Insert(0, p);
831+
}
832+
833+
if (symbol.Parameters.Count() > 0)
834+
{
835+
logger?.WriteLine($"Processing {symbol.Name} parameters");
836+
}
837+
else
838+
{
839+
logger?.WriteLine($"{symbol.Name} has no parameters");
840+
return parameters;
841+
}
842+
813843
var parameterDocumentation = new Dictionary<string, string>();
814844

815845
if (documentationXML != null)
@@ -833,8 +863,10 @@ private static IEnumerable<Parameter> GetParameters(IMethodSymbol symbol, XEleme
833863

834864
foreach (var param in symbol.Parameters)
835865
{
866+
logger?.WriteLine($"\t{param.Name} is a {param.Type.ToDisplayString()}");
867+
836868
parameterDocumentation.TryGetValue(param.Name, out var paramDoc);
837-
yield return new Parameter
869+
var p = new Parameter
838870
{
839871
Name = param.Name,
840872
IsOptional = param.IsOptional,
@@ -843,7 +875,10 @@ private static IEnumerable<Parameter> GetParameters(IMethodSymbol symbol, XEleme
843875
IsParamsArray = param.IsParams,
844876
DefaultValueString = param.HasExplicitDefaultValue ? param.ExplicitDefaultValue?.ToString() : null,
845877
};
878+
parameters.Add(p);
846879
}
880+
881+
return parameters;
847882
}
848883

849884
internal static bool IsAttributeYarnCommand(AttributeData attribute)
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)