@gameslate/sdk 0.3.2 → 0.3.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/README.md +8 -0
- package/bin/gameslate-plugin.mjs +20 -1
- package/build/build-plugin.d.mts +21 -0
- package/build/init-plugin.d.mts +14 -0
- package/build/init-plugin.mjs +45 -0
- package/package.json +21 -3
- package/template/README.md +11 -0
- package/template/github/workflows/ci.yml +16 -0
- package/template/gitignore +4 -0
- package/template/package.json +22 -0
- package/template/plugin/live.ts +65 -0
- package/template/plugin/manifest.json +7 -0
- package/template/test/live.test.ts +36 -0
- package/template/tsconfig.json +14 -0
package/README.md
CHANGED
|
@@ -14,9 +14,17 @@ runs the plugin in a QuickJS sandbox, validates everything it returns, and owns
|
|
|
14
14
|
| `@gameslate/sdk` | Game state, catalogue (items, missions, collectibles, pins), conditions, map and asset schemas, `Bytes` |
|
|
15
15
|
| `@gameslate/sdk/plugin` | The plugin contract (`GamePlugin`, `GameMemory`, `AssetExtractor`, `PluginModule`), `seqlockSample`, and the sandbox adapters |
|
|
16
16
|
| `@gameslate/sdk/host` | (Node) the core's own sandbox and checks, for testing a built plugin the way the core runs it |
|
|
17
|
+
| `gameslate-plugin init` | Scaffolds a new plugin repository: an example plugin, a test in the core's sandbox, CI |
|
|
17
18
|
| `gameslate-plugin build` | Builds a plugin folder into what the core loads |
|
|
18
19
|
| `gameslate-plugin validate` | Checks a built plugin exactly as the core does: manifest, contract, hashes, and its code loaded in the sandbox |
|
|
19
20
|
|
|
21
|
+
## Start a plugin
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx --package @gameslate/sdk gameslate-plugin init gameslate-mygame --id mygame --name "My Game"
|
|
25
|
+
cd gameslate-mygame && yarn install && yarn check
|
|
26
|
+
```
|
|
27
|
+
|
|
20
28
|
## A plugin folder
|
|
21
29
|
|
|
22
30
|
```text
|
package/bin/gameslate-plugin.mjs
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { parseArgs } from 'node:util';
|
|
4
|
+
import fs from 'node:fs';
|
|
4
5
|
import { buildPlugin } from '../build/build-plugin.mjs';
|
|
6
|
+
import { initPlugin } from '../build/init-plugin.mjs';
|
|
5
7
|
|
|
6
8
|
/*
|
|
7
9
|
* gameslate-plugin build <pluginDir> --out <dir> [--tested-with <coreVersion> ...]
|
|
8
10
|
* Builds a plugin folder into what the GameSlate core loads (see build/build-plugin.mjs).
|
|
11
|
+
* gameslate-plugin init <dir> --id <id> [--name <name>]
|
|
12
|
+
* Scaffolds a new plugin repository: an example plugin, a sandbox test, CI.
|
|
9
13
|
* gameslate-plugin validate <builtDir> [--core-version <version>]
|
|
10
14
|
* Checks a built plugin exactly as the core does before running it: manifest, contract,
|
|
11
15
|
* hashes, then its live and asset code loaded in the core's own sandbox.
|
|
@@ -15,12 +19,15 @@ const { values, positionals } = parseArgs({
|
|
|
15
19
|
out: { type: 'string' },
|
|
16
20
|
'tested-with': { type: 'string', multiple: true },
|
|
17
21
|
'core-version': { type: 'string' },
|
|
22
|
+
id: { type: 'string' },
|
|
23
|
+
name: { type: 'string' },
|
|
18
24
|
},
|
|
19
25
|
allowPositionals: true,
|
|
20
26
|
});
|
|
21
27
|
const [command, dir] = positionals;
|
|
22
28
|
const USAGE = [
|
|
23
|
-
'usage: gameslate-plugin
|
|
29
|
+
'usage: gameslate-plugin init <dir> --id <id> [--name <name>]',
|
|
30
|
+
' gameslate-plugin build <pluginDir> --out <dir> [--tested-with <version>]',
|
|
24
31
|
' gameslate-plugin validate <builtDir> [--core-version <version>]',
|
|
25
32
|
].join('\n');
|
|
26
33
|
|
|
@@ -53,6 +60,18 @@ if (command === 'build' && dir && values.out) {
|
|
|
53
60
|
testedWith: values['tested-with'] ?? [],
|
|
54
61
|
});
|
|
55
62
|
console.log(`built ${built.id} ${built.version} -> ${values.out}`);
|
|
63
|
+
} else if (command === 'init' && dir && values.id) {
|
|
64
|
+
const { SDK_API } = await import('../lib/index.js');
|
|
65
|
+
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
66
|
+
initPlugin({
|
|
67
|
+
dir: path.resolve(dir),
|
|
68
|
+
id: values.id,
|
|
69
|
+
name: values.name ?? values.id,
|
|
70
|
+
sdkApi: SDK_API,
|
|
71
|
+
sdkVersion: pkg.version,
|
|
72
|
+
coreVersion: values['core-version'] ?? pkg.gameslate.testedCore,
|
|
73
|
+
});
|
|
74
|
+
console.log(`created ${dir}: yarn install && yarn check`);
|
|
56
75
|
} else if (command === 'validate' && dir) {
|
|
57
76
|
const problems = await validate(path.resolve(dir));
|
|
58
77
|
for (const p of problems) console.error(`refused: ${p}`);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Builds a plugin folder (manifest.json, live.ts, optional assets.ts) into what the core loads. */
|
|
2
|
+
export interface BuildPluginOptions {
|
|
3
|
+
srcDir: string;
|
|
4
|
+
/** replaced */
|
|
5
|
+
outDir: string;
|
|
6
|
+
/** core versions it was tested with (the manifest's testedWith) */
|
|
7
|
+
testedWith: string[];
|
|
8
|
+
/** esbuild aliases for the sandbox builds (a monorepo pointing at sources) */
|
|
9
|
+
alias?: Record<string, string>;
|
|
10
|
+
/** esbuild aliases for the Node build of extract.ts */
|
|
11
|
+
nodeAlias?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The built manifest (id, version, code hashes, …). */
|
|
15
|
+
export interface BuiltManifest {
|
|
16
|
+
id: string;
|
|
17
|
+
version: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildPlugin(options: BuildPluginOptions): Promise<BuiltManifest>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface InitPluginOptions {
|
|
2
|
+
/** where to write it; must not exist or be empty */
|
|
3
|
+
dir: string;
|
|
4
|
+
/** the plugin id: lower-case words and dashes */
|
|
5
|
+
id: string;
|
|
6
|
+
/** shown to the player */
|
|
7
|
+
name: string;
|
|
8
|
+
sdkApi: number;
|
|
9
|
+
sdkVersion: string;
|
|
10
|
+
/** the GameSlate version it's built and tested for */
|
|
11
|
+
coreVersion: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function initPlugin(options: InitPluginOptions): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
* Scaffolds a new plugin repository from template/: a working example plugin, a test that runs
|
|
7
|
+
* its built bundle in the core's own sandbox, CI, and the build/validate scripts.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const TEMPLATE = path.join(path.dirname(fileURLToPath(import.meta.url)), '../template');
|
|
11
|
+
const ID = /^[a-z0-9][a-z0-9-]*$/;
|
|
12
|
+
/* files whose leading dot npm would drop from the package, renamed on the way out */
|
|
13
|
+
const DOTFILES = new Map([
|
|
14
|
+
['gitignore', '.gitignore'],
|
|
15
|
+
['github', '.github'],
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
function copy(from, to, fill) {
|
|
19
|
+
for (const entry of fs.readdirSync(from, { withFileTypes: true })) {
|
|
20
|
+
const src = path.join(from, entry.name);
|
|
21
|
+
const dest = path.join(to, DOTFILES.get(entry.name) ?? entry.name);
|
|
22
|
+
if (entry.isDirectory()) {
|
|
23
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
24
|
+
copy(src, dest, fill);
|
|
25
|
+
} else {
|
|
26
|
+
fs.writeFileSync(dest, fill(fs.readFileSync(src, 'utf8')));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Writes a new plugin into `dir` (which must not exist or be empty). `sdkApi` and `sdkVersion`
|
|
33
|
+
* are this sdk's; `coreVersion` the GameSlate version it's built and tested for.
|
|
34
|
+
*/
|
|
35
|
+
export function initPlugin({ dir, id, name, sdkApi, sdkVersion, coreVersion }) {
|
|
36
|
+
if (!ID.test(id)) throw new Error(`plugin ids are lower-case words and dashes, not "${id}"`);
|
|
37
|
+
if (fs.existsSync(dir) && fs.readdirSync(dir).length > 0) throw new Error(`${dir} isn't empty`);
|
|
38
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
39
|
+
const values = { id, name, sdkVersion, coreVersion };
|
|
40
|
+
copy(TEMPLATE, dir, text => text.replace(/\{\{(\w+)\}\}/g, (m, key) => values[key] ?? m));
|
|
41
|
+
/* the manifest stays valid JSON in the template; the contract version is a number */
|
|
42
|
+
const manifestFile = path.join(dir, 'plugin/manifest.json');
|
|
43
|
+
const manifest = JSON.parse(fs.readFileSync(manifestFile, 'utf8'));
|
|
44
|
+
fs.writeFileSync(manifestFile, `${JSON.stringify({ ...manifest, sdkApi }, null, 2)}\n`);
|
|
45
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gameslate/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
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",
|
|
@@ -20,10 +20,17 @@
|
|
|
20
20
|
"types": "./lib/plugin/index.d.ts",
|
|
21
21
|
"default": "./lib/plugin/index.js"
|
|
22
22
|
},
|
|
23
|
-
"./build":
|
|
23
|
+
"./build": {
|
|
24
|
+
"types": "./build/build-plugin.d.mts",
|
|
25
|
+
"default": "./build/build-plugin.mjs"
|
|
26
|
+
},
|
|
24
27
|
"./host": {
|
|
25
28
|
"types": "./lib/host/index.d.ts",
|
|
26
29
|
"default": "./lib/host/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./init": {
|
|
32
|
+
"types": "./build/init-plugin.d.mts",
|
|
33
|
+
"default": "./build/init-plugin.mjs"
|
|
27
34
|
}
|
|
28
35
|
},
|
|
29
36
|
"bin": {
|
|
@@ -33,6 +40,7 @@
|
|
|
33
40
|
"lib",
|
|
34
41
|
"build",
|
|
35
42
|
"bin",
|
|
43
|
+
"template",
|
|
36
44
|
"README.md",
|
|
37
45
|
"LICENSE"
|
|
38
46
|
],
|
|
@@ -47,10 +55,17 @@
|
|
|
47
55
|
"types": "./lib/plugin/index.d.ts",
|
|
48
56
|
"default": "./lib/plugin/index.js"
|
|
49
57
|
},
|
|
50
|
-
"./build":
|
|
58
|
+
"./build": {
|
|
59
|
+
"types": "./build/build-plugin.d.mts",
|
|
60
|
+
"default": "./build/build-plugin.mjs"
|
|
61
|
+
},
|
|
51
62
|
"./host": {
|
|
52
63
|
"types": "./lib/host/index.d.ts",
|
|
53
64
|
"default": "./lib/host/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./init": {
|
|
67
|
+
"types": "./build/init-plugin.d.mts",
|
|
68
|
+
"default": "./build/init-plugin.mjs"
|
|
54
69
|
}
|
|
55
70
|
}
|
|
56
71
|
},
|
|
@@ -62,5 +77,8 @@
|
|
|
62
77
|
"esbuild": "^0.24.0",
|
|
63
78
|
"quickjs-emscripten-core": "0.31.0",
|
|
64
79
|
"zod": "^3.24.1"
|
|
80
|
+
},
|
|
81
|
+
"gameslate": {
|
|
82
|
+
"testedCore": "2.3.0"
|
|
65
83
|
}
|
|
66
84
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# gameslate-{{id}}
|
|
2
|
+
|
|
3
|
+
A [GameSlate](https://www.npmjs.com/package/@gameslate/sdk) plugin for {{name}}.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
yarn install
|
|
7
|
+
yarn check # typecheck, tests (in the core's own sandbox), build, validate
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
`plugin/live.ts` is an example: replace its reads with offsets verified against the game (pin each
|
|
11
|
+
build by its build id), and fill the pack's catalogue (items, missions, collectibles, pins).
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches: [main]
|
|
5
|
+
pull_request:
|
|
6
|
+
jobs:
|
|
7
|
+
check:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
steps:
|
|
10
|
+
- uses: actions/checkout@v4
|
|
11
|
+
- uses: actions/setup-node@v4
|
|
12
|
+
with:
|
|
13
|
+
node-version: 22
|
|
14
|
+
- run: corepack enable
|
|
15
|
+
- run: yarn install
|
|
16
|
+
- run: yarn check
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gameslate-{{id}}",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"test": "vitest run",
|
|
10
|
+
"build": "gameslate-plugin build plugin --out dist/{{id}} --tested-with {{coreVersion}}",
|
|
11
|
+
"validate": "gameslate-plugin validate dist/{{id}}",
|
|
12
|
+
"check": "yarn typecheck && yarn test && yarn build && yarn validate"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@gameslate/sdk": "^{{sdkVersion}}"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^22.10.0",
|
|
19
|
+
"typescript": "~5.7.0",
|
|
20
|
+
"vitest": "~2.1.8"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { GamePack } from '@gameslate/sdk';
|
|
2
|
+
import type { GamePlugin, PluginModule } from '@gameslate/sdk/plugin';
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* A starting point. A plugin only decodes: the core attaches to the game's process (matched by
|
|
6
|
+
* build id), hands this code its memory, and validates every state it returns.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const GAME_ID = '{{id}}-game';
|
|
10
|
+
|
|
11
|
+
const pack: GamePack = {
|
|
12
|
+
id: GAME_ID,
|
|
13
|
+
title: '{{name}}',
|
|
14
|
+
world: { unitsPerMeter: 1 },
|
|
15
|
+
catalogue: {
|
|
16
|
+
items: [],
|
|
17
|
+
counters: [{ id: 'health', label: 'Health', icon: 'heart' }],
|
|
18
|
+
flagSets: [],
|
|
19
|
+
inventoryLayouts: [],
|
|
20
|
+
missions: [],
|
|
21
|
+
collectibles: [],
|
|
22
|
+
reference: [],
|
|
23
|
+
chips: [],
|
|
24
|
+
pinRules: [],
|
|
25
|
+
},
|
|
26
|
+
describeState: state => [`${state.counters?.health ?? '?'} health`],
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/* Where the game keeps the player's health: replace with offsets verified against the game. */
|
|
30
|
+
const HEALTH_SYMBOL = 'player_health';
|
|
31
|
+
|
|
32
|
+
const source: GamePlugin = {
|
|
33
|
+
gameId: GAME_ID,
|
|
34
|
+
stateSchemaVersion: 2,
|
|
35
|
+
capabilities: ['stats'],
|
|
36
|
+
/* the exact builds these reads were verified against (ELF/PE build id → label) */
|
|
37
|
+
supportedBuildIds: new Map([
|
|
38
|
+
['0000000000000000000000000000000000000000', { region: 'example' }],
|
|
39
|
+
]),
|
|
40
|
+
executableHints: ['{{id}}'],
|
|
41
|
+
createSession: memory => {
|
|
42
|
+
const at = memory.resolveSymbol(HEALTH_SYMBOL);
|
|
43
|
+
if (at === null) return null;
|
|
44
|
+
return {
|
|
45
|
+
poll: () => {
|
|
46
|
+
const health = memory.read(at, 4)?.readInt32LE(0);
|
|
47
|
+
if (health === undefined) return null;
|
|
48
|
+
return {
|
|
49
|
+
phase: 'in_stage',
|
|
50
|
+
provenance: { source: 'ram', asOf: Date.now() },
|
|
51
|
+
locationId: 'unknown',
|
|
52
|
+
position: null,
|
|
53
|
+
inventory: [],
|
|
54
|
+
objectives: {},
|
|
55
|
+
flags: {},
|
|
56
|
+
counters: { health },
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
close: () => {},
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const plugin: PluginModule = { id: '{{id}}', packs: [pack], sources: [source] };
|
|
65
|
+
export default plugin;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Bytes } from '@gameslate/sdk';
|
|
5
|
+
import { buildPlugin } from '@gameslate/sdk/build';
|
|
6
|
+
import { inspectPlugin, loadQuickJs, readPluginManifest } from '@gameslate/sdk/host';
|
|
7
|
+
import type { GameMemory } from '@gameslate/sdk/plugin';
|
|
8
|
+
import { expect, it } from 'vitest';
|
|
9
|
+
|
|
10
|
+
/* The plugin as the core runs it: built, checked and loaded in the core's own sandbox. */
|
|
11
|
+
it('reads health in the sandbox', async () => {
|
|
12
|
+
const out = fs.mkdtempSync(path.join(os.tmpdir(), '{{id}}-'));
|
|
13
|
+
await buildPlugin({
|
|
14
|
+
srcDir: path.resolve(__dirname, '../plugin'),
|
|
15
|
+
outDir: out,
|
|
16
|
+
testedWith: ['test'],
|
|
17
|
+
});
|
|
18
|
+
const read = readPluginManifest(out);
|
|
19
|
+
if ('problem' in read) throw new Error(read.problem);
|
|
20
|
+
const plugin = inspectPlugin(await loadQuickJs(), out, read.manifest, {
|
|
21
|
+
coreVersion: 'test',
|
|
22
|
+
log: () => {},
|
|
23
|
+
});
|
|
24
|
+
if (typeof plugin === 'string') throw new Error(plugin);
|
|
25
|
+
const memory: GameMemory = {
|
|
26
|
+
buildId: null,
|
|
27
|
+
commandLine: [],
|
|
28
|
+
moduleBase: 0n,
|
|
29
|
+
read: () => new Bytes(Uint8Array.of(73, 0, 0, 0)),
|
|
30
|
+
isValidRange: () => true,
|
|
31
|
+
resolveSymbol: name => (name === 'player_health' ? 0x1000n : null),
|
|
32
|
+
refreshMappings: () => {},
|
|
33
|
+
};
|
|
34
|
+
const state = plugin.live.sources[0]?.createSession(memory)?.poll();
|
|
35
|
+
expect(state?.counters).toEqual({ health: 73 });
|
|
36
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"noUncheckedIndexedAccess": true,
|
|
8
|
+
"isolatedModules": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"types": ["node"],
|
|
11
|
+
"noEmit": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["plugin/**/*", "test/**/*"]
|
|
14
|
+
}
|