@zaisma/cli 0.17.0 → 0.19.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/check.d.mts +4 -0
- package/check.mjs +13 -10
- package/package.json +3 -1
- package/performance.d.mts +40 -0
- package/performance.mjs +236 -0
- package/zaisma.mjs +57 -2
package/check.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// Types for check.mjs, so the site imports the game check type-checked.
|
|
2
|
+
import type { PerformanceNote } from "./performance.mjs";
|
|
2
3
|
|
|
3
4
|
export type CheckProblemCode =
|
|
4
5
|
| "no_index_html"
|
|
@@ -28,6 +29,8 @@ export interface CheckedFile {
|
|
|
28
29
|
sizeBytes: number;
|
|
29
30
|
/** The file's text for the checked text files (CHECKED_TEXT_EXTENSIONS), null for the rest. */
|
|
30
31
|
contentText: string | null;
|
|
32
|
+
/** The first bytes performance.mjs's assetHeadSizes asks for; left out, its notes skip the file. */
|
|
33
|
+
headBytes?: Uint8Array | null;
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
export interface CheckProblem {
|
|
@@ -45,6 +48,7 @@ export interface GameCheck {
|
|
|
45
48
|
size_bytes: number;
|
|
46
49
|
problems: CheckProblem[];
|
|
47
50
|
omitted_problem_count: number;
|
|
51
|
+
performance_notes: PerformanceNote[];
|
|
48
52
|
}
|
|
49
53
|
|
|
50
54
|
export type ReferenceKind = "script" | "module" | "style" | "file" | "import_map";
|
package/check.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// The game check behind `zaisma check`, the MCP's check_game and POST /api/dev/check: what Zaisma would
|
|
2
|
-
// refuse in a game, and what won't work inside its game frame, each with how to fix it
|
|
3
|
-
//
|
|
2
|
+
// refuse in a game, and what won't work inside its game frame, each with how to fix it, and the notes on
|
|
3
|
+
// files that could be much smaller (performance.mjs). Plain JavaScript with no imports but its neighbour, so
|
|
4
|
+
// the upload command runs it in Node and the site imports it as it is.
|
|
4
5
|
// Docs: https://github.com/zaisma/zaisma/blob/main/docs/DEVELOPER-API.md#checking-a-game
|
|
6
|
+
import { findAssetNotes } from "./performance.mjs";
|
|
5
7
|
|
|
6
8
|
/** Every file type a game may contain: the extensions in CONTENT_TYPES (packages/core/src/mime.ts), in its order. */
|
|
7
9
|
export const GAME_FILE_EXTENSIONS = [
|
|
@@ -79,7 +81,7 @@ const lineAround = (text, index) => {
|
|
|
79
81
|
* Engine and library output: Unity's framework and loader, a .js beside a .wasm of the same name (Godot,
|
|
80
82
|
* Emscripten), wasm-bindgen's glue (Bevy, Trunk) beside its _bg.wasm, macroquad's and miniquad's loader
|
|
81
83
|
* beside a .wasm, and minified libraries. Their code mentions dialogs, cameras and the like without the game
|
|
82
|
-
* using them, so the
|
|
84
|
+
* using them, so the behavior checks skip them.
|
|
83
85
|
*/
|
|
84
86
|
export function isEngineOutput(path, lowercasePaths) {
|
|
85
87
|
const lowercasePath = path.toLowerCase();
|
|
@@ -301,11 +303,12 @@ function dialogOf(code) {
|
|
|
301
303
|
const THREADS_PATTERNS = [/\bGODOT_THREADS_ENABLED\s*=\s*true\b/, /\bENVIRONMENT_IS_PTHREAD\b/, /coi-serviceworker/i, /\b__wbindgen_thread_destroy\b/, /\binitThreadPool\b/];
|
|
302
304
|
|
|
303
305
|
/**
|
|
304
|
-
* Checks a game's files: [{ path, sizeBytes, contentText }], contentText a string for the checked text
|
|
305
|
-
* (CHECKED_TEXT_EXTENSIONS) and null for the rest
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
306
|
+
* Checks a game's files: [{ path, sizeBytes, contentText, headBytes }], contentText a string for the checked text
|
|
307
|
+
* files (CHECKED_TEXT_EXTENSIONS) and null for the rest, headBytes the first bytes performance.mjs's assetHeadSizes
|
|
308
|
+
* asks for (left out, its notes skip the file). slug names the game in the fixes; ownServerUrls are its own
|
|
309
|
+
* servers, which the code may connect to (null when they aren't known). Answers the API's answer, snake_case:
|
|
310
|
+
* { file_count, size_bytes, problems, omitted_problem_count, performance_notes }, each problem { code,
|
|
311
|
+
* problem_kind, is_fixed_by_upload_command, message, path, quote, fix }; performance_notes never stop an upload.
|
|
309
312
|
*/
|
|
310
313
|
export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}) {
|
|
311
314
|
const game = slug ?? "<slug>";
|
|
@@ -346,7 +349,7 @@ export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}
|
|
|
346
349
|
const extension = extensionOf(path);
|
|
347
350
|
if (PRECOMPRESSED_EXTENSIONS.includes(extension)) {
|
|
348
351
|
const unpackedPath = path.slice(0, -extension.length - 1);
|
|
349
|
-
add("precompressed_file", { message: `${path} is pre-compressed.`, path, fix: `zaisma upload unpacks it into ${unpackedPath} and points the game at it; Zaisma compresses files for players itself. With upload_files or the API, turn off compression in the engine's build settings (Unity: Publishing Settings, Compression Format: Disabled) and build again.` });
|
|
352
|
+
add("precompressed_file", { message: `${path} is pre-compressed.`, path, fix: `zaisma upload unpacks it into ${unpackedPath} and points the game at it; Zaisma compresses files for players itself, even Unity and Godot data files. With upload_files or the API, turn off compression in the engine's build settings (Unity: Publishing Settings, Compression Format: Disabled) and build again.` });
|
|
350
353
|
} else if (!GAME_FILE_EXTENSIONS.includes(extension)) {
|
|
351
354
|
add("file_type_not_allowed", { message: `${path}: .${extension || "(no extension)"} files aren't allowed in games.`, path, fix: `Leave it out of the game's folder. Games can have: ${GAME_FILE_EXTENSIONS.join(", ")}.` });
|
|
352
355
|
}
|
|
@@ -421,5 +424,5 @@ export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}
|
|
|
421
424
|
if ((match = found(feature.pattern))) add("blocked_feature", { message: `${path} uses ${feature.name}, which games on Zaisma can't use.`, path, quote: lineAround(text, match.index), sameAs: feature.name, fix: "Leave it out, or make the game work without it when the browser says no." });
|
|
422
425
|
}
|
|
423
426
|
}
|
|
424
|
-
return { file_count: files.length, size_bytes: sizeBytes, problems, omitted_problem_count: omittedProblemCount };
|
|
427
|
+
return { file_count: files.length, size_bytes: sizeBytes, problems, omitted_problem_count: omittedProblemCount, performance_notes: findAssetNotes(files) };
|
|
425
428
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zaisma/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Upload and publish browser games on Zaisma from the command line.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://zaisma.com",
|
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
"zaisma.mjs",
|
|
28
28
|
"check.mjs",
|
|
29
29
|
"check.d.mts",
|
|
30
|
+
"performance.mjs",
|
|
31
|
+
"performance.d.mts",
|
|
30
32
|
"README.md",
|
|
31
33
|
"LICENSE"
|
|
32
34
|
],
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Types for performance.mjs, so the site and core import the asset notes type-checked.
|
|
2
|
+
|
|
3
|
+
export type AssetNoteCode = "images_could_be_ktx2" | "audio_could_be_opus" | "models_could_be_smaller" | "development_build";
|
|
4
|
+
|
|
5
|
+
export interface AssetFile {
|
|
6
|
+
path: string;
|
|
7
|
+
sizeBytes: number;
|
|
8
|
+
/** The file's text for the checked text files (check.mjs), null for the rest. */
|
|
9
|
+
contentText: string | null;
|
|
10
|
+
/** The first bytes assetHeadSizes asked for, or null when they weren't read. */
|
|
11
|
+
headBytes?: Uint8Array | null;
|
|
12
|
+
/** An image's size, read already, in place of its headBytes. */
|
|
13
|
+
sizePx?: { widthPx: number; heightPx: number } | null;
|
|
14
|
+
/** A sound's length, read already, in place of its headBytes. */
|
|
15
|
+
durationSec?: number | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface PerformanceNote {
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
path: string | null;
|
|
22
|
+
saved_size_bytes: number | null;
|
|
23
|
+
fix: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const ASSET_HEAD_SIZE_BYTES: number;
|
|
27
|
+
export const ASSET_NOTE_LIMITS: {
|
|
28
|
+
minimumImageSizeBytes: number;
|
|
29
|
+
minimumImagesSizeBytes: number;
|
|
30
|
+
minimumImagesSavedSizeBytes: number;
|
|
31
|
+
minimumAudioSizeBytes: number;
|
|
32
|
+
minimumModelsSizeBytes: number;
|
|
33
|
+
ktx2BytesPerPixel: number;
|
|
34
|
+
opusBytesPerSec: number;
|
|
35
|
+
};
|
|
36
|
+
export const ASSET_NOTE_CODES: Record<AssetNoteCode, { meaning: string; fix: string }>;
|
|
37
|
+
export function assetHeadSizes(files: { path: string; sizeBytes: number }[]): Map<string, number>;
|
|
38
|
+
export function imageSizePx(bytes: Uint8Array | null | undefined): { widthPx: number; heightPx: number } | null;
|
|
39
|
+
export function audioDurationSec(bytes: Uint8Array | null | undefined, sizeBytes: number): number | null;
|
|
40
|
+
export function findAssetNotes(files: AssetFile[], options?: { isDrawnWithGpu?: boolean | null }): PerformanceNote[];
|
package/performance.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// Notes on a game's files that would make it much smaller for players (docs/DECISIONS.md, "Every version is
|
|
2
|
+
// started in Chrome, and what it finds are notes."): textures that could be KTX2, WAV and AIFF audio, glTF models
|
|
3
|
+
// without mesh compression, and engine development builds, each with about what it saves and the fix. They never
|
|
4
|
+
// hold a version. `zaisma check`, check_game and the Chrome run after upload share them. Plain JavaScript with no
|
|
5
|
+
// imports, like check.mjs, so the upload command runs it in Node and the site imports it as it is.
|
|
6
|
+
// Docs: https://github.com/zaisma/zaisma/blob/main/docs/DEVELOPER-API.md#how-it-started-in-chrome
|
|
7
|
+
|
|
8
|
+
const MEGABYTE_BYTES = 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
/** How much of an image, sound, model or engine loader the notes read: the first 64 KB, where their headers are. */
|
|
11
|
+
export const ASSET_HEAD_SIZE_BYTES = 64 * 1024;
|
|
12
|
+
|
|
13
|
+
/** A .gltf this size or smaller is read whole, for the .bin files it names; a bigger one has its data inside it. */
|
|
14
|
+
const MAXIMUM_WHOLE_GLTF_SIZE_BYTES = 16 * MEGABYTE_BYTES;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Where a note is worth an AI's time on a PC-grade game and silent on a small one, and the fixed rates the
|
|
18
|
+
* estimates use: KTX2 (Basis Universal ETC1S) at about 2 bits a pixel plus a third for mipmaps, Opus at 96 kbps.
|
|
19
|
+
*/
|
|
20
|
+
export const ASSET_NOTE_LIMITS = {
|
|
21
|
+
minimumImageSizeBytes: MEGABYTE_BYTES,
|
|
22
|
+
minimumImagesSizeBytes: 16 * MEGABYTE_BYTES,
|
|
23
|
+
minimumImagesSavedSizeBytes: 8 * MEGABYTE_BYTES,
|
|
24
|
+
minimumAudioSizeBytes: 8 * MEGABYTE_BYTES,
|
|
25
|
+
minimumModelsSizeBytes: 8 * MEGABYTE_BYTES,
|
|
26
|
+
ktx2BytesPerPixel: (2 / 8) * (4 / 3),
|
|
27
|
+
opusBytesPerSec: 12_000,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** The development or debug builds an engine's loader says it is, by a marker only its debug export has. */
|
|
31
|
+
const DEVELOPMENT_BUILD_MARKERS = [
|
|
32
|
+
// Godot 4's loader names its engine file godot.web.template_debug…wasm in an export with Export With Debug ticked,
|
|
33
|
+
// and template_release otherwise (test/engines/godot/debug-loader.js, from test/engines/godot/source).
|
|
34
|
+
{ engine: "Godot", marker: "godot.web.template_debug.", fix: "Untick Export With Debug in the export dialog and export again." },
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** Every asset note: what it means, and the fix for all but development_build, whose fix is its engine's. */
|
|
38
|
+
export const ASSET_NOTE_CODES = {
|
|
39
|
+
images_could_be_ktx2: {
|
|
40
|
+
meaning: "PNG, JPEG and WebP textures of a WebGL or WebGPU game that would be much smaller as KTX2.",
|
|
41
|
+
fix: "Convert them with KTX-Software (toktx --t2 --encode etc1s --genmipmap out.ktx2 in.png) or basisu, and load them with your engine's KTX2 loader (three.js KTX2Loader, Babylon.js and PlayCanvas have one).",
|
|
42
|
+
},
|
|
43
|
+
audio_could_be_opus: {
|
|
44
|
+
meaning: "WAV and AIFF audio that would be much smaller compressed.",
|
|
45
|
+
fix: "Convert it (ffmpeg -i in.wav -c:a libopus -b:a 96k out.ogg). Safari plays Ogg from 18.4; for older Safari, also ship AAC in .m4a (ffmpeg -i in.wav -c:a aac -b:a 128k out.m4a).",
|
|
46
|
+
},
|
|
47
|
+
models_could_be_smaller: {
|
|
48
|
+
meaning: "glTF models with no mesh compression.",
|
|
49
|
+
fix: "Compress them with gltfpack (meshoptimizer) and load them with your engine's meshopt decoder (three.js GLTFLoader.setMeshoptDecoder); it usually makes models several times smaller.",
|
|
50
|
+
},
|
|
51
|
+
development_build: {
|
|
52
|
+
meaning: "An engine's development or debug build, bigger and slower for players.",
|
|
53
|
+
fix: DEVELOPMENT_BUILD_MARKERS.map(({ engine, fix }) => `${engine}: ${fix}`).join(" "),
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const IMAGE_EXTENSIONS = ["png", "jpg", "jpeg", "webp"];
|
|
58
|
+
const AUDIO_EXTENSIONS = ["wav", "aif", "aiff"];
|
|
59
|
+
const CODE_EXTENSIONS = ["js", "mjs", "html", "htm"];
|
|
60
|
+
/** Code that draws with WebGL or WebGPU: the context names, and the engines that do. */
|
|
61
|
+
const GPU_CODE = /webgl2?|getContext\(\s*["']gpu["']|\bTHREE\.|\bBABYLON\b|\bpc\.Application\b|\bPhaser\b/;
|
|
62
|
+
|
|
63
|
+
const extensionOf = (path) => {
|
|
64
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
65
|
+
const dotIndex = name.lastIndexOf(".");
|
|
66
|
+
return dotIndex > 0 ? name.slice(dotIndex + 1).toLowerCase() : "";
|
|
67
|
+
};
|
|
68
|
+
const megabytesText = (sizeBytes) => `${Math.max(1, Math.round(sizeBytes / MEGABYTE_BYTES))} MB`;
|
|
69
|
+
const latin1 = (bytes, startIndex = 0, endIndex = bytes.length) => String.fromCharCode(...bytes.subarray(startIndex, Math.min(endIndex, startIndex + 4096)));
|
|
70
|
+
const textOf = (bytes) => new TextDecoder().decode(bytes);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The files whose first bytes the notes read, and how many: images of 1 MB or more, WAV and AIFF audio, .glb
|
|
74
|
+
* models, a .gltf whole up to 16 MB (for the .bin files it names), and code, for an engine's development build.
|
|
75
|
+
* Callers read these and hand them to findAssetNotes as each file's headBytes.
|
|
76
|
+
*/
|
|
77
|
+
export function assetHeadSizes(files) {
|
|
78
|
+
const sizes = new Map();
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
const extension = extensionOf(file.path);
|
|
81
|
+
if (IMAGE_EXTENSIONS.includes(extension) && file.sizeBytes >= ASSET_NOTE_LIMITS.minimumImageSizeBytes) sizes.set(file.path, ASSET_HEAD_SIZE_BYTES);
|
|
82
|
+
else if (AUDIO_EXTENSIONS.includes(extension) || extension === "glb" || CODE_EXTENSIONS.includes(extension)) sizes.set(file.path, Math.min(file.sizeBytes, ASSET_HEAD_SIZE_BYTES));
|
|
83
|
+
else if (extension === "gltf") sizes.set(file.path, file.sizeBytes <= MAXIMUM_WHOLE_GLTF_SIZE_BYTES ? file.sizeBytes : ASSET_HEAD_SIZE_BYTES);
|
|
84
|
+
}
|
|
85
|
+
return sizes;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** An image's width and height from its header (PNG IHDR, a JPEG SOFn marker, WebP's VP8, VP8L or VP8X), or null. */
|
|
89
|
+
export function imageSizePx(bytes) {
|
|
90
|
+
if (!bytes || bytes.length < 24) return null;
|
|
91
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
92
|
+
if (bytes[0] === 0x89 && latin1(bytes, 1, 4) === "PNG" && latin1(bytes, 12, 16) === "IHDR") return { widthPx: view.getUint32(16), heightPx: view.getUint32(20) };
|
|
93
|
+
if (bytes[0] === 0xff && bytes[1] === 0xd8) {
|
|
94
|
+
for (let byteIndex = 2; byteIndex + 9 < bytes.length; ) {
|
|
95
|
+
if (bytes[byteIndex] !== 0xff) return null;
|
|
96
|
+
const marker = bytes[byteIndex + 1];
|
|
97
|
+
if (marker === 0xff) {
|
|
98
|
+
byteIndex += 1;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
// SOF0 to SOF15, besides DHT (C4), JPG (C8) and DAC (CC), carry the frame's size.
|
|
102
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) return { widthPx: view.getUint16(byteIndex + 7), heightPx: view.getUint16(byteIndex + 5) };
|
|
103
|
+
byteIndex += 2 + view.getUint16(byteIndex + 2);
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
if (bytes.length >= 30 && latin1(bytes, 0, 4) === "RIFF" && latin1(bytes, 8, 12) === "WEBP") {
|
|
108
|
+
const chunk = latin1(bytes, 12, 16);
|
|
109
|
+
if (chunk === "VP8 ") return { widthPx: view.getUint16(26, true) & 0x3fff, heightPx: view.getUint16(28, true) & 0x3fff };
|
|
110
|
+
if (chunk === "VP8L") {
|
|
111
|
+
const packedNumber = view.getUint32(21, true);
|
|
112
|
+
return { widthPx: (packedNumber & 0x3fff) + 1, heightPx: ((packedNumber >>> 14) & 0x3fff) + 1 };
|
|
113
|
+
}
|
|
114
|
+
if (chunk === "VP8X") return { widthPx: (view.getUint32(24, true) & 0xffffff) + 1, heightPx: (view.getUint32(27, true) & 0xffffff) + 1 };
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** How long a WAV or AIFF file plays, in seconds, from its header; null when the header can't tell. */
|
|
120
|
+
export function audioDurationSec(bytes, sizeBytes) {
|
|
121
|
+
if (!bytes || bytes.length < 12) return null;
|
|
122
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
123
|
+
if (latin1(bytes, 0, 4) === "RIFF" && latin1(bytes, 8, 12) === "WAVE") {
|
|
124
|
+
let byteRate = null;
|
|
125
|
+
for (let byteIndex = 12; byteIndex + 8 <= bytes.length; ) {
|
|
126
|
+
const chunk = latin1(bytes, byteIndex, byteIndex + 4);
|
|
127
|
+
const chunkSizeBytes = view.getUint32(byteIndex + 4, true);
|
|
128
|
+
if (chunk === "fmt " && byteIndex + 16 <= bytes.length) byteRate = view.getUint32(byteIndex + 16, true);
|
|
129
|
+
// The data chunk's own size, or what's left of the file when it's the last and its size wasn't written.
|
|
130
|
+
if (chunk === "data") return byteRate ? Math.min(chunkSizeBytes, sizeBytes - byteIndex - 8) / byteRate : null;
|
|
131
|
+
byteIndex += 8 + chunkSizeBytes + (chunkSizeBytes % 2);
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
if (latin1(bytes, 0, 4) === "FORM" && ["AIFF", "AIFC"].includes(latin1(bytes, 8, 12))) {
|
|
136
|
+
for (let byteIndex = 12; byteIndex + 8 <= bytes.length; ) {
|
|
137
|
+
const chunkSizeBytes = view.getUint32(byteIndex + 4);
|
|
138
|
+
if (latin1(bytes, byteIndex, byteIndex + 4) === "COMM" && byteIndex + 26 <= bytes.length) {
|
|
139
|
+
const frameCount = view.getUint32(byteIndex + 10);
|
|
140
|
+
// The sample rate is an 80-bit IEEE extended number: a 15-bit exponent and a 64-bit mantissa.
|
|
141
|
+
const exponentNumber = (view.getUint16(byteIndex + 16) & 0x7fff) - 16383;
|
|
142
|
+
const mantissaNumber = view.getUint32(byteIndex + 18) * 2 ** 32 + view.getUint32(byteIndex + 22);
|
|
143
|
+
const samplesPerSec = mantissaNumber * 2 ** (exponentNumber - 63);
|
|
144
|
+
return samplesPerSec > 0 ? frameCount / samplesPerSec : null;
|
|
145
|
+
}
|
|
146
|
+
byteIndex += 8 + chunkSizeBytes + (chunkSizeBytes % 2);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A glTF model's JSON, as far as its first bytes reach: a .glb's JSON chunk, or a .gltf's text. */
|
|
153
|
+
function modelJsonText(path, bytes) {
|
|
154
|
+
if (!bytes) return "";
|
|
155
|
+
if (extensionOf(path) === "gltf") return textOf(bytes);
|
|
156
|
+
if (bytes.length < 20 || latin1(bytes, 0, 4) !== "glTF" || latin1(bytes, 16, 20) !== "JSON") return "";
|
|
157
|
+
const chunkSizeBytes = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(12, true);
|
|
158
|
+
return textOf(bytes.subarray(20, 20 + chunkSizeBytes));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The asset notes for a game's files: [{ path, sizeBytes, contentText, headBytes }], contentText as check.mjs takes
|
|
163
|
+
* it (or null) and headBytes the first bytes assetHeadSizes asked for (or null when they weren't read: a note that
|
|
164
|
+
* needs them then leaves that file out). A caller holding many files may read an image's size or a sound's length
|
|
165
|
+
* itself and pass sizePx or durationSec instead of its bytes. isDrawnWithGpu says the game draws with WebGL or WebGPU, as its Chrome run
|
|
166
|
+
* saw; null works it out from its code. Answers notes, snake_case: { code, message, path, saved_size_bytes, fix }.
|
|
167
|
+
*/
|
|
168
|
+
export function findAssetNotes(files, { isDrawnWithGpu = null } = {}) {
|
|
169
|
+
const filesByPath = new Map(files.map((file) => [file.path, file]));
|
|
170
|
+
const codeTextOf = (file) => file.contentText ?? (file.headBytes ? textOf(file.headBytes) : "");
|
|
171
|
+
const codeFiles = files.filter((file) => CODE_EXTENSIONS.includes(extensionOf(file.path)));
|
|
172
|
+
const notes = [];
|
|
173
|
+
const note = (code, message, paths, savedSizeBytes, fix = ASSET_NOTE_CODES[code].fix) => notes.push({ code, message, path: paths.length === 1 ? paths[0] : null, saved_size_bytes: savedSizeBytes, fix });
|
|
174
|
+
|
|
175
|
+
// Textures: only a game that draws with the GPU uploads them as textures, and only an image the estimate halves counts.
|
|
176
|
+
const isGpuGame = isDrawnWithGpu ?? codeFiles.some((file) => GPU_CODE.test(codeTextOf(file)));
|
|
177
|
+
if (isGpuGame) {
|
|
178
|
+
const images = [];
|
|
179
|
+
for (const file of files) {
|
|
180
|
+
if (!IMAGE_EXTENSIONS.includes(extensionOf(file.path)) || file.sizeBytes < ASSET_NOTE_LIMITS.minimumImageSizeBytes) continue;
|
|
181
|
+
const measuredSizePx = file.sizePx ?? imageSizePx(file.headBytes);
|
|
182
|
+
const estimatedSizeBytes = measuredSizePx ? Math.round(measuredSizePx.widthPx * measuredSizePx.heightPx * ASSET_NOTE_LIMITS.ktx2BytesPerPixel) : null;
|
|
183
|
+
if (estimatedSizeBytes !== null && estimatedSizeBytes * 2 <= file.sizeBytes) images.push({ path: file.path, sizeBytes: file.sizeBytes, estimatedSizeBytes });
|
|
184
|
+
}
|
|
185
|
+
const totalSizeBytes = images.reduce((sumBytes, image) => sumBytes + image.sizeBytes, 0);
|
|
186
|
+
const estimatedSizeBytes = images.reduce((sumBytes, image) => sumBytes + image.estimatedSizeBytes, 0);
|
|
187
|
+
if (totalSizeBytes >= ASSET_NOTE_LIMITS.minimumImagesSizeBytes && totalSizeBytes - estimatedSizeBytes >= ASSET_NOTE_LIMITS.minimumImagesSavedSizeBytes) {
|
|
188
|
+
const message = `${megabytesText(totalSizeBytes)} of PNG, JPEG and WebP textures could be about ${megabytesText(estimatedSizeBytes)} as KTX2, and use about a quarter of the graphics memory.`;
|
|
189
|
+
note("images_could_be_ktx2", message, images.map((image) => image.path), totalSizeBytes - estimatedSizeBytes);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const sounds = files.filter((file) => AUDIO_EXTENSIONS.includes(extensionOf(file.path)));
|
|
194
|
+
const soundsSizeBytes = sounds.reduce((sumBytes, file) => sumBytes + file.sizeBytes, 0);
|
|
195
|
+
if (soundsSizeBytes >= ASSET_NOTE_LIMITS.minimumAudioSizeBytes) {
|
|
196
|
+
// A sound whose header can't tell its length is counted as it is: nothing saved on it.
|
|
197
|
+
const estimatedSizeBytes = Math.round(sounds.reduce((sumBytes, file) => {
|
|
198
|
+
const durationSec = file.durationSec ?? audioDurationSec(file.headBytes, file.sizeBytes);
|
|
199
|
+
return sumBytes + (durationSec === null ? file.sizeBytes : Math.min(file.sizeBytes, durationSec * ASSET_NOTE_LIMITS.opusBytesPerSec));
|
|
200
|
+
}, 0));
|
|
201
|
+
if (estimatedSizeBytes < soundsSizeBytes) {
|
|
202
|
+
note("audio_could_be_opus", `${megabytesText(soundsSizeBytes)} of WAV and AIFF audio could be about ${megabytesText(estimatedSizeBytes)} as Ogg Opus.`, sounds.map((file) => file.path), soundsSizeBytes - estimatedSizeBytes);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const models = [];
|
|
207
|
+
for (const file of files) {
|
|
208
|
+
const extension = extensionOf(file.path);
|
|
209
|
+
if ((extension !== "glb" && extension !== "gltf") || !file.headBytes) continue;
|
|
210
|
+
const jsonText = modelJsonText(file.path, file.headBytes);
|
|
211
|
+
if (/EXT_meshopt_compression|KHR_meshopt_compression|KHR_draco_mesh_compression/.test(jsonText)) continue;
|
|
212
|
+
let sizeBytes = file.sizeBytes;
|
|
213
|
+
if (extension === "gltf") {
|
|
214
|
+
const folder = file.path.slice(0, file.path.lastIndexOf("/") + 1);
|
|
215
|
+
try {
|
|
216
|
+
for (const buffer of JSON.parse(jsonText).buffers ?? []) {
|
|
217
|
+
if (typeof buffer.uri !== "string" || buffer.uri.startsWith("data:")) continue;
|
|
218
|
+
sizeBytes += filesByPath.get(folder + decodeURIComponent(buffer.uri))?.sizeBytes ?? 0;
|
|
219
|
+
}
|
|
220
|
+
} catch {
|
|
221
|
+
// A .gltf too big to read whole has its data inside it.
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
models.push({ path: file.path, sizeBytes });
|
|
225
|
+
}
|
|
226
|
+
const modelsSizeBytes = models.reduce((sumBytes, model) => sumBytes + model.sizeBytes, 0);
|
|
227
|
+
if (modelsSizeBytes >= ASSET_NOTE_LIMITS.minimumModelsSizeBytes) {
|
|
228
|
+
note("models_could_be_smaller", `${megabytesText(modelsSizeBytes)} of glTF models have no mesh compression.`, models.map((model) => model.path), null);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
for (const { engine, marker, fix } of DEVELOPMENT_BUILD_MARKERS) {
|
|
232
|
+
const loader = codeFiles.find((file) => codeTextOf(file).includes(marker));
|
|
233
|
+
if (loader) note("development_build", `This is a ${engine} development build: bigger and slower for players.`, [loader.path], null, fix);
|
|
234
|
+
}
|
|
235
|
+
return notes;
|
|
236
|
+
}
|
package/zaisma.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import { dirname, extname, join, posix, relative, sep } from "node:path";
|
|
|
10
10
|
import { pipeline } from "node:stream/promises";
|
|
11
11
|
import { createBrotliDecompress, createGunzip } from "node:zlib";
|
|
12
12
|
import { CHECKED_TEXT_EXTENSIONS, GAME_FILE_EXTENSIONS, PRECOMPRESSED_EXTENSIONS, checkGameFiles, extensionOf, findOutsideReferences } from "./check.mjs";
|
|
13
|
+
import { assetHeadSizes } from "./performance.mjs";
|
|
13
14
|
|
|
14
15
|
const API = (process.env.ZAISMA_API || "https://zaisma.com").replace(/\/+$/, "");
|
|
15
16
|
const KEY = process.env.ZAISMA_KEY || "";
|
|
@@ -21,6 +22,9 @@ const JUNK = new Set([".DS_Store", "Thumbs.db", "desktop.ini", "__MACOSX", ".git
|
|
|
21
22
|
/** How long upload waits for the scan (200 checks, 3 seconds apart) before it says it's still scanning. ZAISMA_SCAN_CHECK_INTERVAL_MS is for tests. */
|
|
22
23
|
const SCAN_CHECK_INTERVAL_MS = Number(process.env.ZAISMA_SCAN_CHECK_INTERVAL_MS) || 3000;
|
|
23
24
|
const SCAN_CHECK_COUNT = 200;
|
|
25
|
+
/** How long upload then waits for the version's run in Chrome (30 checks, 5 seconds apart); the same variable speeds it up in tests. */
|
|
26
|
+
const PERFORMANCE_CHECK_INTERVAL_MS = Number(process.env.ZAISMA_SCAN_CHECK_INTERVAL_MS) || 5000;
|
|
27
|
+
const PERFORMANCE_CHECK_COUNT = 30;
|
|
24
28
|
/** The most SQL database import sends at once (a single bigger statement goes on its own; the API takes up to 5 MB). ZAISMA_DUMP_PART_SIZE_BYTES is for tests. */
|
|
25
29
|
const DUMP_PART_SIZE_BYTES = Number(process.env.ZAISMA_DUMP_PART_SIZE_BYTES) || 4 * 1024 * 1024;
|
|
26
30
|
/** What upload downloads into a game from outside sites: at most this many files, each up to this size, each within this time. */
|
|
@@ -439,7 +443,10 @@ async function walk(root) {
|
|
|
439
443
|
return { files, hiddenPaths };
|
|
440
444
|
}
|
|
441
445
|
|
|
442
|
-
/**
|
|
446
|
+
/**
|
|
447
|
+
* A game's folder for the check and the upload: each file's path, where it is, its size and, for the checked text
|
|
448
|
+
* files, its text; and for the notes on files that could be smaller, the first bytes of images, sounds and models.
|
|
449
|
+
*/
|
|
443
450
|
async function readGameFolder(folder) {
|
|
444
451
|
if (!(await stat(folder).catch(() => null))?.isDirectory()) refuse("not_a_folder", { message: `${folder} isn't a folder.`, field: "folder", fix: "Give the folder with the game's index.html at the top, like ./build." });
|
|
445
452
|
const { files: fullPaths, hiddenPaths } = await walk(folder);
|
|
@@ -448,6 +455,18 @@ async function readGameFolder(folder) {
|
|
|
448
455
|
const path = relative(folder, full).split(sep).join("/");
|
|
449
456
|
files.push({ path, full, sizeBytes: (await stat(full)).size, contentText: CHECKED_TEXT_EXTENSIONS.includes(extensionOf(path)) ? await readFile(full, "utf8") : null });
|
|
450
457
|
}
|
|
458
|
+
const headSizes = assetHeadSizes(files);
|
|
459
|
+
for (const file of files) {
|
|
460
|
+
if (file.contentText !== null || !headSizes.has(file.path)) continue;
|
|
461
|
+
const handle = await open(file.full, "r");
|
|
462
|
+
try {
|
|
463
|
+
const headBytes = new Uint8Array(headSizes.get(file.path));
|
|
464
|
+
const { bytesRead: readByteCount } = await handle.read(headBytes, 0, headBytes.length, 0);
|
|
465
|
+
file.headBytes = headBytes.subarray(0, readByteCount);
|
|
466
|
+
} finally {
|
|
467
|
+
await handle.close();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
451
470
|
return { files, hiddenPaths };
|
|
452
471
|
}
|
|
453
472
|
|
|
@@ -484,6 +503,13 @@ function printProblems(problems, groups = PROBLEM_GROUPS) {
|
|
|
484
503
|
}
|
|
485
504
|
}
|
|
486
505
|
|
|
506
|
+
/** Notes on files that could be much smaller: never a problem, so they never change the exit code. */
|
|
507
|
+
function printSmallerNotes(notes) {
|
|
508
|
+
if (!notes.length) return;
|
|
509
|
+
say("Could be smaller:");
|
|
510
|
+
for (const note of notes) say(` · ${note.message} Fix: ${note.fix}`);
|
|
511
|
+
}
|
|
512
|
+
|
|
487
513
|
/** zaisma check: the problems, or the answer with --json. Ends with problems_found when one isn't fixed by the upload command. */
|
|
488
514
|
async function checkFolder({ folder }, flags) {
|
|
489
515
|
const answer = await checkGame(await readGameFolder(folder), flags.game ?? null);
|
|
@@ -492,6 +518,7 @@ async function checkFolder({ folder }, flags) {
|
|
|
492
518
|
else {
|
|
493
519
|
printProblems(answer.problems);
|
|
494
520
|
if (answer.omitted_problem_count) console.log(`…and ${answer.omitted_problem_count} more.`);
|
|
521
|
+
printSmallerNotes(answer.performance_notes);
|
|
495
522
|
console.log(
|
|
496
523
|
hasProblems
|
|
497
524
|
? "Fix what's above, then check again."
|
|
@@ -708,7 +735,7 @@ async function sendInParts(file, progress) {
|
|
|
708
735
|
await api("POST", `/files/${file.sha256}/parts/done`, { upload_id: start.upload_id, parts });
|
|
709
736
|
}
|
|
710
737
|
|
|
711
|
-
const readableSize = (sizeBytes) => (sizeBytes >= 1048576 ? `${(sizeBytes / 1048576).toFixed(1)} MB` : `${Math.ceil(sizeBytes / 1024)} KB`);
|
|
738
|
+
const readableSize = (sizeBytes) => (sizeBytes >= 1024 ** 3 ? `${(sizeBytes / 1024 ** 3).toFixed(1)} GB` : sizeBytes >= 1048576 ? `${(sizeBytes / 1048576).toFixed(1)} MB` : `${Math.ceil(sizeBytes / 1024)} KB`);
|
|
712
739
|
const sleep = (durationMs) => new Promise((resolve) => setTimeout(resolve, durationMs));
|
|
713
740
|
/** A running count of what's sent, for people watching; left out with --json, where stdout is only the answer. */
|
|
714
741
|
const showProgress = (text) => isJson || process.stdout.write(`\r ${text} `);
|
|
@@ -771,6 +798,12 @@ async function sendVersion(slug, fixedFiles, flags) {
|
|
|
771
798
|
await sleep(SCAN_CHECK_INTERVAL_MS);
|
|
772
799
|
answer = await api("GET", `/games/${slug}/versions/${versionId}`);
|
|
773
800
|
}
|
|
801
|
+
// A passed version is opened in Chrome next, usually within a minute: its numbers and notes come before the preview.
|
|
802
|
+
const isRunning = (checked) => ["waiting", "checking"].includes(checked.version.performance_check?.check_status);
|
|
803
|
+
for (let i = 0; i < PERFORMANCE_CHECK_COUNT && answer.version.scan_status === "approved" && isRunning(answer); i++) {
|
|
804
|
+
await sleep(PERFORMANCE_CHECK_INTERVAL_MS);
|
|
805
|
+
answer = await api("GET", `/games/${slug}/versions/${versionId}`);
|
|
806
|
+
}
|
|
774
807
|
const { version } = answer;
|
|
775
808
|
if (isJson) printJson(answer);
|
|
776
809
|
if (version.scan_status === "scanning") {
|
|
@@ -779,6 +812,8 @@ async function sendVersion(slug, fixedFiles, flags) {
|
|
|
779
812
|
}
|
|
780
813
|
if (version.scan_status === "approved") {
|
|
781
814
|
if (isJson) return EXIT_CODES.done;
|
|
815
|
+
if (isRunning(answer)) console.log(`Still checking how it starts in Chrome; zaisma versions ${slug} --json shows it.`);
|
|
816
|
+
else printPerformance(version);
|
|
782
817
|
if (version.is_live) console.log(`Passed and live: ${API}/g/${slug}`);
|
|
783
818
|
else {
|
|
784
819
|
console.log(`Passed. Your private preview: ${version.preview_url}`);
|
|
@@ -793,6 +828,26 @@ async function sendVersion(slug, fixedFiles, flags) {
|
|
|
793
828
|
return version.scan_status === "held" ? EXIT_CODES.held : EXIT_CODES.rejected;
|
|
794
829
|
}
|
|
795
830
|
|
|
831
|
+
/** How a version started in Chrome, and what would make it smaller: nothing when it wasn't measured (a zip, or over the day's runs). */
|
|
832
|
+
function printPerformance(version) {
|
|
833
|
+
const check = version.performance_check;
|
|
834
|
+
if (!check) return;
|
|
835
|
+
if (check.check_status === "over_daily_limit") return console.log("Not started in Chrome: today's 20 runs are used. Versions are measured again from midnight UTC.");
|
|
836
|
+
if (check.check_status === "could_not_run") return console.log("Chrome couldn't run it on Zaisma's side; nothing for you to fix.");
|
|
837
|
+
console.log(`Started in ${check.browser_description.replace(/^Chrome \d+ on a computer with no graphics card$/, "Chrome (a standard computer, no graphics card)")}`);
|
|
838
|
+
const numbers = [
|
|
839
|
+
check.loading_duration_sec === null ? "Nothing drawn" : `First frame after ${check.loading_duration_sec} s`,
|
|
840
|
+
check.start_download_size_bytes === null ? null : `${readableSize(check.start_download_size_bytes)} downloaded before it`,
|
|
841
|
+
check.start_memory_size_bytes === null ? null : `${readableSize(check.start_memory_size_bytes)} memory at start`,
|
|
842
|
+
];
|
|
843
|
+
console.log(` ${numbers.filter(Boolean).join(" · ")}`);
|
|
844
|
+
const smaller = new Set(["images_could_be_ktx2", "audio_could_be_opus", "models_could_be_smaller", "development_build"]);
|
|
845
|
+
for (const note of check.notes.filter((note) => !smaller.has(note.code))) console.log(` ${note.message} Fix: ${note.fix}`);
|
|
846
|
+
const notes = check.notes.filter((note) => smaller.has(note.code));
|
|
847
|
+
if (notes.length) console.log(" Could be smaller:");
|
|
848
|
+
for (const note of notes) console.log(` · ${note.message} Fix: ${note.fix}`);
|
|
849
|
+
}
|
|
850
|
+
|
|
796
851
|
/** Why a version is held or rejected: a person's or a report's decision, then what the scan found, each with its code and rule, then what to do. */
|
|
797
852
|
function printReasons(version) {
|
|
798
853
|
const reasons = [...(version.review_reason ? [version.review_reason] : []), ...version.findings];
|