@kubohiroya/sb3-toolchain 0.4.0 → 0.6.0
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.ja.md +9 -3
- package/README.md +10 -3
- package/docs/extension-bundles.md +18 -1
- package/docs/ja/extension-bundles.md +17 -1
- package/docs/ja/project-asset-additions.md +200 -0
- package/docs/ja/source-format-v1.md +25 -10
- package/docs/ja/workflows.md +34 -18
- package/docs/project-asset-additions.md +165 -0
- package/docs/source-format-v1.md +26 -9
- package/docs/workflows.md +31 -13
- package/package.json +4 -2
- package/src/build.js +25 -2
- package/src/cli.js +115 -10
- package/src/constants.js +1 -1
- package/src/extension-bundle.js +11 -0
- package/src/extension-dependencies.js +32 -19
- package/src/extension-sync.js +109 -20
- package/src/index.js +5 -0
- package/src/npm-extension-source.js +111 -0
- package/src/project-asset-additions.js +560 -0
- package/src/source.js +49 -8
- package/src/turbowarp-clean-up.js +295 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
|
+
import {lstat, readFile, realpath} from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
import {parseBuffer} from 'music-metadata';
|
|
8
|
+
import {isAlias, isPair, parseAllDocuments, visit} from 'yaml';
|
|
9
|
+
|
|
10
|
+
export const projectAssetAdditionsFormatVersion = 1;
|
|
11
|
+
|
|
12
|
+
const maximumManifestBytes = 1024 * 1024;
|
|
13
|
+
const forbiddenMappingKeys = new Set(['__proto__', 'constructor', 'prototype']);
|
|
14
|
+
const lockedAssetKeys = new Set(['name', 'file', 'size', 'sha256', 'dataFormat', 'license']);
|
|
15
|
+
const imageAssetKeys = new Set([
|
|
16
|
+
...lockedAssetKeys,
|
|
17
|
+
'kind',
|
|
18
|
+
'target',
|
|
19
|
+
'bitmapResolution',
|
|
20
|
+
'rotationCenterX',
|
|
21
|
+
'rotationCenterY',
|
|
22
|
+
]);
|
|
23
|
+
const soundAssetKeys = new Set([...lockedAssetKeys, 'kind', 'target', 'rate', 'sampleCount']);
|
|
24
|
+
const imageFormats = new Set(['svg', 'png', 'jpg', 'jpeg', 'gif']);
|
|
25
|
+
const soundFormats = new Map([
|
|
26
|
+
['wav', 'audio/wav'],
|
|
27
|
+
['mp3', 'audio/mpeg'],
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
function assert(condition, message) {
|
|
31
|
+
if (!condition) throw new Error(message);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function exactKeys(value, allowed, description) {
|
|
39
|
+
const unknown = Object.keys(value).filter((key) => !allowed.has(key));
|
|
40
|
+
assert(unknown.length === 0, `${description} has unknown properties: ${unknown.join(', ')}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function nonEmptyString(value, description) {
|
|
44
|
+
assert(
|
|
45
|
+
typeof value === 'string' &&
|
|
46
|
+
value.length > 0 &&
|
|
47
|
+
value === value.normalize('NFC') &&
|
|
48
|
+
!/[\u0000-\u001f\u007f]/u.test(value),
|
|
49
|
+
`${description} must be a non-empty NFC string without control characters.`,
|
|
50
|
+
);
|
|
51
|
+
assert(!forbiddenMappingKeys.has(value), `${description} is not supported: ${value}`);
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function finiteNumber(value, description) {
|
|
56
|
+
assert(typeof value === 'number' && Number.isFinite(value), `${description} must be finite.`);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function safeInteger(value, description, minimum = 0) {
|
|
61
|
+
assert(
|
|
62
|
+
Number.isSafeInteger(value) && value >= minimum,
|
|
63
|
+
`${description} must be a safe integer greater than or equal to ${minimum}.`,
|
|
64
|
+
);
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function sha256(contents) {
|
|
69
|
+
return createHash('sha256').update(contents).digest('hex');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function md5(contents) {
|
|
73
|
+
return createHash('md5').update(contents).digest('hex');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseRestrictedYaml(source, manifestPath) {
|
|
77
|
+
const documents = parseAllDocuments(source, {
|
|
78
|
+
prettyErrors: false,
|
|
79
|
+
strict: true,
|
|
80
|
+
uniqueKeys: true,
|
|
81
|
+
version: '1.2',
|
|
82
|
+
});
|
|
83
|
+
const parserIssues = documents.flatMap((document) => [...document.errors, ...document.warnings]);
|
|
84
|
+
assert(
|
|
85
|
+
parserIssues.length === 0,
|
|
86
|
+
`Project asset additions manifest is not valid YAML: ${manifestPath} ` +
|
|
87
|
+
`(${parserIssues[0]?.message})`,
|
|
88
|
+
);
|
|
89
|
+
assert(
|
|
90
|
+
documents.length === 1,
|
|
91
|
+
`Project asset additions manifest must contain exactly one YAML document: ${manifestPath}`,
|
|
92
|
+
);
|
|
93
|
+
let restrictedFeature;
|
|
94
|
+
visit(documents[0], (_key, node) => {
|
|
95
|
+
if (restrictedFeature) return visit.BREAK;
|
|
96
|
+
const yamlNode = /** @type {any} */ (node);
|
|
97
|
+
if (isAlias(node) || yamlNode?.anchor) restrictedFeature = 'aliases and anchors';
|
|
98
|
+
else if (isPair(node) && yamlNode.key?.value === '<<') restrictedFeature = 'merge keys';
|
|
99
|
+
else if (yamlNode?.tag) restrictedFeature = 'custom tags';
|
|
100
|
+
else if (isPair(node) && forbiddenMappingKeys.has(String(yamlNode.key?.value))) {
|
|
101
|
+
restrictedFeature = `mapping key ${String(yamlNode.key.value)}`;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
});
|
|
105
|
+
assert(
|
|
106
|
+
!restrictedFeature,
|
|
107
|
+
`Project asset additions YAML does not support ${restrictedFeature}: ${manifestPath}`,
|
|
108
|
+
);
|
|
109
|
+
return documents[0].toJS({maxAliasCount: 0});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function readManifestFile(filePath) {
|
|
113
|
+
let stats;
|
|
114
|
+
try {
|
|
115
|
+
stats = await lstat(filePath);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
throw new Error(`Project asset additions manifest is not readable: ${filePath}`, {
|
|
118
|
+
cause: error,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
assert(
|
|
122
|
+
stats.isFile() && !stats.isSymbolicLink(),
|
|
123
|
+
`Project asset additions manifest must be a regular file, not a symbolic link: ${filePath}`,
|
|
124
|
+
);
|
|
125
|
+
assert(
|
|
126
|
+
stats.size <= maximumManifestBytes,
|
|
127
|
+
`Project asset additions manifest exceeds ${maximumManifestBytes} bytes: ${filePath}`,
|
|
128
|
+
);
|
|
129
|
+
const source = await readFile(filePath, 'utf8');
|
|
130
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
131
|
+
assert(
|
|
132
|
+
extension === '.json' || extension === '.yml' || extension === '.yaml',
|
|
133
|
+
`Project asset additions manifest must use .json, .yml, or .yaml: ${filePath}`,
|
|
134
|
+
);
|
|
135
|
+
if (extension === '.json') {
|
|
136
|
+
let value;
|
|
137
|
+
try {
|
|
138
|
+
value = JSON.parse(source);
|
|
139
|
+
} catch (error) {
|
|
140
|
+
throw new Error(`Project asset additions manifest is not valid JSON: ${filePath}`, {
|
|
141
|
+
cause: error,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
parseRestrictedYaml(source, filePath);
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
return parseRestrictedYaml(source, filePath);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isContained(root, candidate) {
|
|
151
|
+
const relative = path.relative(root, candidate);
|
|
152
|
+
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function validateAllowedRoots(manifestDirectory, allowedAssetRoots) {
|
|
156
|
+
assert(Array.isArray(allowedAssetRoots), 'allowedAssetRoots must be an array.');
|
|
157
|
+
const roots = [manifestDirectory, ...allowedAssetRoots].map((root) => {
|
|
158
|
+
assert(typeof root === 'string', 'Every allowed project asset root must be a string.');
|
|
159
|
+
return path.resolve(root);
|
|
160
|
+
});
|
|
161
|
+
const uniqueRoots = [...new Set(roots)];
|
|
162
|
+
return Promise.all(
|
|
163
|
+
uniqueRoots.map(async (root) => {
|
|
164
|
+
const stats = await lstat(root);
|
|
165
|
+
assert(
|
|
166
|
+
stats.isDirectory() && !stats.isSymbolicLink(),
|
|
167
|
+
`Allowed project asset root must be a directory, not a symbolic link: ${root}`,
|
|
168
|
+
);
|
|
169
|
+
return {lexical: root, real: await realpath(root)};
|
|
170
|
+
}),
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function assertNoSymlinkPath(root, candidate, description) {
|
|
175
|
+
const relative = path.relative(root, candidate);
|
|
176
|
+
assert(isContained(root, candidate), `${description} escapes its allowed root: ${candidate}`);
|
|
177
|
+
let current = root;
|
|
178
|
+
for (const segment of relative.split(path.sep).filter(Boolean)) {
|
|
179
|
+
current = path.join(current, segment);
|
|
180
|
+
const stats = await lstat(current);
|
|
181
|
+
assert(!stats.isSymbolicLink(), `${description} must not traverse a symbolic link: ${current}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function readAssetFile(manifestDirectory, allowedRoots, specification, description) {
|
|
186
|
+
const file = nonEmptyString(specification.file, `${description}.file`);
|
|
187
|
+
assert(!path.isAbsolute(file) && !file.includes('\\'), `${description}.file must be relative.`);
|
|
188
|
+
const resolved = path.resolve(manifestDirectory, file);
|
|
189
|
+
const lexicalRoot = allowedRoots.find(({lexical}) => isContained(lexical, resolved));
|
|
190
|
+
assert(lexicalRoot, `${description}.file is outside the allowed project asset roots: ${file}`);
|
|
191
|
+
await assertNoSymlinkPath(lexicalRoot.lexical, resolved, `${description}.file`);
|
|
192
|
+
const stats = await lstat(resolved);
|
|
193
|
+
assert(stats.isFile(), `${description}.file must be a regular file: ${resolved}`);
|
|
194
|
+
const resolvedRealPath = await realpath(resolved);
|
|
195
|
+
assert(
|
|
196
|
+
allowedRoots.some(({real}) => isContained(real, resolvedRealPath)),
|
|
197
|
+
`${description}.file resolves outside the allowed project asset roots: ${file}`,
|
|
198
|
+
);
|
|
199
|
+
const contents = await readFile(resolved);
|
|
200
|
+
if (specification.size !== undefined) {
|
|
201
|
+
const expectedSize = safeInteger(specification.size, `${description}.size`);
|
|
202
|
+
assert(
|
|
203
|
+
contents.length === expectedSize,
|
|
204
|
+
`${description}.size differs: expected ${expectedSize}, got ${contents.length}`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (specification.sha256 !== undefined) {
|
|
208
|
+
assert(
|
|
209
|
+
typeof specification.sha256 === 'string' && /^[a-f0-9]{64}$/u.test(specification.sha256),
|
|
210
|
+
`${description}.sha256 must be a lowercase SHA-256 hex digest.`,
|
|
211
|
+
);
|
|
212
|
+
const actualSha256 = sha256(contents);
|
|
213
|
+
assert(
|
|
214
|
+
actualSha256 === specification.sha256,
|
|
215
|
+
`${description}.sha256 differs: expected ${specification.sha256}, got ${actualSha256}`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const inferredDataFormat = path.extname(file).slice(1).toLowerCase();
|
|
219
|
+
assert(
|
|
220
|
+
/^[a-z0-9]+$/u.test(inferredDataFormat),
|
|
221
|
+
`${description}.file extension is invalid: ${file}`,
|
|
222
|
+
);
|
|
223
|
+
if (specification.dataFormat !== undefined) {
|
|
224
|
+
const expectedDataFormat = nonEmptyString(
|
|
225
|
+
specification.dataFormat,
|
|
226
|
+
`${description}.dataFormat`,
|
|
227
|
+
);
|
|
228
|
+
assert(
|
|
229
|
+
expectedDataFormat === inferredDataFormat,
|
|
230
|
+
`${description}.dataFormat differs: expected ${expectedDataFormat}, got ${inferredDataFormat}`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (specification.license !== undefined) {
|
|
234
|
+
nonEmptyString(specification.license, `${description}.license`);
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
contents,
|
|
238
|
+
dataFormat: inferredDataFormat,
|
|
239
|
+
file,
|
|
240
|
+
name: specification.name,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function inferSoundMetadata(contents, dataFormat, description) {
|
|
245
|
+
const mimeType = soundFormats.get(dataFormat);
|
|
246
|
+
assert(mimeType, `${description}.file uses unsupported sound format: ${dataFormat}`);
|
|
247
|
+
let metadata;
|
|
248
|
+
try {
|
|
249
|
+
metadata = await parseBuffer(
|
|
250
|
+
contents,
|
|
251
|
+
{mimeType, size: contents.length},
|
|
252
|
+
{duration: true, skipCovers: true},
|
|
253
|
+
);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
throw new Error(`${description}.file is not valid ${dataFormat} audio.`, {cause: error});
|
|
256
|
+
}
|
|
257
|
+
const rate = metadata.format.sampleRate;
|
|
258
|
+
const sampleCount =
|
|
259
|
+
metadata.format.numberOfSamples ??
|
|
260
|
+
(typeof metadata.format.duration === 'number' && typeof rate === 'number'
|
|
261
|
+
? Math.round(metadata.format.duration * rate)
|
|
262
|
+
: undefined);
|
|
263
|
+
safeInteger(rate, `${description} inferred sample rate`, 1);
|
|
264
|
+
safeInteger(sampleCount, `${description} inferred sample count`);
|
|
265
|
+
return {rate, sampleCount};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function validateSprite(specification, name) {
|
|
269
|
+
const description = `Project asset sprite ${JSON.stringify(name)}`;
|
|
270
|
+
assert(isRecord(specification), `${description} must be an object.`);
|
|
271
|
+
exactKeys(
|
|
272
|
+
specification,
|
|
273
|
+
new Set([
|
|
274
|
+
'layerOrder',
|
|
275
|
+
'visible',
|
|
276
|
+
'x',
|
|
277
|
+
'y',
|
|
278
|
+
'size',
|
|
279
|
+
'direction',
|
|
280
|
+
'draggable',
|
|
281
|
+
'rotationStyle',
|
|
282
|
+
'volume',
|
|
283
|
+
]),
|
|
284
|
+
description,
|
|
285
|
+
);
|
|
286
|
+
safeInteger(specification.layerOrder, `${description}.layerOrder`);
|
|
287
|
+
assert(typeof specification.visible === 'boolean', `${description}.visible must be boolean.`);
|
|
288
|
+
finiteNumber(specification.x, `${description}.x`);
|
|
289
|
+
finiteNumber(specification.y, `${description}.y`);
|
|
290
|
+
const size = finiteNumber(specification.size, `${description}.size`);
|
|
291
|
+
assert(size > 0, `${description}.size must be greater than zero.`);
|
|
292
|
+
finiteNumber(specification.direction, `${description}.direction`);
|
|
293
|
+
assert(typeof specification.draggable === 'boolean', `${description}.draggable must be boolean.`);
|
|
294
|
+
assert(
|
|
295
|
+
['all around', 'left-right', "don't rotate"].includes(specification.rotationStyle),
|
|
296
|
+
`${description}.rotationStyle is invalid: ${specification.rotationStyle}`,
|
|
297
|
+
);
|
|
298
|
+
const volume = finiteNumber(specification.volume, `${description}.volume`);
|
|
299
|
+
assert(volume >= 0 && volume <= 100, `${description}.volume must be between 0 and 100.`);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function validateAsset(specification, assetId) {
|
|
303
|
+
const description = `Project asset ${JSON.stringify(assetId)}`;
|
|
304
|
+
assert(isRecord(specification), `${description} must be an object.`);
|
|
305
|
+
assert(
|
|
306
|
+
specification.kind === 'backdrop' ||
|
|
307
|
+
specification.kind === 'costume' ||
|
|
308
|
+
specification.kind === 'sound',
|
|
309
|
+
`${description}.kind must be backdrop, costume, or sound.`,
|
|
310
|
+
);
|
|
311
|
+
exactKeys(
|
|
312
|
+
specification,
|
|
313
|
+
specification.kind === 'sound' ? soundAssetKeys : imageAssetKeys,
|
|
314
|
+
description,
|
|
315
|
+
);
|
|
316
|
+
if (specification.kind === 'backdrop') {
|
|
317
|
+
assert(
|
|
318
|
+
specification.target === undefined,
|
|
319
|
+
`${description}.target is not valid for a backdrop.`,
|
|
320
|
+
);
|
|
321
|
+
} else if (specification.kind === 'costume') {
|
|
322
|
+
nonEmptyString(specification.target, `${description}.target`);
|
|
323
|
+
} else if (specification.target !== undefined) {
|
|
324
|
+
nonEmptyString(specification.target, `${description}.target`);
|
|
325
|
+
}
|
|
326
|
+
if (specification.name !== undefined) nonEmptyString(specification.name, `${description}.name`);
|
|
327
|
+
return description;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function addAssetContents(assetContents, archiveEntries, contents, filename, description) {
|
|
331
|
+
const existing = assetContents.get(filename);
|
|
332
|
+
if (existing) {
|
|
333
|
+
assert(
|
|
334
|
+
Buffer.from(existing).equals(contents),
|
|
335
|
+
`${description} collides with different contents: ${filename}`,
|
|
336
|
+
);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
assetContents.set(filename, contents);
|
|
340
|
+
archiveEntries.push(filename);
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Validate a JSON or YAML project-assets manifest and compose it into one in-memory build.
|
|
346
|
+
* The expanded source and all manifest inputs remain unchanged.
|
|
347
|
+
*/
|
|
348
|
+
export async function applyProjectAssetAdditions({
|
|
349
|
+
allowedAssetRoots = [],
|
|
350
|
+
assetContents,
|
|
351
|
+
archiveEntries,
|
|
352
|
+
manifestPath,
|
|
353
|
+
project,
|
|
354
|
+
}) {
|
|
355
|
+
assert(typeof manifestPath === 'string', 'projectAssetsPath must be a string.');
|
|
356
|
+
assert(isRecord(project), 'Project asset additions require a project object.');
|
|
357
|
+
assert(assetContents instanceof Map, 'Project asset additions require an asset contents Map.');
|
|
358
|
+
assert(Array.isArray(archiveEntries), 'Project asset additions require archive entries.');
|
|
359
|
+
const resolvedManifestPath = path.resolve(manifestPath);
|
|
360
|
+
const manifestDirectory = path.dirname(resolvedManifestPath);
|
|
361
|
+
const [manifest, allowedRoots] = await Promise.all([
|
|
362
|
+
readManifestFile(resolvedManifestPath),
|
|
363
|
+
validateAllowedRoots(manifestDirectory, allowedAssetRoots),
|
|
364
|
+
]);
|
|
365
|
+
assert(isRecord(manifest), 'Project asset additions manifest must be an object.');
|
|
366
|
+
exactKeys(
|
|
367
|
+
manifest,
|
|
368
|
+
new Set(['formatVersion', 'sprites', 'assets']),
|
|
369
|
+
'Project asset additions manifest',
|
|
370
|
+
);
|
|
371
|
+
assert(
|
|
372
|
+
manifest.formatVersion === projectAssetAdditionsFormatVersion,
|
|
373
|
+
`Unsupported project asset additions formatVersion: ${manifest.formatVersion}`,
|
|
374
|
+
);
|
|
375
|
+
assert(
|
|
376
|
+
manifest.sprites === undefined || isRecord(manifest.sprites),
|
|
377
|
+
'Project asset additions sprites must be an object when present.',
|
|
378
|
+
);
|
|
379
|
+
assert(
|
|
380
|
+
isRecord(manifest.assets) && Object.keys(manifest.assets).length > 0,
|
|
381
|
+
'Project asset additions assets must be a non-empty object.',
|
|
382
|
+
);
|
|
383
|
+
assert(Array.isArray(project.targets), 'Project asset additions require project.targets.');
|
|
384
|
+
|
|
385
|
+
const composedProject = structuredClone(project);
|
|
386
|
+
const composedAssetContents = new Map(assetContents);
|
|
387
|
+
const composedArchiveEntries = [...archiveEntries];
|
|
388
|
+
const targetByName = new Map();
|
|
389
|
+
for (const target of composedProject.targets) {
|
|
390
|
+
const targetName = nonEmptyString(target.name, 'Existing project target name');
|
|
391
|
+
assert(!targetByName.has(targetName), `Project has duplicate target name: ${targetName}`);
|
|
392
|
+
assert(
|
|
393
|
+
Array.isArray(target.costumes),
|
|
394
|
+
`Project target ${targetName}.costumes must be an array.`,
|
|
395
|
+
);
|
|
396
|
+
assert(Array.isArray(target.sounds), `Project target ${targetName}.sounds must be an array.`);
|
|
397
|
+
targetByName.set(targetName, target);
|
|
398
|
+
}
|
|
399
|
+
const stages = composedProject.targets.filter(({isStage}) => isStage);
|
|
400
|
+
assert(stages.length === 1, 'Project asset additions require exactly one Stage target.');
|
|
401
|
+
const stage = stages[0];
|
|
402
|
+
const newSpriteNames = [];
|
|
403
|
+
|
|
404
|
+
for (const [rawName, specification] of Object.entries(manifest.sprites ?? {})) {
|
|
405
|
+
const name = nonEmptyString(rawName, 'Project asset sprite name');
|
|
406
|
+
validateSprite(specification, name);
|
|
407
|
+
assert(!targetByName.has(name), `Project asset sprite already exists in the project: ${name}`);
|
|
408
|
+
const target = {
|
|
409
|
+
isStage: false,
|
|
410
|
+
name,
|
|
411
|
+
variables: {},
|
|
412
|
+
lists: {},
|
|
413
|
+
broadcasts: {},
|
|
414
|
+
blocks: {},
|
|
415
|
+
comments: {},
|
|
416
|
+
currentCostume: 0,
|
|
417
|
+
costumes: [],
|
|
418
|
+
sounds: [],
|
|
419
|
+
volume: specification.volume,
|
|
420
|
+
layerOrder: specification.layerOrder,
|
|
421
|
+
visible: specification.visible,
|
|
422
|
+
x: specification.x,
|
|
423
|
+
y: specification.y,
|
|
424
|
+
size: specification.size,
|
|
425
|
+
direction: specification.direction,
|
|
426
|
+
draggable: specification.draggable,
|
|
427
|
+
rotationStyle: specification.rotationStyle,
|
|
428
|
+
};
|
|
429
|
+
composedProject.targets.push(target);
|
|
430
|
+
targetByName.set(name, target);
|
|
431
|
+
newSpriteNames.push(name);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
let backdropCount = 0;
|
|
435
|
+
let costumeCount = 0;
|
|
436
|
+
let soundCount = 0;
|
|
437
|
+
let assetFileCount = 0;
|
|
438
|
+
|
|
439
|
+
for (const [rawAssetId, specification] of Object.entries(manifest.assets)) {
|
|
440
|
+
const assetId = nonEmptyString(rawAssetId, 'Project asset ID');
|
|
441
|
+
const description = validateAsset(specification, assetId);
|
|
442
|
+
const asset = await readAssetFile(manifestDirectory, allowedRoots, specification, description);
|
|
443
|
+
if (specification.kind !== 'sound') {
|
|
444
|
+
assert(
|
|
445
|
+
imageFormats.has(asset.dataFormat),
|
|
446
|
+
`${description}.file uses unsupported image format: ${asset.dataFormat}`,
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
const scratchName = asset.name ?? assetId;
|
|
450
|
+
const owner =
|
|
451
|
+
specification.kind === 'backdrop' || specification.target === undefined
|
|
452
|
+
? stage
|
|
453
|
+
: targetByName.get(specification.target);
|
|
454
|
+
assert(owner, `${description}.target does not exist in the project: ${specification.target}`);
|
|
455
|
+
assert(
|
|
456
|
+
specification.kind === 'backdrop' || !owner.isStage || specification.kind === 'sound',
|
|
457
|
+
`${description}.target must name a sprite.`,
|
|
458
|
+
);
|
|
459
|
+
assert(
|
|
460
|
+
specification.kind !== 'sound' || specification.target === undefined || !owner.isStage,
|
|
461
|
+
`${description}.target must be omitted for a Stage sound.`,
|
|
462
|
+
);
|
|
463
|
+
const collection = specification.kind === 'sound' ? owner.sounds : owner.costumes;
|
|
464
|
+
assert(
|
|
465
|
+
!collection.some(({name}) => name === scratchName),
|
|
466
|
+
`${description}.name already exists on target ${owner.name}: ${scratchName}`,
|
|
467
|
+
);
|
|
468
|
+
const contentAssetId = md5(asset.contents);
|
|
469
|
+
const filename = `${contentAssetId}.${asset.dataFormat}`;
|
|
470
|
+
if (
|
|
471
|
+
addAssetContents(
|
|
472
|
+
composedAssetContents,
|
|
473
|
+
composedArchiveEntries,
|
|
474
|
+
asset.contents,
|
|
475
|
+
filename,
|
|
476
|
+
description,
|
|
477
|
+
)
|
|
478
|
+
) {
|
|
479
|
+
assetFileCount += 1;
|
|
480
|
+
}
|
|
481
|
+
if (specification.kind === 'sound') {
|
|
482
|
+
const inferred = await inferSoundMetadata(asset.contents, asset.dataFormat, description);
|
|
483
|
+
if (specification.rate !== undefined) {
|
|
484
|
+
const expectedRate = safeInteger(specification.rate, `${description}.rate`, 1);
|
|
485
|
+
assert(
|
|
486
|
+
inferred.rate === expectedRate,
|
|
487
|
+
`${description}.rate differs: expected ${expectedRate}, got ${inferred.rate}`,
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
if (specification.sampleCount !== undefined) {
|
|
491
|
+
const expectedSampleCount = safeInteger(
|
|
492
|
+
specification.sampleCount,
|
|
493
|
+
`${description}.sampleCount`,
|
|
494
|
+
);
|
|
495
|
+
assert(
|
|
496
|
+
inferred.sampleCount === expectedSampleCount,
|
|
497
|
+
`${description}.sampleCount differs: expected ${expectedSampleCount}, got ${inferred.sampleCount}`,
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
collection.push({
|
|
501
|
+
name: scratchName,
|
|
502
|
+
assetId: contentAssetId,
|
|
503
|
+
dataFormat: asset.dataFormat,
|
|
504
|
+
format: '',
|
|
505
|
+
md5ext: filename,
|
|
506
|
+
rate: inferred.rate,
|
|
507
|
+
sampleCount: inferred.sampleCount,
|
|
508
|
+
});
|
|
509
|
+
soundCount += 1;
|
|
510
|
+
} else {
|
|
511
|
+
const bitmapResolution =
|
|
512
|
+
specification.bitmapResolution === undefined
|
|
513
|
+
? 1
|
|
514
|
+
: safeInteger(specification.bitmapResolution, `${description}.bitmapResolution`, 1);
|
|
515
|
+
assert(
|
|
516
|
+
bitmapResolution === 1 || bitmapResolution === 2,
|
|
517
|
+
`${description}.bitmapResolution must be 1 or 2.`,
|
|
518
|
+
);
|
|
519
|
+
collection.push({
|
|
520
|
+
name: scratchName,
|
|
521
|
+
bitmapResolution,
|
|
522
|
+
dataFormat: asset.dataFormat,
|
|
523
|
+
assetId: contentAssetId,
|
|
524
|
+
md5ext: filename,
|
|
525
|
+
rotationCenterX: finiteNumber(
|
|
526
|
+
specification.rotationCenterX,
|
|
527
|
+
`${description}.rotationCenterX`,
|
|
528
|
+
),
|
|
529
|
+
rotationCenterY: finiteNumber(
|
|
530
|
+
specification.rotationCenterY,
|
|
531
|
+
`${description}.rotationCenterY`,
|
|
532
|
+
),
|
|
533
|
+
});
|
|
534
|
+
if (specification.kind === 'backdrop') backdropCount += 1;
|
|
535
|
+
else costumeCount += 1;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
for (const name of newSpriteNames) {
|
|
540
|
+
assert(
|
|
541
|
+
targetByName.get(name).costumes.length > 0,
|
|
542
|
+
`Project asset sprite must receive at least one costume: ${name}`,
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return {
|
|
547
|
+
archiveEntries: composedArchiveEntries,
|
|
548
|
+
assetContents: composedAssetContents,
|
|
549
|
+
assetReferenceCount: backdropCount + costumeCount + soundCount,
|
|
550
|
+
manifestPath: resolvedManifestPath,
|
|
551
|
+
project: composedProject,
|
|
552
|
+
summary: Object.freeze({
|
|
553
|
+
assetFileCount,
|
|
554
|
+
backdropCount,
|
|
555
|
+
costumeCount,
|
|
556
|
+
soundCount,
|
|
557
|
+
spriteCount: newSpriteNames.length,
|
|
558
|
+
}),
|
|
559
|
+
};
|
|
560
|
+
}
|
package/src/source.js
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
validateExtensionSourceMetadata,
|
|
14
14
|
validateManagedExtensionContents,
|
|
15
15
|
} from './extension-dependencies.js';
|
|
16
|
+
import {applyProjectAssetAdditions} from './project-asset-additions.js';
|
|
17
|
+
import {cleanUpTurboWarpBlocks} from './turbowarp-clean-up.js';
|
|
16
18
|
|
|
17
19
|
export const sourceFormatVersion = 1;
|
|
18
20
|
export const fixedZipTimestamp = new Date(1980, 0, 1, 0, 0, 0, 0);
|
|
@@ -386,15 +388,52 @@ export async function validateSb3Source(sourceDirectory) {
|
|
|
386
388
|
return source;
|
|
387
389
|
}
|
|
388
390
|
|
|
389
|
-
|
|
391
|
+
/**
|
|
392
|
+
* @param {string} sourceDirectory
|
|
393
|
+
* @param {{allowedAssetRoots?: string[], cleanUpBlocks?: boolean, projectAssetsPath?: string}} [options]
|
|
394
|
+
*/
|
|
395
|
+
export async function createDeterministicSb3(sourceDirectory, options = {}) {
|
|
396
|
+
const {allowedAssetRoots = [], cleanUpBlocks = false, projectAssetsPath} = options;
|
|
397
|
+
assert(typeof cleanUpBlocks === 'boolean', 'cleanUpBlocks must be a boolean.');
|
|
398
|
+
assert(Array.isArray(allowedAssetRoots), 'allowedAssetRoots must be an array.');
|
|
399
|
+
assert(
|
|
400
|
+
projectAssetsPath === undefined || typeof projectAssetsPath === 'string',
|
|
401
|
+
'projectAssetsPath must be a string when provided.',
|
|
402
|
+
);
|
|
390
403
|
const source = await validateSb3Source(sourceDirectory);
|
|
404
|
+
const projectAssets = projectAssetsPath
|
|
405
|
+
? await applyProjectAssetAdditions({
|
|
406
|
+
allowedAssetRoots,
|
|
407
|
+
assetContents: source.assetContents,
|
|
408
|
+
archiveEntries: source.sourceManifest.archiveEntries,
|
|
409
|
+
manifestPath: projectAssetsPath,
|
|
410
|
+
project: source.project,
|
|
411
|
+
})
|
|
412
|
+
: {
|
|
413
|
+
archiveEntries: source.sourceManifest.archiveEntries,
|
|
414
|
+
assetContents: source.assetContents,
|
|
415
|
+
assetReferenceCount: 0,
|
|
416
|
+
project: source.project,
|
|
417
|
+
summary: null,
|
|
418
|
+
};
|
|
391
419
|
const bundled = buildExtensionBundles({
|
|
392
420
|
extensionBundles: source.extensionBundles,
|
|
393
421
|
extensionContents: source.extensionContents,
|
|
394
422
|
extensions: source.extensions,
|
|
395
|
-
project:
|
|
423
|
+
project: projectAssets.project,
|
|
396
424
|
});
|
|
397
|
-
|
|
425
|
+
let project = structuredClone(bundled.project);
|
|
426
|
+
let blockCleanUp = null;
|
|
427
|
+
if (cleanUpBlocks) {
|
|
428
|
+
const cleaned = cleanUpTurboWarpBlocks(project);
|
|
429
|
+
project = cleaned.project;
|
|
430
|
+
blockCleanUp = {
|
|
431
|
+
movedCommentCount: cleaned.movedCommentCount,
|
|
432
|
+
movedScriptCount: cleaned.movedScriptCount,
|
|
433
|
+
scriptCount: cleaned.scriptCount,
|
|
434
|
+
targetCount: cleaned.targetCount,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
398
437
|
for (const extension of bundled.extensions) {
|
|
399
438
|
project.extensionURLs[extension.id] = encodeExtensionDataUrl(
|
|
400
439
|
extension,
|
|
@@ -404,11 +443,11 @@ export async function createDeterministicSb3(sourceDirectory) {
|
|
|
404
443
|
|
|
405
444
|
/** @type {Record<string, Uint8Array>} */
|
|
406
445
|
const archiveEntries = {};
|
|
407
|
-
for (const entryName of
|
|
446
|
+
for (const entryName of projectAssets.archiveEntries) {
|
|
408
447
|
archiveEntries[entryName] =
|
|
409
448
|
entryName === 'project.json'
|
|
410
449
|
? strToU8(`${JSON.stringify(project)}\n`)
|
|
411
|
-
:
|
|
450
|
+
: projectAssets.assetContents.get(entryName);
|
|
412
451
|
}
|
|
413
452
|
const archive = zipSync(archiveEntries, {
|
|
414
453
|
level: 6,
|
|
@@ -417,11 +456,13 @@ export async function createDeterministicSb3(sourceDirectory) {
|
|
|
417
456
|
|
|
418
457
|
return {
|
|
419
458
|
archive,
|
|
420
|
-
assetCount:
|
|
421
|
-
assetReferenceCount: source.assetReferenceCount,
|
|
459
|
+
assetCount: projectAssets.assetContents.size,
|
|
460
|
+
assetReferenceCount: source.assetReferenceCount + projectAssets.assetReferenceCount,
|
|
461
|
+
blockCleanUp,
|
|
422
462
|
bundlePlans: bundled.bundlePlans,
|
|
423
463
|
embeddedExtensionCount: bundled.extensions.length,
|
|
424
|
-
entryCount:
|
|
464
|
+
entryCount: projectAssets.archiveEntries.length,
|
|
465
|
+
projectAssetAdditions: projectAssets.summary,
|
|
425
466
|
source,
|
|
426
467
|
};
|
|
427
468
|
}
|