规范为cococs wasm

This commit is contained in:
YHH
2025-06-09 18:00:50 +08:00
parent 757eff2937
commit f6250b6d5b
9 changed files with 459 additions and 1017 deletions

View File

@@ -2,46 +2,23 @@ const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
console.log('🚀 构建WASM发布包...');
console.log('🚀 构建 WASM 发布包(支持 Cocos Creator...');
async function main() {
try {
// 确保WASM已构建
if (!fs.existsSync('./bin/wasm')) {
console.log('📦 构建WASM...');
execSync('npm run build:wasm', { stdio: 'inherit' });
}
// 构建 Cocos Creator 版本
console.log('🎮 构建 Cocos Creator 版本...');
await buildCocosVersion();
// 创建发布目录
const releaseDir = './wasm-release';
if (fs.existsSync(releaseDir)) {
execSync(`rimraf ${releaseDir}`, { stdio: 'inherit' });
}
fs.mkdirSync(releaseDir);
// 构建通用版本(保持兼容性)
console.log('🌐 构建通用版本...');
await buildUniversalVersion();
// 复制WASM文件
console.log('📁 复制WASM文件...');
const wasmDir = './bin/wasm';
fs.readdirSync(wasmDir).forEach(file => {
if (file !== '.gitignore') {
fs.copyFileSync(
path.join(wasmDir, file),
path.join(releaseDir, file)
);
console.log(`${file}`);
}
});
// 创建使用说明
console.log('📋 生成使用说明...');
generateUsageInstructions(releaseDir);
// 显示结果
showReleaseResults(releaseDir);
console.log('\n✅ WASM发布包构建完成');
console.log(`📦 发布目录: ${releaseDir}`);
console.log('💡 可以将整个目录打包为zip文件上传到GitHub Release');
console.log('\n✅ WASM 发布包构建完成!');
console.log('📦 输出目录:');
console.log(' - cocos-release/ (Cocos Creator 专用包)');
console.log(' - wasm-release/ (通用包)');
console.log('💡 可以将整个目录打包为 zip 文件上传到 GitHub Release');
} catch (error) {
console.error('❌ 构建失败:', error.message);
@@ -49,6 +26,155 @@ async function main() {
}
}
async function buildCocosVersion() {
// 确保 Cocos 版本已构建
if (!fs.existsSync('./cocos-pkg')) {
console.log('📦 构建 Cocos Creator WASM...');
execSync('scripts\\build-cocos.bat', { stdio: 'inherit' });
}
// 创建 Cocos 发布目录
const releaseDir = './cocos-release';
if (fs.existsSync(releaseDir)) {
execSync(`rimraf ${releaseDir}`, { stdio: 'inherit' });
}
fs.mkdirSync(releaseDir);
// 复制 Cocos 包文件
console.log('📁 复制 Cocos Creator 文件...');
const cocosDir = './cocos-pkg';
fs.readdirSync(cocosDir).forEach(file => {
fs.copyFileSync(
path.join(cocosDir, file),
path.join(releaseDir, file)
);
console.log(`${file}`);
});
// 生成 Cocos 包信息
console.log('📋 生成 Cocos Creator 包信息...');
generateCocosPackageInfo(releaseDir);
// 复制 build-templates 示例
generateBuildTemplate(releaseDir);
// 显示结果
showReleaseResults(releaseDir, 'Cocos Creator');
}
async function buildUniversalVersion() {
// 确保通用 WASM 已构建
if (!fs.existsSync('./bin/wasm')) {
console.log('📦 构建通用 WASM...');
execSync('npm run build:wasm', { stdio: 'inherit' });
}
// 创建通用发布目录
const releaseDir = './wasm-release';
if (fs.existsSync(releaseDir)) {
execSync(`rimraf ${releaseDir}`, { stdio: 'inherit' });
}
fs.mkdirSync(releaseDir);
// 复制通用 WASM 文件
console.log('📁 复制通用 WASM 文件...');
const wasmDir = './bin/wasm';
fs.readdirSync(wasmDir).forEach(file => {
if (file !== '.gitignore') {
fs.copyFileSync(
path.join(wasmDir, file),
path.join(releaseDir, file)
);
console.log(`${file}`);
}
});
// 创建使用说明
console.log('📋 生成通用使用说明...');
generateUsageInstructions(releaseDir);
// 显示结果
showReleaseResults(releaseDir, '通用');
}
function generateCocosPackageInfo(releaseDir) {
const packageInfo = {
name: "@esengine/ecs-framework-wasm-cocos",
version: "1.0.0",
description: "ECS Framework WASM 加速模块 - Cocos Creator 专用版",
main: "ecs_wasm_core.ts",
files: [
"ecs_wasm_core.ts",
"ecs_wasm_core_bg.bin",
"ecs_wasm_core_bg.wasm",
"*.d.ts",
"README.md",
"build-templates/"
],
keywords: ["ecs", "wasm", "cocos-creator", "game-engine"],
author: "ESEngine Team",
license: "MIT",
peerDependencies: {
"@esengine/ecs-framework": "^1.0.0"
},
engines: {
"cocos-creator": ">=3.8.0"
}
};
fs.writeFileSync(
path.join(releaseDir, 'package.json'),
JSON.stringify(packageInfo, null, 2)
);
}
function generateBuildTemplate(releaseDir) {
const templateDir = path.join(releaseDir, 'build-templates', 'wechatgame');
fs.mkdirSync(templateDir, { recursive: true });
const buildScript = `/**
* 微信小游戏 build-templates 脚本
* 自动将 WASM 文件复制到发布目录
*
* 使用方法:
* 1. 将此文件复制到项目的 build-templates/wechatgame/ 目录
* 2. 重命名为 build-finish.js
* 3. 构建微信小游戏时会自动执行
*/
const fs = require('fs');
const path = require('path');
function copyWasmFiles(options, callback) {
const { buildPath } = options;
console.log('🎮 复制 ECS WASM 文件到微信小游戏目录...');
// WASM 源文件路径(需要根据实际项目结构调整)
const wasmSourcePath = path.join(__dirname, '../../assets/scripts/ecs_wasm_core_bg.wasm');
const wasmTargetPath = path.join(buildPath, 'ecs_wasm_core_bg.wasm');
try {
if (fs.existsSync(wasmSourcePath)) {
fs.copyFileSync(wasmSourcePath, wasmTargetPath);
console.log('✅ WASM 文件复制成功');
} else {
console.warn('⚠️ WASM 文件不存在,跳过复制');
}
} catch (error) {
console.error('❌ 复制 WASM 文件失败:', error.message);
}
if (callback) callback();
}
module.exports = {
onBuildFinished: copyWasmFiles
};`;
fs.writeFileSync(path.join(templateDir, 'build-finish.js'), buildScript);
}
function generateUsageInstructions(releaseDir) {
const instructions = `# ECS Framework WASM 支持包
@@ -64,29 +190,7 @@ function generateUsageInstructions(releaseDir) {
## 使用方法
### 1. Cocos Creator 3.8+
\`\`\`typescript
import { ecsCore } from '@esengine/ecs-framework';
// 1. 将WASM文件导入到项目资源中
// 2. 导入胶水代码
import('./ecs_wasm_core.js').then(({ default: wasmFactory }) => {
// 3. 使用Cocos API加载WASM文件
this.loadWasmOrAsm("wasmFiles", "ecs_wasm_core", "your-wasm-uuid").then((wasmFile) => {
// 4. 初始化WASM支持
ecsCore.initializeWasm(wasmFactory, wasmFile).then((success) => {
if (success) {
console.log("ECS WASM加速已启用");
} else {
console.log("回退到JavaScript实现");
}
});
});
});
\`\`\`
### 2. 其他环境(浏览器/Node.js
### 1. 其他环境(浏览器/Node.js
\`\`\`typescript
import { ecsCore } from '@esengine/ecs-framework';
@@ -107,12 +211,41 @@ import('./ecs_wasm_core.js').then(({ default: wasmFactory }) => {
});
\`\`\`
### 2. Webpack/Vite 等构建工具
\`\`\`typescript
import { ecsCore } from '@esengine/ecs-framework';
import wasmFactory from './ecs_wasm_core.js';
import wasmUrl from './ecs_wasm_core_bg.wasm?url';
async function initWasm() {
try {
const wasmFile = await fetch(wasmUrl).then(r => r.arrayBuffer());
const success = await ecsCore.initializeWasm(wasmFactory, wasmFile);
if (success) {
console.log("ECS WASM加速已启用");
} else {
console.log("回退到JavaScript实现");
}
} catch (error) {
console.error("WASM初始化失败:", error);
}
}
initWasm();
\`\`\`
## 注意事项
1. 如果不使用此WASM包框架会自动使用JavaScript实现功能完全正常
2. WASM主要提供查询性能优化对于大多数应用场景JavaScript实现已足够
3. 确保在ECS系统初始化之前调用\`initializeWasm\`方法
## Cocos Creator 用户
Cocos Creator 用户请使用专门的 Cocos Creator 包:\`cocos-release/\`
## 技术支持
如遇问题,请访问:
@@ -123,12 +256,33 @@ import('./ecs_wasm_core.js').then(({ default: wasmFactory }) => {
fs.writeFileSync(path.join(releaseDir, 'README.md'), instructions);
}
function showReleaseResults(releaseDir) {
console.log('\n📊 WASM发布包内容:');
function showReleaseResults(releaseDir, packageType = '') {
console.log(`\n📊 ${packageType} WASM 发布包内容:`);
fs.readdirSync(releaseDir).forEach(file => {
const filePath = path.join(releaseDir, file);
const size = fs.statSync(filePath).size;
console.log(` ${file}: ${(size / 1024).toFixed(1)}KB`);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
console.log(` 📁 ${file}/`);
// 显示子目录内容
try {
fs.readdirSync(filePath).forEach(subFile => {
const subFilePath = path.join(filePath, subFile);
const subStats = fs.statSync(subFilePath);
if (subStats.isFile()) {
const size = (subStats.size / 1024).toFixed(1);
console.log(` ${subFile}: ${size}KB`);
} else {
console.log(` 📁 ${subFile}/`);
}
});
} catch (e) {
// 忽略读取错误
}
} else {
const size = (stats.size / 1024).toFixed(1);
console.log(` ${file}: ${size}KB`);
}
});
}