@liminalia/runtime 0.0.0-stage → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +106 -0
- package/dist/src/chat.js +60 -0
- package/dist/src/commands-play.js +300 -0
- package/dist/src/commands.js +534 -0
- package/dist/src/coverage.js +208 -0
- package/dist/src/headless.js +109 -0
- package/dist/src/host.js +1 -0
- package/dist/src/index.js +15 -0
- package/dist/src/prefetch.js +80 -0
- package/dist/src/program.js +131 -0
- package/dist/src/project-options.js +84 -0
- package/dist/src/retranslate.js +44 -0
- package/dist/src/route.js +242 -0
- package/dist/src/runtime.js +1929 -0
- package/dist/src/save.js +51 -0
- package/dist/src/scenario-test.js +200 -0
- package/dist/src/state.js +62 -0
- package/dist/src/value.js +464 -0
- package/dist/src/voice-observe.js +82 -0
- package/package.json +33 -4
- package/src/chat.ts +109 -0
- package/src/commands-play.ts +280 -0
- package/src/commands.ts +457 -0
- package/src/coverage.ts +257 -0
- package/src/headless.ts +139 -0
- package/src/host.ts +116 -0
- package/src/index.ts +16 -0
- package/src/prefetch.ts +78 -0
- package/src/program.ts +186 -0
- package/src/project-options.ts +128 -0
- package/src/retranslate.ts +67 -0
- package/src/route.ts +365 -0
- package/src/runtime.ts +2143 -0
- package/src/save.ts +60 -0
- package/src/scenario-test.ts +218 -0
- package/src/state.ts +353 -0
- package/src/value.ts +443 -0
- package/src/voice-observe.ts +85 -0
- package/types/src/chat.d.ts +63 -0
- package/types/src/commands-play.d.ts +2 -0
- package/types/src/commands.d.ts +2 -0
- package/types/src/coverage.d.ts +100 -0
- package/types/src/headless.d.ts +77 -0
- package/types/src/host.d.ts +140 -0
- package/types/src/index.d.ts +16 -0
- package/types/src/prefetch.d.ts +18 -0
- package/types/src/program.d.ts +107 -0
- package/types/src/project-options.d.ts +103 -0
- package/types/src/retranslate.d.ts +39 -0
- package/types/src/route.d.ts +181 -0
- package/types/src/runtime.d.ts +553 -0
- package/types/src/save.d.ts +21 -0
- package/types/src/scenario-test.d.ts +43 -0
- package/types/src/state.d.ts +357 -0
- package/types/src/value.d.ts +61 -0
- package/types/src/voice-observe.d.ts +26 -0
- package/README.md +0 -3
package/dist/src/save.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* セーブデータの版。セーブの形(SaveData や舞台の状態)を、古いセーブがそのまま読めない形に変えたら一つ上げ、
|
|
3
|
+
* MIGRATIONS の末尾に「一つ前の版 → この版」の変換を足す。変換は消さない(どれだけ古いセーブも順にたどって今の版にする)。
|
|
4
|
+
* 項目を足すだけで、無ければ既定で補える変更(読み込み時に補う)は版を上げない。
|
|
5
|
+
*/
|
|
6
|
+
export const SAVE_VERSION = 1;
|
|
7
|
+
/** MIGRATIONS[i] は版 i + 1 のセーブを版 i + 2 にする。 */
|
|
8
|
+
const MIGRATIONS = [];
|
|
9
|
+
export class SaveVersionError extends Error {
|
|
10
|
+
problem;
|
|
11
|
+
constructor(problem, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.problem = problem;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** セーブを今の版にする。読めなければ SaveVersionError。元の値は変えない。 */
|
|
17
|
+
export function migrateSave(raw) {
|
|
18
|
+
return migrateWith(raw, MIGRATIONS, SAVE_VERSION);
|
|
19
|
+
}
|
|
20
|
+
/** 読めないセーブなら理由、読めるなら null(ロードの前に知らせるため)。 */
|
|
21
|
+
export function saveProblem(raw) {
|
|
22
|
+
try {
|
|
23
|
+
migrateSave(raw);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
return e instanceof SaveVersionError ? e.problem : "broken";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** 変換の連なりをたどる(テストのため変換と今の版を渡せる)。 */
|
|
31
|
+
export function migrateWith(raw, migrations, current) {
|
|
32
|
+
if (!isRaw(raw) || !isRaw(raw.position) || typeof raw.position.unit !== "string")
|
|
33
|
+
throw new SaveVersionError("broken", "セーブデータの形をしていません");
|
|
34
|
+
const version = raw.version;
|
|
35
|
+
if (typeof version !== "number" || !Number.isInteger(version) || version < 1)
|
|
36
|
+
throw new SaveVersionError("broken", `セーブデータのバージョン(${String(version)})を読み込めません`);
|
|
37
|
+
if (version > current)
|
|
38
|
+
throw new SaveVersionError("newer", `新しいバージョンのゲームで作られたセーブデータです(セーブデータのバージョン ${version}、このゲームは ${current} まで対応)`);
|
|
39
|
+
let save = structuredClone(raw);
|
|
40
|
+
for (let v = version; v < current; v++) {
|
|
41
|
+
const step = migrations[v - 1];
|
|
42
|
+
if (!step)
|
|
43
|
+
throw new SaveVersionError("broken", `バージョン ${v} のセーブデータを ${v + 1} に変換する処理がありません`);
|
|
44
|
+
save = step(save);
|
|
45
|
+
save.version = v + 1;
|
|
46
|
+
}
|
|
47
|
+
return save;
|
|
48
|
+
}
|
|
49
|
+
function isRaw(v) {
|
|
50
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
51
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { endingKinds, endingLabel, endingMatches, endingRef, parse, parseExpr, parseTargetText, variablePaths } from "@liminalia/lang";
|
|
2
|
+
import { HeadlessHost } from "./headless.js";
|
|
3
|
+
import { Runtime } from "./runtime.js";
|
|
4
|
+
import { display, evaluate, EvalError, truthy } from "./value.js";
|
|
5
|
+
/** テストの既定のページ数の上限(これに達したら、終わらないテストとして失敗にする)。 */
|
|
6
|
+
export const DEFAULT_TEST_LIMIT = 5000;
|
|
7
|
+
export async function runScenarioTest(test, o) {
|
|
8
|
+
const failures = [];
|
|
9
|
+
const fail = (line, ja, en, diagnostic) => failures.push({ line, ja, en, ...(diagnostic ? { diagnostic } : {}) });
|
|
10
|
+
const limit = test.limit ?? DEFAULT_TEST_LIMIT;
|
|
11
|
+
const host = new HeadlessHost({ loadSource: (f) => o.loadSource(f), choices: test.choices.map((c) => c.pick), whenChoicesRunOut: test.rest, maxPages: limit });
|
|
12
|
+
const reached = new Set();
|
|
13
|
+
const files = new Set();
|
|
14
|
+
const runtime = new Runtime(host, {
|
|
15
|
+
...o.runtime,
|
|
16
|
+
seed: test.seed ?? 1,
|
|
17
|
+
onStep: (at) => {
|
|
18
|
+
reached.add(`${at.file}:${at.line}`);
|
|
19
|
+
files.add(at.file);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
let limited = false;
|
|
23
|
+
host.onLimit = () => {
|
|
24
|
+
limited = true;
|
|
25
|
+
runtime.stop();
|
|
26
|
+
};
|
|
27
|
+
const value = (src) => {
|
|
28
|
+
try {
|
|
29
|
+
return evaluate(src.value, { lookup: () => null, random: () => 0 });
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
fail(src.line, `set の式を求められません: ${e instanceof Error ? e.message : String(e)}`, `Can't evaluate the set expression: ${src.source}`);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
// 始める前の値(system は実行器に、game と temp は始めるときに入れる)
|
|
37
|
+
const start = {};
|
|
38
|
+
for (const s of test.sets) {
|
|
39
|
+
const v = value(s);
|
|
40
|
+
if (v === undefined)
|
|
41
|
+
continue;
|
|
42
|
+
if (s.path.startsWith("system."))
|
|
43
|
+
runtime.setVariable(s.path, v);
|
|
44
|
+
else
|
|
45
|
+
start[s.path] = v;
|
|
46
|
+
}
|
|
47
|
+
let result;
|
|
48
|
+
if (test.fromState !== undefined) {
|
|
49
|
+
let save;
|
|
50
|
+
try {
|
|
51
|
+
const raw = o.readState?.(test.fromState);
|
|
52
|
+
save = structuredClone((raw && typeof raw === "object" && raw.save ? raw.save : raw));
|
|
53
|
+
if (!save || typeof save !== "object" || !save.position)
|
|
54
|
+
throw new Error("セーブの形ではありません");
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
fail(test.line, `from-state ${test.fromState} を読めません(${e instanceof Error ? e.message : String(e)})`, `Can't read from-state ${test.fromState}`);
|
|
58
|
+
return { name: test.name, line: test.line, ok: false, failures, result: { reason: "error", at: { unit: "", index: 0 }, message: "from-state" }, pages: 0, choices: [], game: {} };
|
|
59
|
+
}
|
|
60
|
+
for (const [path, v] of Object.entries(start)) {
|
|
61
|
+
if (path.startsWith("temp."))
|
|
62
|
+
runtime.setVariable(path, v);
|
|
63
|
+
else
|
|
64
|
+
setPath(save.game, path.slice("game.".length), v);
|
|
65
|
+
}
|
|
66
|
+
result = await runtime.load(save);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
result = await runtime.start(test.from ?? o.entry, { variables: start });
|
|
70
|
+
}
|
|
71
|
+
const trace = host.trace;
|
|
72
|
+
const pages = trace.flatMap((t) => (t.type === "page" ? [t] : []));
|
|
73
|
+
const choices = trace.flatMap((t) => (t.type === "choice" ? [{ options: t.options, selected: t.selected }] : []));
|
|
74
|
+
// 選ぶ文が無かった選択肢と、使わなかった choose の行
|
|
75
|
+
let made = 0;
|
|
76
|
+
for (const [i, t] of trace.entries()) {
|
|
77
|
+
if (t.type === "choice")
|
|
78
|
+
made++;
|
|
79
|
+
if (t.type === "diagnostic" && t.diagnostic.code === "unknown-option") {
|
|
80
|
+
const c = test.choices[made];
|
|
81
|
+
// 並んでいた選択肢(診断のすぐ後の選択の記録)。日本語の文は診断に入っている
|
|
82
|
+
const shown = trace.slice(i + 1).find((x) => x.type === "choice");
|
|
83
|
+
const options = shown?.type === "choice" ? ` (${shown.options.join(" / ")})` : "";
|
|
84
|
+
fail(c?.line ?? test.line, t.diagnostic.message, `The option to choose isn't there: ${String(c?.pick ?? "")}${options}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const unused = test.choices[Math.min(made, test.choices.length)];
|
|
88
|
+
if (made < test.choices.length && unused)
|
|
89
|
+
fail(unused.line, `この choose を使う前に終わりました(選択肢は ${made} 回)`, `It ended before using this choose (${made} choices made)`);
|
|
90
|
+
// 実行時のエラーは、書かなくても失敗にする
|
|
91
|
+
const diagnostics = trace.flatMap((t) => (t.type === "diagnostic" && t.diagnostic.code !== "unknown-option" ? [t.diagnostic] : []));
|
|
92
|
+
for (const d of diagnostics.filter((x) => x.severity === "error"))
|
|
93
|
+
fail(test.line, `実行時のエラー(${d.file}:${d.line}): ${d.message}`, `Runtime error (${d.file}:${d.line})`, d);
|
|
94
|
+
if (result.reason === "max-steps")
|
|
95
|
+
fail(test.line, "命令の数の上限に達しました(終わらない繰り返しがないか確かめます)", "Reached the step limit (check for an endless loop)");
|
|
96
|
+
if (limited && test.limit === undefined)
|
|
97
|
+
fail(test.line, `ページの数の上限(${limit})に達しました。終わらないなら choose を足すか、limit を書きます`, `Reached the page limit (${limit}). Add choose lines or write limit`);
|
|
98
|
+
for (const e of test.expects) {
|
|
99
|
+
switch (e.kind) {
|
|
100
|
+
case "expr": {
|
|
101
|
+
let v;
|
|
102
|
+
try {
|
|
103
|
+
v = runtime.eval(e.expr);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
if (!(err instanceof EvalError))
|
|
107
|
+
throw err;
|
|
108
|
+
fail(e.line, `expect ${e.source} を求められません: ${err.message}`, `Can't evaluate expect ${e.source}`);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (!truthy(v)) {
|
|
112
|
+
const values = [...new Set(variablePaths(e.expr))].map((p) => `${p} = ${shown(runtime.eval(parseExpr(p)))}`);
|
|
113
|
+
fail(e.line, `expect ${e.source} が偽です${values.length > 0 ? `(${values.join("、")})` : ""}`, `expect ${e.source} is false${values.length > 0 ? ` (${values.join(", ")})` : ""}`);
|
|
114
|
+
}
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "reach": {
|
|
118
|
+
const hit = await reachedPlace(e.place, reached, files, o.loadSource);
|
|
119
|
+
if (hit === null)
|
|
120
|
+
fail(e.line, `${e.place} はありません`, `${e.place} doesn't exist`);
|
|
121
|
+
else if (hit === e.not)
|
|
122
|
+
fail(e.line, e.not ? `${e.place} を通りました` : `${e.place} を通りませんでした`, e.not ? `Went through ${e.place}` : `Didn't go through ${e.place}`);
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case "text": {
|
|
126
|
+
const found = pages.some((p) => p.text.includes(e.text) || (p.label ?? p.speaker ?? "").includes(e.text));
|
|
127
|
+
if (found === e.not)
|
|
128
|
+
fail(e.line, e.not ? `「${e.text}」が出ました` : `「${e.text}」はどのページにも出ませんでした`, e.not ? `"${e.text}" was shown` : `"${e.text}" wasn't shown on any page`);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
case "ending": {
|
|
132
|
+
// 種類(bad など)で引く。種類でない文字は、エンディングの名前(title=。前の書き方なら書いた文言)に含むもの
|
|
133
|
+
const kinds = endingKinds(o.runtime?.endingKinds);
|
|
134
|
+
const endings = trace.flatMap((t) => {
|
|
135
|
+
if (t.type !== "command" || runtime.registry.get(t.name)?.ends !== true)
|
|
136
|
+
return [];
|
|
137
|
+
const ref = endingRef(t.args.ending, t.args.title, kinds);
|
|
138
|
+
return ref ? [ref] : [];
|
|
139
|
+
});
|
|
140
|
+
const seen = endings.map(endingLabel);
|
|
141
|
+
if (!endings.some((r) => endingMatches(r, e.name, kinds)))
|
|
142
|
+
fail(e.line, `エンディング ${e.name} で終わりませんでした${seen.length > 0 ? `(${seen.join("、")})` : ""}`, `Didn't end with the ending ${e.name}${seen.length > 0 ? ` (${seen.join(", ")})` : ""}`);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case "end": {
|
|
146
|
+
// プレイを終えるコマンド(@title-screen など)で止まったのも終わり(選択肢を使い切ったり、ページの上限で止めたりしたのとは分ける)
|
|
147
|
+
const last = trace.findLast((t) => t.type !== "diagnostic");
|
|
148
|
+
const endedByCommand = result.reason === "stopped" && !limited && last?.type === "command" && runtime.registry.get(last.name)?.ends === true;
|
|
149
|
+
if (result.reason !== "end" && !endedByCommand)
|
|
150
|
+
fail(e.line, `台本の終わりまで流れませんでした(${result.reason})`, `Didn't run to the end of the script (${result.reason})`);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case "no-errors":
|
|
154
|
+
// エラーは上で知らせている
|
|
155
|
+
break;
|
|
156
|
+
case "no-warnings":
|
|
157
|
+
for (const d of diagnostics.filter((x) => x.severity === "warning"))
|
|
158
|
+
fail(e.line, `実行時の警告(${d.file}:${d.line}): ${d.message}`, `Runtime warning (${d.file}:${d.line})`, d);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return { name: test.name, line: test.line, ok: failures.length === 0, failures, result, pages: pages.length, choices, game: structuredClone(runtime.state.game) };
|
|
163
|
+
}
|
|
164
|
+
/** 場所を通ったか(`ファイル`、`ファイル#ラベル`、`ファイル:行`)。ファイルかラベルが無ければ null。 */
|
|
165
|
+
async function reachedPlace(place, reached, files, loadSource) {
|
|
166
|
+
const line = /^(.+):(\d+)$/.exec(place);
|
|
167
|
+
if (line)
|
|
168
|
+
return reached.has(`${line[1]}:${line[2]}`);
|
|
169
|
+
const target = parseTargetText(place);
|
|
170
|
+
if (!target?.file)
|
|
171
|
+
return null;
|
|
172
|
+
if (target.label === null) {
|
|
173
|
+
try {
|
|
174
|
+
await loadSource(target.file);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
return files.has(target.file);
|
|
180
|
+
}
|
|
181
|
+
let source;
|
|
182
|
+
try {
|
|
183
|
+
source = await loadSource(target.file);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const label = parse(source, target.file).body.find((s) => s.kind === "label" && s.name === target.label);
|
|
189
|
+
return label ? reached.has(`${target.file}:${label.line}`) : null;
|
|
190
|
+
}
|
|
191
|
+
function setPath(root, path, value) {
|
|
192
|
+
const parts = path.split(".");
|
|
193
|
+
let o = root;
|
|
194
|
+
for (const p of parts.slice(0, -1))
|
|
195
|
+
o = (o[p] && typeof o[p] === "object" && !Array.isArray(o[p]) ? o[p] : (o[p] = {}));
|
|
196
|
+
o[parts.at(-1)] = value;
|
|
197
|
+
}
|
|
198
|
+
function shown(v) {
|
|
199
|
+
return typeof v === "string" ? JSON.stringify(v) : v === null ? "null" : display(v);
|
|
200
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { SAVE_VERSION } from "./save.js";
|
|
2
|
+
export { voiceFile } from "@liminalia/lang";
|
|
3
|
+
/** その話者のボイスに掛け続けているフィルター(@voice-filter。話者のものが無ければすべての話者のもの)。 */
|
|
4
|
+
export function voiceFilterFor(stage, who) {
|
|
5
|
+
const filters = stage.voiceFilter;
|
|
6
|
+
if (!filters)
|
|
7
|
+
return null;
|
|
8
|
+
return (who !== null ? filters[who] : undefined) ?? filters[""] ?? null;
|
|
9
|
+
}
|
|
10
|
+
/** 命令の filter・filter-strength を状態の形にする(none・書いていない・強さ 0 は null)。 */
|
|
11
|
+
export function filterArg(a) {
|
|
12
|
+
if (typeof a.filter !== "string" || a.filter === "none")
|
|
13
|
+
return null;
|
|
14
|
+
const strength = typeof a["filter-strength"] === "number" ? Math.max(0, Math.min(100, a["filter-strength"])) : 100;
|
|
15
|
+
return strength === 0 ? null : { type: a.filter, strength };
|
|
16
|
+
}
|
|
17
|
+
export function initialStage() {
|
|
18
|
+
return {
|
|
19
|
+
background: null,
|
|
20
|
+
sprites: {},
|
|
21
|
+
bgm: null,
|
|
22
|
+
loopingSe: {},
|
|
23
|
+
window: false,
|
|
24
|
+
mask: null,
|
|
25
|
+
camera: { zoom: 1, x: 0, y: 0, rotate: 0 },
|
|
26
|
+
textSpeed: null,
|
|
27
|
+
extra: {},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function createState(unit, seed = 1) {
|
|
31
|
+
return {
|
|
32
|
+
version: SAVE_VERSION,
|
|
33
|
+
position: { unit, index: 0 },
|
|
34
|
+
locals: {},
|
|
35
|
+
callStack: [],
|
|
36
|
+
game: {},
|
|
37
|
+
temp: {},
|
|
38
|
+
stage: initialStage(),
|
|
39
|
+
seed,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function toSaveData(state) {
|
|
43
|
+
const { temp: _, ...save } = state;
|
|
44
|
+
return clonePlain(save);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 状態の深い写し。状態はセーブ(JSON)に書ける値だけなので、配列とふつうのオブジェクトを辿って写す。
|
|
48
|
+
* structuredClone と同じ結果だが、台詞ごとに取る写し(再開の控え)では数倍速い。ふつうでないもの(Map、Date など)はそこだけ structuredClone にする。
|
|
49
|
+
*/
|
|
50
|
+
export function clonePlain(value) {
|
|
51
|
+
if (value === null || typeof value !== "object")
|
|
52
|
+
return value;
|
|
53
|
+
if (Array.isArray(value))
|
|
54
|
+
return value.map(clonePlain);
|
|
55
|
+
const proto = Object.getPrototypeOf(value);
|
|
56
|
+
if (proto !== Object.prototype && proto !== null)
|
|
57
|
+
return structuredClone(value);
|
|
58
|
+
const out = {};
|
|
59
|
+
for (const key of Object.keys(value))
|
|
60
|
+
out[key] = clonePlain(value[key]);
|
|
61
|
+
return out;
|
|
62
|
+
}
|