@gameslate/sdk 0.3.17 → 0.3.19
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 +23 -3
- package/bin/gameslate-plugin.mjs +6 -2
- package/build/build-plugin.d.mts +8 -2
- package/build/build-plugin.mjs +40 -36
- package/build/zip.d.mts +6 -0
- package/build/zip.mjs +94 -0
- package/lib/assets.d.ts +41 -1
- package/lib/assets.js +22 -0
- package/lib/host/asset-sandbox.d.ts +2 -1
- package/lib/host/asset-sandbox.js +7 -2
- package/lib/host/inspect.js +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/plugin/contract.d.ts +2 -45
- package/lib/plugin-manifest.d.ts +172 -21
- package/lib/plugin-manifest.js +88 -11
- package/lib/plugin-permissions.d.ts +20 -0
- package/lib/plugin-permissions.js +58 -0
- package/lib/plugins-api.d.ts +470 -0
- package/lib/plugins-api.js +56 -1
- package/package.json +1 -1
- package/template/README.md +8 -0
- package/template/claude/skills/build-gameslate-plugin/SKILL.md +144 -4
- package/template/github/workflows/release.yml +38 -0
- package/template/gitignore +2 -0
- package/template/plugin/tracked-documents.test.ts +68 -0
package/lib/plugin-manifest.d.ts
CHANGED
|
@@ -2,13 +2,55 @@ import { z } from 'zod';
|
|
|
2
2
|
/**
|
|
3
3
|
* The plugin contract this core speaks; a plugin built for another can't work.
|
|
4
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`)
|
|
6
|
-
* the
|
|
5
|
+
* 3: asset extractors are a sandbox script (`assets`, `exposeAssetsToHost`). Since sdk 0.3.19 no
|
|
6
|
+
* plugin code runs outside the sandbox: `extract` is gone, and data from the web comes in as
|
|
7
|
+
* declared `downloads`.
|
|
7
8
|
*/
|
|
8
9
|
export declare const SDK_API = 3;
|
|
10
|
+
/** A plugin or game id. `constructor` passes it: records keyed by id use `Object.hasOwn`. */
|
|
11
|
+
export declare const PluginIdSchema: z.ZodString;
|
|
9
12
|
/** Why a manifest may not offer `folder`, or null. */
|
|
10
13
|
export declare function homeFolderProblem(folder: string): string | null;
|
|
11
|
-
export declare const
|
|
14
|
+
export declare const Sha256Schema: z.ZodString;
|
|
15
|
+
/** Why `raw` can't be a download's URL, or null. */
|
|
16
|
+
export declare function downloadUrlProblem(raw: string): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* A file from the web an asset job needs (a port's source tarball at a pinned commit): the core fetches it
|
|
19
|
+
* when a job runs, checks its sha256, caches it and hands it to the asset code as the input
|
|
20
|
+
* `downloads/<id>`. Each one is a permission the player sees.
|
|
21
|
+
*/
|
|
22
|
+
export declare const PluginDownloadSchema: z.ZodObject<{
|
|
23
|
+
id: z.ZodString;
|
|
24
|
+
url: z.ZodEffects<z.ZodString, string, string>;
|
|
25
|
+
sha256: z.ZodString;
|
|
26
|
+
}, "strict", z.ZodTypeAny, {
|
|
27
|
+
id: string;
|
|
28
|
+
url: string;
|
|
29
|
+
sha256: string;
|
|
30
|
+
}, {
|
|
31
|
+
id: string;
|
|
32
|
+
url: string;
|
|
33
|
+
sha256: string;
|
|
34
|
+
}>;
|
|
35
|
+
export type PluginDownload = z.infer<typeof PluginDownloadSchema>;
|
|
36
|
+
/** The input name a download reaches the asset code under. */
|
|
37
|
+
export declare const downloadInputName: (id: string) => string;
|
|
38
|
+
/** major.minor.patch, the only form `compareVersions` orders. */
|
|
39
|
+
export declare const PluginVersionSchema: z.ZodString;
|
|
40
|
+
/** The release zip a published manifest names: its URL (absolute, or relative to the URL the
|
|
41
|
+
* player gave for the manifest, as `gameslate-plugin build --release` writes it) and its sha256. */
|
|
42
|
+
export declare const PluginBundleSchema: z.ZodObject<{
|
|
43
|
+
url: z.ZodString;
|
|
44
|
+
sha256: z.ZodString;
|
|
45
|
+
}, "strip", z.ZodTypeAny, {
|
|
46
|
+
url: string;
|
|
47
|
+
sha256: string;
|
|
48
|
+
}, {
|
|
49
|
+
url: string;
|
|
50
|
+
sha256: string;
|
|
51
|
+
}>;
|
|
52
|
+
export type PluginBundle = z.infer<typeof PluginBundleSchema>;
|
|
53
|
+
export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
12
54
|
id: z.ZodString;
|
|
13
55
|
name: z.ZodString;
|
|
14
56
|
version: z.ZodString;
|
|
@@ -37,18 +79,6 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
37
79
|
file: string;
|
|
38
80
|
sha256: string;
|
|
39
81
|
}>>;
|
|
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 */
|
|
42
|
-
extract: z.ZodOptional<z.ZodObject<{
|
|
43
|
-
file: z.ZodString;
|
|
44
|
-
sha256: z.ZodString;
|
|
45
|
-
}, "strip", z.ZodTypeAny, {
|
|
46
|
-
file: string;
|
|
47
|
-
sha256: string;
|
|
48
|
-
}, {
|
|
49
|
-
file: string;
|
|
50
|
-
sha256: string;
|
|
51
|
-
}>>;
|
|
52
82
|
games: z.ZodArray<z.ZodObject<{
|
|
53
83
|
id: z.ZodString;
|
|
54
84
|
title: z.ZodString;
|
|
@@ -70,7 +100,29 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
70
100
|
folders: string[];
|
|
71
101
|
subfolders?: string[] | undefined;
|
|
72
102
|
}>, "many">>;
|
|
73
|
-
|
|
103
|
+
/** files from the web its asset code reads (fetched only when a job runs) */
|
|
104
|
+
downloads: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{
|
|
105
|
+
id: z.ZodString;
|
|
106
|
+
url: z.ZodEffects<z.ZodString, string, string>;
|
|
107
|
+
sha256: z.ZodString;
|
|
108
|
+
}, "strict", z.ZodTypeAny, {
|
|
109
|
+
id: string;
|
|
110
|
+
url: string;
|
|
111
|
+
sha256: string;
|
|
112
|
+
}, {
|
|
113
|
+
id: string;
|
|
114
|
+
url: string;
|
|
115
|
+
sha256: string;
|
|
116
|
+
}>, "many">, {
|
|
117
|
+
id: string;
|
|
118
|
+
url: string;
|
|
119
|
+
sha256: string;
|
|
120
|
+
}[], {
|
|
121
|
+
id: string;
|
|
122
|
+
url: string;
|
|
123
|
+
sha256: string;
|
|
124
|
+
}[]>>;
|
|
125
|
+
}, "strict", z.ZodTypeAny, {
|
|
74
126
|
id: string;
|
|
75
127
|
title: string;
|
|
76
128
|
inputs: {
|
|
@@ -78,6 +130,11 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
78
130
|
folders: string[];
|
|
79
131
|
subfolders?: string[] | undefined;
|
|
80
132
|
}[];
|
|
133
|
+
downloads: {
|
|
134
|
+
id: string;
|
|
135
|
+
url: string;
|
|
136
|
+
sha256: string;
|
|
137
|
+
}[];
|
|
81
138
|
aliases?: string[] | undefined;
|
|
82
139
|
}, {
|
|
83
140
|
id: string;
|
|
@@ -88,8 +145,26 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
88
145
|
folders: string[];
|
|
89
146
|
subfolders?: string[] | undefined;
|
|
90
147
|
}[] | undefined;
|
|
148
|
+
downloads?: {
|
|
149
|
+
id: string;
|
|
150
|
+
url: string;
|
|
151
|
+
sha256: string;
|
|
152
|
+
}[] | undefined;
|
|
91
153
|
}>, "many">;
|
|
92
|
-
|
|
154
|
+
/** only in a published manifest (`manifest.json` beside the release zip, which a player adds
|
|
155
|
+
* by URL): the zip it describes. The manifest inside the zip has none, since it can't carry
|
|
156
|
+
* its own zip's hash. */
|
|
157
|
+
bundle: z.ZodOptional<z.ZodObject<{
|
|
158
|
+
url: z.ZodString;
|
|
159
|
+
sha256: z.ZodString;
|
|
160
|
+
}, "strip", z.ZodTypeAny, {
|
|
161
|
+
url: string;
|
|
162
|
+
sha256: string;
|
|
163
|
+
}, {
|
|
164
|
+
url: string;
|
|
165
|
+
sha256: string;
|
|
166
|
+
}>>;
|
|
167
|
+
}, "strict", z.ZodTypeAny, {
|
|
93
168
|
code: {
|
|
94
169
|
file: string;
|
|
95
170
|
sha256: string;
|
|
@@ -107,14 +182,19 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
107
182
|
folders: string[];
|
|
108
183
|
subfolders?: string[] | undefined;
|
|
109
184
|
}[];
|
|
185
|
+
downloads: {
|
|
186
|
+
id: string;
|
|
187
|
+
url: string;
|
|
188
|
+
sha256: string;
|
|
189
|
+
}[];
|
|
110
190
|
aliases?: string[] | undefined;
|
|
111
191
|
}[];
|
|
112
|
-
|
|
192
|
+
assets?: {
|
|
113
193
|
file: string;
|
|
114
194
|
sha256: string;
|
|
115
195
|
} | undefined;
|
|
116
|
-
|
|
117
|
-
|
|
196
|
+
bundle?: {
|
|
197
|
+
url: string;
|
|
118
198
|
sha256: string;
|
|
119
199
|
} | undefined;
|
|
120
200
|
}, {
|
|
@@ -135,15 +215,86 @@ export declare const PluginManifestSchema: z.ZodObject<{
|
|
|
135
215
|
folders: string[];
|
|
136
216
|
subfolders?: string[] | undefined;
|
|
137
217
|
}[] | undefined;
|
|
218
|
+
downloads?: {
|
|
219
|
+
id: string;
|
|
220
|
+
url: string;
|
|
221
|
+
sha256: string;
|
|
222
|
+
}[] | undefined;
|
|
138
223
|
}[];
|
|
139
|
-
|
|
224
|
+
assets?: {
|
|
140
225
|
file: string;
|
|
141
226
|
sha256: string;
|
|
142
227
|
} | undefined;
|
|
228
|
+
testedWith?: string[] | undefined;
|
|
229
|
+
bundle?: {
|
|
230
|
+
url: string;
|
|
231
|
+
sha256: string;
|
|
232
|
+
} | undefined;
|
|
233
|
+
}>, {
|
|
234
|
+
code: {
|
|
235
|
+
file: string;
|
|
236
|
+
sha256: string;
|
|
237
|
+
};
|
|
238
|
+
id: string;
|
|
239
|
+
name: string;
|
|
240
|
+
version: string;
|
|
241
|
+
sdkApi: number;
|
|
242
|
+
testedWith: string[];
|
|
243
|
+
games: {
|
|
244
|
+
id: string;
|
|
245
|
+
title: string;
|
|
246
|
+
inputs: {
|
|
247
|
+
label: string;
|
|
248
|
+
folders: string[];
|
|
249
|
+
subfolders?: string[] | undefined;
|
|
250
|
+
}[];
|
|
251
|
+
downloads: {
|
|
252
|
+
id: string;
|
|
253
|
+
url: string;
|
|
254
|
+
sha256: string;
|
|
255
|
+
}[];
|
|
256
|
+
aliases?: string[] | undefined;
|
|
257
|
+
}[];
|
|
258
|
+
assets?: {
|
|
259
|
+
file: string;
|
|
260
|
+
sha256: string;
|
|
261
|
+
} | undefined;
|
|
262
|
+
bundle?: {
|
|
263
|
+
url: string;
|
|
264
|
+
sha256: string;
|
|
265
|
+
} | undefined;
|
|
266
|
+
}, {
|
|
267
|
+
code: {
|
|
268
|
+
file: string;
|
|
269
|
+
sha256: string;
|
|
270
|
+
};
|
|
271
|
+
id: string;
|
|
272
|
+
name: string;
|
|
273
|
+
version: string;
|
|
274
|
+
sdkApi: number;
|
|
275
|
+
games: {
|
|
276
|
+
id: string;
|
|
277
|
+
title: string;
|
|
278
|
+
aliases?: string[] | undefined;
|
|
279
|
+
inputs?: {
|
|
280
|
+
label: string;
|
|
281
|
+
folders: string[];
|
|
282
|
+
subfolders?: string[] | undefined;
|
|
283
|
+
}[] | undefined;
|
|
284
|
+
downloads?: {
|
|
285
|
+
id: string;
|
|
286
|
+
url: string;
|
|
287
|
+
sha256: string;
|
|
288
|
+
}[] | undefined;
|
|
289
|
+
}[];
|
|
143
290
|
assets?: {
|
|
144
291
|
file: string;
|
|
145
292
|
sha256: string;
|
|
146
293
|
} | undefined;
|
|
147
294
|
testedWith?: string[] | undefined;
|
|
295
|
+
bundle?: {
|
|
296
|
+
url: string;
|
|
297
|
+
sha256: string;
|
|
298
|
+
} | undefined;
|
|
148
299
|
}>;
|
|
149
300
|
export type PluginManifest = z.infer<typeof PluginManifestSchema>;
|
package/lib/plugin-manifest.js
CHANGED
|
@@ -7,13 +7,16 @@ import { z } from 'zod';
|
|
|
7
7
|
/**
|
|
8
8
|
* The plugin contract this core speaks; a plugin built for another can't work.
|
|
9
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`)
|
|
11
|
-
* the
|
|
10
|
+
* 3: asset extractors are a sandbox script (`assets`, `exposeAssetsToHost`). Since sdk 0.3.19 no
|
|
11
|
+
* plugin code runs outside the sandbox: `extract` is gone, and data from the web comes in as
|
|
12
|
+
* declared `downloads`.
|
|
12
13
|
*/
|
|
13
14
|
export const SDK_API = 3;
|
|
14
|
-
|
|
15
|
+
/** A plugin or game id. `constructor` passes it: records keyed by id use `Object.hasOwn`. */
|
|
16
|
+
export const PluginIdSchema = z
|
|
15
17
|
.string()
|
|
16
18
|
.regex(/^[a-z0-9][a-z0-9-]*$/, 'plugin and game ids are lower-case words and dashes');
|
|
19
|
+
const Id = PluginIdSchema;
|
|
17
20
|
/* a file next to the manifest, never a path out of the plugin's folder */
|
|
18
21
|
const FileName = z.string().regex(/^[A-Za-z0-9_][A-Za-z0-9_.-]*$/, 'a plain file name');
|
|
19
22
|
/* Hidden folders where games live; any other (`~/.ssh`, `~/.config/gameslate` with the pairing
|
|
@@ -50,14 +53,71 @@ const InputLocation = z.object({
|
|
|
50
53
|
* (e.g. a port's `mods`) */
|
|
51
54
|
subfolders: z.array(FileName).max(10).optional(),
|
|
52
55
|
});
|
|
56
|
+
export const Sha256Schema = z.string().regex(/^[0-9a-f]{64}$/, 'a sha256 in lower-case hex');
|
|
57
|
+
const Sha256 = Sha256Schema;
|
|
58
|
+
/* An exact https URL, the same string a browser would show for it: no templates, no credentials,
|
|
59
|
+
* so nothing but the manifest decides what is fetched. Whether the host is public is checked
|
|
60
|
+
* when it is fetched (each redirect too). */
|
|
61
|
+
const DownloadUrl = z
|
|
62
|
+
.string()
|
|
63
|
+
.max(2000)
|
|
64
|
+
.superRefine((raw, ctx) => {
|
|
65
|
+
const problem = downloadUrlProblem(raw);
|
|
66
|
+
if (problem)
|
|
67
|
+
ctx.addIssue({ code: 'custom', message: problem });
|
|
68
|
+
});
|
|
69
|
+
/** Why `raw` can't be a download's URL, or null. */
|
|
70
|
+
export function downloadUrlProblem(raw) {
|
|
71
|
+
let url;
|
|
72
|
+
try {
|
|
73
|
+
url = new URL(raw);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return 'not a URL';
|
|
77
|
+
}
|
|
78
|
+
if (url.protocol !== 'https:')
|
|
79
|
+
return 'a download is fetched over https only';
|
|
80
|
+
if (url.username || url.password)
|
|
81
|
+
return 'a download URL carries no credentials';
|
|
82
|
+
if (/[{}]|%7b|%7d/i.test(raw))
|
|
83
|
+
return 'a download URL is exact: no templates';
|
|
84
|
+
if (url.href !== raw)
|
|
85
|
+
return `a download URL is written exactly as ${url.href}`;
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A file from the web an asset job needs (a port's source tarball at a pinned commit): the core fetches it
|
|
90
|
+
* when a job runs, checks its sha256, caches it and hands it to the asset code as the input
|
|
91
|
+
* `downloads/<id>`. Each one is a permission the player sees.
|
|
92
|
+
*/
|
|
93
|
+
export const PluginDownloadSchema = z
|
|
94
|
+
.object({
|
|
95
|
+
id: PluginIdSchema,
|
|
96
|
+
url: DownloadUrl,
|
|
97
|
+
sha256: Sha256,
|
|
98
|
+
})
|
|
99
|
+
.strict();
|
|
100
|
+
/** The input name a download reaches the asset code under. */
|
|
101
|
+
export const downloadInputName = (id) => `downloads/${id}`;
|
|
53
102
|
const CodeFile = z.object({
|
|
54
103
|
file: FileName,
|
|
55
|
-
sha256:
|
|
104
|
+
sha256: Sha256,
|
|
105
|
+
});
|
|
106
|
+
/** major.minor.patch, the only form `compareVersions` orders. */
|
|
107
|
+
export const PluginVersionSchema = z
|
|
108
|
+
.string()
|
|
109
|
+
.regex(/^\d+\.\d+\.\d+$/, 'version is major.minor.patch');
|
|
110
|
+
/** The release zip a published manifest names: its URL (absolute, or relative to the URL the
|
|
111
|
+
* player gave for the manifest, as `gameslate-plugin build --release` writes it) and its sha256. */
|
|
112
|
+
export const PluginBundleSchema = z.object({
|
|
113
|
+
url: z.string().min(1).max(2000),
|
|
114
|
+
sha256: Sha256,
|
|
56
115
|
});
|
|
57
|
-
export const PluginManifestSchema = z
|
|
116
|
+
export const PluginManifestSchema = z
|
|
117
|
+
.object({
|
|
58
118
|
id: Id,
|
|
59
119
|
name: z.string().min(1),
|
|
60
|
-
version:
|
|
120
|
+
version: PluginVersionSchema,
|
|
61
121
|
sdkApi: z.number().int().positive(),
|
|
62
122
|
/** core versions it was tested with; outside them it still loads, with a warning */
|
|
63
123
|
testedWith: z.array(z.string()).default([]),
|
|
@@ -65,17 +125,34 @@ export const PluginManifestSchema = z.object({
|
|
|
65
125
|
code: CodeFile,
|
|
66
126
|
/** asset extractors, run in the core's sandbox by extraction jobs */
|
|
67
127
|
assets: CodeFile.optional(),
|
|
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 */
|
|
70
|
-
extract: CodeFile.optional(),
|
|
71
128
|
games: z
|
|
72
|
-
.array(z
|
|
129
|
+
.array(z
|
|
130
|
+
.object({
|
|
73
131
|
id: Id,
|
|
74
132
|
title: z.string().min(1),
|
|
75
133
|
/** short names a player may type for it (`gameslate extract-assets oot`) */
|
|
76
134
|
aliases: z.array(Id).max(5).optional(),
|
|
77
135
|
/** where the player's own files for asset extraction usually are */
|
|
78
136
|
inputs: z.array(InputLocation).default([]),
|
|
79
|
-
|
|
137
|
+
/** files from the web its asset code reads (fetched only when a job runs) */
|
|
138
|
+
downloads: z
|
|
139
|
+
.array(PluginDownloadSchema)
|
|
140
|
+
.max(20)
|
|
141
|
+
.refine(ds => new Set(ds.map(d => d.id)).size === ds.length, "a game's download ids are unique")
|
|
142
|
+
.default([]),
|
|
143
|
+
})
|
|
144
|
+
/* a misspelt key (`download`) is refused, not dropped with what it declared */
|
|
145
|
+
.strict())
|
|
80
146
|
.min(1),
|
|
147
|
+
/** only in a published manifest (`manifest.json` beside the release zip, which a player adds
|
|
148
|
+
* by URL): the zip it describes. The manifest inside the zip has none, since it can't carry
|
|
149
|
+
* its own zip's hash. */
|
|
150
|
+
bundle: PluginBundleSchema.optional(),
|
|
151
|
+
})
|
|
152
|
+
/* strict: a key this core doesn't know (an older contract's `extract`, code that ran outside
|
|
153
|
+
* the sandbox) is refused, not silently dropped */
|
|
154
|
+
.strict()
|
|
155
|
+
.refine(m => m.assets || m.games.every(g => g.downloads.length === 0), {
|
|
156
|
+
message: 'downloads are read only by asset code: a plugin with downloads needs assets',
|
|
157
|
+
path: ['games'],
|
|
81
158
|
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type PluginManifest } from './plugin-manifest.js';
|
|
3
|
+
export declare const PluginPermissionSchema: z.ZodObject<{
|
|
4
|
+
/** stable across versions: an update is compared key by key */
|
|
5
|
+
key: z.ZodString;
|
|
6
|
+
label: z.ZodString;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
label: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}, {
|
|
11
|
+
label: string;
|
|
12
|
+
key: string;
|
|
13
|
+
}>;
|
|
14
|
+
export type PluginPermission = z.infer<typeof PluginPermissionSchema>;
|
|
15
|
+
export declare function pluginPermissions(manifest: PluginManifest): PluginPermission[];
|
|
16
|
+
/** The permissions `next` asks for that `current` didn't have. */
|
|
17
|
+
export declare function addedPermissions(current: readonly PluginPermission[], next: readonly PluginPermission[]): PluginPermission[];
|
|
18
|
+
/** -1, 0 or 1 as version `a` (major.minor.patch) is older than, the same as or newer than `b`.
|
|
19
|
+
* Throws on any other form: a guess would install the wrong version or none. */
|
|
20
|
+
export declare function compareVersions(a: string, b: string): number;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { PluginVersionSchema } from './plugin-manifest.js';
|
|
3
|
+
/*
|
|
4
|
+
* What a plugin may do, in the player's words, derived from its manifest (docs/plugin-architecture.md,
|
|
5
|
+
* "Capabilities"): read the memory of each game it names, read the folders it offers for building
|
|
6
|
+
* maps, fetch each file it declares from the web. The cog shows these before an install, and an
|
|
7
|
+
* update that adds one waits for the player's consent. No plugin code runs outside the sandbox,
|
|
8
|
+
* so no permission names that.
|
|
9
|
+
*/
|
|
10
|
+
export const PluginPermissionSchema = z.object({
|
|
11
|
+
/** stable across versions: an update is compared key by key */
|
|
12
|
+
key: z.string(),
|
|
13
|
+
label: z.string(),
|
|
14
|
+
});
|
|
15
|
+
export function pluginPermissions(manifest) {
|
|
16
|
+
const memory = manifest.games.map(g => ({
|
|
17
|
+
key: `memory:${g.id}`,
|
|
18
|
+
label: `Read ${g.title}'s memory while it runs`,
|
|
19
|
+
}));
|
|
20
|
+
/* the offered folders are read only by asset code */
|
|
21
|
+
const folders = manifest.assets
|
|
22
|
+
? [...new Set(manifest.games.flatMap(g => g.inputs.flatMap(i => i.folders)))].map(f => ({
|
|
23
|
+
key: `files:${f}`,
|
|
24
|
+
label: `Read your game files in ${f} to build maps`,
|
|
25
|
+
}))
|
|
26
|
+
: [];
|
|
27
|
+
/* keyed by URL and hash: pointing a download at other bytes asks again */
|
|
28
|
+
const downloads = new Map(manifest.games.flatMap(g => g.downloads.map(d => [
|
|
29
|
+
`download:${d.url}#${d.sha256}`,
|
|
30
|
+
`Downloads ${downloadPlace(d.url)} to build maps`,
|
|
31
|
+
])));
|
|
32
|
+
return [...memory, ...folders, ...[...downloads].map(([key, label]) => ({ key, label }))];
|
|
33
|
+
}
|
|
34
|
+
/* host and path, as the player reads a link ("example.com/a/b.tar.gz") */
|
|
35
|
+
function downloadPlace(url) {
|
|
36
|
+
const u = new URL(url);
|
|
37
|
+
return `${u.host}${u.pathname}${u.search}`;
|
|
38
|
+
}
|
|
39
|
+
/** The permissions `next` asks for that `current` didn't have. */
|
|
40
|
+
export function addedPermissions(current, next) {
|
|
41
|
+
const had = new Set(current.map(p => p.key));
|
|
42
|
+
return next.filter(p => !had.has(p.key));
|
|
43
|
+
}
|
|
44
|
+
/** -1, 0 or 1 as version `a` (major.minor.patch) is older than, the same as or newer than `b`.
|
|
45
|
+
* Throws on any other form: a guess would install the wrong version or none. */
|
|
46
|
+
export function compareVersions(a, b) {
|
|
47
|
+
const pa = versionParts(a);
|
|
48
|
+
const pb = versionParts(b);
|
|
49
|
+
for (let i = 0; i < 3; i++) {
|
|
50
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
51
|
+
if (d !== 0)
|
|
52
|
+
return Math.sign(d);
|
|
53
|
+
}
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
function versionParts(version) {
|
|
57
|
+
return PluginVersionSchema.parse(version).split('.').map(Number);
|
|
58
|
+
}
|