@gameslate/sdk 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +38 -2
- package/bin/gameslate-plugin.mjs +26 -0
- package/build/build-plugin.mjs +127 -0
- package/lib/assets.d.ts +1788 -0
- package/lib/assets.js +107 -0
- package/lib/bytes.d.ts +33 -0
- package/lib/bytes.js +89 -0
- package/lib/catalogue.d.ts +1228 -0
- package/lib/catalogue.js +305 -0
- package/lib/chips.d.ts +18 -0
- package/lib/chips.js +28 -0
- package/lib/condition.d.ts +58 -0
- package/lib/condition.js +125 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +16 -0
- package/lib/logic.d.ts +68 -0
- package/lib/logic.js +194 -0
- package/lib/navigation.d.ts +139 -0
- package/lib/navigation.js +38 -0
- package/lib/pack.d.ts +933 -0
- package/lib/pack.js +29 -0
- package/lib/pins.d.ts +19 -0
- package/lib/pins.js +35 -0
- package/lib/plugin/contract.d.ts +111 -0
- package/lib/plugin/contract.js +1 -0
- package/lib/plugin/index.d.ts +3 -0
- package/lib/plugin/index.js +9 -0
- package/lib/plugin/sandbox-guest.d.ts +12 -0
- package/lib/plugin/sandbox-guest.js +128 -0
- package/lib/plugin/seqlock.d.ts +8 -0
- package/lib/plugin/seqlock.js +19 -0
- package/lib/plugin-manifest.d.ts +131 -0
- package/lib/plugin-manifest.js +54 -0
- package/lib/plugins-api.d.ts +445 -0
- package/lib/plugins-api.js +38 -0
- package/lib/registry.d.ts +2231 -0
- package/lib/registry.js +170 -0
- package/lib/sandbox-protocol.d.ts +1363 -0
- package/lib/sandbox-protocol.js +39 -0
- package/lib/status.d.ts +24 -0
- package/lib/status.js +75 -0
- package/lib/types.d.ts +2684 -0
- package/lib/types.js +282 -0
- package/lib/views.d.ts +204 -0
- package/lib/views.js +35 -0
- package/package.json +53 -3
package/lib/pack.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { CatalogueSchema } from './catalogue.js';
|
|
3
|
+
/**
|
|
4
|
+
* A pack's data: everything the web app needs about one game that doesn't depend on the live
|
|
5
|
+
* state. The daemon serves it (`/api/packs/<gameId>`); shared code never branches on `id`.
|
|
6
|
+
*/
|
|
7
|
+
export const PackDataSchema = z.object({
|
|
8
|
+
id: z.string(),
|
|
9
|
+
title: z.string(),
|
|
10
|
+
catalogue: CatalogueSchema,
|
|
11
|
+
/** Scale of the game's world coordinates, for distances shown to the player and the AI. */
|
|
12
|
+
world: z.object({
|
|
13
|
+
unitsPerMeter: z.number().positive(),
|
|
14
|
+
/** true when +Z points north (Doom); otherwise +Z points south (the Zelda ports). */
|
|
15
|
+
northIsPositiveZ: z.boolean().optional(),
|
|
16
|
+
}),
|
|
17
|
+
});
|
|
18
|
+
/** The data part of a pack, without its code. */
|
|
19
|
+
export function packToData(pack) {
|
|
20
|
+
return { id: pack.id, title: pack.title, catalogue: pack.catalogue, world: pack.world };
|
|
21
|
+
}
|
|
22
|
+
/** Runs a pack's code over the live state. */
|
|
23
|
+
export function packViewsFor(pack, state) {
|
|
24
|
+
return {
|
|
25
|
+
facts: pack.describeState?.(state) ?? [],
|
|
26
|
+
statusStrip: pack.ui?.statusStrip?.(state) ?? null,
|
|
27
|
+
timelines: pack.ui?.timelines?.(state) ?? [],
|
|
28
|
+
};
|
|
29
|
+
}
|
package/lib/pins.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PinRule } from './catalogue.js';
|
|
2
|
+
import type { Condition } from './condition.js';
|
|
3
|
+
import type { GameState } from './types.js';
|
|
4
|
+
export interface PinRef {
|
|
5
|
+
id: string;
|
|
6
|
+
label: string;
|
|
7
|
+
/** effective kind (the registry default applied), for rules that match on it */
|
|
8
|
+
kind?: string;
|
|
9
|
+
/** registry-declared status condition, when the registry builder emitted one */
|
|
10
|
+
collectedBy?: Condition;
|
|
11
|
+
/** registry-declared absence (e.g. a Doom thing that only spawns on some skills) */
|
|
12
|
+
hiddenWhen?: Condition;
|
|
13
|
+
}
|
|
14
|
+
/** Only a definite `met` counts: an unstreamed flag must not mark a pin collected. */
|
|
15
|
+
export declare function isPinCollected(pin: PinRef, rules: readonly PinRule[], state: GameState): boolean;
|
|
16
|
+
/** Hidden only on a definite `met`: when unsure, show the pin. */
|
|
17
|
+
export declare function isPinHidden(pin: PinRef, rules: readonly PinRule[], state: GameState): boolean;
|
|
18
|
+
/** The first matching rule's icon, in rule order (put specific rules before general ones). */
|
|
19
|
+
export declare function findPinIcon(pin: PinRef, rules: readonly PinRule[], state: GameState): string | undefined;
|
package/lib/pins.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { evaluate } from './condition.js';
|
|
2
|
+
function ruleMatches(rule, pin, state) {
|
|
3
|
+
const { at, kind, ids, label, labelContains } = rule.match;
|
|
4
|
+
if (at !== undefined && state.locationId !== at)
|
|
5
|
+
return false;
|
|
6
|
+
if (kind !== undefined && pin.kind !== kind)
|
|
7
|
+
return false;
|
|
8
|
+
if (ids !== undefined && !ids.includes(pin.id))
|
|
9
|
+
return false;
|
|
10
|
+
if (label !== undefined && pin.label !== label)
|
|
11
|
+
return false;
|
|
12
|
+
if (labelContains !== undefined && !pin.label.includes(labelContains))
|
|
13
|
+
return false;
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
/** Only a definite `met` counts: an unstreamed flag must not mark a pin collected. */
|
|
17
|
+
export function isPinCollected(pin, rules, state) {
|
|
18
|
+
if (pin.collectedBy && evaluate(pin.collectedBy, state) === 'met')
|
|
19
|
+
return true;
|
|
20
|
+
return rules.some(r => r.collectedBy !== undefined &&
|
|
21
|
+
ruleMatches(r, pin, state) &&
|
|
22
|
+
evaluate(r.collectedBy, state) === 'met');
|
|
23
|
+
}
|
|
24
|
+
/** Hidden only on a definite `met`: when unsure, show the pin. */
|
|
25
|
+
export function isPinHidden(pin, rules, state) {
|
|
26
|
+
if (pin.hiddenWhen && evaluate(pin.hiddenWhen, state) === 'met')
|
|
27
|
+
return true;
|
|
28
|
+
return rules.some(r => r.hiddenWhen !== undefined &&
|
|
29
|
+
ruleMatches(r, pin, state) &&
|
|
30
|
+
evaluate(r.hiddenWhen, state) === 'met');
|
|
31
|
+
}
|
|
32
|
+
/** The first matching rule's icon, in rule order (put specific rules before general ones). */
|
|
33
|
+
export function findPinIcon(pin, rules, state) {
|
|
34
|
+
return rules.find(r => r.icon !== undefined && ruleMatches(r, pin, state))?.icon;
|
|
35
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { AssetList, AssetOutput, Bytes, GameCapability, GamePack, GameState, InputFile, InputPick } from '../index.js';
|
|
2
|
+
/**
|
|
3
|
+
* What a plugin may do with the matched game's memory: read it, and nothing else. The core opens
|
|
4
|
+
* and closes the process; a plugin never sees a pid, a path or a file handle.
|
|
5
|
+
*/
|
|
6
|
+
export interface GameMemory {
|
|
7
|
+
/** The matched build (already checked against `supportedBuildIds`). */
|
|
8
|
+
readonly buildId: string | null;
|
|
9
|
+
/** The game's command line (e.g. which WADs a Doom port was started with). */
|
|
10
|
+
readonly commandLine: readonly string[];
|
|
11
|
+
/** Where the matched executable image is loaded (for PE images and PIE binaries). */
|
|
12
|
+
readonly moduleBase: bigint;
|
|
13
|
+
read(address: bigint, byteLength: number): Bytes | null;
|
|
14
|
+
isValidRange(address: bigint, byteLength: number): boolean;
|
|
15
|
+
resolveSymbol(name: string): bigint | null;
|
|
16
|
+
/** Re-read the process's memory map (after a level load moves heap regions). */
|
|
17
|
+
refreshMappings(): void;
|
|
18
|
+
}
|
|
19
|
+
/** A memory-read state source for one game. Packs export theirs from games/<id>/source.ts. */
|
|
20
|
+
export interface GamePlugin {
|
|
21
|
+
readonly gameId: string;
|
|
22
|
+
readonly stateSchemaVersion: number;
|
|
23
|
+
readonly capabilities: GameCapability[];
|
|
24
|
+
readonly supportedBuildIds: ReadonlyMap<string, {
|
|
25
|
+
region: string;
|
|
26
|
+
}>;
|
|
27
|
+
readonly executableHints: readonly string[];
|
|
28
|
+
/**
|
|
29
|
+
* Stats a launch wrapper from an older bundle may not send. While any is missing, the daemon
|
|
30
|
+
* fills them (and flags) from its own direct read of the game, without a relaunch.
|
|
31
|
+
*/
|
|
32
|
+
readonly wrapperMayOmitStats?: readonly string[];
|
|
33
|
+
/**
|
|
34
|
+
* Versions of the game this plugin can't read (e.g. a closed-source re-release), by executable
|
|
35
|
+
* name, with the reason shown to the player instead of silently doing nothing.
|
|
36
|
+
*/
|
|
37
|
+
readonly unsupportedExecutables?: ReadonlyArray<{
|
|
38
|
+
executable: string;
|
|
39
|
+
reason: string;
|
|
40
|
+
}>;
|
|
41
|
+
createSession(memory: GameMemory): PluginSession | null;
|
|
42
|
+
}
|
|
43
|
+
export interface PluginSession {
|
|
44
|
+
poll(): GameState | null;
|
|
45
|
+
/** The plugin's own clean-up; the core closes the game's memory afterwards. */
|
|
46
|
+
close(): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Builds a game's assets from the player's files, one at a time, in the core's sandbox. The core
|
|
50
|
+
* makes one instance per run (so it can keep parsed files between assets), hands in the files it
|
|
51
|
+
* opened, and writes and validates whatever comes back (docs/plugin-architecture.md).
|
|
52
|
+
*/
|
|
53
|
+
export interface AssetExtractor {
|
|
54
|
+
/** what can be built from these files: cheap, no heavy parsing */
|
|
55
|
+
listAssets(inputs: readonly InputFile[], pick: InputPick): AssetList;
|
|
56
|
+
/** one asset's outputs: data in the sdk's shapes, never files */
|
|
57
|
+
buildAsset(id: string, inputs: readonly InputFile[]): AssetOutput[];
|
|
58
|
+
}
|
|
59
|
+
/** A Python-based extraction (the Zelda map renderer, until it's ported): what to run. */
|
|
60
|
+
export type PythonExtractPlan = {
|
|
61
|
+
kind: 'error';
|
|
62
|
+
message: string;
|
|
63
|
+
} | {
|
|
64
|
+
kind: 'run';
|
|
65
|
+
gameId: string;
|
|
66
|
+
pythonArgs: string[];
|
|
67
|
+
playerArgs: string[];
|
|
68
|
+
outDir: string;
|
|
69
|
+
};
|
|
70
|
+
export interface PythonExtract {
|
|
71
|
+
/** the games it extracts */
|
|
72
|
+
games: readonly string[];
|
|
73
|
+
plan(args: readonly string[], paths: {
|
|
74
|
+
script: string;
|
|
75
|
+
registryDir: string;
|
|
76
|
+
localAssetsDir: string;
|
|
77
|
+
}): PythonExtractPlan;
|
|
78
|
+
/** a game's step after the renderer succeeded */
|
|
79
|
+
after: ReadonlyMap<string, (ctx: {
|
|
80
|
+
args: readonly string[];
|
|
81
|
+
outDir: string;
|
|
82
|
+
extractorScript: string;
|
|
83
|
+
}) => Promise<void>>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* What a plugin's live code exports (its default export): the packs and memory sources for the
|
|
87
|
+
* games it covers. It runs in the core's sandbox, built with `exposeToHost`, so it may use no Node
|
|
88
|
+
* APIs.
|
|
89
|
+
*/
|
|
90
|
+
export interface PluginModule {
|
|
91
|
+
/** must equal the manifest's id */
|
|
92
|
+
id: string;
|
|
93
|
+
packs: readonly GamePack[];
|
|
94
|
+
sources: readonly GamePlugin[];
|
|
95
|
+
}
|
|
96
|
+
/** What a plugin's asset code exports: an extractor per game. Runs in the core's sandbox. */
|
|
97
|
+
export interface PluginAssetsModule {
|
|
98
|
+
/** must equal the manifest's id */
|
|
99
|
+
id: string;
|
|
100
|
+
/** a new extractor for each run, by game id */
|
|
101
|
+
extractors: ReadonlyMap<string, () => AssetExtractor>;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The Python map renderer's hooks (the Zelda ports). Still unsandboxed, with Node, until the
|
|
105
|
+
* renderer is ported and those games get an AssetExtractor.
|
|
106
|
+
*/
|
|
107
|
+
export interface PluginExtractModule {
|
|
108
|
+
/** must equal the manifest's id */
|
|
109
|
+
id: string;
|
|
110
|
+
pythonExtract?: PythonExtract;
|
|
111
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* `@gameslate/sdk/plugin`: what a plugin's code builds against. The contract (GamePlugin,
|
|
3
|
+
* GameMemory, AssetExtractor, PluginModule), pure helpers, and the sandbox's guest side
|
|
4
|
+
* (`exposeToHost`, `exposeAssetsToHost`). No Node APIs: plugin bundles run in the core's QuickJS
|
|
5
|
+
* sandbox.
|
|
6
|
+
*/
|
|
7
|
+
export * from './contract.js';
|
|
8
|
+
export * from './seqlock.js';
|
|
9
|
+
export * from './sandbox-guest.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type SandboxAssetsGuestApi, type SandboxAssetsHostApi, type SandboxGuestApi, type SandboxHostApi } from '../index.js';
|
|
2
|
+
import type { PluginAssetsModule, PluginModule } from './contract.js';
|
|
3
|
+
declare global {
|
|
4
|
+
var __gs: SandboxGuestApi | undefined;
|
|
5
|
+
var __gsHost: SandboxHostApi | undefined;
|
|
6
|
+
var __gsAssets: SandboxAssetsGuestApi | undefined;
|
|
7
|
+
var __gsAssetsHost: SandboxAssetsHostApi | undefined;
|
|
8
|
+
}
|
|
9
|
+
/** Makes `plugin` callable by the core. A plugin bundle's entry calls this once. */
|
|
10
|
+
export declare function exposeToHost(plugin: PluginModule): void;
|
|
11
|
+
/** Makes a plugin's asset extractors callable by the core. An asset bundle's entry calls this. */
|
|
12
|
+
export declare function exposeAssetsToHost(plugin: PluginAssetsModule): void;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Bytes, SandboxInputFilesSchema, SandboxMemoryInfoSchema, packToData, packViewsFor, } from '../index.js';
|
|
2
|
+
function memoryFor(host, handle, info) {
|
|
3
|
+
return {
|
|
4
|
+
buildId: info.buildId,
|
|
5
|
+
commandLine: info.commandLine,
|
|
6
|
+
moduleBase: BigInt(info.moduleBase),
|
|
7
|
+
read: (address, byteLength) => {
|
|
8
|
+
const buf = host.read(handle, address.toString(), byteLength);
|
|
9
|
+
return buf ? new Bytes(new Uint8Array(buf)) : null;
|
|
10
|
+
},
|
|
11
|
+
isValidRange: (address, byteLength) => host.isValidRange(handle, address.toString(), byteLength),
|
|
12
|
+
resolveSymbol: name => {
|
|
13
|
+
const at = host.resolveSymbol(handle, name);
|
|
14
|
+
return at === null ? null : BigInt(at);
|
|
15
|
+
},
|
|
16
|
+
refreshMappings: () => host.refreshMappings(handle),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function describe(plugin) {
|
|
20
|
+
return {
|
|
21
|
+
id: plugin.id,
|
|
22
|
+
packs: plugin.packs.map(packToData),
|
|
23
|
+
sources: plugin.sources.map(s => ({
|
|
24
|
+
gameId: s.gameId,
|
|
25
|
+
stateSchemaVersion: s.stateSchemaVersion,
|
|
26
|
+
capabilities: [...s.capabilities],
|
|
27
|
+
supportedBuildIds: [...s.supportedBuildIds],
|
|
28
|
+
executableHints: [...s.executableHints],
|
|
29
|
+
...(s.wrapperMayOmitStats ? { wrapperMayOmitStats: [...s.wrapperMayOmitStats] } : {}),
|
|
30
|
+
...(s.unsupportedExecutables
|
|
31
|
+
? { unsupportedExecutables: [...s.unsupportedExecutables] }
|
|
32
|
+
: {}),
|
|
33
|
+
})),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/* A state the core validated before handing it in. Re-checking it with zod here cost ~4 ms per
|
|
37
|
+
* call in the interpreter (most of a views call), so the guard only narrows its type. */
|
|
38
|
+
function isStateShaped(value) {
|
|
39
|
+
return typeof value === 'object' && value !== null && 'phase' in value && 'locationId' in value;
|
|
40
|
+
}
|
|
41
|
+
function trustedState(json) {
|
|
42
|
+
const value = JSON.parse(json);
|
|
43
|
+
if (!isStateShaped(value))
|
|
44
|
+
throw new Error('the core sent no game state');
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
function find(list, match, what) {
|
|
48
|
+
const found = list.find(match);
|
|
49
|
+
if (!found)
|
|
50
|
+
throw new Error(`no ${what}`);
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
/** Makes `plugin` callable by the core. A plugin bundle's entry calls this once. */
|
|
54
|
+
export function exposeToHost(plugin) {
|
|
55
|
+
const sessions = new Map();
|
|
56
|
+
const source = (gameId) => find(plugin.sources, s => s.gameId === gameId, `source for ${gameId}`);
|
|
57
|
+
const pack = (gameId) => find(plugin.packs, p => p.id === gameId, `pack for ${gameId}`);
|
|
58
|
+
globalThis.__gs = {
|
|
59
|
+
describe: () => JSON.stringify(describe(plugin)),
|
|
60
|
+
createSession: (gameId, handle, memoryInfo) => {
|
|
61
|
+
const host = globalThis.__gsHost;
|
|
62
|
+
if (!host)
|
|
63
|
+
throw new Error('the core gave no memory capability');
|
|
64
|
+
const info = SandboxMemoryInfoSchema.parse(JSON.parse(memoryInfo));
|
|
65
|
+
const session = source(gameId).createSession(memoryFor(host, handle, info));
|
|
66
|
+
if (session)
|
|
67
|
+
sessions.set(handle, session);
|
|
68
|
+
return session !== null;
|
|
69
|
+
},
|
|
70
|
+
poll: handle => {
|
|
71
|
+
const state = sessions.get(handle)?.poll() ?? null;
|
|
72
|
+
return state === null ? null : JSON.stringify(state);
|
|
73
|
+
},
|
|
74
|
+
closeSession: handle => {
|
|
75
|
+
sessions.get(handle)?.close();
|
|
76
|
+
sessions.delete(handle);
|
|
77
|
+
},
|
|
78
|
+
views: (gameId, state) => JSON.stringify(packViewsFor(pack(gameId), trustedState(state))),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/* the asset capabilities, which the core installs only in an asset sandbox */
|
|
82
|
+
function assetsHost() {
|
|
83
|
+
const host = globalThis.__gsAssetsHost;
|
|
84
|
+
if (!host)
|
|
85
|
+
throw new Error('the core gave no input capability');
|
|
86
|
+
return host;
|
|
87
|
+
}
|
|
88
|
+
function inputFiles(host, json) {
|
|
89
|
+
const files = SandboxInputFilesSchema.parse(JSON.parse(json));
|
|
90
|
+
return files.map(f => ({
|
|
91
|
+
name: f.name,
|
|
92
|
+
size: f.size,
|
|
93
|
+
read: (offset, length) => new Uint8Array(host.inputRead(f.handle, offset, length)),
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
/* an exact-size copy, so only the output's own bytes cross */
|
|
97
|
+
const bufferOf = (data) => data.slice().buffer;
|
|
98
|
+
function toWire(host, out) {
|
|
99
|
+
if (out.kind === 'mesh')
|
|
100
|
+
return { ...out, data: host.emitBinary(bufferOf(out.data)) };
|
|
101
|
+
if (out.kind === 'image')
|
|
102
|
+
return { ...out, pixels: host.emitBinary(bufferOf(out.pixels)) };
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
/** Makes a plugin's asset extractors callable by the core. An asset bundle's entry calls this. */
|
|
106
|
+
export function exposeAssetsToHost(plugin) {
|
|
107
|
+
const running = new Map();
|
|
108
|
+
const extractor = (gameId) => {
|
|
109
|
+
const existing = running.get(gameId);
|
|
110
|
+
if (existing)
|
|
111
|
+
return existing;
|
|
112
|
+
const make = plugin.extractors.get(gameId);
|
|
113
|
+
if (!make)
|
|
114
|
+
throw new Error(`no extractor for ${gameId}`);
|
|
115
|
+
const made = make();
|
|
116
|
+
running.set(gameId, made);
|
|
117
|
+
return made;
|
|
118
|
+
};
|
|
119
|
+
globalThis.__gsAssets = {
|
|
120
|
+
games: () => [...plugin.extractors.keys()],
|
|
121
|
+
listAssets: (gameId, inputs, pick) => JSON.stringify(extractor(gameId).listAssets(inputFiles(assetsHost(), inputs), pick === 'folder' ? 'folder' : 'files')),
|
|
122
|
+
buildAsset: (gameId, id, inputs) => {
|
|
123
|
+
const host = assetsHost();
|
|
124
|
+
const outputs = extractor(gameId).buildAsset(id, inputFiles(host, inputs));
|
|
125
|
+
return JSON.stringify(outputs.map(o => toWire(host, o)));
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seqlock-style double-read helper (H4):
|
|
3
|
+
* Reads frame counter before and after sampling structs so mid-frame mutations are discarded.
|
|
4
|
+
*/
|
|
5
|
+
export declare function seqlockSample<T>(readFrameCounter: () => number | null, readPayload: () => T | null, maxRetries?: number): {
|
|
6
|
+
frame: number;
|
|
7
|
+
value: T;
|
|
8
|
+
} | null;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seqlock-style double-read helper (H4):
|
|
3
|
+
* Reads frame counter before and after sampling structs so mid-frame mutations are discarded.
|
|
4
|
+
*/
|
|
5
|
+
export function seqlockSample(readFrameCounter, readPayload, maxRetries = 2) {
|
|
6
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
7
|
+
const f1 = readFrameCounter();
|
|
8
|
+
if (f1 === null)
|
|
9
|
+
return null;
|
|
10
|
+
const payload = readPayload();
|
|
11
|
+
if (payload === null)
|
|
12
|
+
return null;
|
|
13
|
+
const f2 = readFrameCounter();
|
|
14
|
+
if (f2 !== null && f1 === f2) {
|
|
15
|
+
return { frame: f1, value: payload };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* The plugin contract this core speaks; a plugin built for another can't work.
|
|
4
|
+
* 2: `code` is a sandbox script built with `exposeToHost`, and extractors are a separate file.
|
|
5
|
+
* 3: asset extractors are a sandbox script (`assets`, `exposeAssetsToHost`); `extract` is only
|
|
6
|
+
* the Python renderer's hooks.
|
|
7
|
+
*/
|
|
8
|
+
export declare const SDK_API = 3;
|
|
9
|
+
export declare const PluginManifestSchema: z.ZodObject<{
|
|
10
|
+
id: z.ZodString;
|
|
11
|
+
name: z.ZodString;
|
|
12
|
+
version: z.ZodString;
|
|
13
|
+
sdkApi: z.ZodNumber;
|
|
14
|
+
/** core versions it was tested with; outside them it still loads, with a warning */
|
|
15
|
+
testedWith: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
|
|
16
|
+
/** the live code (packs and memory sources), run in the core's sandbox */
|
|
17
|
+
code: z.ZodObject<{
|
|
18
|
+
file: z.ZodString;
|
|
19
|
+
sha256: z.ZodString;
|
|
20
|
+
}, "strip", z.ZodTypeAny, {
|
|
21
|
+
file: string;
|
|
22
|
+
sha256: string;
|
|
23
|
+
}, {
|
|
24
|
+
file: string;
|
|
25
|
+
sha256: string;
|
|
26
|
+
}>;
|
|
27
|
+
/** asset extractors, run in the core's sandbox by extraction jobs */
|
|
28
|
+
assets: z.ZodOptional<z.ZodObject<{
|
|
29
|
+
file: z.ZodString;
|
|
30
|
+
sha256: z.ZodString;
|
|
31
|
+
}, "strip", z.ZodTypeAny, {
|
|
32
|
+
file: string;
|
|
33
|
+
sha256: string;
|
|
34
|
+
}, {
|
|
35
|
+
file: string;
|
|
36
|
+
sha256: string;
|
|
37
|
+
}>>;
|
|
38
|
+
/** the Python map renderer's hooks: still unsandboxed, until that renderer is ported */
|
|
39
|
+
extract: z.ZodOptional<z.ZodObject<{
|
|
40
|
+
file: z.ZodString;
|
|
41
|
+
sha256: z.ZodString;
|
|
42
|
+
}, "strip", z.ZodTypeAny, {
|
|
43
|
+
file: string;
|
|
44
|
+
sha256: string;
|
|
45
|
+
}, {
|
|
46
|
+
file: string;
|
|
47
|
+
sha256: string;
|
|
48
|
+
}>>;
|
|
49
|
+
games: z.ZodArray<z.ZodObject<{
|
|
50
|
+
id: z.ZodString;
|
|
51
|
+
title: z.ZodString;
|
|
52
|
+
/** where the player's own files for asset extraction usually are */
|
|
53
|
+
inputs: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
54
|
+
label: z.ZodString;
|
|
55
|
+
folders: z.ZodArray<z.ZodEffects<z.ZodString, string, string>, "many">;
|
|
56
|
+
}, "strip", z.ZodTypeAny, {
|
|
57
|
+
label: string;
|
|
58
|
+
folders: string[];
|
|
59
|
+
}, {
|
|
60
|
+
label: string;
|
|
61
|
+
folders: string[];
|
|
62
|
+
}>, "many">>;
|
|
63
|
+
}, "strip", z.ZodTypeAny, {
|
|
64
|
+
id: string;
|
|
65
|
+
title: string;
|
|
66
|
+
inputs: {
|
|
67
|
+
label: string;
|
|
68
|
+
folders: string[];
|
|
69
|
+
}[];
|
|
70
|
+
}, {
|
|
71
|
+
id: string;
|
|
72
|
+
title: string;
|
|
73
|
+
inputs?: {
|
|
74
|
+
label: string;
|
|
75
|
+
folders: string[];
|
|
76
|
+
}[] | undefined;
|
|
77
|
+
}>, "many">;
|
|
78
|
+
}, "strip", z.ZodTypeAny, {
|
|
79
|
+
code: {
|
|
80
|
+
file: string;
|
|
81
|
+
sha256: string;
|
|
82
|
+
};
|
|
83
|
+
id: string;
|
|
84
|
+
name: string;
|
|
85
|
+
games: {
|
|
86
|
+
id: string;
|
|
87
|
+
title: string;
|
|
88
|
+
inputs: {
|
|
89
|
+
label: string;
|
|
90
|
+
folders: string[];
|
|
91
|
+
}[];
|
|
92
|
+
}[];
|
|
93
|
+
version: string;
|
|
94
|
+
sdkApi: number;
|
|
95
|
+
testedWith: string[];
|
|
96
|
+
extract?: {
|
|
97
|
+
file: string;
|
|
98
|
+
sha256: string;
|
|
99
|
+
} | undefined;
|
|
100
|
+
assets?: {
|
|
101
|
+
file: string;
|
|
102
|
+
sha256: string;
|
|
103
|
+
} | undefined;
|
|
104
|
+
}, {
|
|
105
|
+
code: {
|
|
106
|
+
file: string;
|
|
107
|
+
sha256: string;
|
|
108
|
+
};
|
|
109
|
+
id: string;
|
|
110
|
+
name: string;
|
|
111
|
+
games: {
|
|
112
|
+
id: string;
|
|
113
|
+
title: string;
|
|
114
|
+
inputs?: {
|
|
115
|
+
label: string;
|
|
116
|
+
folders: string[];
|
|
117
|
+
}[] | undefined;
|
|
118
|
+
}[];
|
|
119
|
+
version: string;
|
|
120
|
+
sdkApi: number;
|
|
121
|
+
extract?: {
|
|
122
|
+
file: string;
|
|
123
|
+
sha256: string;
|
|
124
|
+
} | undefined;
|
|
125
|
+
assets?: {
|
|
126
|
+
file: string;
|
|
127
|
+
sha256: string;
|
|
128
|
+
} | undefined;
|
|
129
|
+
testedWith?: string[] | undefined;
|
|
130
|
+
}>;
|
|
131
|
+
export type PluginManifest = z.infer<typeof PluginManifestSchema>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/*
|
|
3
|
+
* A plugin's manifest (docs/plugin-architecture.md): who it is, which contract it speaks, the
|
|
4
|
+
* core versions it was tested with, its code file and that file's hash, and the games it covers.
|
|
5
|
+
* The core refuses a plugin for another contract version or whose code doesn't match its hash.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The plugin contract this core speaks; a plugin built for another can't work.
|
|
9
|
+
* 2: `code` is a sandbox script built with `exposeToHost`, and extractors are a separate file.
|
|
10
|
+
* 3: asset extractors are a sandbox script (`assets`, `exposeAssetsToHost`); `extract` is only
|
|
11
|
+
* the Python renderer's hooks.
|
|
12
|
+
*/
|
|
13
|
+
export const SDK_API = 3;
|
|
14
|
+
const Id = z
|
|
15
|
+
.string()
|
|
16
|
+
.regex(/^[a-z0-9][a-z0-9-]*$/, 'plugin and game ids are lower-case words and dashes');
|
|
17
|
+
/* a file next to the manifest, never a path out of the plugin's folder */
|
|
18
|
+
const FileName = z.string().regex(/^[A-Za-z0-9_][A-Za-z0-9_.-]*$/, 'a plain file name');
|
|
19
|
+
/* A folder under the player's home (`~/...`), never a path out of it. */
|
|
20
|
+
const HomeFolder = z
|
|
21
|
+
.string()
|
|
22
|
+
.regex(/^~\/[^\0]*$/, 'a folder under the home directory, starting with ~/')
|
|
23
|
+
.refine(p => !p.split('/').includes('..'), 'no ".." in folders');
|
|
24
|
+
/** Where to find the files an extractor builds from, offered to the player to pick from. */
|
|
25
|
+
const InputLocation = z.object({
|
|
26
|
+
label: z.string().min(1).max(100),
|
|
27
|
+
folders: z.array(HomeFolder).min(1).max(20),
|
|
28
|
+
});
|
|
29
|
+
const CodeFile = z.object({
|
|
30
|
+
file: FileName,
|
|
31
|
+
sha256: z.string().regex(/^[0-9a-f]{64}$/),
|
|
32
|
+
});
|
|
33
|
+
export const PluginManifestSchema = z.object({
|
|
34
|
+
id: Id,
|
|
35
|
+
name: z.string().min(1),
|
|
36
|
+
version: z.string().regex(/^\d+\.\d+\.\d+$/, 'version is major.minor.patch'),
|
|
37
|
+
sdkApi: z.number().int().positive(),
|
|
38
|
+
/** core versions it was tested with; outside them it still loads, with a warning */
|
|
39
|
+
testedWith: z.array(z.string()).default([]),
|
|
40
|
+
/** the live code (packs and memory sources), run in the core's sandbox */
|
|
41
|
+
code: CodeFile,
|
|
42
|
+
/** asset extractors, run in the core's sandbox by extraction jobs */
|
|
43
|
+
assets: CodeFile.optional(),
|
|
44
|
+
/** the Python map renderer's hooks: still unsandboxed, until that renderer is ported */
|
|
45
|
+
extract: CodeFile.optional(),
|
|
46
|
+
games: z
|
|
47
|
+
.array(z.object({
|
|
48
|
+
id: Id,
|
|
49
|
+
title: z.string().min(1),
|
|
50
|
+
/** where the player's own files for asset extraction usually are */
|
|
51
|
+
inputs: z.array(InputLocation).default([]),
|
|
52
|
+
}))
|
|
53
|
+
.min(1),
|
|
54
|
+
});
|