@gameslate/sdk 0.3.5 → 0.3.7
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 +26 -0
- package/bin/gameslate-plugin.mjs +14 -0
- package/build/init-plugin.mjs +2 -0
- package/build/verify-effects.mjs +120 -0
- package/lib/assets.d.ts +155 -8
- package/lib/assets.js +15 -2
- package/lib/catalogue.d.ts +6 -6
- package/lib/host/asset-sandbox.d.ts +17 -3
- package/lib/host/asset-sandbox.js +27 -6
- package/lib/host/index.d.ts +1 -0
- package/lib/host/index.js +1 -0
- package/lib/host/live-sandbox.js +10 -1
- package/lib/host/quickjs-guest.d.ts +3 -0
- package/lib/host/quickjs-guest.js +17 -1
- package/lib/host/verify.d.ts +210 -0
- package/lib/host/verify.js +146 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/n64-address.d.ts +5 -0
- package/lib/n64-address.js +25 -0
- package/lib/pack.d.ts +6 -6
- package/lib/plugin/contract.d.ts +16 -1
- package/lib/plugin/sandbox-guest.js +8 -2
- package/lib/plugin-manifest.d.ts +19 -1
- package/lib/plugin-manifest.js +33 -6
- package/lib/plugins-api.d.ts +12 -0
- package/lib/plugins-api.js +3 -0
- package/lib/sandbox-protocol.d.ts +10 -8
- package/lib/signature.d.ts +46 -0
- package/lib/signature.js +80 -0
- package/package.json +1 -1
- package/template/claude/skills/build-gameslate-plugin/SKILL.md +182 -0
- package/template/claude/skills/verify-gameslate-plugin/SKILL.md +121 -0
- package/template/test/live.test.ts +5 -2
package/lib/plugin-manifest.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { z } from 'zod';
|
|
|
6
6
|
* the Python renderer's hooks.
|
|
7
7
|
*/
|
|
8
8
|
export declare const SDK_API = 3;
|
|
9
|
+
/** Why a manifest may not offer `folder`, or null. */
|
|
10
|
+
export declare function homeFolderProblem(folder: string): string | null;
|
|
9
11
|
export declare const PluginManifestSchema: z.ZodObject<{
|
|
10
12
|
id: z.ZodString;
|
|
11
13
|
name: z.ZodString;
|
|
@@ -35,7 +37,8 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
35
37
|
file: string;
|
|
36
38
|
sha256: string;
|
|
37
39
|
}>>;
|
|
38
|
-
/**
|
|
40
|
+
/** steps that run unsandboxed after a game's extraction job (e.g. data fetched over the
|
|
41
|
+
* network); the Python renderer's hooks lived here too */
|
|
39
42
|
extract: z.ZodOptional<z.ZodObject<{
|
|
40
43
|
file: z.ZodString;
|
|
41
44
|
sha256: z.ZodString;
|
|
@@ -49,16 +52,23 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
49
52
|
games: z.ZodArray<z.ZodObject<{
|
|
50
53
|
id: z.ZodString;
|
|
51
54
|
title: z.ZodString;
|
|
55
|
+
/** short names a player may type for it (`gameslate extract-assets oot`) */
|
|
56
|
+
aliases: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
52
57
|
/** where the player's own files for asset extraction usually are */
|
|
53
58
|
inputs: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
54
59
|
label: z.ZodString;
|
|
55
60
|
folders: z.ZodArray<z.ZodEffects<z.ZodString, string, string>, "many">;
|
|
61
|
+
/** folders inside the picked one whose files are inputs too, named `<subfolder>/<file>`
|
|
62
|
+
* (e.g. a port's `mods`) */
|
|
63
|
+
subfolders: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
56
64
|
}, "strip", z.ZodTypeAny, {
|
|
57
65
|
label: string;
|
|
58
66
|
folders: string[];
|
|
67
|
+
subfolders?: string[] | undefined;
|
|
59
68
|
}, {
|
|
60
69
|
label: string;
|
|
61
70
|
folders: string[];
|
|
71
|
+
subfolders?: string[] | undefined;
|
|
62
72
|
}>, "many">>;
|
|
63
73
|
}, "strip", z.ZodTypeAny, {
|
|
64
74
|
id: string;
|
|
@@ -66,13 +76,17 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
66
76
|
inputs: {
|
|
67
77
|
label: string;
|
|
68
78
|
folders: string[];
|
|
79
|
+
subfolders?: string[] | undefined;
|
|
69
80
|
}[];
|
|
81
|
+
aliases?: string[] | undefined;
|
|
70
82
|
}, {
|
|
71
83
|
id: string;
|
|
72
84
|
title: string;
|
|
85
|
+
aliases?: string[] | undefined;
|
|
73
86
|
inputs?: {
|
|
74
87
|
label: string;
|
|
75
88
|
folders: string[];
|
|
89
|
+
subfolders?: string[] | undefined;
|
|
76
90
|
}[] | undefined;
|
|
77
91
|
}>, "many">;
|
|
78
92
|
}, "strip", z.ZodTypeAny, {
|
|
@@ -88,7 +102,9 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
88
102
|
inputs: {
|
|
89
103
|
label: string;
|
|
90
104
|
folders: string[];
|
|
105
|
+
subfolders?: string[] | undefined;
|
|
91
106
|
}[];
|
|
107
|
+
aliases?: string[] | undefined;
|
|
92
108
|
}[];
|
|
93
109
|
version: string;
|
|
94
110
|
sdkApi: number;
|
|
@@ -111,9 +127,11 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
111
127
|
games: {
|
|
112
128
|
id: string;
|
|
113
129
|
title: string;
|
|
130
|
+
aliases?: string[] | undefined;
|
|
114
131
|
inputs?: {
|
|
115
132
|
label: string;
|
|
116
133
|
folders: string[];
|
|
134
|
+
subfolders?: string[] | undefined;
|
|
117
135
|
}[] | undefined;
|
|
118
136
|
}[];
|
|
119
137
|
version: string;
|
package/lib/plugin-manifest.js
CHANGED
|
@@ -16,15 +16,39 @@ const Id = z
|
|
|
16
16
|
.regex(/^[a-z0-9][a-z0-9-]*$/, 'plugin and game ids are lower-case words and dashes');
|
|
17
17
|
/* a file next to the manifest, never a path out of the plugin's folder */
|
|
18
18
|
const FileName = z.string().regex(/^[A-Za-z0-9_][A-Za-z0-9_.-]*$/, 'a plain file name');
|
|
19
|
-
/*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
/* Hidden folders where games live; any other (`~/.ssh`, `~/.config/gameslate` with the pairing
|
|
20
|
+
* token) is refused, since a sandboxed plugin can read every file in an offered folder and show
|
|
21
|
+
* it in what it builds. */
|
|
22
|
+
const GAME_DOT_FOLDERS = ['~/.var/app/', '~/.local/share/', '~/.steam/'];
|
|
23
|
+
const OWN_FOLDER = '~/.local/share/gameslate';
|
|
24
|
+
/** Why a manifest may not offer `folder`, or null. */
|
|
25
|
+
export function homeFolderProblem(folder) {
|
|
26
|
+
if (!/^~\/[^\0]+$/.test(folder))
|
|
27
|
+
return 'a folder under the home directory, starting with ~/';
|
|
28
|
+
const parts = folder.split('/');
|
|
29
|
+
if (parts.includes('..') || parts.includes('.'))
|
|
30
|
+
return 'no "." or ".." in folders';
|
|
31
|
+
const prefix = GAME_DOT_FOLDERS.find(p => folder.startsWith(p));
|
|
32
|
+
const rest = prefix ? folder.slice(prefix.length) : folder.slice(2);
|
|
33
|
+
if (rest === '' || rest.split('/').some(p => p.startsWith('.')))
|
|
34
|
+
return `a game's folder: no hidden folders except under ${GAME_DOT_FOLDERS.join(', ')}`;
|
|
35
|
+
if (`${folder}/`.startsWith(`${OWN_FOLDER}/`))
|
|
36
|
+
return "not GameSlate's own folder";
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
/* A folder under the player's home (`~/...`) that a game's files may be in. */
|
|
40
|
+
const HomeFolder = z.string().superRefine((p, ctx) => {
|
|
41
|
+
const problem = homeFolderProblem(p);
|
|
42
|
+
if (problem)
|
|
43
|
+
ctx.addIssue({ code: 'custom', message: problem });
|
|
44
|
+
});
|
|
24
45
|
/** Where to find the files an extractor builds from, offered to the player to pick from. */
|
|
25
46
|
const InputLocation = z.object({
|
|
26
47
|
label: z.string().min(1).max(100),
|
|
27
48
|
folders: z.array(HomeFolder).min(1).max(20),
|
|
49
|
+
/** folders inside the picked one whose files are inputs too, named `<subfolder>/<file>`
|
|
50
|
+
* (e.g. a port's `mods`) */
|
|
51
|
+
subfolders: z.array(FileName).max(10).optional(),
|
|
28
52
|
});
|
|
29
53
|
const CodeFile = z.object({
|
|
30
54
|
file: FileName,
|
|
@@ -41,12 +65,15 @@ export const PluginManifestSchema = z.object({
|
|
|
41
65
|
code: CodeFile,
|
|
42
66
|
/** asset extractors, run in the core's sandbox by extraction jobs */
|
|
43
67
|
assets: CodeFile.optional(),
|
|
44
|
-
/**
|
|
68
|
+
/** steps that run unsandboxed after a game's extraction job (e.g. data fetched over the
|
|
69
|
+
* network); the Python renderer's hooks lived here too */
|
|
45
70
|
extract: CodeFile.optional(),
|
|
46
71
|
games: z
|
|
47
72
|
.array(z.object({
|
|
48
73
|
id: Id,
|
|
49
74
|
title: z.string().min(1),
|
|
75
|
+
/** short names a player may type for it (`gameslate extract-assets oot`) */
|
|
76
|
+
aliases: z.array(Id).max(5).optional(),
|
|
50
77
|
/** where the player's own files for asset extraction usually are */
|
|
51
78
|
inputs: z.array(InputLocation).default([]),
|
|
52
79
|
}))
|
package/lib/plugins-api.d.ts
CHANGED
|
@@ -134,7 +134,10 @@ export declare const JobStatusSchema: z.ZodObject<{
|
|
|
134
134
|
/** assets skipped because an earlier run built them from the same code and inputs */
|
|
135
135
|
upToDate: z.ZodNumber;
|
|
136
136
|
failed: z.ZodNumber;
|
|
137
|
+
/** the asset that finished last */
|
|
137
138
|
current: z.ZodOptional<z.ZodString>;
|
|
139
|
+
/** the assets being built right now (several, across builder threads) */
|
|
140
|
+
building: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
138
141
|
notes: z.ZodArray<z.ZodString, "many">;
|
|
139
142
|
failures: z.ZodArray<z.ZodObject<{
|
|
140
143
|
label: z.ZodString;
|
|
@@ -164,6 +167,7 @@ export declare const JobStatusSchema: z.ZodObject<{
|
|
|
164
167
|
}[];
|
|
165
168
|
current?: string | undefined;
|
|
166
169
|
error?: string | undefined;
|
|
170
|
+
building?: string[] | undefined;
|
|
167
171
|
}, {
|
|
168
172
|
id: number;
|
|
169
173
|
done: number;
|
|
@@ -181,6 +185,7 @@ export declare const JobStatusSchema: z.ZodObject<{
|
|
|
181
185
|
}[];
|
|
182
186
|
current?: string | undefined;
|
|
183
187
|
error?: string | undefined;
|
|
188
|
+
building?: string[] | undefined;
|
|
184
189
|
}>;
|
|
185
190
|
export type JobStatus = z.infer<typeof JobStatusSchema>;
|
|
186
191
|
export declare const PluginsResponseSchema: z.ZodObject<{
|
|
@@ -317,7 +322,10 @@ export declare const PluginsResponseSchema: z.ZodObject<{
|
|
|
317
322
|
/** assets skipped because an earlier run built them from the same code and inputs */
|
|
318
323
|
upToDate: z.ZodNumber;
|
|
319
324
|
failed: z.ZodNumber;
|
|
325
|
+
/** the asset that finished last */
|
|
320
326
|
current: z.ZodOptional<z.ZodString>;
|
|
327
|
+
/** the assets being built right now (several, across builder threads) */
|
|
328
|
+
building: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
321
329
|
notes: z.ZodArray<z.ZodString, "many">;
|
|
322
330
|
failures: z.ZodArray<z.ZodObject<{
|
|
323
331
|
label: z.ZodString;
|
|
@@ -347,6 +355,7 @@ export declare const PluginsResponseSchema: z.ZodObject<{
|
|
|
347
355
|
}[];
|
|
348
356
|
current?: string | undefined;
|
|
349
357
|
error?: string | undefined;
|
|
358
|
+
building?: string[] | undefined;
|
|
350
359
|
}, {
|
|
351
360
|
id: number;
|
|
352
361
|
done: number;
|
|
@@ -364,6 +373,7 @@ export declare const PluginsResponseSchema: z.ZodObject<{
|
|
|
364
373
|
}[];
|
|
365
374
|
current?: string | undefined;
|
|
366
375
|
error?: string | undefined;
|
|
376
|
+
building?: string[] | undefined;
|
|
367
377
|
}>>;
|
|
368
378
|
}, "strip", z.ZodTypeAny, {
|
|
369
379
|
plugins: {
|
|
@@ -407,6 +417,7 @@ export declare const PluginsResponseSchema: z.ZodObject<{
|
|
|
407
417
|
}[];
|
|
408
418
|
current?: string | undefined;
|
|
409
419
|
error?: string | undefined;
|
|
420
|
+
building?: string[] | undefined;
|
|
410
421
|
} | null;
|
|
411
422
|
}, {
|
|
412
423
|
plugins: {
|
|
@@ -450,6 +461,7 @@ export declare const PluginsResponseSchema: z.ZodObject<{
|
|
|
450
461
|
}[];
|
|
451
462
|
current?: string | undefined;
|
|
452
463
|
error?: string | undefined;
|
|
464
|
+
building?: string[] | undefined;
|
|
453
465
|
} | null;
|
|
454
466
|
}>;
|
|
455
467
|
export type PluginsResponse = z.infer<typeof PluginsResponseSchema>;
|
package/lib/plugins-api.js
CHANGED
|
@@ -29,7 +29,10 @@ export const JobStatusSchema = z.object({
|
|
|
29
29
|
/** assets skipped because an earlier run built them from the same code and inputs */
|
|
30
30
|
upToDate: z.number().int(),
|
|
31
31
|
failed: z.number().int(),
|
|
32
|
+
/** the asset that finished last */
|
|
32
33
|
current: z.string().optional(),
|
|
34
|
+
/** the assets being built right now (several, across builder threads) */
|
|
35
|
+
building: z.array(z.string()).optional(),
|
|
33
36
|
notes: z.array(z.string()),
|
|
34
37
|
failures: z.array(z.object({ label: z.string(), error: z.string() })),
|
|
35
38
|
error: z.string().optional(),
|
|
@@ -92,17 +92,17 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
92
92
|
id: string;
|
|
93
93
|
icon?: string | undefined;
|
|
94
94
|
max?: string | number | undefined;
|
|
95
|
+
format?: "number" | "percent" | undefined;
|
|
95
96
|
display?: Record<string, string> | undefined;
|
|
96
97
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
97
|
-
format?: "number" | "percent" | undefined;
|
|
98
98
|
}, {
|
|
99
99
|
label: string;
|
|
100
100
|
id: string;
|
|
101
101
|
icon?: string | undefined;
|
|
102
102
|
max?: string | number | undefined;
|
|
103
|
+
format?: "number" | "percent" | undefined;
|
|
103
104
|
display?: Record<string, string> | undefined;
|
|
104
105
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
105
|
-
format?: "number" | "percent" | undefined;
|
|
106
106
|
}>, "many">;
|
|
107
107
|
flagSets: z.ZodArray<z.ZodString, "many">;
|
|
108
108
|
inventoryLayouts: z.ZodArray<z.ZodDiscriminatedUnion<"kind", [z.ZodObject<{
|
|
@@ -507,9 +507,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
507
507
|
id: string;
|
|
508
508
|
icon?: string | undefined;
|
|
509
509
|
max?: string | number | undefined;
|
|
510
|
+
format?: "number" | "percent" | undefined;
|
|
510
511
|
display?: Record<string, string> | undefined;
|
|
511
512
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
512
|
-
format?: "number" | "percent" | undefined;
|
|
513
513
|
}[];
|
|
514
514
|
flagSets: string[];
|
|
515
515
|
items: {
|
|
@@ -626,9 +626,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
626
626
|
id: string;
|
|
627
627
|
icon?: string | undefined;
|
|
628
628
|
max?: string | number | undefined;
|
|
629
|
+
format?: "number" | "percent" | undefined;
|
|
629
630
|
display?: Record<string, string> | undefined;
|
|
630
631
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
631
|
-
format?: "number" | "percent" | undefined;
|
|
632
632
|
}[];
|
|
633
633
|
flagSets: string[];
|
|
634
634
|
items: {
|
|
@@ -763,9 +763,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
763
763
|
id: string;
|
|
764
764
|
icon?: string | undefined;
|
|
765
765
|
max?: string | number | undefined;
|
|
766
|
+
format?: "number" | "percent" | undefined;
|
|
766
767
|
display?: Record<string, string> | undefined;
|
|
767
768
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
768
|
-
format?: "number" | "percent" | undefined;
|
|
769
769
|
}[];
|
|
770
770
|
flagSets: string[];
|
|
771
771
|
items: {
|
|
@@ -890,9 +890,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
890
890
|
id: string;
|
|
891
891
|
icon?: string | undefined;
|
|
892
892
|
max?: string | number | undefined;
|
|
893
|
+
format?: "number" | "percent" | undefined;
|
|
893
894
|
display?: Record<string, string> | undefined;
|
|
894
895
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
895
|
-
format?: "number" | "percent" | undefined;
|
|
896
896
|
}[];
|
|
897
897
|
flagSets: string[];
|
|
898
898
|
items: {
|
|
@@ -1070,9 +1070,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
1070
1070
|
id: string;
|
|
1071
1071
|
icon?: string | undefined;
|
|
1072
1072
|
max?: string | number | undefined;
|
|
1073
|
+
format?: "number" | "percent" | undefined;
|
|
1073
1074
|
display?: Record<string, string> | undefined;
|
|
1074
1075
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
1075
|
-
format?: "number" | "percent" | undefined;
|
|
1076
1076
|
}[];
|
|
1077
1077
|
flagSets: string[];
|
|
1078
1078
|
items: {
|
|
@@ -1214,9 +1214,9 @@ export declare const SandboxDescriptionSchema: z.ZodObject<{
|
|
|
1214
1214
|
id: string;
|
|
1215
1215
|
icon?: string | undefined;
|
|
1216
1216
|
max?: string | number | undefined;
|
|
1217
|
+
format?: "number" | "percent" | undefined;
|
|
1217
1218
|
display?: Record<string, string> | undefined;
|
|
1218
1219
|
role?: "keys" | "health" | "magic" | "armor" | undefined;
|
|
1219
|
-
format?: "number" | "percent" | undefined;
|
|
1220
1220
|
}[];
|
|
1221
1221
|
flagSets: string[];
|
|
1222
1222
|
items: {
|
|
@@ -1378,6 +1378,8 @@ export interface SandboxHostApi {
|
|
|
1378
1378
|
isValidRange(handle: number, address: string, byteLength: number): boolean;
|
|
1379
1379
|
resolveSymbol(handle: number, name: string): string | null;
|
|
1380
1380
|
refreshMappings(handle: number): void;
|
|
1381
|
+
/** a signature scan (JSON of a Signature); the address as a decimal string, or null */
|
|
1382
|
+
scan(handle: number, signature: string): string | null;
|
|
1381
1383
|
}
|
|
1382
1384
|
/** A player's file as the asset guest sees it: a handle for `inputRead`, its name and size. */
|
|
1383
1385
|
export declare const SandboxInputFileSchema: z.ZodObject<{
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const SignatureSchema: z.ZodEffects<z.ZodObject<{
|
|
3
|
+
pattern: z.ZodEffects<z.ZodString, string, string>;
|
|
4
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
5
|
+
kind: z.ZodEnum<["match", "rip32", "abs32", "abs64"]>;
|
|
6
|
+
instructionEnd: z.ZodDefault<z.ZodNumber>;
|
|
7
|
+
}, "strict", z.ZodTypeAny, {
|
|
8
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
9
|
+
pattern: string;
|
|
10
|
+
offset: number;
|
|
11
|
+
instructionEnd: number;
|
|
12
|
+
}, {
|
|
13
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
14
|
+
pattern: string;
|
|
15
|
+
offset?: number | undefined;
|
|
16
|
+
instructionEnd?: number | undefined;
|
|
17
|
+
}>, {
|
|
18
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
19
|
+
pattern: string;
|
|
20
|
+
offset: number;
|
|
21
|
+
instructionEnd: number;
|
|
22
|
+
}, {
|
|
23
|
+
kind: "match" | "rip32" | "abs32" | "abs64";
|
|
24
|
+
pattern: string;
|
|
25
|
+
offset?: number | undefined;
|
|
26
|
+
instructionEnd?: number | undefined;
|
|
27
|
+
}>;
|
|
28
|
+
export type Signature = z.input<typeof SignatureSchema>;
|
|
29
|
+
export type ParsedSignature = z.output<typeof SignatureSchema>;
|
|
30
|
+
/** A pattern as bytes and which of them must match. */
|
|
31
|
+
export declare function parsePattern(pattern: string): {
|
|
32
|
+
bytes: Uint8Array;
|
|
33
|
+
fixed: boolean[];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Every offset in `haystack` where the pattern matches (at most `limit`). The first byte is
|
|
37
|
+
* fixed (the schema insists), so the native indexOf finds candidates.
|
|
38
|
+
*/
|
|
39
|
+
export declare function findPattern(haystack: Uint8Array, pattern: string, limit?: number): number[];
|
|
40
|
+
/** How many bytes past the field `resolveSignature` needs to read. */
|
|
41
|
+
export declare function fieldBytes(sig: ParsedSignature): number;
|
|
42
|
+
/**
|
|
43
|
+
* The address a match points to. `fieldAddress` is match + offset; `field` holds the bytes there
|
|
44
|
+
* (little-endian, `fieldBytes(sig)` of them).
|
|
45
|
+
*/
|
|
46
|
+
export declare function resolveSignature(sig: ParsedSignature, fieldAddress: bigint, field: Uint8Array): bigint;
|
package/lib/signature.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/*
|
|
3
|
+
* Signature scans (issue #14): finding where a closed-source game keeps something by a byte
|
|
4
|
+
* pattern in its code, so a plugin survives patches and store versions instead of hard-coding an
|
|
5
|
+
* address. The core runs the scan over the game's executable image; this module is the pure part
|
|
6
|
+
* (patterns, matching, and turning a match into an address) shared by the core and tests.
|
|
7
|
+
*
|
|
8
|
+
* pattern "48 8B 05 ?? ?? ?? ??" hex bytes, `??` for any byte
|
|
9
|
+
* offset bytes from the match's start to the field of interest
|
|
10
|
+
* kind what the field is:
|
|
11
|
+
* match the field's own address
|
|
12
|
+
* rip32 a 32-bit displacement relative to the end of the instruction (x86-64 RIP
|
|
13
|
+
* addressing); `instructionEnd` counts any bytes after the displacement (an
|
|
14
|
+
* immediate) that still belong to the instruction
|
|
15
|
+
* abs32 / abs64 an absolute pointer stored there
|
|
16
|
+
*/
|
|
17
|
+
const PATTERN_RE = /^(?:[0-9A-Fa-f]{2}|\?\?)(?: (?:[0-9A-Fa-f]{2}|\?\?))*$/;
|
|
18
|
+
/* Long enough to be unique in a game's code, short enough to read in one go. */
|
|
19
|
+
const MAX_PATTERN_BYTES = 64;
|
|
20
|
+
export const SignatureSchema = z
|
|
21
|
+
.object({
|
|
22
|
+
pattern: z
|
|
23
|
+
.string()
|
|
24
|
+
.regex(PATTERN_RE, 'hex bytes separated by spaces, ?? for any byte')
|
|
25
|
+
.refine(p => !p.startsWith('??'), 'must start with a fixed byte'),
|
|
26
|
+
offset: z.number().int().min(-4096).max(4096).default(0),
|
|
27
|
+
kind: z.enum(['match', 'rip32', 'abs32', 'abs64']),
|
|
28
|
+
instructionEnd: z.number().int().min(0).max(16).default(0),
|
|
29
|
+
})
|
|
30
|
+
.strict()
|
|
31
|
+
.refine(s => s.pattern.split(' ').length <= MAX_PATTERN_BYTES, 'pattern is too long');
|
|
32
|
+
/** A pattern as bytes and which of them must match. */
|
|
33
|
+
export function parsePattern(pattern) {
|
|
34
|
+
const tokens = pattern.split(' ');
|
|
35
|
+
return {
|
|
36
|
+
bytes: Uint8Array.from(tokens, t => (t === '??' ? 0 : parseInt(t, 16))),
|
|
37
|
+
fixed: tokens.map(t => t !== '??'),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Every offset in `haystack` where the pattern matches (at most `limit`). The first byte is
|
|
42
|
+
* fixed (the schema insists), so the native indexOf finds candidates.
|
|
43
|
+
*/
|
|
44
|
+
export function findPattern(haystack, pattern, limit = 2) {
|
|
45
|
+
const { bytes, fixed } = parsePattern(pattern);
|
|
46
|
+
const first = bytes[0] ?? 0;
|
|
47
|
+
const hits = [];
|
|
48
|
+
for (let i = haystack.indexOf(first); i >= 0 && i + bytes.length <= haystack.length; i = haystack.indexOf(first, i + 1)) {
|
|
49
|
+
let k = 1;
|
|
50
|
+
while (k < bytes.length && (!fixed[k] || haystack[i + k] === bytes[k]))
|
|
51
|
+
k++;
|
|
52
|
+
if (k === bytes.length) {
|
|
53
|
+
hits.push(i);
|
|
54
|
+
if (hits.length >= limit)
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return hits;
|
|
59
|
+
}
|
|
60
|
+
/** How many bytes past the field `resolveSignature` needs to read. */
|
|
61
|
+
export function fieldBytes(sig) {
|
|
62
|
+
return sig.kind === 'abs64' ? 8 : sig.kind === 'match' ? 0 : 4;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The address a match points to. `fieldAddress` is match + offset; `field` holds the bytes there
|
|
66
|
+
* (little-endian, `fieldBytes(sig)` of them).
|
|
67
|
+
*/
|
|
68
|
+
export function resolveSignature(sig, fieldAddress, field) {
|
|
69
|
+
const view = new DataView(field.buffer, field.byteOffset, field.byteLength);
|
|
70
|
+
switch (sig.kind) {
|
|
71
|
+
case 'match':
|
|
72
|
+
return fieldAddress;
|
|
73
|
+
case 'rip32':
|
|
74
|
+
return fieldAddress + 4n + BigInt(sig.instructionEnd) + BigInt(view.getInt32(0, true));
|
|
75
|
+
case 'abs32':
|
|
76
|
+
return BigInt(view.getUint32(0, true));
|
|
77
|
+
case 'abs64':
|
|
78
|
+
return view.getBigUint64(0, true);
|
|
79
|
+
}
|
|
80
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gameslate/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
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",
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: build-gameslate-plugin
|
|
3
|
+
description: Build a GameSlate plugin for a game, from choosing how to read its state through memory addresses, packs, asset extractors, tests and release. Use when adding a game, reading a new value from a game, or making a plugin's map assets.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Building a GameSlate plugin
|
|
7
|
+
|
|
8
|
+
A plugin teaches GameSlate one or more games. It runs in GameSlate's sandbox (QuickJS: no files, no
|
|
9
|
+
network, no Node APIs) and gets only what the core hands it: a game's memory (`GameMemory`) for live
|
|
10
|
+
state, and the player's own files (`InputFile`s) for assets. Work through the steps in order. Don't
|
|
11
|
+
skip verification: a wrong address that looks plausible is worse than no address.
|
|
12
|
+
|
|
13
|
+
## 1. Pick the state source
|
|
14
|
+
|
|
15
|
+
Decide before writing code, and write the decision in the README.
|
|
16
|
+
|
|
17
|
+
| The game is… | Read state from | Addresses come from |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| an open-source port (Ship of Harkinian, dsda-doom) | its process memory | `offsetof` / `sizeof` from its own headers, compiled for its ABI |
|
|
20
|
+
| a console game in an emulator (N64) | the emulated RAM (the core finds it) | a decompilation built **byte-identical** to the player's ROM |
|
|
21
|
+
| a closed PC game | its process memory | leads (LiveSplit autosplitters, RetroAchievements code notes), verified live, read by signature (`memory.scan()`) |
|
|
22
|
+
| protected by anti-cheat (VAC, EAC) | **nothing** | don't: use its mods or console, or skip it |
|
|
23
|
+
|
|
24
|
+
## 2. Scaffold
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
yarn dlx -p @gameslate/sdk gameslate-plugin init my-plugin --id my-plugin --name "My Plugin"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then `yarn install && yarn check`. `check` runs the typecheck, the tests, the build and
|
|
31
|
+
`validate`, and it's what CI runs. You get:
|
|
32
|
+
|
|
33
|
+
- `plugin/manifest.json`: the plugin's id, name, version, `sdkApi`, and `games[]`;
|
|
34
|
+
- `plugin/live.ts`: the plugin module, with its packs and memory sources. Put the decoder in
|
|
35
|
+
`plugin/decode.ts` and the offsets in `plugin/offsets.ts`, both imported by `live.ts`. The
|
|
36
|
+
bundler follows the imports.
|
|
37
|
+
- optionally `plugin/assets.ts`: asset extractors (add `assets` to the manifest);
|
|
38
|
+
- `test/live.test.ts`: builds the plugin, loads it in the core's own sandbox, reads a fake
|
|
39
|
+
memory, and runs the pack contract check (`checkCatalogue`). Keep it.
|
|
40
|
+
- CI, and `.claude/skills/`: this skill and `verify-gameslate-plugin`.
|
|
41
|
+
|
|
42
|
+
Each manifest game has:
|
|
43
|
+
|
|
44
|
+
- `id`, `title`, and optional `aliases` (short names a player may type);
|
|
45
|
+
- optional `inputs[]`, only for asset extractors: a `label`, the `folders` under `~` where the
|
|
46
|
+
player's files usually are, and `subfolders` whose files count too, such as a port's `mods/`.
|
|
47
|
+
Hidden folders are refused except under `~/.var/app`, `~/.local/share` and `~/.steam`.
|
|
48
|
+
|
|
49
|
+
The build fills in the code files' hashes. Never edit those by hand.
|
|
50
|
+
|
|
51
|
+
**Against an unreleased SDK** (you're changing the SDK too): `yarn pack` in the SDK, then depend
|
|
52
|
+
on the tarball (`"@gameslate/sdk": "file:../gameslate-sdk.tgz"`). With Yarn's PnP, `link:` skips
|
|
53
|
+
the SDK's own dependencies and type-checks its sources, so don't use it.
|
|
54
|
+
|
|
55
|
+
## 3. The pack: what the game means
|
|
56
|
+
|
|
57
|
+
Write the decoder against **fixtures first**: raw snapshots you captured, turned into `GameState`.
|
|
58
|
+
The catalogue (items, missions, counters, collectibles, reference) and its conditions are data. One
|
|
59
|
+
evaluator in the SDK decides "done", "collected" and "available". Rules:
|
|
60
|
+
|
|
61
|
+
- Every condition references ids that exist. `checkCatalogue(pack.catalogue)` checks it, and the
|
|
62
|
+
template's test already calls it.
|
|
63
|
+
- Every poll returns a whole `GameState`: `phase`, `provenance` (`{ source: 'ram', asOf, frame }`),
|
|
64
|
+
`locationId`, `position` (or `null`), `inventory`, `objectives`, `flags`, `enemies`. Add
|
|
65
|
+
`counters`, `difficulty` and `variant` as needed. The core validates every state against
|
|
66
|
+
`GameStateSchema` and drops one that fails.
|
|
67
|
+
- **Unknown is not false.** A value you can't read is "untracked", never "missing": leave the
|
|
68
|
+
counter out of `counters` rather than reporting 0.
|
|
69
|
+
- Give counters a `role` (`health`, `magic`, `armor`, `keys`) so shared UI finds them.
|
|
70
|
+
- Use `format: 'percent'` with a `max` for meters.
|
|
71
|
+
|
|
72
|
+
## 4. Memory addresses, verified
|
|
73
|
+
|
|
74
|
+
**Pin the build.** List the exact builds you verified in the source's `supportedBuildIds` (a
|
|
75
|
+
`Map` from build id to `{ region }` on the `GamePlugin` in `live.ts`, not in the manifest):
|
|
76
|
+
|
|
77
|
+
- ELF or PE build id for PC games;
|
|
78
|
+
- `CODE.version` from the ROM header for N64 (e.g. `CZLE.2`).
|
|
79
|
+
|
|
80
|
+
An unknown build means refuse to attach, never "try the nearest offsets".
|
|
81
|
+
|
|
82
|
+
**Open-source ports.** Compile a probe against the game's headers: print `sizeof` of a big struct
|
|
83
|
+
(it must equal what the shipped build uses) and the `offsetof` of each field. Put each value in
|
|
84
|
+
`offsets.ts`, with a comment naming the struct field and where the number came from. Header
|
|
85
|
+
comments like `/* 0x0E9C */` are not a source.
|
|
86
|
+
|
|
87
|
+
**Console decompilations:**
|
|
88
|
+
|
|
89
|
+
1. Build the decomp and check the output's **md5 equals the player's ROM**. Mind the region: some
|
|
90
|
+
Makefiles default to another region and still print "OK" against their own checksum.
|
|
91
|
+
2. Probe the struct offsets by compiling the decomp's headers for the console's ABI. For N64 that's
|
|
92
|
+
MIPS o32: `clang --target=mips-unknown-linux-gnu -mabi=32 -S`, then read the constants back.
|
|
93
|
+
3. Take global addresses from the build's symbol map.
|
|
94
|
+
4. If a global pointer outlives its object (a pointer that isn't cleared when the scene unloads),
|
|
95
|
+
trust it only with a check from the same build, such as "this state's update function is the
|
|
96
|
+
one the map says".
|
|
97
|
+
|
|
98
|
+
**Leads, from LiveSplit or RetroAchievements:**
|
|
99
|
+
|
|
100
|
+
- A lead is not a source. Verify every value live, across levels, menus and loads.
|
|
101
|
+
- Record where it came from, and **never copy their code**: addresses are facts, scripts aren't.
|
|
102
|
+
- **RetroAchievements N64 addresses are in the emulator's little-endian 32-bit words.** To get the
|
|
103
|
+
console's big-endian address: byte = address ^ 3, 16-bit = address ^ 2, 32-bit as is.
|
|
104
|
+
- RA notes are per game, not per revision. Struct offsets carry across revisions; absolute
|
|
105
|
+
addresses don't.
|
|
106
|
+
- A byte-pattern lead stays a pattern: read it with `memory.scan({ pattern, kind, offset })`, so it
|
|
107
|
+
survives patches. It must match exactly once.
|
|
108
|
+
|
|
109
|
+
**Reading:** read whole blocks (the save struct in one read) where the values sit together.
|
|
110
|
+
Sample with `seqlockSample(readFrameCounter, readPayload)` (from `@gameslate/sdk/plugin`) so a
|
|
111
|
+
frame isn't half old and half new: it rereads until the game's frame counter is the same before
|
|
112
|
+
and after. A game without a frame counter you can read has nothing to sample against: read once
|
|
113
|
+
per poll. Numbers are explicit about width, sign and byte order (`readUInt16BE` for N64,
|
|
114
|
+
`readInt32LE` for PC).
|
|
115
|
+
|
|
116
|
+
## 5. Assets (optional): maps, icons, meshes from the player's files
|
|
117
|
+
|
|
118
|
+
`assets.ts` exports an extractor per game:
|
|
119
|
+
|
|
120
|
+
- `listAssets(inputs, pick)` must be cheap: name what can be built.
|
|
121
|
+
- `buildAsset(id, inputs)` returns outputs in the SDK's shapes: floorplan, location, nav, mesh
|
|
122
|
+
(GS3D), and image.
|
|
123
|
+
|
|
124
|
+
Images have a `purpose` (`atlas`, `map`, `icon`, `actor`, which picks the folder) and an `encoding`:
|
|
125
|
+
|
|
126
|
+
- PNG;
|
|
127
|
+
- `{ format: 'webp', lossless: true }`;
|
|
128
|
+
- `{ format: 'webp', quality: 88 }`.
|
|
129
|
+
|
|
130
|
+
Every image of a game uses one format. In the sandbox:
|
|
131
|
+
|
|
132
|
+
- **No `TextDecoder`, `TextEncoder` or `Buffer`.** Decode UTF-8 by hand, and decode files in
|
|
133
|
+
chunks (`String.fromCharCode(...bytes)` has an argument limit).
|
|
134
|
+
- **One asset per unit of work** (per map, per actor): each gets its own time budget (10 minutes)
|
|
135
|
+
and its own up-to-date stamp, and jobs build assets across several threads.
|
|
136
|
+
- Inputs are named relative to the folder the player picked (`mods/pack.o2r`). The core also hands
|
|
137
|
+
in the game's registry manifest as `registry/manifest.json` when there is one, so renders line up
|
|
138
|
+
with the registry's maps.
|
|
139
|
+
- **Data licensing:** ship the extractor, never its output. Textures, models and renders are
|
|
140
|
+
built on the player's machine and never committed.
|
|
141
|
+
|
|
142
|
+
## 6. Tests
|
|
143
|
+
|
|
144
|
+
- **Decoders:** fixture snapshot in, expected `GameState` out, including menus, no save, and edge
|
|
145
|
+
values.
|
|
146
|
+
- **Conditions:** table tests, one row per branch.
|
|
147
|
+
- **The sandbox bundle:** the template's test runs the built `live.js` in the core's sandbox. Keep
|
|
148
|
+
it, and add an extraction test on a synthetic archive.
|
|
149
|
+
- **Ports of existing tools:** compare against the original's real output with **the same library
|
|
150
|
+
versions** that made it. A newer Pillow or numpy changes pixels on its own: Pillow 12 changed its
|
|
151
|
+
resample order, and numpy's matmul differs per CPU.
|
|
152
|
+
- **Every bug fix gets a test that fails without the fix.** Break the code, watch it fail, restore.
|
|
153
|
+
- **No game data in fixtures:** build synthetic ones (bytes you wrote) and record what the reference
|
|
154
|
+
tool produced from them.
|
|
155
|
+
|
|
156
|
+
## 7. Verify live
|
|
157
|
+
|
|
158
|
+
Use the `verify-gameslate-plugin` skill. It runs the game from a save state, reads what your plugin
|
|
159
|
+
reports, screenshots the game and GameSlate, and checks them against `verify.json`. Until a build
|
|
160
|
+
passes, say so in the README ("unverified: offsets from leads") and keep it out of a release.
|
|
161
|
+
|
|
162
|
+
## 8. Release
|
|
163
|
+
|
|
164
|
+
```sh
|
|
165
|
+
yarn check
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
That's the typecheck, the tests, the build, then `validate dist/<id>`, which checks the built
|
|
169
|
+
plugin exactly as the core will before running it. Then bump `version`, tag `vX.Y.Z`, and publish the release bundle. GameSlate installs presets from
|
|
170
|
+
`plugins.lock.json` (an entry with the release URL and its sha256).
|
|
171
|
+
|
|
172
|
+
## Pitfalls seen in real plugins
|
|
173
|
+
|
|
174
|
+
- **Texture packs:** a port's HD packs (`mods/*.o2r`) replace textures. Read them the way the game
|
|
175
|
+
does: packs first, in name order, each preferring its `alt/` asset, then the base archive.
|
|
176
|
+
Otherwise you overwrite HD output with vanilla.
|
|
177
|
+
- **Big inputs:** files over 256 MiB are identified by size and modification time, not hashed.
|
|
178
|
+
Don't rely on a byte change inside a huge file being noticed unless its size or mtime changes too.
|
|
179
|
+
- **Emulators:** the core finds an N64 game's RAM by the ROM's boot code. The ROM must be on the
|
|
180
|
+
emulator's command line, and the game must have booted.
|
|
181
|
+
- **Prove it on real data before writing to a player's files.** Back up what's there before the
|
|
182
|
+
first run on a real machine.
|