同步主站 Cocos 当前工程:关卡资源、运行脚本与打包工具。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,6 +17,8 @@ const {
|
||||
minifyLevelsDatabase,
|
||||
brotliCompressFile,
|
||||
brotliCompressWebglBundles,
|
||||
writeSlimCatalog,
|
||||
stripPlainIfBrotli,
|
||||
formatBytes,
|
||||
} = require('./package-optimize');
|
||||
const { listRuntimeFiles, assertRuntimePack } = require('./runtime-pack');
|
||||
@@ -88,11 +90,16 @@ function attachLevelsDatabase(outDir) {
|
||||
}
|
||||
splitLevelsDatabase(levelsDbSrc, outDir);
|
||||
|
||||
const levelsDbDst = path.join(outDir, 'levels-database.json');
|
||||
const { before, after } = minifyLevelsDatabase(levelsDbSrc, levelsDbDst);
|
||||
console.log(`>>> levels-database.json (legacy 回退): ${formatBytes(before)} → ${formatBytes(after)}`);
|
||||
const { raw, br } = brotliCompressFile(levelsDbDst, path.join(outDir, 'levels-database.json.br'));
|
||||
console.log(`>>> levels-database.json.br: ${formatBytes(raw)} → ${formatBytes(br)}`);
|
||||
// 运行时只走分片;全量库仅 KEEP_LEGACY_DB=1 保留(约 6 MB)
|
||||
if (process.env.KEEP_LEGACY_DB === '1') {
|
||||
const levelsDbDst = path.join(outDir, 'levels-database.json');
|
||||
const { before, after } = minifyLevelsDatabase(levelsDbSrc, levelsDbDst);
|
||||
console.log(`>>> levels-database.json (legacy 回退): ${formatBytes(before)} → ${formatBytes(after)}`);
|
||||
const { raw, br } = brotliCompressFile(levelsDbDst, path.join(outDir, 'levels-database.json.br'));
|
||||
console.log(`>>> levels-database.json.br: ${formatBytes(raw)} → ${formatBytes(br)}`);
|
||||
} else {
|
||||
console.log('>>> 跳过全量 levels-database.json(运行时用分片;KEEP_LEGACY_DB=1 可保留)');
|
||||
}
|
||||
}
|
||||
|
||||
if (!buildDir || !outDir) {
|
||||
@@ -282,6 +289,11 @@ copyDir(path.join(unityRef, 'StreamingAssets/aa/AddressablesLink'), path.join(aa
|
||||
|
||||
const BUNDLE = parseCatalogBundles(path.join(aaDir, 'catalog.json'));
|
||||
console.log('>>> catalog bundles:', BUNDLE.all.join(', '));
|
||||
const slimBytes = writeSlimCatalog(
|
||||
path.join(aaDir, 'catalog.json'),
|
||||
[BUNDLE.shaders, BUNDLE.assetsAll, BUNDLE.scenesAll].filter(Boolean),
|
||||
);
|
||||
console.log(`>>> catalog.json 已精简为 ${formatBytes(slimBytes)}(仅 bundle 名)`);
|
||||
if (BUNDLE.shaders) {
|
||||
copyFile(
|
||||
path.join(unityRef, 'StreamingAssets/aa/WebGL', BUNDLE.shaders),
|
||||
@@ -361,7 +373,7 @@ console.log(`>>> assets_core (assets_all): ${formatBytes(fs.statSync(assetsCoreZ
|
||||
const assetsBr = brotliCompressFile(assetsCoreZip, assetsCoreZip + '.br');
|
||||
console.log(`>>> ${BUNDLE.assetsAll}.br: ${formatBytes(assetsBr.raw)} → ${formatBytes(assetsBr.br)}`);
|
||||
|
||||
// —— 3b. 每关独立 bundle + levels-manifest.json ——
|
||||
// —— 3b. 每 15 关一包 + levels-manifest.json ——
|
||||
const levelPrefabsSrc = path.join(buildDir, 'assets', 'level-prefabs');
|
||||
let levelPackStats = null;
|
||||
if (!MERGE_LEVELS) {
|
||||
@@ -375,10 +387,26 @@ if (!MERGE_LEVELS) {
|
||||
levelPackStats = splitLevelBundles(levelPrefabsSrc, webglDir, manifestPath, tmp);
|
||||
const brStats = brotliCompressWebglBundles(webglDir);
|
||||
console.log(`>>> bundle Brotli: ${brStats.count} 个, 节省 ${formatBytes(brStats.saved)}`);
|
||||
if (fs.existsSync(manifestPath)) {
|
||||
const manBr = brotliCompressFile(manifestPath, manifestPath + '.br');
|
||||
console.log(`>>> levels-manifest.json.br: ${formatBytes(manBr.raw)} → ${formatBytes(manBr.br)}`);
|
||||
}
|
||||
}
|
||||
|
||||
attachLevelsDatabase(outDir);
|
||||
|
||||
const catalogPath = path.join(aaDir, 'catalog.json');
|
||||
if (fs.existsSync(catalogPath)) {
|
||||
brotliCompressFile(catalogPath, catalogPath + '.br');
|
||||
}
|
||||
|
||||
const stripped = stripPlainIfBrotli(outDir);
|
||||
if (stripped.skipped) {
|
||||
console.log('>>> KEEP_PLAIN=1: 保留未压缩 .bundle / .json');
|
||||
} else {
|
||||
console.log(`>>> 去掉已有 .br 的原文: ${stripped.removed} 个, 节省 ${formatBytes(stripped.saved)}`);
|
||||
}
|
||||
|
||||
// —— 4. Build/ 仅 4 个文件(与 Unity 同名)——
|
||||
const buildOut = path.join(outDir, 'Build');
|
||||
mkdirp(buildOut);
|
||||
@@ -434,7 +462,10 @@ const requiredBundles = [
|
||||
];
|
||||
if (BUNDLE.shaders) requiredBundles.push(path.join(webglDir, BUNDLE.shaders));
|
||||
for (const p of requiredBundles) {
|
||||
if (!fs.existsSync(p) || fs.statSync(p).size < 100) {
|
||||
const br = `${p}.br`;
|
||||
const ok = (fs.existsSync(p) && fs.statSync(p).size >= 100)
|
||||
|| (fs.existsSync(br) && fs.statSync(br).size >= 50);
|
||||
if (!ok) {
|
||||
console.error('>>> bundle 无效:', p);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -442,6 +473,6 @@ for (const p of requiredBundles) {
|
||||
|
||||
const preloadNote = MERGE_LEVELS
|
||||
? 'scenes ∥ assets_all 合并包 (MERGE_LEVELS=1)'
|
||||
: `scenes ∥ assets_core 首屏(引擎启动必需);关卡库分片 + 关卡 bundle 进关按需 (${levelPackStats ? levelPackStats.packed : '?'} 关)`;
|
||||
: `scenes ∥ assets_core 首屏(引擎启动必需);关卡库分片 + 关卡 bundle 进关按需 (${levelPackStats ? levelPackStats.packed : '?'} 关 / ${levelPackStats ? levelPackStats.packs : '?'} 包)`;
|
||||
console.log('\n完成。运行时包 → 本地 import-to-unity.sh / OSS unitycdndir(同一目录)');
|
||||
printPackageReport(outDir, { preloadNote });
|
||||
|
||||
Reference in New Issue
Block a user