@gameslate/sdk 0.3.25 → 0.3.27
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 +7 -0
- package/build/build-plugin.d.mts +5 -0
- package/build/build-plugin.mjs +1 -0
- package/build/run-in-sandbox.mjs +39 -0
- package/lib/bytes.js +12 -5
- package/lib/plugin-manifest.d.ts +5 -0
- package/lib/plugin-manifest.js +7 -1
- package/package.json +1 -1
- package/template/claude/skills/build-gameslate-plugin/SKILL.md +16 -4
package/README.md
CHANGED
|
@@ -90,6 +90,13 @@ the input `downloads/<id>`; the player sees it among the plugin's permissions.
|
|
|
90
90
|
|
|
91
91
|
`0.3.x` speaks plugin contract (`sdkApi`) 3: new 0.3 versions add tools, never change the contract. The core refuses a plugin built for another contract.
|
|
92
92
|
|
|
93
|
+
- **0.3.27**: \`runInSandbox(code, dir, budgetMs)\` from \`@gameslate/sdk/build\`, for tests: bundles
|
|
94
|
+
\`code\` and runs it in QuickJS as the core does, so a cost only the sandbox has (a string built
|
|
95
|
+
by \`+=\`) fails a test instead of a player's build.
|
|
96
|
+
- **0.3.26**: `Bytes#toString` decodes 8 KiB per call and joins the pieces, since building the
|
|
97
|
+
string a byte at a time was quadratic in QuickJS (a 1 MB string took 147 s). `isCoreInput(name)`
|
|
98
|
+
and `REGISTRY_INPUT_NAME` name the inputs the core adds (the registry manifest, downloads), which
|
|
99
|
+
code reading every picked file must leave out.
|
|
93
100
|
- **0.3.25**: `extent` and `append` fold and append by loop; `gameslate-plugin build` refuses a
|
|
94
101
|
call whose argument count is an array's length (a spread argument, `.apply`, `Reflect.apply`,
|
|
95
102
|
`Reflect.construct`) unless a `/* spread-ok: <why> */` marker sits directly above its
|
package/build/build-plugin.d.mts
CHANGED
|
@@ -29,3 +29,8 @@ export function zipEntries(
|
|
|
29
29
|
/** The loader's limits on a plugin's WebAssembly, checked at build time too. */
|
|
30
30
|
export const MAX_WASM_MODULES: number;
|
|
31
31
|
export const MAX_WASM_BYTES: number;
|
|
32
|
+
|
|
33
|
+
/** For tests: runs `code` (TypeScript importing relative to `dir`) in QuickJS as the core's
|
|
34
|
+
* sandbox does and returns what it leaves in `globalThis.result`; fails past `budgetMs`. A cost
|
|
35
|
+
* only the sandbox has (a long string built by `+=`) shows here and not in vitest. */
|
|
36
|
+
export function runInSandbox(code: string, dir: string, budgetMs: number): Promise<unknown>;
|
package/build/build-plugin.mjs
CHANGED
|
@@ -8,6 +8,7 @@ import { checkSpreadLimit } from './spread-limit.mjs';
|
|
|
8
8
|
import { renderLicences } from './third-party-licences.mjs';
|
|
9
9
|
import { zipFolder } from './zip.mjs';
|
|
10
10
|
|
|
11
|
+
export { runInSandbox } from './run-in-sandbox.mjs';
|
|
11
12
|
export { zipEntries } from './zip.mjs';
|
|
12
13
|
|
|
13
14
|
/*
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import releaseSync from '@jitl/quickjs-wasmfile-release-sync';
|
|
2
|
+
import * as esbuild from 'esbuild';
|
|
3
|
+
import {
|
|
4
|
+
newQuickJSWASMModuleFromVariant,
|
|
5
|
+
shouldInterruptAfterDeadline,
|
|
6
|
+
} from 'quickjs-emscripten-core';
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* For tests: runs `code` (TypeScript, importing relative to `dir`) in QuickJS as the core's
|
|
10
|
+
* sandbox does, and returns what it leaves in `globalThis.result`. Some costs exist only there:
|
|
11
|
+
* QuickJS copies a whole string on every `+=`, so a long string built a piece at a time is
|
|
12
|
+
* quadratic in the sandbox and linear in Node. Such a test fails by running out of `budgetMs`.
|
|
13
|
+
*/
|
|
14
|
+
export async function runInSandbox(code, dir, budgetMs) {
|
|
15
|
+
const out = await esbuild.build({
|
|
16
|
+
stdin: { contents: code, resolveDir: dir, loader: 'ts' },
|
|
17
|
+
bundle: true,
|
|
18
|
+
format: 'iife',
|
|
19
|
+
platform: 'neutral',
|
|
20
|
+
write: false,
|
|
21
|
+
logLevel: 'silent',
|
|
22
|
+
});
|
|
23
|
+
const [file] = out.outputFiles;
|
|
24
|
+
if (!file) throw new Error('esbuild wrote no bundle');
|
|
25
|
+
const quickJs = await newQuickJSWASMModuleFromVariant(releaseSync);
|
|
26
|
+
const runtime = quickJs.newRuntime();
|
|
27
|
+
runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + budgetMs));
|
|
28
|
+
const vm = runtime.newContext();
|
|
29
|
+
try {
|
|
30
|
+
vm.unwrapResult(vm.evalCode(file.text)).dispose();
|
|
31
|
+
const result = vm.getProp(vm.global, 'result');
|
|
32
|
+
const value = vm.dump(result);
|
|
33
|
+
result.dispose();
|
|
34
|
+
return value;
|
|
35
|
+
} finally {
|
|
36
|
+
vm.dispose();
|
|
37
|
+
runtime.dispose();
|
|
38
|
+
}
|
|
39
|
+
}
|
package/lib/bytes.js
CHANGED
|
@@ -4,6 +4,10 @@
|
|
|
4
4
|
* match Buffer's so decoders read the same either way. Reads past the end throw a RangeError,
|
|
5
5
|
* as Buffer's do.
|
|
6
6
|
*/
|
|
7
|
+
/* Text is decoded this many bytes per call and the pieces joined: QuickJS copies the whole string
|
|
8
|
+
* on every `+=`, so a byte at a time is quadratic (a 1 MB string took 147 s in the sandbox), and
|
|
9
|
+
* one call per string would pass the sandbox's 65535-argument limit. */
|
|
10
|
+
const TEXT_CHUNK_BYTES = 8192;
|
|
7
11
|
export class Bytes {
|
|
8
12
|
data;
|
|
9
13
|
view;
|
|
@@ -79,11 +83,14 @@ export class Bytes {
|
|
|
79
83
|
* clears the high bit first, as Buffer does.
|
|
80
84
|
*/
|
|
81
85
|
toString(encoding, start = 0, end = this.data.length) {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
for (let i = Math.max(0, start); i <
|
|
85
|
-
|
|
86
|
+
const to = Math.min(end, this.data.length);
|
|
87
|
+
const parts = [];
|
|
88
|
+
for (let i = Math.max(0, start); i < to; i += TEXT_CHUNK_BYTES) {
|
|
89
|
+
const raw = this.data.subarray(i, Math.min(to, i + TEXT_CHUNK_BYTES));
|
|
90
|
+
const chunk = encoding === 'ascii' ? raw.map(b => b & 0x7f) : raw;
|
|
91
|
+
/* spread-ok: at most TEXT_CHUNK_BYTES bytes */
|
|
92
|
+
parts.push(String.fromCharCode(...chunk));
|
|
86
93
|
}
|
|
87
|
-
return
|
|
94
|
+
return parts.join('');
|
|
88
95
|
}
|
|
89
96
|
}
|
package/lib/plugin-manifest.d.ts
CHANGED
|
@@ -35,6 +35,11 @@ export declare const PluginDownloadSchema: z.ZodObject<{
|
|
|
35
35
|
export type PluginDownload = z.infer<typeof PluginDownloadSchema>;
|
|
36
36
|
/** The input name a download reaches the asset code under. */
|
|
37
37
|
export declare const downloadInputName: (id: string) => string;
|
|
38
|
+
/** The input name the game's registry manifest reaches the asset code under. */
|
|
39
|
+
export declare const REGISTRY_INPUT_NAME = "registry/manifest.json";
|
|
40
|
+
/** Whether an input is one the core adds (a download, the registry manifest), not the player's
|
|
41
|
+
* own file: code reading every picked file must leave these out. */
|
|
42
|
+
export declare const isCoreInput: (name: string) => boolean;
|
|
38
43
|
/**
|
|
39
44
|
* A build of a game that the plugin's live code reads memory from: the id its source matches
|
|
40
45
|
* (an ELF or PE build id, or an N64 ROM's code and version such as `CZLE.2`) and what the player
|
package/lib/plugin-manifest.js
CHANGED
|
@@ -97,8 +97,14 @@ export const PluginDownloadSchema = z
|
|
|
97
97
|
sha256: Sha256,
|
|
98
98
|
})
|
|
99
99
|
.strict();
|
|
100
|
+
const DOWNLOADS_FOLDER = 'downloads/';
|
|
100
101
|
/** The input name a download reaches the asset code under. */
|
|
101
|
-
export const downloadInputName = (id) =>
|
|
102
|
+
export const downloadInputName = (id) => `${DOWNLOADS_FOLDER}${id}`;
|
|
103
|
+
/** The input name the game's registry manifest reaches the asset code under. */
|
|
104
|
+
export const REGISTRY_INPUT_NAME = 'registry/manifest.json';
|
|
105
|
+
/** Whether an input is one the core adds (a download, the registry manifest), not the player's
|
|
106
|
+
* own file: code reading every picked file must leave these out. */
|
|
107
|
+
export const isCoreInput = (name) => name === REGISTRY_INPUT_NAME || name.startsWith(DOWNLOADS_FOLDER);
|
|
102
108
|
/**
|
|
103
109
|
* A build of a game that the plugin's live code reads memory from: the id its source matches
|
|
104
110
|
* (an ELF or PE build id, or an N64 ROM's code and version such as `CZLE.2`) and what the player
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gameslate/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.27",
|
|
4
4
|
"description": "Build plugins for GameSlate, the second-screen companion for PC games: the plugin contract, game-state and catalogue schemas, and the plugin builder.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -355,13 +355,16 @@ Images have a `purpose` (`atlas`, `map`, `icon`, `actor`, which picks the folder
|
|
|
355
355
|
|
|
356
356
|
Every image of a game uses one format. In the sandbox:
|
|
357
357
|
|
|
358
|
-
- **No `TextDecoder`, `TextEncoder` or `Buffer`.** Decode
|
|
359
|
-
|
|
358
|
+
- **No `TextDecoder`, `TextEncoder` or `Buffer`.** Decode Latin-1 and ASCII with `new
|
|
359
|
+
Bytes(data).toString('latin1', start, end)` (sdk >= 0.3.26 is fast at any length), and UTF-8 by
|
|
360
|
+
hand.
|
|
360
361
|
- **One asset per unit of work** (per map, per actor): each gets its own time budget (10 minutes)
|
|
361
362
|
and its own up-to-date stamp, and jobs build assets across several threads.
|
|
362
363
|
- Inputs are named relative to the folder the player picked (`mods/pack.o2r`). The core also hands
|
|
363
364
|
in the game's registry manifest as `registry/manifest.json` when there is one, so renders line up
|
|
364
|
-
with the registry's maps
|
|
365
|
+
with the registry's maps, and each download as `downloads/<id>`. Code that reads every picked
|
|
366
|
+
file leaves those out with `isCoreInput(name)` (sdk 0.3.26): idtech1 read the registry manifest
|
|
367
|
+
as a WAD, so every build from picked WADs failed until 2026-12-06.
|
|
365
368
|
- **Data from the web is a download, never a fetch.** Plugin code has no network and nothing
|
|
366
369
|
runs outside the sandbox (`extract.ts` fails the build). Declare the file in the game's
|
|
367
370
|
manifest entry, `downloads: [{ id, url, sha256 }]` with an exact https URL (a release asset, a
|
|
@@ -837,11 +840,20 @@ the plugin repos) in the same change as the fix. The next game should not have t
|
|
|
837
840
|
*/` on the line directly above the statement (a function, class, `export default` or class
|
|
838
841
|
property counts); it covers only calls that start on that statement's first line, never a loop
|
|
839
842
|
body or a multi-line arrow's body below, and the build refuses an empty reason or a marker that
|
|
840
|
-
marks nothing. Decode text
|
|
843
|
+
marks nothing. Decode text with `Bytes#toString`. Before marking a spread "small", find what
|
|
844
|
+
bounds it (a 4-byte field, a 3-vertex triangle, a format's u8 count): a string that only looks
|
|
845
|
+
short (a name, a path) is bounded by nothing. Test a fix with at least 300k items (Node starts
|
|
846
|
+
throwing at about 150k); a test that size also catches a loop that went quadratic. Name
|
|
847
|
+
`extent`'s `what` as a plural (`'vertex xs'`): its errors read "no vertex xs". Dependencies
|
|
841
848
|
aren't checked: if one handles map-sized arrays, test a big input in the sandbox
|
|
842
849
|
(`@gameslate/sdk/host`). After a change that grows the geometry, run `gameslate extract-assets
|
|
843
850
|
<game> --input <rom> --out-dir <scratch>` (the core's `dist/gameslate.mjs` with
|
|
844
851
|
`GAMESLATE_PLUGINS_DIR=<plugin>/dist`).
|
|
852
|
+
- **Never build a long string with `+=` a piece at a time.** QuickJS copies the whole string on
|
|
853
|
+
every `+=`, so it is quadratic where Node is linear: a 1 MB string a byte at a time took 147 s
|
|
854
|
+
in the sandbox (the sdk's own `Bytes#toString` did this until 0.3.26). Push the pieces into an
|
|
855
|
+
array and `join('')`, and run a multi-MB input through the sandbox with `runInSandbox(code,
|
|
856
|
+
dir, budgetMs)` from `@gameslate/sdk/build` (sdk 0.3.27), not only vitest.
|
|
845
857
|
- **Follow every source the floor or water query asks.** DK64's floor query merges the map mesh,
|
|
846
858
|
placed models and actor boxes; Conker's water height comes from water props, trigger-zone
|
|
847
859
|
volumes and surface type 8 at once. Port the game's query, not one of its sources, and say in
|