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
refactor(build): one desktop assembler, not one per OS
esengine510d3a690d04
diff --git a/build-tools/tasks/native.js b/build-tools/tasks/native.jsindex 74ef252d4..16130a882 100644--- a/build-tools/tasks/native.js+++ b/build-tools/tasks/native.js@@ -25,7 +25,7 @@ import {} from '../utils/nativeTemplate.js';import { readAppConfig, fillTemplate, iosInterfaceOrientations } from '../utils/nativeApp.js';import { emitIosXcodeProject } from '../utils/iosProject.js';-import { assembleMacApp } from '../utils/desktopApp.js';+import { assembleDesktopApp } from '../utils/desktopApp.js';import { assembleApk, apkFileName } from '../utils/apk.js';import { assembleAab, aabFileName } from '../utils/aab.js';import { debugSigningKey, signingKeyFromPem } from '../utils/androidKeystore.js';@@ -685,15 +685,17 @@ function requireTemplate(platform) {* template stayed on the machine that built it.*/async function packageDesktopApp(options) {- const template = requireTemplate('macos');+ const platform = hostDesktopTarget();+ const template = requireTemplate(platform);const contentDir = packagedContent(options);const app = readAppConfig(contentDir, (m) => logger.warn(m));const outDir = options.out? (path.isAbsolute(options.out) ? options.out : path.join(config.paths.root, options.out)): path.dirname(contentDir);- logger.step(`Assembling ${app.name}.app...`);- const bundle = await assembleMacApp({+ logger.step(`Assembling ${app.name} for ${platform}...`);+ const bundle = await assembleDesktopApp({+ platform,templateDir: template.dir,contentDir,outDir,@@ -703,8 +705,8 @@ async function packageDesktopApp(options) {signIdentity: options.signIdentity,warn: (m) => logger.warn(m),});- logger.success(`macOS app: ${path.relative(config.paths.root, bundle) || bundle}`);- logger.info(`Run it with: open "${bundle}"`);+ logger.success(`${platform} app: ${path.relative(config.paths.root, bundle) || bundle}`);+ logger.info(platform === 'macos' ? `Run it with: open "${bundle}"` : `Run it: ${bundle}`);return bundle;}diff --git a/build-tools/utils/desktopApp.d.ts b/build-tools/utils/desktopApp.d.tsindex 87a4d49b1..b50158836 100644--- a/build-tools/utils/desktopApp.d.ts+++ b/build-tools/utils/desktopApp.d.ts@@ -10,12 +10,14 @@ export interface DesktopAppIdentity {versionCode: number;}-export function assembleMacApp(options: {- /** An installed macos runtime template. */+export function assembleDesktopApp(options: {+ /** Which desktop OS the app is for; also which template layout is read. */+ platform: 'macos' | 'windows';+ /** An installed runtime template for that OS. */templateDir: string;/** The export: cooked content plus the two configs. */contentDir: string;- /** Where `<name>.app` is written. */+ /** Where the app directory is written. */outDir: string;app: DesktopAppIdentity;/** Square PNG; the template's mark is used when absent. */diff --git a/build-tools/utils/desktopApp.js b/build-tools/utils/desktopApp.jsindex 8b706dbbb..80f60e09b 100644--- a/build-tools/utils/desktopApp.js+++ b/build-tools/utils/desktopApp.js@@ -1,6 +1,5 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team-///*** @file The desktop assembler: a runtime template plus an editor export, out the* other side a thing you can double-click.@@ -9,6 +8,10 @@* §3) — the toolchain stays on the machine that built the template and this step* is pure Node. Desktop had no such step at all: it wrote an npm project and* asked the user to install electron-builder.+ *+ * ONE function for every desktop OS, because they differ in less than they share:+ * a directory named for the app, the runtime renamed to it, one asset namespace,+ * an icon. Only the shape of that directory and how it is signed are per-OS.*/import path from 'path';@@ -22,24 +25,49 @@ import { fillTemplate } from './nativeApp.js';const PLIST_DEFAULT_MIN = '11.0';/**- * Assemble `<out>/<name>.app` from @p template and @p contentDir.+ * Where each OS puts the pieces, relative to the directory the assembler produces.+ *+ * `root` is that directory's own name — it is what the Steam depot maps and what+ * a player drags, so it is the app's name on both.+ */+const LAYOUT = {+ macos: {+ root: (name) => `${name}.app`,+ executable: (name) => path.join('Contents', 'MacOS', name),+ content: path.join('Contents', 'Resources', 'Content'),+ beside: path.join('Contents', 'Resources'),+ },+ windows: {+ root: (name) => name,+ executable: (name) => `${name}.exe`,+ content: 'Content',+ beside: '.',+ },+};++/**+ * Assemble the app for @p platform from a runtime template and an export.*- * The bundle's Resources hold ONE asset namespace — the game's exported files- * plus the runtime's precompiled bytecode — because that is what the host reads+ * The app's asset directory holds ONE namespace — the game's exported files plus+ * the runtime's precompiled bytecode — because that is what the host reads* through Platform::readAsset, exactly as the APK's assets/ is one namespace.** @param {object} options+ * @param {'macos'|'windows'} options.platform* @param {string} options.templateDir Installed runtime template.* @param {string} options.contentDir The editor export to ship.- * @param {string} options.outDir Where the .app is written.+ * @param {string} options.outDir Where the app directory is written.* @param {{id: string, name: string, version: string, versionCode: number}} options.app* @param {string} [options.iconPng] Project icon; the template's is used otherwise.* @param {string} [options.macosMin] LSMinimumSystemVersion.- * @returns {Promise<string>} the bundle's path.+ * @returns {Promise<string>} the app directory's path.*/-export async function assembleMacApp(options) {- const { templateDir, contentDir, outDir, app } = options;- const sources = desktopTemplateSources(templateDir);+export async function assembleDesktopApp(options) {+ const { platform, templateDir, contentDir, outDir, app } = options;+ const layout = LAYOUT[platform];+ if (!layout) throw new Error(`no desktop layout for "${platform}" (macos, windows)`);++ const sources = desktopTemplateSources(templateDir, platform);if (!existsSync(sources.executable)) {throw new Error(`runtime template has no executable at ${sources.executable}`);}@@ -47,49 +75,54 @@ export async function assembleMacApp(options) {throw new Error(`${contentDir} is not an editor export (no game.config.json)`);}- // The bundle is named by the app, and so is the executable inside it: on+ // The directory is named by the app, and so is the executable inside it: on// desktop the executable IS the identity, which is why the host reads its own// name rather than parsing a config (see desktop.cpp appName).- const bundle = path.join(outDir, `${app.name}.app`);- await rm(bundle, { recursive: true, force: true });- const macos = path.join(bundle, 'Contents', 'MacOS');- const resources = path.join(bundle, 'Contents', 'Resources');- await mkdir(macos, { recursive: true });- await mkdir(resources, { recursive: true });+ const root = path.join(outDir, layout.root(app.name));+ await rm(root, { recursive: true, force: true });- const executable = path.join(macos, app.name);+ const executable = path.join(root, layout.executable(app.name));+ await mkdir(path.dirname(executable), { recursive: true });await cp(sources.executable, executable);await chmod(executable, 0o755);- // Entry by entry: the bundle usually lands INSIDE the export directory (the- // layout Android has), and copying a directory into itself is refused before- // any filter is consulted. Bundles are skipped as not-content.- const content = path.join(resources, 'Content');+ // Entry by entry: the app directory usually lands INSIDE the export directory+ // (the layout Android has), and copying a directory into itself is refused+ // before any filter is consulted. Previous packages are skipped as not-content.+ const content = path.join(root, layout.content);await mkdir(content, { recursive: true });+ const packaged = new Set(Object.values(LAYOUT).map((l) => l.root(app.name)));for (const entry of await readdir(contentDir)) {- if (entry.endsWith('.app')) continue;+ if (entry.endsWith('.app') || packaged.has(entry)) continue;await cp(path.join(contentDir, entry), path.join(content, entry), { recursive: true });}if (existsSync(sources.bytecode)) {await cp(sources.bytecode, path.join(content, path.basename(sources.bytecode)));}- const iconPng = options.iconPng && existsSync(options.iconPng) ? options.iconPng : sources.icon;- if (existsSync(iconPng)) {- await writeFile(path.join(resources, 'AppIcon.icns'), pngToIcns(await readFile(iconPng)));+ // Whatever else the runtime needs beside the executable. On Windows that is+ // the HLSL compiler Dawn loads before it can create a device at all.+ if (platform === 'windows' && existsSync(sources.d3dCompiler)) {+ await cp(sources.d3dCompiler, path.join(root, path.basename(sources.d3dCompiler)));}- const template = await readFile(sources.infoPlistIn, 'utf8');- await writeFile(path.join(bundle, 'Contents', 'Info.plist'), fillTemplate(template, {- APP_NAME: app.name,- APP_ID: app.id,- VERSION_NAME: app.version,- VERSION_CODE: String(app.versionCode),- MACOS_MIN: options.macosMin || PLIST_DEFAULT_MIN,- }));+ const iconPng = options.iconPng && existsSync(options.iconPng) ? options.iconPng : sources.icon;+ if (platform === 'macos' && existsSync(iconPng)) {+ await writeFile(path.join(root, layout.beside, 'AppIcon.icns'), pngToIcns(await readFile(iconPng)));+ }- await signBundle(bundle, options);- return bundle;+ if (platform === 'macos') {+ const template = await readFile(sources.infoPlistIn, 'utf8');+ await writeFile(path.join(root, 'Contents', 'Info.plist'), fillTemplate(template, {+ APP_NAME: app.name,+ APP_ID: app.id,+ VERSION_NAME: app.version,+ VERSION_CODE: String(app.versionCode),+ MACOS_MIN: options.macosMin || PLIST_DEFAULT_MIN,+ }));+ await signBundle(root, options);+ }+ return root;}/**diff --git a/desktop/electron/exportGame.ts b/desktop/electron/exportGame.tsindex 06a53bd78..1a75bab7d 100644--- a/desktop/electron/exportGame.ts+++ b/desktop/electron/exportGame.ts@@ -46,7 +46,7 @@ import { emitAndroidGradleProject } from '../../build-tools/utils/gradleProject.import { androidTemplateSources } from '../../build-tools/utils/nativeTemplate.js';import { assembleApk, apkFileName } from '../../build-tools/utils/apk.js';import { assembleAab, aabFileName } from '../../build-tools/utils/aab.js';-import { assembleMacApp } from '../../build-tools/utils/desktopApp.js';+import { assembleDesktopApp } from '../../build-tools/utils/desktopApp.js';import { emitSteamBuild, defaultDepotId } from '../../build-tools/utils/steamChannel.js';import { debugSigningKey, type SigningKey } from '../../build-tools/utils/androidKeystore.js';import { isNativePlatform, type ExportPlatform } from '../src/project/platforms';@@ -726,9 +726,10 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>warnings.push('No desktop runtime template is installed for this editor version, so no app '+ 'was assembled — the content is here. Install one from the Desktop row in Package '+ 'Project, then export again.');- } else if (process.platform === 'darwin') {- progress({ phase: 'Assembling the app bundle' });- appBundle = await assembleMacApp({+ } else if (process.platform === 'darwin' || process.platform === 'win32') {+ progress({ phase: 'Assembling the app' });+ appBundle = await assembleDesktopApp({+ platform: process.platform === 'darwin' ? 'macos' : 'windows',templateDir: template, contentDir: absOut, outDir: absOut, app: appConfig,iconPng: opts.appIcon ? path.join(opts.root, opts.appIcon) : undefined,warn: (m: string) => warnings.push(m),diff --git a/desktop/tests/desktop-app.test.ts b/desktop/tests/desktop-app.test.tsindex d0bc212a8..2e51127eb 100644--- a/desktop/tests/desktop-app.test.ts+++ b/desktop/tests/desktop-app.test.ts@@ -14,7 +14,7 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';import { mkdtempSync, rmSync, mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';-import { assembleMacApp } from '../../build-tools/utils/desktopApp.js';+import { assembleDesktopApp } from '../../build-tools/utils/desktopApp.js';import { pngToIcns, pngSize } from '../../build-tools/utils/icns.js';import { templateLayout, desktopTemplateSources } from '../../build-tools/utils/nativeTemplate.js';@@ -71,7 +71,7 @@ afterEach(() => { rmSync(dir, { recursive: true, force: true }); });describe('macOS app assembly', () => {it('produces a bundle named and executed by the app, not by the runtime', async () => {- const bundle = await assembleMacApp({ templateDir, contentDir, outDir, app: APP });+ const bundle = await assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP });expect(bundle).toBe(path.join(outDir, 'Acme Game.app'));// The executable carries the app's name because on desktop the executable// IS the identity — the host reads its own argv[0] rather than a config.@@ -80,7 +80,7 @@ describe('macOS app assembly', () => {});it('puts the runtime bytecode in the SAME asset namespace as the game', async () => {- const bundle = await assembleMacApp({ templateDir, contentDir, outDir, app: APP });+ const bundle = await assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP });const content = path.join(bundle, 'Contents/Resources/Content');expect(readFileSync(path.join(content, 'game.config.json'), 'utf8')).toContain('entryScene');// Beside the game's files, since that is the one path readAsset resolves.@@ -89,8 +89,8 @@ describe('macOS app assembly', () => {});it('fills the identity into Info.plist', async () => {- const bundle = await assembleMacApp({- templateDir, contentDir, outDir, app: APP, macosMin: '12.3',+ const bundle = await assembleDesktopApp({+ platform: 'macos', templateDir, contentDir, outDir, app: APP, macosMin: '12.3',});const plist = readFileSync(path.join(bundle, 'Contents/Info.plist'), 'utf8');expect(plist).toContain('<string>Acme Game</string>');@@ -102,24 +102,64 @@ describe('macOS app assembly', () => {it('refuses a content dir that is not an export', async () => {rmSync(path.join(contentDir, 'game.config.json'));- await expect(assembleMacApp({ templateDir, contentDir, outDir, app: APP }))+ await expect(assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP })).rejects.toThrow(/not an editor export/);});it('refuses a template with no runtime in it', async () => {rmSync(desktopTemplateSources(templateDir).executable);- await expect(assembleMacApp({ templateDir, contentDir, outDir, app: APP }))+ await expect(assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP })).rejects.toThrow(/no executable/);});it('reassembling replaces the bundle rather than merging into it', async () => {- const bundle = await assembleMacApp({ templateDir, contentDir, outDir, app: APP });+ const bundle = await assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP });writeFileSync(path.join(bundle, 'Contents/Resources/Content/stale.txt'), 'x');- await assembleMacApp({ templateDir, contentDir, outDir, app: APP });+ await assembleDesktopApp({ platform: 'macos', templateDir, contentDir, outDir, app: APP });expect(existsSync(path.join(bundle, 'Contents/Resources/Content/stale.txt'))).toBe(false);});});+describe('Windows assembly', () => {+ beforeEach(() => {+ // The Windows template names its runtime .exe and carries the HLSL compiler.+ writeFileSync(path.join(templateDir, 'estella_desktop.exe'), 'runtime');+ writeFileSync(path.join(templateDir, 'd3dcompiler_47.dll'), 'compiler');+ });++ it('lays the app out as a directory the depot can map whole', async () => {+ const root = await assembleDesktopApp({+ platform: 'windows', templateDir, contentDir, outDir, app: APP,+ });+ expect(root).toBe(path.join(outDir, 'Acme Game'));+ expect(existsSync(path.join(root, 'Acme Game.exe'))).toBe(true);+ expect(existsSync(path.join(root, 'Content', 'game.config.json'))).toBe(true);+ // One namespace, as on macOS: the runtime's bytecode joins the game's files.+ expect(existsSync(path.join(root, 'Content', 'esengine.native.qjsbc'))).toBe(true);+ });++ it('ships the HLSL compiler, without which Dawn creates no device at all', async () => {+ const root = await assembleDesktopApp({+ platform: 'windows', templateDir, contentDir, outDir, app: APP,+ });+ expect(existsSync(path.join(root, 'd3dcompiler_47.dll'))).toBe(true);+ });++ it('writes no Info.plist and no icns — those describe a bundle', async () => {+ const root = await assembleDesktopApp({+ platform: 'windows', templateDir, contentDir, outDir, app: APP,+ });+ expect(existsSync(path.join(root, 'Info.plist'))).toBe(false);+ expect(existsSync(path.join(root, 'AppIcon.icns'))).toBe(false);+ });++ it('refuses a platform with no layout', async () => {+ await expect(assembleDesktopApp({+ platform: 'linux' as 'windows', templateDir, contentDir, outDir, app: APP,+ })).rejects.toThrow(/no desktop layout/);+ });+});+describe('the icon container', () => {it('writes a slot matching the icon and a length covering the file', () => {const icns = pngToIcns(png(1024));