Skip to content

Commit

Permalink
Don't automatically run generated nodes when no Start node present
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Dec 4, 2024
1 parent 4453960 commit 198593d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
36 changes: 32 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSHeader> Headers { get; set; } = new();
}

[Serializable]
public class JSCompilation
{
public bool Compiled { get; set; } = false;
public List<string> Nodes { get; set; } = new List<string>();
public List<JSNodeInfo> Nodes { get; set; } = new List<JSNodeInfo>();
public Dictionary<string, string> StringTable { get; set; } = new Dictionary<string, string>();
public List<Diagnostic> Diagnostics { get; set; } = new List<Diagnostic>();
public byte[] ProgramData { get; set; } = Array.Empty<byte>();
Expand Down Expand Up @@ -207,7 +221,7 @@ public Task<JSCompilation> SetProgramSource(string source)
JSCompilation compilation = new JSCompilation()
{
Compiled = false,
Nodes = new List<string>(),
Nodes = new List<JSNodeInfo>(),
StringTable = new Dictionary<string, string>(),
Diagnostics = result.Diagnostics.ToList(),
ProgramData = Array.Empty<byte>(),
Expand All @@ -225,8 +239,22 @@ public Task<JSCompilation> 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<string, string>(),
Diagnostics = result.Diagnostics.ToList(),
ProgramData = result.Program?.ToByteArray() ?? Array.Empty<byte>(),
});
Expand Down
30 changes: 23 additions & 7 deletions src/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/yarnspinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 198593d

Please sign in to comment.