@team-antioch/sc2-adjutant 0.1.0 → 0.1.1
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/README.md +2 -0
- package/data/blizzard-baseline.json +3061 -0
- package/data/button-hotkeys.json +5722 -0
- package/data/game-assets.json +50909 -0
- package/data/package-entries.json +32361 -2
- package/dist/capture/bank-fixtures.d.ts +13 -0
- package/dist/capture/bank-fixtures.js +35 -0
- package/dist/capture/bank-snapshots.d.ts +95 -0
- package/dist/capture/bank-snapshots.js +302 -0
- package/dist/capture/editor-broker-client.d.ts +27 -4
- package/dist/capture/editor-broker-client.js +3 -3
- package/dist/capture/editor-broker.js +154 -9
- package/dist/capture/effect-tour.d.ts +113 -0
- package/dist/capture/effect-tour.js +283 -0
- package/dist/capture/effect-video.d.ts +74 -0
- package/dist/capture/effect-video.js +173 -0
- package/dist/capture/exemplar-capture.d.ts +4 -0
- package/dist/capture/exemplar-capture.js +4 -1
- package/dist/capture/shots.d.ts +6 -1
- package/dist/capture/shots.js +1 -1
- package/dist/capture/stop-tour.d.ts +3 -1
- package/dist/capture/stop-tour.js +1 -1
- package/dist/capture/style-pack-captures.d.ts +67 -0
- package/dist/capture/style-pack-captures.js +172 -0
- package/dist/catalog/stock-catalog.d.ts +8 -0
- package/dist/catalog/stock-catalog.js +13 -0
- package/dist/cli/capture-exemplars.js +18 -32
- package/dist/cli/effect-tour.d.ts +2 -0
- package/dist/cli/effect-tour.js +378 -0
- package/dist/cli/make-frames.d.ts +1 -0
- package/dist/cli/make-frames.js +21 -0
- package/dist/cli/sound-mix.d.ts +1 -0
- package/dist/cli/sound-mix.js +75 -0
- package/dist/cli/study.js +6 -26
- package/dist/domain/blueprint-trigger.d.ts +355 -3
- package/dist/domain/blueprint-trigger.js +149 -8
- package/dist/domain/library-interface.d.ts +50 -29
- package/dist/domain/library-interface.js +54 -41
- package/dist/domain/mission-blueprint.d.ts +13 -6
- package/dist/domain/mission-blueprint.js +6 -0
- package/dist/domain/mod-blueprint.d.ts +4 -4
- package/dist/host/game-logs.d.ts +63 -0
- package/dist/host/game-logs.js +178 -0
- package/dist/host/game-ownership.d.ts +37 -0
- package/dist/host/game-ownership.js +84 -0
- package/dist/host/game-run-report.d.ts +63 -0
- package/dist/host/game-run-report.js +119 -0
- package/dist/host/known-errors.d.ts +34 -0
- package/dist/host/known-errors.js +73 -0
- package/dist/init/init-workspace.d.ts +3 -1
- package/dist/init/init-workspace.js +10 -1
- package/dist/map/convert-mission-triggers.d.ts +115 -7
- package/dist/map/convert-mission-triggers.js +527 -47
- package/dist/map/editor-changes.d.ts +1 -1
- package/dist/map/editor-form.d.ts +5 -0
- package/dist/map/editor-form.js +72 -0
- package/dist/map/game-packages.d.ts +3 -2
- package/dist/map/game-packages.js +5 -2
- package/dist/map/kept-references.js +47 -1
- package/dist/map/kept-session.js +3 -1
- package/dist/map/outline-diff.d.ts +24 -0
- package/dist/map/outline-diff.js +95 -0
- package/dist/map/outline-trigger.d.ts +7 -0
- package/dist/map/outline-trigger.js +29 -20
- package/dist/map/ownership-plan.d.ts +6 -2
- package/dist/map/ownership-plan.js +1 -1
- package/dist/map/package-output.js +13 -8
- package/dist/map/tune-kept-triggers.d.ts +7 -0
- package/dist/map/tune-kept-triggers.js +33 -6
- package/dist/map/write-blueprint-trigger.d.ts +9 -4
- package/dist/map/write-blueprint-trigger.js +46 -13
- package/dist/map/write-map-package.js +20 -9
- package/dist/mcp/capture-jobs.d.ts +2 -0
- package/dist/mcp/capture-jobs.js +2 -0
- package/dist/mcp/server.d.ts +11 -2
- package/dist/mcp/server.js +17 -4
- package/dist/mcp/tools/bank-report.d.ts +19 -0
- package/dist/mcp/tools/bank-report.js +51 -0
- package/dist/mcp/tools/blueprint.js +18 -14
- package/dist/mcp/tools/capture.d.ts +4 -0
- package/dist/mcp/tools/capture.js +22 -5
- package/dist/mcp/tools/catalog.d.ts +1 -1
- package/dist/mcp/tools/catalog.js +30 -1
- package/dist/mcp/tools/compile.d.ts +1 -1
- package/dist/mcp/tools/compile.js +100 -9
- package/dist/mcp/tools/editor-handoff.d.ts +4 -1
- package/dist/mcp/tools/editor-handoff.js +6 -3
- package/dist/mcp/tools/style-pack.d.ts +11 -0
- package/dist/mcp/tools/style-pack.js +245 -0
- package/dist/mcp/tools/tool-group.d.ts +9 -0
- package/dist/mods/button-hotkeys.d.ts +45 -0
- package/dist/mods/button-hotkeys.js +185 -0
- package/dist/mods/compile-mod.js +19 -5
- package/dist/mods/custom-doodads.d.ts +21 -1
- package/dist/mods/custom-doodads.js +53 -7
- package/dist/mods/data-rules.js +8 -1
- package/dist/mods/effective-entry.d.ts +18 -0
- package/dist/mods/effective-entry.js +52 -0
- package/dist/mods/function-search.js +12 -0
- package/dist/mods/library-xml.d.ts +34 -5
- package/dist/mods/library-xml.js +94 -22
- package/dist/mods/sound-mix.d.ts +92 -0
- package/dist/mods/sound-mix.js +195 -0
- package/dist/plugins/check-workspace.js +22 -3
- package/dist/plugins/plugin-api.d.ts +59 -5
- package/dist/plugins/plugin-api.js +6 -0
- package/dist/plugins/plugin-sdk.d.ts +13 -1
- package/dist/plugins/plugin-sdk.js +16 -1
- package/dist/plugins/plugin-sources.js +1 -0
- package/dist/plugins/registry.d.ts +4 -0
- package/dist/plugins/registry.js +6 -2
- package/dist/plugins/workspace.d.ts +5 -0
- package/dist/plugins/workspace.js +10 -2
- package/dist/study/blizzard-baseline.d.ts +76 -0
- package/dist/study/blizzard-baseline.js +137 -0
- package/dist/study/exemplar-report.d.ts +71 -0
- package/dist/study/exemplar-report.js +163 -0
- package/dist/study/find-places.d.ts +127 -0
- package/dist/study/find-places.js +306 -0
- package/dist/study/load-map-package.d.ts +3 -0
- package/dist/study/load-map-package.js +11 -0
- package/dist/study/map-study.d.ts +1 -0
- package/dist/study/map-study.js +3 -0
- package/dist/study/measure-lighting.d.ts +6 -0
- package/dist/study/measure-lighting.js +1 -1
- package/dist/study/measures.d.ts +225 -0
- package/dist/study/measures.js +366 -0
- package/dist/study/scaffold-style-pack.d.ts +58 -0
- package/dist/study/scaffold-style-pack.js +261 -0
- package/dist/study/study-exemplars.d.ts +67 -0
- package/dist/study/study-exemplars.js +133 -0
- package/dist/triggers/blueprint-triggers.d.ts +71 -11
- package/dist/triggers/blueprint-triggers.js +403 -48
- package/dist/triggers/kept-trigger-galaxy.d.ts +29 -0
- package/dist/triggers/kept-trigger-galaxy.js +103 -7
- package/dist/triggers/splice-map-script.d.ts +2 -0
- package/dist/triggers/splice-map-script.js +309 -8
- package/dist/triggers/write-triggers-xml.js +20 -5
- package/dist/ui/frame-path-check.d.ts +43 -4
- package/dist/ui/frame-path-check.js +121 -13
- package/dist/ui/frame-paths.d.ts +21 -1
- package/dist/ui/frame-paths.js +26 -6
- package/dist/ui/toast.d.ts +4 -5
- package/dist/ui/toast.js +6 -10
- package/examples/example-campaign/README.md +27 -0
- package/examples/example-campaign/missions/ashfall-relay/blueprint.json +11 -1
- package/examples/example-campaign/missions/ashfall-relay/triggers/ash-tremors.json +69 -0
- package/examples/example-campaign/missions/ashfall-relay/triggers/start-ash-tremors.json +31 -0
- package/package.json +3 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Where a Workspace keeps fixtures `save_bank_fixture` makes, beside the Missions. */
|
|
2
|
+
export declare const BANK_FIXTURES_FOLDER = "bank-fixtures";
|
|
3
|
+
export type MissionBankFixture = {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly dir: string;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Why the fixture `declared` (as a Blueprint names it) can't be used, if it can't: it is outside
|
|
9
|
+
* the Workspace, isn't a folder, or holds no `.SC2Bank` file.
|
|
10
|
+
*/
|
|
11
|
+
export declare function bankFixtureProblem(workspaceRoot: string, blueprintPath: string, declared: string): Promise<string | undefined>;
|
|
12
|
+
/** The fixture a Blueprint names, resolved; refused with the reason when it can't be used. Undefined when it names none. */
|
|
13
|
+
export declare function missionBankFixture(workspaceRoot: string, blueprintPath: string, declared: string | undefined): Promise<MissionBankFixture | undefined>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// A Mission's bank fixture (#55): a folder of `.SC2Bank` files in the Workspace that a test game
|
|
2
|
+
// plays on instead of the author's own banks, so a Mission can be tested from a given campaign
|
|
3
|
+
// state (after mission 2, with research X). The Blueprint's `bankFixture` names it, as a path from
|
|
4
|
+
// the Blueprint's folder, like the Screenplay's source.
|
|
5
|
+
import { existsSync, statSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { isInside } from "../util/inside.js";
|
|
8
|
+
import { fixtureBanks } from "./bank-snapshots.js";
|
|
9
|
+
/** Where a Workspace keeps fixtures `save_bank_fixture` makes, beside the Missions. */
|
|
10
|
+
export const BANK_FIXTURES_FOLDER = "bank-fixtures";
|
|
11
|
+
/**
|
|
12
|
+
* Why the fixture `declared` (as a Blueprint names it) can't be used, if it can't: it is outside
|
|
13
|
+
* the Workspace, isn't a folder, or holds no `.SC2Bank` file.
|
|
14
|
+
*/
|
|
15
|
+
export async function bankFixtureProblem(workspaceRoot, blueprintPath, declared) {
|
|
16
|
+
const dir = path.resolve(path.dirname(blueprintPath), declared);
|
|
17
|
+
if (!(await isInside(workspaceRoot, dir)))
|
|
18
|
+
return `bankFixture ${declared} is outside the Workspace`;
|
|
19
|
+
if (!existsSync(dir))
|
|
20
|
+
return `bankFixture ${declared} names a folder that doesn't exist (${dir}); save one with save_bank_fixture or fix the name`;
|
|
21
|
+
if (!statSync(dir).isDirectory())
|
|
22
|
+
return `bankFixture ${declared} isn't a folder`;
|
|
23
|
+
if ((await fixtureBanks(dir)).length === 0)
|
|
24
|
+
return `bankFixture ${declared} holds no .SC2Bank files`;
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/** The fixture a Blueprint names, resolved; refused with the reason when it can't be used. Undefined when it names none. */
|
|
28
|
+
export async function missionBankFixture(workspaceRoot, blueprintPath, declared) {
|
|
29
|
+
if (declared === undefined)
|
|
30
|
+
return undefined;
|
|
31
|
+
const problem = await bankFixtureProblem(workspaceRoot, blueprintPath, declared);
|
|
32
|
+
if (problem)
|
|
33
|
+
throw new Error(problem);
|
|
34
|
+
return { name: declared, dir: path.resolve(path.dirname(blueprintPath), declared) };
|
|
35
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** The runs kept in the cache, newest first, once pruned. */
|
|
2
|
+
export declare const KEPT_BANK_RUNS = 5;
|
|
3
|
+
export type BankKeyChange = {
|
|
4
|
+
readonly key: string;
|
|
5
|
+
readonly status: "added" | "changed" | "removed";
|
|
6
|
+
/** The key's value before the run, as `int="5"`; absent for an added key. */
|
|
7
|
+
readonly before?: string;
|
|
8
|
+
/** The key's value after the run; absent for a removed key. */
|
|
9
|
+
readonly after?: string;
|
|
10
|
+
};
|
|
11
|
+
export type BankSectionChange = {
|
|
12
|
+
readonly section: string;
|
|
13
|
+
readonly status: "added" | "changed" | "removed";
|
|
14
|
+
readonly keys: BankKeyChange[];
|
|
15
|
+
};
|
|
16
|
+
export type BankChange = {
|
|
17
|
+
/** The file's path inside the Banks folder, with forward slashes. */
|
|
18
|
+
readonly bank: string;
|
|
19
|
+
readonly status: "added" | "changed" | "removed";
|
|
20
|
+
/** What changed inside an SC2Bank; empty for another file, or when only formatting or the signature differ. */
|
|
21
|
+
readonly sections: BankSectionChange[];
|
|
22
|
+
/** What the author chose; absent while it waits for them. */
|
|
23
|
+
resolution?: "kept" | "rolled-back";
|
|
24
|
+
};
|
|
25
|
+
export type BankRun = {
|
|
26
|
+
readonly id: string;
|
|
27
|
+
readonly createdAt: string;
|
|
28
|
+
readonly banksDir: string;
|
|
29
|
+
/** The Map Package the test game ran. */
|
|
30
|
+
readonly source: string;
|
|
31
|
+
/** `running` until the game's quit is seen and the changes are recorded. */
|
|
32
|
+
state: "running" | "finished";
|
|
33
|
+
/**
|
|
34
|
+
* The bank fixture the run played on (#55), as the Mission names it. The fixture's banks were
|
|
35
|
+
* copied into the Banks folder for the run, so the author's are put back when the game quits and
|
|
36
|
+
* the changes the game made are recorded as rolled back, never asked about. Absent for a run on the author's own banks.
|
|
37
|
+
*/
|
|
38
|
+
readonly fixture?: string;
|
|
39
|
+
changes: BankChange[];
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* An SC2Bank's keys by section and key name, each value as the attributes of its `<Value>` (`int="5"`),
|
|
43
|
+
* so a change shows as a before and an after. The Signature, which changes with any save, isn't read.
|
|
44
|
+
*/
|
|
45
|
+
export declare function parseBank(xml: string): Map<string, Map<string, string>>;
|
|
46
|
+
/** What changed between two versions of a bank, section by section, key by key. */
|
|
47
|
+
export declare function diffBankText(before: string, after: string): BankSectionChange[];
|
|
48
|
+
/** Compares `banksDir` with a snapshot's files: each bank added, changed or removed since. */
|
|
49
|
+
export declare function diffBanks(snapshot: string, banksDir: string): Promise<BankChange[]>;
|
|
50
|
+
/** Every recorded run, oldest first. */
|
|
51
|
+
export declare function bankRuns(cacheDir: string): Promise<BankRun[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Deletes all but the newest `keep` runs. A fixture run whose author's banks aren't back yet is
|
|
54
|
+
* never deleted: its snapshot is the only copy of them.
|
|
55
|
+
*/
|
|
56
|
+
export declare function pruneBankRuns(cacheDir: string, keep?: number): Promise<void>;
|
|
57
|
+
/** The fixture run whose game hasn't been seen to quit: the Banks folder may still hold its banks instead of the author's. */
|
|
58
|
+
export declare function unrestoredFixtureRun(cacheDir: string): Promise<BankRun | undefined>;
|
|
59
|
+
/** The `.SC2Bank` files under `source`, as paths relative to it. */
|
|
60
|
+
export declare function fixtureBanks(source: string): Promise<string[]>;
|
|
61
|
+
/**
|
|
62
|
+
* Copies the Banks folder's `.SC2Bank` files into a fixture folder, which is the only place it
|
|
63
|
+
* writes: it is made when missing, and a bank already there is replaced. Returns the banks copied.
|
|
64
|
+
*/
|
|
65
|
+
export declare function saveBanksAsFixture(banksDir: string, fixtureDir: string): Promise<string[]>;
|
|
66
|
+
/** Copies `banksDir` into the cache as a new running run, and prunes the old ones. */
|
|
67
|
+
export declare function snapshotBanks(cacheDir: string, banksDir: string, source: string, now?: Date, fixture?: {
|
|
68
|
+
readonly name: string;
|
|
69
|
+
readonly dir: string;
|
|
70
|
+
}): Promise<BankRun>;
|
|
71
|
+
/** Forgets a run: a test game that never started. */
|
|
72
|
+
export declare const discardBankRun: (cacheDir: string, id: string) => Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Puts the author's banks back after a fixture run, then forgets the run. For a game that never
|
|
75
|
+
* started or a failed snapshot; a fixture run that played goes through finishBankRun.
|
|
76
|
+
*/
|
|
77
|
+
export declare function restoreBankRun(cacheDir: string, run: BankRun): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Records what the run changed, once its game has quit. A run that changed nothing is deleted.
|
|
80
|
+
* `record: false` only computes the changes (a game that may still run).
|
|
81
|
+
*/
|
|
82
|
+
export declare function finishBankRun(cacheDir: string, run: BankRun, record?: boolean): Promise<BankRun>;
|
|
83
|
+
/** The newest run with something the author hasn't answered: a game still running, or changes with no choice. */
|
|
84
|
+
export declare function pendingBankRun(cacheDir: string): Promise<BankRun | undefined>;
|
|
85
|
+
/**
|
|
86
|
+
* Applies the author's choices to a finished run's changes: `keep` leaves a bank as the game left
|
|
87
|
+
* it; `rollback` puts back the snapshot's version (deleting a bank the run added). A name that
|
|
88
|
+
* isn't one of the run's changed banks is an error, and nothing is applied.
|
|
89
|
+
*/
|
|
90
|
+
export declare function resolveBanks(cacheDir: string, run: BankRun, choices: {
|
|
91
|
+
readonly keep?: readonly string[];
|
|
92
|
+
readonly rollback?: readonly string[];
|
|
93
|
+
}): Promise<BankRun>;
|
|
94
|
+
/** A run's changes as lines for the author, each unanswered bank with its sections and keys. */
|
|
95
|
+
export declare function describeBankRun(run: BankRun): string[];
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
// Snapshots of the game's shared Banks folder around a test game (#54). A Test Document runs the
|
|
2
|
+
// Mission's real triggers, which may save banks, so the Editor Broker copies the folder into the
|
|
3
|
+
// machine's cache before the game starts and, once it quits, records which banks the run added,
|
|
4
|
+
// changed or removed (and for an SC2Bank, which sections and keys). Nothing is rolled back unless
|
|
5
|
+
// the author asks, bank by bank, through the bank_changes tool.
|
|
6
|
+
//
|
|
7
|
+
// Layout, under `<cacheDir>/banks/<run id>/`: `run.json` (the run's record) and `files/` (the
|
|
8
|
+
// folder as it was). A run that changed nothing is deleted at once; older runs are pruned.
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { cp, mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
11
|
+
import path from "node:path";
|
|
12
|
+
/** The runs kept in the cache, newest first, once pruned. */
|
|
13
|
+
export const KEPT_BANK_RUNS = 5;
|
|
14
|
+
const banksCache = (cacheDir) => path.join(cacheDir, "banks");
|
|
15
|
+
const runFolder = (cacheDir, id) => path.join(banksCache(cacheDir), id);
|
|
16
|
+
const recordFile = (cacheDir, id) => path.join(runFolder(cacheDir, id), "run.json");
|
|
17
|
+
/** The files under `root`, as paths relative to it with forward slashes. */
|
|
18
|
+
async function listFiles(root, relative = "") {
|
|
19
|
+
if (!existsSync(path.join(root, relative)))
|
|
20
|
+
return [];
|
|
21
|
+
const found = [];
|
|
22
|
+
for (const entry of await readdir(path.join(root, relative), { withFileTypes: true })) {
|
|
23
|
+
const child = relative ? `${relative}/${entry.name}` : entry.name;
|
|
24
|
+
if (entry.isDirectory())
|
|
25
|
+
found.push(...(await listFiles(root, child)));
|
|
26
|
+
else if (entry.isFile())
|
|
27
|
+
found.push(child);
|
|
28
|
+
}
|
|
29
|
+
return found.sort();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* An SC2Bank's keys by section and key name, each value as the attributes of its `<Value>` (`int="5"`),
|
|
33
|
+
* so a change shows as a before and an after. The Signature, which changes with any save, isn't read.
|
|
34
|
+
*/
|
|
35
|
+
export function parseBank(xml) {
|
|
36
|
+
const sections = new Map();
|
|
37
|
+
for (const section of xml.matchAll(/<Section\s+name="([^"]*)"\s*>([\s\S]*?)<\/Section>/g)) {
|
|
38
|
+
const keys = sections.get(section[1]) ?? new Map();
|
|
39
|
+
for (const key of section[2].matchAll(/<Key\s+name="([^"]*)"\s*>([\s\S]*?)<\/Key>/g)) {
|
|
40
|
+
const values = [...key[2].matchAll(/<Value\s+([^>]*?)\s*\/?>/g)].map((value) => value[1].replace(/\s+/g, " "));
|
|
41
|
+
keys.set(key[1], values.join(" | "));
|
|
42
|
+
}
|
|
43
|
+
sections.set(section[1], keys);
|
|
44
|
+
}
|
|
45
|
+
return sections;
|
|
46
|
+
}
|
|
47
|
+
/** What changed between two versions of a bank, section by section, key by key. */
|
|
48
|
+
export function diffBankText(before, after) {
|
|
49
|
+
const [was, now] = [parseBank(before), parseBank(after)];
|
|
50
|
+
const changes = [];
|
|
51
|
+
for (const name of [...new Set([...was.keys(), ...now.keys()])]) {
|
|
52
|
+
const [oldKeys, newKeys] = [was.get(name), now.get(name)];
|
|
53
|
+
const keys = [];
|
|
54
|
+
for (const key of [...new Set([...(oldKeys?.keys() ?? []), ...(newKeys?.keys() ?? [])])]) {
|
|
55
|
+
const [old, current] = [oldKeys?.get(key), newKeys?.get(key)];
|
|
56
|
+
if (old === current)
|
|
57
|
+
continue;
|
|
58
|
+
keys.push(old === undefined ? { key, status: "added", after: current } : current === undefined ? { key, status: "removed", before: old } : { key, status: "changed", before: old, after: current });
|
|
59
|
+
}
|
|
60
|
+
if (!oldKeys)
|
|
61
|
+
changes.push({ section: name, status: "added", keys });
|
|
62
|
+
else if (!newKeys)
|
|
63
|
+
changes.push({ section: name, status: "removed", keys });
|
|
64
|
+
else if (keys.length > 0)
|
|
65
|
+
changes.push({ section: name, status: "changed", keys });
|
|
66
|
+
}
|
|
67
|
+
return changes;
|
|
68
|
+
}
|
|
69
|
+
/** Compares `banksDir` with a snapshot's files: each bank added, changed or removed since. */
|
|
70
|
+
export async function diffBanks(snapshot, banksDir) {
|
|
71
|
+
const files = [...new Set([...(await listFiles(snapshot)), ...(await listFiles(banksDir))])].sort();
|
|
72
|
+
const changes = [];
|
|
73
|
+
for (const bank of files) {
|
|
74
|
+
const [was, now] = [path.join(snapshot, bank), path.join(banksDir, bank)];
|
|
75
|
+
const [hadBefore, hasNow] = [existsSync(was), existsSync(now)];
|
|
76
|
+
if (hadBefore && hasNow) {
|
|
77
|
+
const [oldBytes, newBytes] = [await readFile(was), await readFile(now)];
|
|
78
|
+
if (oldBytes.equals(newBytes))
|
|
79
|
+
continue;
|
|
80
|
+
changes.push({ bank, status: "changed", sections: bank.toLowerCase().endsWith(".sc2bank") ? diffBankText(oldBytes.toString("utf8"), newBytes.toString("utf8")) : [] });
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const text = bank.toLowerCase().endsWith(".sc2bank") ? (await readFile(hasNow ? now : was)).toString("utf8") : "";
|
|
84
|
+
const sections = diffBankText(hasNow ? "" : text, hasNow ? text : "");
|
|
85
|
+
changes.push({ bank, status: hasNow ? "added" : "removed", sections });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return changes;
|
|
89
|
+
}
|
|
90
|
+
async function save(cacheDir, run) {
|
|
91
|
+
await writeFile(recordFile(cacheDir, run.id), `${JSON.stringify(run, null, 2)}\n`);
|
|
92
|
+
}
|
|
93
|
+
/** Every recorded run, oldest first. */
|
|
94
|
+
export async function bankRuns(cacheDir) {
|
|
95
|
+
const root = banksCache(cacheDir);
|
|
96
|
+
if (!existsSync(root))
|
|
97
|
+
return [];
|
|
98
|
+
const runs = [];
|
|
99
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
100
|
+
if (!entry.isDirectory() || !existsSync(recordFile(cacheDir, entry.name)))
|
|
101
|
+
continue;
|
|
102
|
+
try {
|
|
103
|
+
runs.push(JSON.parse(await readFile(recordFile(cacheDir, entry.name), "utf8")));
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// A record cut short by a crash: left for pruning.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return runs.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.id.localeCompare(b.id));
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Deletes all but the newest `keep` runs. A fixture run whose author's banks aren't back yet is
|
|
113
|
+
* never deleted: its snapshot is the only copy of them.
|
|
114
|
+
*/
|
|
115
|
+
export async function pruneBankRuns(cacheDir, keep = KEPT_BANK_RUNS) {
|
|
116
|
+
const runs = await bankRuns(cacheDir);
|
|
117
|
+
for (const run of runs.slice(0, Math.max(0, runs.length - keep))) {
|
|
118
|
+
if (run.fixture !== undefined && run.state === "running")
|
|
119
|
+
continue;
|
|
120
|
+
await rm(runFolder(cacheDir, run.id), { recursive: true, force: true });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** The fixture run whose game hasn't been seen to quit: the Banks folder may still hold its banks instead of the author's. */
|
|
124
|
+
export async function unrestoredFixtureRun(cacheDir) {
|
|
125
|
+
return (await bankRuns(cacheDir)).find((run) => run.fixture !== undefined && run.state === "running");
|
|
126
|
+
}
|
|
127
|
+
/** The `.SC2Bank` files under `source`, as paths relative to it. */
|
|
128
|
+
export async function fixtureBanks(source) {
|
|
129
|
+
return (await listFiles(source)).filter((file) => file.toLowerCase().endsWith(".sc2bank"));
|
|
130
|
+
}
|
|
131
|
+
/** Copies the fixture's banks over the Banks folder, creating it if it isn't there; any other bank is left alone. */
|
|
132
|
+
async function copyBanks(from, to) {
|
|
133
|
+
for (const file of await fixtureBanks(from)) {
|
|
134
|
+
await mkdir(path.dirname(path.join(to, file)), { recursive: true });
|
|
135
|
+
await cp(path.join(from, file), path.join(to, file));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Copies the Banks folder's `.SC2Bank` files into a fixture folder, which is the only place it
|
|
140
|
+
* writes: it is made when missing, and a bank already there is replaced. Returns the banks copied.
|
|
141
|
+
*/
|
|
142
|
+
export async function saveBanksAsFixture(banksDir, fixtureDir) {
|
|
143
|
+
const banks = await fixtureBanks(banksDir);
|
|
144
|
+
if (banks.length === 0)
|
|
145
|
+
throw new Error(`${banksDir} holds no .SC2Bank files to save`);
|
|
146
|
+
await copyBanks(banksDir, fixtureDir);
|
|
147
|
+
return banks;
|
|
148
|
+
}
|
|
149
|
+
/** Puts the Banks folder back as `snapshot` has it: every file the run added is deleted, every changed or removed one copied back. */
|
|
150
|
+
async function restoreFromSnapshot(snapshot, banksDir) {
|
|
151
|
+
for (const change of await diffBanks(snapshot, banksDir)) {
|
|
152
|
+
const target = path.join(banksDir, change.bank);
|
|
153
|
+
if (change.status === "added")
|
|
154
|
+
await rm(target, { force: true });
|
|
155
|
+
else {
|
|
156
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
157
|
+
await cp(path.join(snapshot, change.bank), target);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Copies `banksDir` into the cache as a new running run, and prunes the old ones. */
|
|
162
|
+
export async function snapshotBanks(cacheDir, banksDir, source, now = new Date(), fixture) {
|
|
163
|
+
const id = `${now.getTime().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
|
|
164
|
+
const files = path.join(runFolder(cacheDir, id), "files");
|
|
165
|
+
await mkdir(files, { recursive: true });
|
|
166
|
+
if (existsSync(banksDir))
|
|
167
|
+
await cp(banksDir, files, { recursive: true });
|
|
168
|
+
const run = { id, createdAt: now.toISOString(), banksDir, source, state: "running", changes: [], ...(fixture ? { fixture: fixture.name } : {}) };
|
|
169
|
+
// Saved before the fixture is copied in, so a failure part-way still leaves the record that puts the author's banks back.
|
|
170
|
+
await save(cacheDir, run);
|
|
171
|
+
if (fixture) {
|
|
172
|
+
try {
|
|
173
|
+
if ((await fixtureBanks(fixture.dir)).length === 0)
|
|
174
|
+
throw new Error(`The bank fixture ${fixture.name} holds no .SC2Bank files (${fixture.dir})`);
|
|
175
|
+
await copyBanks(fixture.dir, banksDir);
|
|
176
|
+
await mkdir(appliedFolder(cacheDir, id), { recursive: true });
|
|
177
|
+
await cp(banksDir, appliedFolder(cacheDir, id), { recursive: true });
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
await restoreBankRun(cacheDir, run).catch(() => undefined);
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
await pruneBankRuns(cacheDir);
|
|
185
|
+
return run;
|
|
186
|
+
}
|
|
187
|
+
/** A fixture run's Banks folder just after the fixture went in: what the game's own changes are compared with. */
|
|
188
|
+
const appliedFolder = (cacheDir, id) => path.join(runFolder(cacheDir, id), "applied");
|
|
189
|
+
/** Forgets a run: a test game that never started. */
|
|
190
|
+
export const discardBankRun = (cacheDir, id) => rm(runFolder(cacheDir, id), { recursive: true, force: true });
|
|
191
|
+
/**
|
|
192
|
+
* Puts the author's banks back after a fixture run, then forgets the run. For a game that never
|
|
193
|
+
* started or a failed snapshot; a fixture run that played goes through finishBankRun.
|
|
194
|
+
*/
|
|
195
|
+
export async function restoreBankRun(cacheDir, run) {
|
|
196
|
+
await restoreFromSnapshot(path.join(runFolder(cacheDir, run.id), "files"), run.banksDir);
|
|
197
|
+
await discardBankRun(cacheDir, run.id);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Records what the run changed, once its game has quit. A run that changed nothing is deleted.
|
|
201
|
+
* `record: false` only computes the changes (a game that may still run).
|
|
202
|
+
*/
|
|
203
|
+
export async function finishBankRun(cacheDir, run, record = true) {
|
|
204
|
+
if (run.fixture !== undefined)
|
|
205
|
+
return finishFixtureRun(cacheDir, run, record);
|
|
206
|
+
const changes = await diffBanks(path.join(runFolder(cacheDir, run.id), "files"), run.banksDir);
|
|
207
|
+
if (!record)
|
|
208
|
+
return { ...run, changes };
|
|
209
|
+
if (changes.length === 0) {
|
|
210
|
+
await discardBankRun(cacheDir, run.id);
|
|
211
|
+
return { ...run, state: "finished", changes };
|
|
212
|
+
}
|
|
213
|
+
const finished = { ...run, state: "finished", changes };
|
|
214
|
+
await save(cacheDir, finished);
|
|
215
|
+
return finished;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* A fixture run changes the banks on purpose, so what the game itself changed (against the folder
|
|
219
|
+
* just after the fixture went in) is recorded for the author's information, already `rolled-back`:
|
|
220
|
+
* the author's banks are put back whatever the game did, and nothing waits for an answer. If the
|
|
221
|
+
* restore fails the run stays `running`, and the next call tries again.
|
|
222
|
+
*/
|
|
223
|
+
async function finishFixtureRun(cacheDir, run, record) {
|
|
224
|
+
const changes = await diffBanks(appliedFolder(cacheDir, run.id), run.banksDir);
|
|
225
|
+
if (!record)
|
|
226
|
+
return { ...run, changes };
|
|
227
|
+
await restoreFromSnapshot(path.join(runFolder(cacheDir, run.id), "files"), run.banksDir);
|
|
228
|
+
for (const change of changes)
|
|
229
|
+
change.resolution = "rolled-back";
|
|
230
|
+
const finished = { ...run, state: "finished", changes };
|
|
231
|
+
if (changes.length === 0)
|
|
232
|
+
await discardBankRun(cacheDir, run.id);
|
|
233
|
+
else {
|
|
234
|
+
await rm(appliedFolder(cacheDir, run.id), { recursive: true, force: true });
|
|
235
|
+
await save(cacheDir, finished);
|
|
236
|
+
}
|
|
237
|
+
return finished;
|
|
238
|
+
}
|
|
239
|
+
/** The newest run with something the author hasn't answered: a game still running, or changes with no choice. */
|
|
240
|
+
export async function pendingBankRun(cacheDir) {
|
|
241
|
+
const runs = await bankRuns(cacheDir);
|
|
242
|
+
return runs.reverse().find((run) => run.state === "running" || run.changes.some((change) => !change.resolution));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Applies the author's choices to a finished run's changes: `keep` leaves a bank as the game left
|
|
246
|
+
* it; `rollback` puts back the snapshot's version (deleting a bank the run added). A name that
|
|
247
|
+
* isn't one of the run's changed banks is an error, and nothing is applied.
|
|
248
|
+
*/
|
|
249
|
+
export async function resolveBanks(cacheDir, run, choices) {
|
|
250
|
+
const known = new Set(run.changes.map((change) => change.bank));
|
|
251
|
+
const unknown = [...(choices.keep ?? []), ...(choices.rollback ?? [])].filter((name) => !known.has(name));
|
|
252
|
+
if (unknown.length > 0)
|
|
253
|
+
throw new Error(`Not banks this run changed: ${unknown.join(", ")}. It changed: ${[...known].join(", ")}`);
|
|
254
|
+
const both = (choices.keep ?? []).filter((name) => (choices.rollback ?? []).includes(name));
|
|
255
|
+
if (both.length > 0)
|
|
256
|
+
throw new Error(`Asked to both keep and roll back: ${both.join(", ")}`);
|
|
257
|
+
const files = path.join(runFolder(cacheDir, run.id), "files");
|
|
258
|
+
for (const change of run.changes) {
|
|
259
|
+
if (choices.keep?.includes(change.bank))
|
|
260
|
+
change.resolution = "kept";
|
|
261
|
+
else if (choices.rollback?.includes(change.bank)) {
|
|
262
|
+
const target = path.join(run.banksDir, change.bank);
|
|
263
|
+
if (change.status === "added")
|
|
264
|
+
await rm(target, { force: true });
|
|
265
|
+
else {
|
|
266
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
267
|
+
await cp(path.join(files, change.bank), target);
|
|
268
|
+
}
|
|
269
|
+
change.resolution = "rolled-back";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// Every change answered: the snapshot isn't needed.
|
|
273
|
+
if (run.changes.every((change) => change.resolution))
|
|
274
|
+
await rm(files, { recursive: true, force: true });
|
|
275
|
+
await save(cacheDir, run);
|
|
276
|
+
return run;
|
|
277
|
+
}
|
|
278
|
+
/** A run's changes as lines for the author, each unanswered bank with its sections and keys. */
|
|
279
|
+
export function describeBankRun(run) {
|
|
280
|
+
const open = run.changes.filter((change) => !change.resolution);
|
|
281
|
+
const lines = [];
|
|
282
|
+
const source = path.basename(run.source);
|
|
283
|
+
if (run.fixture !== undefined && run.state === "running")
|
|
284
|
+
lines.push(`The test game of ${source} plays on the bank fixture ${run.fixture}, so the Banks folder holds the fixture's banks until the game quits; the author's own banks are put back then.`);
|
|
285
|
+
else if (run.state === "running")
|
|
286
|
+
lines.push(`The test game of ${source} (started ${run.createdAt}) hasn't been seen to quit, so what it changes in the Banks folder isn't recorded yet.`);
|
|
287
|
+
for (const change of run.changes) {
|
|
288
|
+
lines.push(` ${change.bank}: ${change.status}${change.resolution ? ` (${change.resolution === "kept" ? "kept" : "rolled back"})` : ""}`);
|
|
289
|
+
if (change.resolution)
|
|
290
|
+
continue;
|
|
291
|
+
if (change.status === "changed" && change.sections.length === 0)
|
|
292
|
+
lines.push(" the signature or formatting only");
|
|
293
|
+
for (const section of change.sections) {
|
|
294
|
+
lines.push(` [${section.section}]${section.status === "changed" ? "" : ` ${section.status}`}`);
|
|
295
|
+
for (const key of section.keys)
|
|
296
|
+
lines.push(` ${key.key}: ${key.status === "added" ? `added ${key.after}` : key.status === "removed" ? `removed (was ${key.before})` : `${key.before} -> ${key.after}`}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (open.length > 0)
|
|
300
|
+
lines.unshift(`The test game of ${source} changed ${open.length} bank${open.length === 1 ? "" : "s"} in ${run.banksDir}. Nothing is rolled back unless the author says so: ask which to keep and which to roll back, then call bank_changes with keep and rollback.`);
|
|
301
|
+
return lines;
|
|
302
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import net from "node:net";
|
|
2
2
|
import type { Host } from "../host/host.js";
|
|
3
|
-
import type { CaptureTourRequest, CaptureTourResult } from "../plugins/plugin-api.js";
|
|
3
|
+
import type { CaptureTourRequest, CaptureTourResult, GameProcess } from "../plugins/plugin-api.js";
|
|
4
4
|
import type { CaptureCopyOptions } from "./capture-copy.js";
|
|
5
|
-
/** The protocol both ends speak; a broker built from another version refuses the job. 2: editor jobs. */
|
|
6
|
-
export declare const BROKER_PROTOCOL =
|
|
5
|
+
/** The protocol both ends speak; a broker built from another version refuses the job. 2: editor jobs. 3: a Test Document's bank fixture. 4: the games it recorded, in its status. 5: a capture result's games and start time, for its logs. */
|
|
6
|
+
export declare const BROKER_PROTOCOL = 5;
|
|
7
7
|
/** The Editor actions the broker runs between captures: each clicks the Editor's menus or brings it forward. */
|
|
8
8
|
export declare const BROKERED_EDITOR_ACTIONS: readonly ["editor.open", "editor.refresh", "editor.testDocument", "editor.save"];
|
|
9
9
|
export type BrokeredEditorAction = (typeof BROKERED_EDITOR_ACTIONS)[number];
|
|
10
10
|
/** How a tour is captured: the frames' naming and size, and how long to wait for the game. */
|
|
11
|
-
export type TourSettings = Pick<CaptureTourRequest, "frameDigits" | "recordKeys" | "width" | "quality" | "startSeconds" | "pressSpaceAfterSeconds">;
|
|
11
|
+
export type TourSettings = Pick<CaptureTourRequest, "frameDigits" | "recordKeys" | "width" | "quality" | "startSeconds" | "pressSpaceAfterSeconds" | "video">;
|
|
12
12
|
/**
|
|
13
13
|
* A capture job. `tour`: a capture copy of `source` whose Triggers are the camera tour of `copy`
|
|
14
14
|
* (makeCaptureCopy's options); waiting tour jobs with the same source, copy options other than
|
|
@@ -43,10 +43,19 @@ export type CaptureJob = {
|
|
|
43
43
|
readonly source: string;
|
|
44
44
|
readonly seconds?: number;
|
|
45
45
|
readonly ifIdle?: boolean;
|
|
46
|
+
/** A Test Document's bank fixture (#55): its name, and its folder, absolute. The broker copies its banks in for the run and puts the author's back. */
|
|
47
|
+
readonly bankFixture?: {
|
|
48
|
+
readonly name: string;
|
|
49
|
+
readonly dir: string;
|
|
50
|
+
};
|
|
46
51
|
};
|
|
47
52
|
export type CaptureJobResult = CaptureTourResult & {
|
|
48
53
|
/** The capture copy the tour ran on, if the broker made one: removed when the tour finished, kept to debug when it ended early. */
|
|
49
54
|
readonly copy?: string;
|
|
55
|
+
/** The games this job's tour started, as the broker recorded them: the process a crash folder's `P<pid>` names. */
|
|
56
|
+
readonly games?: readonly GameProcess[];
|
|
57
|
+
/** When the broker began the tour (ISO), before any game of it: the earliest a game's log file of this run can be. */
|
|
58
|
+
readonly startedAt?: string;
|
|
50
59
|
};
|
|
51
60
|
/** A job as the status lists it: an editor job by its action, and `holding` once its Test Document's game runs, which no capture starts under. */
|
|
52
61
|
export type BrokerJobSummary = {
|
|
@@ -58,6 +67,14 @@ export type BrokerJobSummary = {
|
|
|
58
67
|
readonly action?: BrokeredEditorAction;
|
|
59
68
|
readonly holding?: true;
|
|
60
69
|
};
|
|
70
|
+
/** A game the broker recorded: whose it is decides whether anything may quit it. */
|
|
71
|
+
export type BrokerGameSummary = {
|
|
72
|
+
readonly pid: number;
|
|
73
|
+
/** `tool`: started by a capture, which quits it; `author`: a Test Document's, for the author to play, which the broker holds the queue for and never quits. */
|
|
74
|
+
readonly owner: "tool" | "author";
|
|
75
|
+
/** The job that started it. */
|
|
76
|
+
readonly job: string;
|
|
77
|
+
};
|
|
61
78
|
export type BrokerStatus = {
|
|
62
79
|
readonly pid: number;
|
|
63
80
|
/** The script the broker runs from, naming the checkout it was built from; absent from a broker built before it was sent. */
|
|
@@ -65,6 +82,8 @@ export type BrokerStatus = {
|
|
|
65
82
|
/** The jobs of the tour running now, or null. */
|
|
66
83
|
readonly running: readonly BrokerJobSummary[] | null;
|
|
67
84
|
readonly queue: readonly BrokerJobSummary[];
|
|
85
|
+
/** The games the broker started a job for and that still run. */
|
|
86
|
+
readonly games: readonly BrokerGameSummary[];
|
|
68
87
|
/** The last failure: a job's (by its id), or closing capture copies' (no job). */
|
|
69
88
|
readonly lastError: {
|
|
70
89
|
readonly job?: string;
|
|
@@ -156,6 +175,10 @@ export declare class EditorBusyError extends Error {
|
|
|
156
175
|
export declare function runEditorAction(host: Host, action: BrokeredEditorAction, request: {
|
|
157
176
|
readonly mapPath: string;
|
|
158
177
|
readonly seconds?: number;
|
|
178
|
+
readonly bankFixture?: {
|
|
179
|
+
readonly name: string;
|
|
180
|
+
readonly dir: string;
|
|
181
|
+
};
|
|
159
182
|
}, options?: {
|
|
160
183
|
readonly ifIdle?: boolean;
|
|
161
184
|
readonly log?: (line: string) => void;
|
|
@@ -13,8 +13,8 @@ import os from "node:os";
|
|
|
13
13
|
import path from "node:path";
|
|
14
14
|
import { fileURLToPath } from "node:url";
|
|
15
15
|
import { hostContext } from "../host/machine-config.js";
|
|
16
|
-
/** The protocol both ends speak; a broker built from another version refuses the job. 2: editor jobs. */
|
|
17
|
-
export const BROKER_PROTOCOL =
|
|
16
|
+
/** The protocol both ends speak; a broker built from another version refuses the job. 2: editor jobs. 3: a Test Document's bank fixture. 4: the games it recorded, in its status. 5: a capture result's games and start time, for its logs. */
|
|
17
|
+
export const BROKER_PROTOCOL = 5;
|
|
18
18
|
/** The Editor actions the broker runs between captures: each clicks the Editor's menus or brings it forward. */
|
|
19
19
|
export const BROKERED_EDITOR_ACTIONS = ["editor.open", "editor.refresh", "editor.testDocument", "editor.save"];
|
|
20
20
|
/** The longest path macOS takes for a Unix socket is 103 bytes. */
|
|
@@ -130,7 +130,7 @@ export async function runEditorAction(host, action, request, options = {}) {
|
|
|
130
130
|
return;
|
|
131
131
|
}
|
|
132
132
|
const cacheDir = hostContext(host.machine).requirePath("cacheDir");
|
|
133
|
-
const job = { kind: "editor", action, source: request.mapPath, ...(request.seconds !== undefined ? { seconds: request.seconds } : {}), ...(options.ifIdle ? { ifIdle: true } : {}) };
|
|
133
|
+
const job = { kind: "editor", action, source: request.mapPath, ...(request.seconds !== undefined ? { seconds: request.seconds } : {}), ...(request.bankFixture ? { bankFixture: request.bankFixture } : {}), ...(options.ifIdle ? { ifIdle: true } : {}) };
|
|
134
134
|
await runEditorJob(job, { socketPath: brokerSocketPath(cacheDir), start: () => spawnBroker(host, cacheDir), script: brokerScript(), ...(options.log ? { log: options.log } : {}) });
|
|
135
135
|
}
|
|
136
136
|
/** The broker's script in this checkout's build: what a client starts, and what a broker reports it runs from. */
|