Skip to content

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.

fix: replace shell zip with pure Node.js implementation for Windows CI

esengine681c87e2d377
diff --git a/build-tools/tasks/examples.js b/build-tools/tasks/examples.js
index 8aa870f00..3f7a7b1cd 100644
--- a/build-tools/tasks/examples.js
+++ b/build-tools/tasks/examples.js
@@ -3,9 +3,9 @@
* @brief Pack example projects into zip files for editor
*/
-import { execSync } from 'child_process';
-import { existsSync, mkdirSync } from 'fs';
+import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from 'fs';
import path from 'path';
+import { deflateRawSync } from 'zlib';
import * as logger from '../utils/logger.js';
const EXAMPLES = [
@@ -14,6 +14,111 @@ const EXAMPLES = [
const OUTPUT_DIR = 'desktop/public/examples';
+const EXCLUDE_PATTERNS = [
+ '.DS_Store',
+ 'node_modules',
+ 'dist',
+ '.esengine/cache',
+ 'tools',
+ '__pycache__',
+];
+
+function shouldExclude(relativePath) {
+ for (const pattern of EXCLUDE_PATTERNS) {
+ if (relativePath === pattern || relativePath.startsWith(pattern + '/') ||
+ relativePath.includes('/' + pattern + '/') || relativePath.endsWith('/' + pattern)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+function collectFiles(dir, baseDir) {
+ const results = [];
+ for (const entry of readdirSync(dir)) {
+ const fullPath = path.join(dir, entry);
+ const relativePath = path.relative(baseDir, fullPath).split(path.sep).join('/');
+ if (shouldExclude(relativePath)) continue;
+ const stat = statSync(fullPath);
+ if (stat.isDirectory()) {
+ results.push(...collectFiles(fullPath, baseDir));
+ } else {
+ results.push({ fullPath, relativePath });
+ }
+ }
+ return results;
+}
+
+function crc32(buf) {
+ let crc = 0xffffffff;
+ for (let i = 0; i < buf.length; i++) {
+ crc ^= buf[i];
+ for (let j = 0; j < 8; j++) {
+ crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
+ }
+ }
+ return (crc ^ 0xffffffff) >>> 0;
+}
+
+function createZip(srcDir, outputPath) {
+ const files = collectFiles(srcDir, srcDir);
+ const localParts = [];
+ const centralEntries = [];
+ let offset = 0;
+
+ for (const file of files) {
+ const content = readFileSync(file.fullPath);
+ const compressed = deflateRawSync(content);
+ const nameBytes = Buffer.from(file.relativePath, 'utf8');
+ const crc = crc32(content);
+
+ const local = Buffer.alloc(30);
+ local.writeUInt32LE(0x04034b50, 0);
+ local.writeUInt16LE(20, 4);
+ local.writeUInt16LE(0, 6);
+ local.writeUInt16LE(8, 8);
+ local.writeUInt16LE(0, 10);
+ local.writeUInt16LE(0, 12);
+ local.writeUInt32LE(crc, 14);
+ local.writeUInt32LE(compressed.length, 18);
+ local.writeUInt32LE(content.length, 22);
+ local.writeUInt16LE(nameBytes.length, 26);
+ local.writeUInt16LE(0, 28);
+
+ localParts.push(local, nameBytes, compressed);
+
+ const central = Buffer.alloc(46);
+ central.writeUInt32LE(0x02014b50, 0);
+ central.writeUInt16LE(20, 4);
+ central.writeUInt16LE(20, 6);
+ central.writeUInt16LE(0, 8);
+ central.writeUInt16LE(8, 10);
+ central.writeUInt16LE(0, 12);
+ central.writeUInt16LE(0, 14);
+ central.writeUInt32LE(crc, 16);
+ central.writeUInt32LE(compressed.length, 20);
+ central.writeUInt32LE(content.length, 24);
+ central.writeUInt16LE(nameBytes.length, 28);
+ central.writeUInt32LE(offset, 42);
+
+ centralEntries.push(central, nameBytes);
+ offset += 30 + nameBytes.length + compressed.length;
+ }
+
+ const centralDirOffset = offset;
+ let centralDirSize = 0;
+ for (const buf of centralEntries) centralDirSize += buf.length;
+
+ const eocd = Buffer.alloc(22);
+ eocd.writeUInt32LE(0x06054b50, 0);
+ eocd.writeUInt16LE(files.length, 8);
+ eocd.writeUInt16LE(files.length, 10);
+ eocd.writeUInt32LE(centralDirSize, 12);
+ eocd.writeUInt32LE(centralDirOffset, 16);
+
+ writeFileSync(outputPath, Buffer.concat([...localParts, ...centralEntries, eocd]));
+}
+
export async function zipExamples(rootDir) {
logger.step('Packing example projects...');
@@ -31,20 +136,8 @@ export async function zipExamples(rootDir) {
continue;
}
- const excludes = [
- '-x', '*.DS_Store',
- '-x', '*node_modules/*',
- '-x', '*dist/*',
- '-x', '*.esengine/cache/*',
- '-x', '*tools/*',
- '-x', '*__pycache__/*',
- ];
-
try {
- execSync(
- `cd "${srcDir}" && zip -r "${path.resolve(zipPath)}" . ${excludes.join(' ')}`,
- { stdio: 'pipe' }
- );
+ createZip(srcDir, zipPath);
logger.success(`${example.name}.zip`);
} catch (err) {
logger.error(`Failed to zip ${example.name}: ${err.message}`);