From 198593d7e4f51e90ac50211adae118ac92bc9e51 Mon Sep 17 00:00:00 2001 From: Jon Manning Date: Wed, 4 Dec 2024 22:35:33 +1100 Subject: [PATCH] Don't automatically run generated nodes when no Start node present --- Program.cs | 36 ++++++++++++++++++++++++++++++++---- src/playground.ts | 30 +++++++++++++++++++++++------- src/yarnspinner.ts | 2 +- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/Program.cs b/Program.cs index 987c42a..d8217f5 100644 --- a/Program.cs +++ b/Program.cs @@ -117,11 +117,25 @@ public JSDialogue(IVariableStorage variableStorage) : base(variableStorage) this.ContentSaliencyStrategy = new Yarn.Saliency.RandomBestLeastRecentlyViewedSalienceStrategy(variableStorage); } + [Serializable] + public class JSHeader + { + public string Key { get; set; } = ""; + public string Value { get; set; } = ""; + } + + [Serializable] + public class JSNodeInfo + { + public string Name { get; set; } = ""; + public List Headers { get; set; } = new(); + } + [Serializable] public class JSCompilation { public bool Compiled { get; set; } = false; - public List Nodes { get; set; } = new List(); + public List Nodes { get; set; } = new List(); public Dictionary StringTable { get; set; } = new Dictionary(); public List Diagnostics { get; set; } = new List(); public byte[] ProgramData { get; set; } = Array.Empty(); @@ -207,7 +221,7 @@ public Task SetProgramSource(string source) JSCompilation compilation = new JSCompilation() { Compiled = false, - Nodes = new List(), + Nodes = new List(), StringTable = new Dictionary(), Diagnostics = result.Diagnostics.ToList(), ProgramData = Array.Empty(), @@ -225,8 +239,22 @@ public Task SetProgramSource(string source) return Task.FromResult(new JSCompilation { Compiled = true, - Nodes = result.Program.Nodes.Keys.ToList(), - StringTable = result.StringTable.ToDictionary(kv => kv.Key, kv => kv.Value.text), + Nodes = result.Program.Nodes.Values.Select(n => + { + return new JSNodeInfo + { + Name = n.Name, + Headers = n.Headers.Select(h => + { + return new JSHeader + { + Key = h.Key, + Value = h.Value + }; + }).ToList() + }; + }).ToList(), + StringTable = result.StringTable?.ToDictionary(kv => kv.Key, kv => kv.Value.text ?? "") ?? new Dictionary(), Diagnostics = result.Diagnostics.ToList(), ProgramData = result.Program?.ToByteArray() ?? Array.Empty(), }); diff --git a/src/playground.ts b/src/playground.ts index 3109aba..daaf3b4 100644 --- a/src/playground.ts +++ b/src/playground.ts @@ -333,14 +333,30 @@ export async function load(script: string) { if (compilation.compiled) { let nodeToRun; - if (compilation.nodes.includes("Start")) { - // If we have a node name Start, start from that - nodeToRun = "Start"; - } else { - // Otherwise, use the first node present. - nodeToRun = compilation.nodes[0]; + + // If we have a node name Start, start from that + nodeToRun = compilation.nodes.find((n) => n.name === "Start"); + + if (!nodeToRun) { + // Otherwise, use the first node present that is not internal or part of + // a node group. + + nodeToRun = compilation.nodes.find((n) => { + // Is the node title prefixed with a tag that indicates that it's internal? + if (n.name.startsWith("$Yarn.Internal")) { + return false; + } + + // Does the node have a header that indicates it's part of a node group? + if (n.headers.find((h) => h.key == "$Yarn.Internal.NodeGroup")) { + return false; + } + + // The node is able to run. + return true; + }); } - await dialogue.startDialogue(nodeToRun); + await dialogue.startDialogue(nodeToRun.name); } }); diff --git a/src/yarnspinner.ts b/src/yarnspinner.ts index 25a6495..5f24b23 100644 --- a/src/yarnspinner.ts +++ b/src/yarnspinner.ts @@ -17,7 +17,7 @@ type StringTable = { [lineID: string]: string }; interface CompileResult { compiled: boolean; - nodes: string[]; + nodes: { name: string; headers: { key: string; value: string }[] }[]; stringTable: StringTable; diagnostics: Diagnostic[]; programData?: Uint8Array;