A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(export): a playable that is far over its cap is built at once and reports its size
yhhb128cf122abf
diff --git a/pipeline/src/export/exportPlayable.ts b/pipeline/src/export/exportPlayable.tsindex a86710e46..f86b48eb8 100644--- a/pipeline/src/export/exportPlayable.ts+++ b/pipeline/src/export/exportPlayable.ts@@ -29,7 +29,7 @@ import { deflateRawSync } from 'node:zlib';import path from 'node:path';import { cookAssets } from '../assets/cookAssets';import { scanAssetDatabase } from '../assets/assetDb';-import { optimizePlayableAssets, type PlayableOptimizationAsset, type PlayablePayload } from './playableOptimization';+import { optimizePlayableAssets, playableAssetsJsonBytes, type PlayableOptimizationAsset, type PlayablePayload } from './playableOptimization';import type { OnExportProgress } from './exportProgress';import { esengineAlias } from '../bundle/esengineResolve';import { officialPackagesPlugin } from '../bundle/officialPackages';@@ -419,6 +419,7 @@ export async function exportPlayable(opts: {...archiveFiles.map(file => ({ name: file.name, data: Buffer.from(file.content) }))]) : html;return { html, delivery, htmlBytes: html.byteLength, deliveryBytes: delivery.byteLength };};+ const emptyHtmlBytes = assemble(new Map()).htmlBytes;const targetDeliveryBytes = Math.min(adProfile.maxBytes, opts.sizeBudgetBytes ?? Infinity, 3 * 1024 * 1024);const baseline = assemble(new Map(optimizationAssets.map(asset => [asset.key, asset])));if (errors.length === 0 && (opts.resizeTextures || opts.compressAudio) &&@@ -440,7 +441,9 @@ export async function exportPlayable(opts: {const optimized = await optimizePlayableAssets({ root: opts.root, assets: optimizationAssets,textures: errors.length === 0 && !!opts.resizeTextures, audio: errors.length === 0 && !!opts.compressAudio,targetDeliveryBytes, maxHtmlBytes: adProfile.maxHtmlBytes,- measure: payloads => assemble(payloads), warnings });+ // A single-file page measures by arithmetic (its size is a fixed part plus the assets' JSON); a zip has to be built to be weighed.+ measure: payloads => zipFile ? assemble(payloads) : (() => { const htmlBytes = emptyHtmlBytes - 2 + playableAssetsJsonBytes(payloads); return { htmlBytes, deliveryBytes: htmlBytes }; })(),+ onStage: (stage, assets) => progress({ phase: 'Optimizing playable budget', detail: `${stage} — up to ${assets} assets` }), warnings });optimized.report.mode = opts.resizeTextures || opts.compressAudio ? 'auto' : 'skip';const final = assemble(optimized.payloads);await writeFile(outFile, final.html);@@ -453,7 +456,8 @@ export async function exportPlayable(opts: {}if (!optimized.report.meetsTarget) warnings.push(`Playable optimization target not met: delivery ${bytes}/${targetDeliveryBytes} bytes, HTML ${htmlBytes}/${adProfile.maxHtmlBytes ?? 'unlimited'} bytes. Reduce assets or engine modules; no quality below WebP 80 / MP3 96 kbps is selected.`);if (adProfile.maxHtmlBytes !== undefined && htmlBytes > adProfile.maxHtmlBytes) {- errors.push(`${adProfile.label}: index.html is ${htmlBytes} bytes, exceeding the uncompressed HTML limit of ${adProfile.maxHtmlBytes} bytes. ${adProfile.limitNote}`);+ // The file is written and its size is on the screen: whether to slim it or ship it anyway is the developer's call, not a failed build.+ warnings.push(`${adProfile.label}: index.html is ${htmlBytes} bytes, exceeding the uncompressed HTML limit of ${adProfile.maxHtmlBytes} bytes. ${adProfile.limitNote}`);}const inlined = spans.reduce((n, s) => n + s.bytes, 0);spans.push({ path: 'index.html', bytes: Math.max(0, htmlBytes - inlined) });diff --git a/pipeline/src/export/playableOptimization.ts b/pipeline/src/export/playableOptimization.tsindex 20ffa954f..48abac59b 100644--- a/pipeline/src/export/playableOptimization.ts+++ b/pipeline/src/export/playableOptimization.ts@@ -30,6 +30,21 @@ export interface PlayableOptimizationReport {changes: { path: string; codec: PlayableSelectedCodec; inputBytes: number; outputBytes: number }[];}+/**+ * The bytes `JSON.stringify` writes for a `key -> data URL` map, worked out from the payload sizes alone. The page carries+ * every asset as one such JSON object, so its size is a fixed part plus this; measuring it by building the object (a base64+ * copy of every asset) for each candidate encoding made the search cost the whole package once per candidate.+ */+export function playableAssetsJsonBytes(payloads: ReadonlyMap<string, PlayablePayload>): number {+ if (payloads.size === 0) return 2;+ let bytes = 1 + (payloads.size - 1) + 1; // { , , }+ for (const [key, payload] of payloads) {+ const url = 'data:'.length + payload.mime.length + ';base64,'.length + 4 * Math.ceil(payload.data.byteLength / 3);+ bytes += Buffer.byteLength(JSON.stringify(key)) + 1 + (url + 2); // "key":"url"+ }+ return bytes;+}+/** WebP cannot retain PNG gamma/profile chunks, animation or 16-bit samples. */function canReencodePng(data: Uint8Array): boolean {const png = Buffer.from(data);@@ -92,6 +107,8 @@ export async function optimizePlayableAssets(opts: {maxHtmlBytes?: number;measure: (payloads: ReadonlyMap<string, PlayablePayload>) => PlayableMeasuredSize;warnings: string[];+ /** Called as each stage of the search begins, so a long one is not a silent spinner. */+ onStage?: (stage: string, assets: number) => void;}): Promise<{ payloads: Map<string, PlayablePayload>; report: PlayableOptimizationReport }> {const payloads = new Map<string, PlayablePayload>(opts.assets.map(asset => [asset.key, { data: asset.data, mime: asset.mime }]));const measure = () => {@@ -113,10 +130,23 @@ export async function optimizePlayableAssets(opts: {}}}+ // What no encoder here can shrink (fonts, data, anything that is not a PNG or a WAV) is a floor under the page. When that+ // part alone, weighed the way the page is weighed, is already over the target, no search will get there: the package is+ // made as it is and the size is reported, instead of trying every image at every quality to arrive at the same answer.+ const fixed = opts.assets.filter(a => !(a.mime === 'image/png' || a.sourceWav));+ if (fixed.length > 0 && score(selected) > 1) {+ const floor = opts.measure(new Map(fixed.map(a => [a.key, { data: a.data, mime: a.mime }])));+ if (score(floor) > 1) {+ opts.warnings.push(`Playable size cannot reach the target: the parts no encoder can shrink (fonts and other files) alone come to ${(floor.deliveryBytes / 1048576).toFixed(1)} MB, against a target of ${(opts.targetDeliveryBytes / 1048576).toFixed(1)} MB. The package is built as it is; its size and what it is made of are reported.`);+ return { payloads, report: { mode: opts.textures || opts.audio ? 'auto' : 'skip', targetDeliveryBytes: opts.targetDeliveryBytes,+ maxHtmlBytes: opts.maxHtmlBytes, baseline, selected, meetsTarget: false, changes: [] } };+ }+ }let encoder: { bin: string; id: string } | null | undefined;const failures = new Set<string>();for (const stage of ['lossless', 'audio-128', 'audio-96', 90, 80] as const) {if (score(selected) <= 1) break;+ opts.onStage?.(String(stage), sorted.length);for (const asset of sorted) {if (score(selected) <= 1) break;const previous = payloads.get(asset.key)!;diff --git a/pipeline/tests/export-playable.test.ts b/pipeline/tests/export-playable.test.tsindex e2e81b4f0..3c3f3f0e6 100644--- a/pipeline/tests/export-playable.test.ts+++ b/pipeline/tests/export-playable.test.ts@@ -377,7 +377,7 @@ describe('exportGame (playable)', () => {expect(res.errors.join('\n')).toMatch(/unique portable root filenames/);}, 60_000);- it('rejects an oversized HTML even when a ZIP is the uploaded file', async () => {+ it('builds an oversized HTML anyway and says how big it is, even when a ZIP is the uploaded file', async () => {const res = await exportGame({root, entryScene: 'scenes/main.esscene', hostsDir: HOSTS, packagesDir: OFFICIAL_PACKAGES,scriptsEntry: 'src/main.ts', sdkDistDir: path.join(root, '_sdk'), wasmDir: path.join(root, '_wasm'),@@ -385,8 +385,11 @@ describe('exportGame (playable)', () => {playableAdProfile: { id: 'htmlcap', label: 'HTML cap', maxBytes: 5 * 1024 * 1024,maxHtmlBytes: 1, limitNote: 'HTML cap docs', delivery: 'zip' },});- expect(res.ok).toBe(false);- expect(res.errors.join('\n')).toMatch(/uncompressed HTML limit/);+ // The file is there and its size is on the screen; slimming it or shipping it is the developer's call, so this is not a failed build.+ expect(res.ok).toBe(true);+ expect(res.errors).toEqual([]);+ expect(res.warnings.join('\n')).toMatch(/index\.html is \d+ bytes, exceeding the uncompressed HTML limit/);+ expect(res.htmlBytes).toBeGreaterThan(1);}, 60_000);it('injects nothing and keeps the default cap with no network selected', async () => {diff --git a/pipeline/tests/playable-optimization.test.ts b/pipeline/tests/playable-optimization.test.tsindex f92edf1f6..fdb46d63c 100644--- a/pipeline/tests/playable-optimization.test.ts+++ b/pipeline/tests/playable-optimization.test.ts@@ -5,7 +5,8 @@ import { mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';import { execFileSync } from 'node:child_process';-import { optimizePlayableAssets, type PlayablePayload, type PlayableOptimizationAsset } from '../src/export/playableOptimization';+import { randomBytes } from 'node:crypto';+import { optimizePlayableAssets, playableAssetsJsonBytes, type PlayablePayload, type PlayableOptimizationAsset } from '../src/export/playableOptimization';import { makeZip } from '../../build-tools/utils/zip.js';import { noisePng } from './fixtures/solidPng.mjs';import { decodePngImage, encodeRgbaPng } from '../src/assets/atlasPacker';@@ -133,3 +134,39 @@ describe('playable network budget optimization', () => {}}, 60_000);});++describe('playable size is worked out, not rebuilt', () => {+ it('the arithmetic size of the assets object is the size JSON.stringify writes', () => {+ const maps: Array<Map<string, PlayablePayload>> = [+ new Map(),+ new Map([['@uuid:a', { data: new Uint8Array(0), mime: 'image/png' }]]),+ new Map([['@uuid:a', { data: new Uint8Array([1]), mime: 'image/png' }], ['@uuid:b', { data: new Uint8Array([1, 2]), mime: 'font/woff2' }],+ ['@uuid:c', { data: new Uint8Array(1000).fill(7), mime: 'image/webp' }], ['@uuid:汉字', { data: new Uint8Array(4097), mime: 'audio/mpeg' }]]),+ ];+ for (const m of maps) {+ const urls = Object.fromEntries([...m].map(([k, v]) => [k, `data:${v.mime};base64,${Buffer.from(v.data).toString('base64')}`]));+ expect(playableAssetsJsonBytes(m)).toBe(Buffer.byteLength(JSON.stringify(urls)));+ }+ });++ it('gives up the search at once when what cannot be shrunk is already over the target, and still reports the size', async () => {+ const font: PlayableOptimizationAsset = { key: 'font', path: 'font.woff2', data: new Uint8Array(randomBytes(300_000)), mime: 'font/woff2' };+ const warnings: string[] = []; const stages: string[] = [];+ const t0 = Date.now();+ const result = await run([font, image()], { targetDeliveryBytes: 100_000, warnings, onStage: (s: string) => stages.push(s) });+ expect(stages).toEqual([]);+ expect(result.report.changes).toEqual([]);+ expect(result.report.meetsTarget).toBe(false);+ expect(result.report.selected).toEqual(result.report.baseline);+ expect(result.payloads.get('image')!.mime).toBe('image/png');+ expect(warnings.some(w => /cannot reach the target/.test(w))).toBe(true);+ expect(Date.now() - t0).toBeLessThan(2000);+ });++ it('reports each stage it starts', async () => {+ const stages: string[] = [];+ await run([image()], { onStage: (s: string) => stages.push(s) });+ expect(stages[0]).toBe('lossless');+ expect(stages.length).toBeGreaterThan(1);+ }, 60_000);+});