@gameslate/sdk 0.3.19 → 0.3.21

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.
@@ -2,7 +2,7 @@ import { execFile, spawn } from 'node:child_process';
2
2
  import dgram from 'node:dgram';
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
5
- import { firstBusyUdpPort, RETROARCH_UDP_PORTS } from '../lib/host/index.js';
5
+ import { firstBusyUdpPort, launchPrefixFor, RETROARCH_UDP_PORTS } from '../lib/host/index.js';
6
6
 
7
7
  /*
8
8
  * The real effects of `gameslate-plugin verify` (src/host/verify.ts has the steps and their
@@ -57,9 +57,8 @@ function started(child, stop) {
57
57
  });
58
58
  }
59
59
 
60
- /* The emulator's own pid: the child, or the launch wrapper's child when there's a prefix. */
61
- function emulatorPid(child, wrapped) {
62
- if (!wrapped) return child.pid;
60
+ /* The emulator's own pid: the launch wrapper's child. */
61
+ function emulatorPid(child) {
63
62
  const children = `/proc/${child.pid}/task/${child.pid}/children`;
64
63
  const pid = Number(fs.readFileSync(children, 'utf8').trim().split(/\s+/)[0]);
65
64
  if (!pid) throw new Error('the launch wrapper started no emulator');
@@ -68,13 +67,17 @@ function emulatorPid(child, wrapped) {
68
67
 
69
68
  async function launchEmulator(spec, at, log) {
70
69
  const { command, launchPrefix } = spec.emulator;
71
- const [first, ...rest] = [...launchPrefix, ...command];
70
+ const prefix = launchPrefixFor(launchPrefix, gameslatePath(spec, at), process.execPath);
71
+ const [first, ...rest] = [...prefix, ...command];
72
72
  const child = spawn(first, rest, { stdio: 'ignore', cwd: at('.') });
73
73
  log(`started ${path.basename(command[0])} (pid ${child.pid})`);
74
74
  await started(child, null);
75
- return { pid: emulatorPid(child, launchPrefix.length > 0), stop: () => child.kill('SIGTERM') };
75
+ return { pid: emulatorPid(child), stop: () => child.kill('SIGTERM') };
76
76
  }
77
77
 
78
+ /* the spec's dist/gameslate.mjs, resolved beside the spec */
79
+ const gameslatePath = (spec, at) => (spec.gameslate ? at(spec.gameslate) : undefined);
80
+
78
81
  /* a `gameslate drive` command; a frame's file goes beside the spec */
79
82
  function drive(spec, at, args) {
80
83
  if (!spec.gameslate) throw new Error('steps need "gameslate": the path to dist/gameslate.mjs');
@@ -98,7 +101,8 @@ function launchRetroArch(spec, at, log) {
98
101
  if (!ra) return Promise.resolve({ stop: () => undefined });
99
102
  const args = ['-L', at(ra.core), at(ra.rom), '-e', String(ra.stateSlot)];
100
103
  if (ra.config) args.push('--appendconfig', at(ra.config));
101
- const [first, ...rest] = [...ra.launchPrefix, at(ra.binary), ...args];
104
+ const prefix = launchPrefixFor(ra.launchPrefix, gameslatePath(spec, at), process.execPath);
105
+ const [first, ...rest] = [...prefix, at(ra.binary), ...args];
102
106
  const child = spawn(first, rest, { stdio: 'ignore' });
103
107
  log(`started RetroArch (pid ${child.pid}) on state slot ${ra.stateSlot}`);
104
108
  return new Promise((resolve, reject) => {
@@ -20,6 +20,9 @@ export interface InspectedPlugin {
20
20
  registryGames: string[];
21
21
  /** things that don't stop it loading (built for other core versions) */
22
22
  warnings: string[];
23
+ /** frees the live code's sandbox (a few MB): call it once the plugin is no longer used, or
24
+ * right away when it was only inspected */
25
+ dispose: () => void;
23
26
  }
24
27
  export interface InspectOptions {
25
28
  /** the core version it will run in; others it was tested with become a warning */
@@ -34,8 +34,25 @@ function describedProblem(manifest, d) {
34
34
  return `its code says it is ${d.id}`;
35
35
  const games = new Set(manifest.games.map(g => g.id));
36
36
  const stray = [...d.sources.map(s => s.gameId), ...d.packs.map(p => p.id)].filter(id => !games.has(id));
37
- return stray.length
38
- ? `its code covers games its manifest doesn't list (${stray.join(', ')})`
37
+ if (stray.length)
38
+ return `its code covers games its manifest doesn't list (${stray.join(', ')})`;
39
+ return undeclaredBuilds(manifest, d);
40
+ }
41
+ /* The builds a source matches are what the player consented to read: a source may match only
42
+ * builds its game's manifest entry lists (a manifest from before builds were declared lists none,
43
+ * and is not checked). */
44
+ function undeclaredBuilds(manifest, d) {
45
+ const declared = new Map(manifest.games.flatMap(g => g.builds ? [[g.id, new Set(g.builds.map(b => b.id))]] : []));
46
+ const undeclared = d.sources.flatMap(s => {
47
+ const builds = declared.get(s.gameId);
48
+ if (!builds)
49
+ return [];
50
+ return s.supportedBuildIds
51
+ .filter(([id]) => !builds.has(id))
52
+ .map(([id]) => `${s.gameId} ${id}`);
53
+ });
54
+ return undeclared.length
55
+ ? `its code reads builds its manifest doesn't list (${undeclared.join(', ')})`
39
56
  : null;
40
57
  }
41
58
  function loadLive(quickJs, dir, manifest, log) {
@@ -45,7 +62,7 @@ function loadLive(quickJs, dir, manifest, log) {
45
62
  const description = sandbox.describe();
46
63
  const problem = describedProblem(manifest, description);
47
64
  if (!problem)
48
- return sandbox.module(description);
65
+ return { module: sandbox.module(description), sandbox };
49
66
  sandbox.dispose();
50
67
  return problem;
51
68
  }
@@ -94,8 +111,24 @@ export function inspectPlugin(quickJs, dir, manifest, opts) {
94
111
  const live = loadLive(quickJs, dir, manifest, opts.log);
95
112
  if (typeof live === 'string')
96
113
  return live;
97
- const assets = loadAssets(quickJs, dir, manifest, opts.log);
98
- if (typeof assets === 'string')
114
+ let assets;
115
+ try {
116
+ assets = loadAssets(quickJs, dir, manifest, opts.log);
117
+ }
118
+ catch (e) {
119
+ live.sandbox.dispose();
120
+ throw e;
121
+ }
122
+ if (typeof assets === 'string') {
123
+ live.sandbox.dispose();
99
124
  return assets;
100
- return { manifest, live, assets, registryGames: registry.games, warnings };
125
+ }
126
+ return {
127
+ manifest,
128
+ live: live.module,
129
+ assets,
130
+ registryGames: registry.games,
131
+ warnings,
132
+ dispose: () => live.sandbox.dispose(),
133
+ };
101
134
  }
@@ -401,8 +401,8 @@ export declare const VerifySpecSchema: z.ZodObject<{
401
401
  core: z.ZodString;
402
402
  rom: z.ZodString;
403
403
  stateSlot: z.ZodNumber;
404
- /** run RetroArch through this (`["node", "<gameslate>/dist/gameslate.mjs", "run", "--"]`)
405
- * where only a game's parent may read its memory (ptrace_scope 1, ChimeraOS) */
404
+ /** run RetroArch through this; by default GameSlate's launch wrapper (`gameslate`
405
+ * `run --`), since GameSlate reads only games launched through it */
406
406
  launchPrefix: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
407
407
  /** a config appended to RetroArch's, which must turn network commands on */
408
408
  config: z.ZodOptional<z.ZodString>;
@@ -427,7 +427,7 @@ export declare const VerifySpecSchema: z.ZodObject<{
427
427
  /** any emulator, started with this command (the ROM on its command line, so the core finds it) */
428
428
  emulator: z.ZodOptional<z.ZodObject<{
429
429
  command: z.ZodArray<z.ZodString, "many">;
430
- /** as for RetroArch: through GameSlate's launch wrapper where only a parent may read */
430
+ /** as for RetroArch: by default through GameSlate's launch wrapper */
431
431
  launchPrefix: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
432
432
  }, "strip", z.ZodTypeAny, {
433
433
  launchPrefix: string[];
@@ -1181,6 +1181,12 @@ export declare const VerifySpecSchema: z.ZodObject<{
1181
1181
  }>;
1182
1182
  export type VerifySpec = z.infer<typeof VerifySpecSchema>;
1183
1183
  type MemoryCheck = VerifySpec['memory'][number];
1184
+ /**
1185
+ * What the emulator is started through: the spec's own prefix, else GameSlate's launch wrapper
1186
+ * (`node <gameslate> run --`). GameSlate reads memory only from games launched through it
1187
+ * ("Closing the sandbox" item 4), so an emulator started bare would never be read.
1188
+ */
1189
+ export declare function launchPrefixFor(launchPrefix: readonly string[], gameslate: string | undefined, nodePath: string): string[];
1184
1190
  /**
1185
1191
  * `${NAME}` in any string of a spec, from the environment, so a spec can be shared without one
1186
1192
  * machine's paths (`"${GOPHER64}"`, `"${N64_ROMS}/Mario Kart 64 (U).z64"`). Throws on a name that
@@ -79,8 +79,8 @@ export const VerifySpecSchema = z.object({
79
79
  core: z.string(),
80
80
  rom: z.string(),
81
81
  stateSlot: z.number().int().min(0).max(999),
82
- /** run RetroArch through this (`["node", "<gameslate>/dist/gameslate.mjs", "run", "--"]`)
83
- * where only a game's parent may read its memory (ptrace_scope 1, ChimeraOS) */
82
+ /** run RetroArch through this; by default GameSlate's launch wrapper (`gameslate`
83
+ * `run --`), since GameSlate reads only games launched through it */
84
84
  launchPrefix: z.array(z.string()).default([]),
85
85
  /** a config appended to RetroArch's, which must turn network commands on */
86
86
  config: z.string().optional(),
@@ -91,7 +91,7 @@ export const VerifySpecSchema = z.object({
91
91
  emulator: z
92
92
  .object({
93
93
  command: z.array(z.string()).min(1),
94
- /** as for RetroArch: through GameSlate's launch wrapper where only a parent may read */
94
+ /** as for RetroArch: by default through GameSlate's launch wrapper */
95
95
  launchPrefix: z.array(z.string()).default([]),
96
96
  })
97
97
  .optional(),
@@ -126,6 +126,19 @@ export const VerifySpecSchema = z.object({
126
126
  }))
127
127
  .default([]),
128
128
  });
129
+ /**
130
+ * What the emulator is started through: the spec's own prefix, else GameSlate's launch wrapper
131
+ * (`node <gameslate> run --`). GameSlate reads memory only from games launched through it
132
+ * ("Closing the sandbox" item 4), so an emulator started bare would never be read.
133
+ */
134
+ export function launchPrefixFor(launchPrefix, gameslate, nodePath) {
135
+ if (launchPrefix.length > 0)
136
+ return [...launchPrefix];
137
+ if (gameslate === undefined) {
138
+ throw new Error('GameSlate reads only games launched through it: give "gameslate" (its dist/gameslate.mjs) or a "launchPrefix"');
139
+ }
140
+ return [nodePath, gameslate, 'run', '--'];
141
+ }
129
142
  /**
130
143
  * `${NAME}` in any string of a spec, from the environment, so a spec can be shared without one
131
144
  * machine's paths (`"${GOPHER64}"`, `"${N64_ROMS}/Mario Kart 64 (U).z64"`). Throws on a name that