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: data-driven toolchain packaging via manifest, reduce 665MB to 7MB
esengined86bc389561d
diff --git a/build-tools/toolchain/package.js b/build-tools/toolchain/package.jsindex 297b20637..7961aea7b 100644--- a/build-tools/toolchain/package.js+++ b/build-tools/toolchain/package.js@@ -1,7 +1,8 @@#!/usr/bin/env node/**- * Copies engine C++ source and cmake into Tauri resources for editor bundling.+ * Packages engine source and cmake into Tauri resources for editor bundling.+ * All paths and versions are driven by toolchain.manifest.json.** Usage: node package.js*/@@ -9,16 +10,12 @@import fs from 'fs';import path from 'path';import os from 'os';-import { pipeline } from 'stream/promises';-import { createReadStream } from 'fs';-import { createGunzip } from 'zlib';-import { mkdir, rm, cp, readdir, stat, writeFile, chmod } from 'fs/promises';+import { mkdir, rm, cp, readdir, stat, chmod } from 'fs/promises';import { execSync } from 'child_process';-const CMAKE_VERSION = '3.31.7';-const ROOT_DIR = path.resolve(import.meta.dirname, '../..');const OUTPUT_DIR = path.join(ROOT_DIR, 'desktop/src-tauri/toolchain');+const MANIFEST_PATH = path.join(ROOT_DIR, 'toolchain.manifest.json');function log(msg) {console.log(`[toolchain] ${msg}`);@@ -42,128 +39,147 @@ function formatSize(bytes) {return `${(bytes / 1024).toFixed(1)} KB`;}-async function main() {- log('Packaging engine source for editor bundling');+function loadManifest() {+ if (!fs.existsSync(MANIFEST_PATH)) {+ throw new Error(`Manifest not found: ${MANIFEST_PATH}`);+ }+ return JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));+}++function resolveTemplate(str, vars) {+ return str.replace(/\$\{(\w+)\}/g, (_, key) => vars[key] ?? '');+}+// =============================================================================+// Engine source packaging+// =============================================================================++async function packageEngineSource(manifest) {const engineSrcDir = path.join(OUTPUT_DIR, 'engine-src');if (fs.existsSync(engineSrcDir)) {await rm(engineSrcDir, { recursive: true, force: true });}await mkdir(engineSrcDir, { recursive: true });- // CMakeLists.txt- await cp(path.join(ROOT_DIR, 'CMakeLists.txt'), path.join(engineSrcDir, 'CMakeLists.txt'));+ const { engine, third_party } = manifest;- // cmake/ modules- await cp(path.join(ROOT_DIR, 'cmake'), path.join(engineSrcDir, 'cmake'), { recursive: true });- log(' copied: cmake/');+ // Root files (CMakeLists.txt etc.)+ for (const file of engine.root_files) {+ await cp(path.join(ROOT_DIR, file), path.join(engineSrcDir, file));+ }- // tools/ (EHT code generator)- await cp(path.join(ROOT_DIR, 'tools'), path.join(engineSrcDir, 'tools'), { recursive: true });- log(' copied: tools/');+ // Full directories+ for (const dir of engine.directories) {+ const src = path.join(ROOT_DIR, dir);+ const dest = path.join(engineSrcDir, dir);+ if (fs.existsSync(src)) {+ await mkdir(path.dirname(dest), { recursive: true });+ await cp(src, dest, { recursive: true });+ log(` copied: ${dir}/`);+ }+ }- // src/esengine/- await mkdir(path.join(engineSrcDir, 'src'), { recursive: true });- await cp(path.join(ROOT_DIR, 'src/esengine'), path.join(engineSrcDir, 'src/esengine'), { recursive: true });- log(' copied: src/esengine/');+ // Third-party+ const tpDest = path.join(engineSrcDir, 'third_party');+ await mkdir(tpDest, { recursive: true });- // third_party/- await mkdir(path.join(engineSrcDir, 'third_party'), { recursive: true });- const thirdPartyCmake = path.join(ROOT_DIR, 'third_party', 'CMakeLists.txt');- if (fs.existsSync(thirdPartyCmake)) {- await cp(thirdPartyCmake, path.join(engineSrcDir, 'third_party', 'CMakeLists.txt'));+ for (const file of third_party.root_files) {+ const src = path.join(ROOT_DIR, 'third_party', file);+ if (fs.existsSync(src)) {+ await cp(src, path.join(tpDest, file));+ }}- const thirdPartyDirs = [- 'glm', 'cute_tiled', 'stb',- 'spine-runtimes-3.8', 'spine-runtimes-4.1', 'spine-runtimes-4.2',- 'nlohmann',- ];- for (const dir of thirdPartyDirs) {- const srcDir = path.join(ROOT_DIR, 'third_party', dir);- if (fs.existsSync(srcDir)) {- await cp(srcDir, path.join(engineSrcDir, 'third_party', dir), { recursive: true });++ // Full third-party copies+ for (const dir of third_party.full) {+ const src = path.join(ROOT_DIR, 'third_party', dir);+ if (fs.existsSync(src)) {+ await cp(src, path.join(tpDest, dir), { recursive: true });log(` copied: third_party/${dir}`);}}+ // Partial third-party copies (only specified subdirs)+ for (const [lib, subdirs] of Object.entries(third_party.partial)) {+ const libSrc = path.join(ROOT_DIR, 'third_party', lib);+ if (!fs.existsSync(libSrc)) continue;++ for (const subdir of subdirs) {+ const src = path.join(libSrc, subdir);+ const dest = path.join(tpDest, lib, subdir);+ if (fs.existsSync(src)) {+ await mkdir(path.dirname(dest), { recursive: true });+ await cp(src, dest, { recursive: true });+ }+ }+ log(` copied: third_party/${lib} (partial: ${subdirs.length} dirs)`);+ }+const size = await dirSize(engineSrcDir);log(`Engine source: ${formatSize(size)}`);-- // Download and bundle cmake- await downloadCmake();-- const totalSize = await dirSize(OUTPUT_DIR);- log(`\nDone! Total toolchain: ${formatSize(totalSize)}`);- log(`Output: ${OUTPUT_DIR}`);}-function getCmakeDownloadInfo() {+// =============================================================================+// CMake packaging+// =============================================================================++function getCmakePlatformKey() {const platform = os.platform();+ if (platform === 'darwin') return 'darwin';+ if (platform === 'win32') return 'win32';const arch = os.arch();-- if (platform === 'darwin') {- return {- filename: `cmake-${CMAKE_VERSION}-macos-universal.tar.gz`,- stripPrefix: `cmake-${CMAKE_VERSION}-macos-universal/CMake.app/Contents`,- format: 'tar.gz',- };- } else if (platform === 'win32') {- return {- filename: `cmake-${CMAKE_VERSION}-windows-x86_64.zip`,- stripPrefix: `cmake-${CMAKE_VERSION}-windows-x86_64`,- format: 'zip',- };- } else {- const linuxArch = arch === 'arm64' ? 'aarch64' : 'x86_64';- return {- filename: `cmake-${CMAKE_VERSION}-linux-${linuxArch}.tar.gz`,- stripPrefix: `cmake-${CMAKE_VERSION}-linux-${linuxArch}`,- format: 'tar.gz',- };- }+ return arch === 'arm64' ? 'linux-arm64' : 'linux-x64';}-async function downloadCmake() {+async function packageCmake(manifest) {const cmakeDir = path.join(OUTPUT_DIR, 'cmake');- const cmakeBin = path.join(cmakeDir, 'bin', os.platform() === 'win32' ? 'cmake.exe' : 'cmake');+ const cmakeExeName = os.platform() === 'win32' ? 'cmake.exe' : 'cmake';+ const cmakeBin = path.join(cmakeDir, 'bin', cmakeExeName);if (fs.existsSync(cmakeBin)) {log(`cmake already present at ${cmakeDir}`);return;}- const info = getCmakeDownloadInfo();- const url = `https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${info.filename}`;+ const { version, platforms } = manifest.cmake;+ const platformKey = getCmakePlatformKey();+ const platformInfo = platforms[platformKey];+ if (!platformInfo) {+ throw new Error(`No cmake config for platform: ${platformKey}`);+ }- log(`Downloading cmake ${CMAKE_VERSION}...`);+ const vars = { version };+ const filename = resolveTemplate(platformInfo.filename, vars);+ const stripPrefix = resolveTemplate(platformInfo.strip_prefix, vars);+ const url = `https://github.com/Kitware/CMake/releases/download/v${version}/${filename}`;++ log(`Downloading cmake ${version}...`);log(` URL: ${url}`);const tmpDir = path.join(os.tmpdir(), `cmake-download-${Date.now()}`);await mkdir(tmpDir, { recursive: true });- const archivePath = path.join(tmpDir, info.filename);+ const archivePath = path.join(tmpDir, filename);try {- execSync(`curl -L -o "${archivePath}" "${url}"`, { stdio: 'inherit' });+ execSync(`curl -sL -o "${archivePath}" "${url}"`, { stdio: 'inherit' });log('Extracting cmake...');if (fs.existsSync(cmakeDir)) {await rm(cmakeDir, { recursive: true, force: true });}- await mkdir(cmakeDir, { recursive: true });+ await mkdir(path.join(cmakeDir, 'bin'), { recursive: true });- if (info.format === 'tar.gz') {- execSync(`tar xzf "${archivePath}" -C "${tmpDir}"`, { stdio: 'inherit' });- } else {+ const isZip = filename.endsWith('.zip');+ if (isZip) {execSync(`unzip -q "${archivePath}" -d "${tmpDir}"`, { stdio: 'inherit' });+ } else {+ execSync(`tar xzf "${archivePath}" -C "${tmpDir}"`, { stdio: 'inherit' });}- const extractedRoot = path.join(tmpDir, info.stripPrefix);-- // Copy only bin/cmake and share/cmake-* (minimal set)- await mkdir(path.join(cmakeDir, 'bin'), { recursive: true });+ const extractedRoot = path.join(tmpDir, stripPrefix);- const cmakeExeName = os.platform() === 'win32' ? 'cmake.exe' : 'cmake';+ // Copy cmake binaryawait cp(path.join(extractedRoot, 'bin', cmakeExeName),path.join(cmakeDir, 'bin', cmakeExeName),@@ -172,7 +188,7 @@ async function downloadCmake() {await chmod(path.join(cmakeDir, 'bin', 'cmake'), 0o755);}- // Find the share/cmake-* directory (e.g., share/cmake-3.31)+ // Copy share/cmake-* directoryconst shareDir = path.join(extractedRoot, 'share');const shareEntries = await readdir(shareDir);const cmakeShareDir = shareEntries.find(e => e.startsWith('cmake-'));@@ -186,7 +202,7 @@ async function downloadCmake() {);// Verify- const testResult = execSync(`"${path.join(cmakeDir, 'bin', cmakeExeName)}" --version`, { encoding: 'utf8' });+ const testResult = execSync(`"${cmakeBin}" --version`, { encoding: 'utf8' });log(` cmake installed: ${testResult.split('\n')[0]}`);const cmakeSize = await dirSize(cmakeDir);@@ -196,6 +212,22 @@ async function downloadCmake() {}}+// =============================================================================+// Main+// =============================================================================++async function main() {+ log('Packaging toolchain for editor bundling');+ const manifest = loadManifest();++ await packageEngineSource(manifest);+ await packageCmake(manifest);++ const totalSize = await dirSize(OUTPUT_DIR);+ log(`\nDone! Total toolchain: ${formatSize(totalSize)}`);+ log(`Output: ${OUTPUT_DIR}`);+}+main().catch(err => {console.error(`[toolchain] ERROR: ${err.message}`);process.exit(1);diff --git a/toolchain.manifest.json b/toolchain.manifest.jsonnew file mode 100644index 000000000..8450613d4--- /dev/null+++ b/toolchain.manifest.json@@ -0,0 +1,49 @@+{+ "$schema": "Declares which files to bundle into the editor toolchain for WASM compilation.",+ "engine": {+ "root_files": ["CMakeLists.txt"],+ "directories": [+ "cmake",+ "tools",+ "src/esengine"+ ]+ },+ "third_party": {+ "root_files": ["CMakeLists.txt"],+ "full": [+ "stb",+ "nlohmann",+ "cute_tiled"+ ],+ "partial": {+ "glm": ["glm"],+ "spine-runtimes-3.8": ["spine-c/spine-c/src", "spine-c/spine-c/include"],+ "spine-runtimes-4.1": ["spine-c/spine-c/src", "spine-c/spine-c/include"],+ "spine-runtimes-4.2": [+ "spine-cpp/spine-cpp/src", "spine-cpp/spine-cpp/include",+ "spine-c/spine-c/src", "spine-c/spine-c/include"+ ]+ }+ },+ "cmake": {+ "version": "3.31.7",+ "platforms": {+ "darwin": {+ "filename": "cmake-${version}-macos-universal.tar.gz",+ "strip_prefix": "cmake-${version}-macos-universal/CMake.app/Contents"+ },+ "win32": {+ "filename": "cmake-${version}-windows-x86_64.zip",+ "strip_prefix": "cmake-${version}-windows-x86_64"+ },+ "linux-x64": {+ "filename": "cmake-${version}-linux-x86_64.tar.gz",+ "strip_prefix": "cmake-${version}-linux-x86_64"+ },+ "linux-arm64": {+ "filename": "cmake-${version}-linux-aarch64.tar.gz",+ "strip_prefix": "cmake-${version}-linux-aarch64"+ }+ }+ }+}