From b9c0dc5d5f9ffe7d48eba3dabd6c77d6cc99e474 Mon Sep 17 00:00:00 2001 From: tangkai <1944876319@qq.com> Date: Tue, 2 Jun 2026 16:13:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1.=E5=8E=BB=E6=8E=89MtimeCache=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E5=AE=9E=E9=99=85=E6=98=AF=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=AF=BC=E8=87=B4=EF=BC=8C=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9C=AA=E6=9B=B4=E6=96=B0=202.=E5=88=A0=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84builder=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/builder/share/utils.ts | 2 +- .../texture-compress/compress-tool.ts | 4 +- .../worker/builder/manager/asset-library.ts | 65 ++----------------- .../worker/builder/manager/build-result.ts | 4 +- 4 files changed, 12 insertions(+), 63 deletions(-) diff --git a/src/core/builder/share/utils.ts b/src/core/builder/share/utils.ts index a41128f27..2fd60119b 100644 --- a/src/core/builder/share/utils.ts +++ b/src/core/builder/share/utils.ts @@ -140,7 +140,7 @@ export function transTimeToNumber(time: string) { * @returns */ export function getTaskLogDest(taskName: string, time: number | string) { - return Utils.Path.resolveToUrl(join(builderConfig.projectTempDir, 'cli', 'builder', 'log', taskName + changeToLocalTime(time, 5).replace(/:/g, '-') + '.log'), 'project'); + return Utils.Path.resolveToUrl(join(builderConfig.projectTempDir, 'cli', 'log', taskName + changeToLocalTime(time, 5).replace(/:/g, '-') + '.log'), 'project'); } /** diff --git a/src/core/builder/worker/builder/asset-handler/texture-compress/compress-tool.ts b/src/core/builder/worker/builder/asset-handler/texture-compress/compress-tool.ts index 1bc75d491..a70cebbfb 100644 --- a/src/core/builder/worker/builder/asset-handler/texture-compress/compress-tool.ts +++ b/src/core/builder/worker/builder/asset-handler/texture-compress/compress-tool.ts @@ -70,7 +70,7 @@ export async function compressPVR(option: ICompressConfig) { console.debug('start compress pvr', option); let src = option.src; if (option.format.endsWith('rgb_a')) { - const tempDest = Path.join(builderConfig.projectTempDir, 'builder', 'CompressTexture', 'pvr_alpha', option.uuid + Path.extname(src)); + const tempDest = Path.join(builderConfig.projectTempDir, 'CompressTexture', 'pvr_alpha', option.uuid + Path.extname(src)); await createAlphaAtlas(src, tempDest); src = tempDest; } @@ -163,7 +163,7 @@ export async function compressEtc(option: ICompressConfig) { // 理论上同一资源的 alpha 贴图可以复用,且应该走 getAssetTempDirByUuid 使用缓存即可,但由于这个工具需要单独可以走测试例试,所以暂时先不走通用地址 // 理论上 etc 和 pvr 的 alpha 贴图也可以复用,但由于可能存在并发的权限问题,暂不复用 // NOTE: 注意,这里的图片名称必须和 dest 保持一致,因为此压缩工具压缩出来的结果无法改变图片名称 - const tempDest = Path.join(builderConfig.projectTempDir, 'builder', 'CompressTexture', 'etc_alpha', uuid, Path.basename(dest, Path.extname(dest)) + Path.extname(src)); + const tempDest = Path.join(builderConfig.projectTempDir, 'CompressTexture', 'etc_alpha', uuid, Path.basename(dest, Path.extname(dest)) + Path.extname(src)); await createAlphaAtlas(src, tempDest); src = tempDest; } diff --git a/src/core/builder/worker/builder/manager/asset-library.ts b/src/core/builder/worker/builder/manager/asset-library.ts index d98e7d1f3..9fcca5b89 100644 --- a/src/core/builder/worker/builder/manager/asset-library.ts +++ b/src/core/builder/worker/builder/manager/asset-library.ts @@ -1,6 +1,6 @@ 'use strict'; -import { readJSON, existsSync, outputJSON, removeSync, copy, statSync } from 'fs-extra'; +import { readJSON, existsSync, outputJSON, removeSync, copy } from 'fs-extra'; import { basename, dirname, extname, join } from 'path'; import { CCON } from 'cc/editor/serialization'; import { transformCCON } from './cconb-utils'; @@ -46,9 +46,8 @@ class BuildAssetLibrary { private meta: IMetaMap = {}; // 缓存地址 - private cacheTempDir: string = join(builderConfig.projectTempDir, 'asset-db'); private assetMtimeCache: Record = {}; - private assetMtimeCacheFile: string = join(builderConfig.projectTempDir, 'builder', 'assets-mtime.json'); + private assetMtimeCacheFile: string = join(builderConfig.projectTempDir, 'assets-mtime.json'); // 是否使用缓存开关 public useCache = true; @@ -117,9 +116,8 @@ class BuildAssetLibrary { * @param uuid */ public getAssetTempDirByUuid(uuid: string) { - // 缓存目录需要根据 db 目录的不同发生变化 - const dbName = this.getAsset(uuid)._assetDB.options.name; - return join(this.cacheTempDir, dbName, uuid.substr(0, 2), uuid, 'build' + CACHE_VERSION); + const asset = this.getAsset(uuid); + return join(asset._assetDB.options.temp, uuid.substr(0, 2), uuid, 'build' + CACHE_VERSION); } /** @@ -280,7 +278,7 @@ class BuildAssetLibrary { } // 构建缓存的文件夹 const cacheFile = join(this.getAssetTempDirByUuid(uuid)!, `${options.debug ? 'debug' : 'release'}.json`); - if (this.checkUseCache(asset) && this.checkSerializedCacheValid(asset, cacheFile)) { + if (this.checkUseCache(asset) && existsSync(cacheFile)) { try { return await readJSON(cacheFile); } catch (error) { @@ -305,7 +303,6 @@ class BuildAssetLibrary { spaces: 4, }); this.assetMtimeCache[asset.uuid] = assetManager.queryAssetProperty(asset, 'mtime'); - await this.saveMtimeCache(); } } catch (error) { unExpectException(error); @@ -319,10 +316,9 @@ class BuildAssetLibrary { * @param debug */ public async outputAssets(uuid: string, dest: string, debug: boolean) { - const asset = this.getAsset(uuid); const cacheFile = join(this.getAssetTempDirByUuid(uuid)!, `${debug ? 'debug' : 'release'}.json`); try { - if (asset && this.checkUseCache(asset) && this.checkCanSaveCache(uuid) && this.checkSerializedCacheValid(asset, cacheFile)) { + if (this.checkCanSaveCache(uuid)) { await copy(cacheFile, dest); return; } @@ -456,7 +452,7 @@ class BuildAssetLibrary { return this.getRawInstanceFromData(data, asset); } - getRawInstanceFromData(data: CCON | Object, asset: IAsset) { + getRawInstanceFromData(data: CCON | object, asset: IAsset) { const result: { asset: CCAsset | null; detail: string | null; @@ -558,53 +554,6 @@ class BuildAssetLibrary { return true; } - private checkSerializedCacheValid(asset: IAsset, cacheFile: string): boolean { - if (!existsSync(cacheFile)) { - return false; - } - - const currentMtime = assetManager.queryAssetProperty(asset, 'mtime'); - const cachedMtime = this.assetMtimeCache[asset.uuid]; - if (currentMtime !== null && currentMtime !== undefined) { - if (cachedMtime === undefined) { - console.debug(`Serialized cache is stale because asset mtime cache is missing: {asset(${asset.uuid})}`); - return false; - } - if (cachedMtime !== currentMtime) { - console.debug(`Serialized cache is stale by asset mtime: {asset(${asset.uuid})}`); - return false; - } - } - - let cacheMtime = 0; - try { - cacheMtime = statSync(cacheFile).mtimeMs; - } catch (error) { - unExpectException(error); - return false; - } - - for (const file of this.getAssetLibraryFiles(asset)) { - try { - if (existsSync(file) && statSync(file).mtimeMs > cacheMtime) { - console.debug(`Serialized cache is stale by library file: {asset(${asset.uuid})}`); - return false; - } - } catch (error) { - unExpectException(error); - return false; - } - } - - return true; - } - - private getAssetLibraryFiles(asset: IAsset): string[] { - return asset.meta.files.map((file) => { - return file.startsWith('.') ? `${asset.library}${file}` : join(asset.library, file); - }); - } - private checkCanSaveCache(uuid: string): boolean { // 场景、prefab 资源的缓存,在发生脚本变化后就需要失效 if (this.hasMissingClassUuids.has(uuid) || this.hasMissingClassUuids.has(uuid)) { diff --git a/src/core/builder/worker/builder/manager/build-result.ts b/src/core/builder/worker/builder/manager/build-result.ts index bf7112bab..8cf73b10f 100644 --- a/src/core/builder/worker/builder/manager/build-result.ts +++ b/src/core/builder/worker/builder/manager/build-result.ts @@ -27,7 +27,7 @@ class Paths implements IBuildPaths { this.dir = dir || ''; this.output = this.dir; this.compileConfig = join(dir, BuildGlobalInfo.buildOptionsFileName); - this.tempDir = join(builderConfig.projectTempDir, 'builder', platform); + this.tempDir = join(builderConfig.projectTempDir, platform); this.projectRoot = builderConfig.projectRoot; } @@ -303,4 +303,4 @@ export class BuildResult implements IBuildResult { }); } -} \ No newline at end of file +} From 18efbc482c252026dbfea8a0dab3c712ca119251 Mon Sep 17 00:00:00 2001 From: tangkai <1944876319@qq.com> Date: Wed, 3 Jun 2026 17:57:34 +0800 Subject: [PATCH 2/2] delete assetMtimeCache and assetMtimeCacheFile --- .../worker/builder/manager/asset-library.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/core/builder/worker/builder/manager/asset-library.ts b/src/core/builder/worker/builder/manager/asset-library.ts index 9fcca5b89..9afaaa591 100644 --- a/src/core/builder/worker/builder/manager/asset-library.ts +++ b/src/core/builder/worker/builder/manager/asset-library.ts @@ -13,7 +13,6 @@ import { BundleFilterConfig } from '../../../@types'; import assetManager from '../../../../assets/manager/asset'; import { IAsset, QueryAssetsOption, IAssetInfo as IAssetInfoFromDB } from '../../../../assets/@types/protected'; import assetDBManager from '../../../../assets/manager/asset-db'; -import builderConfig from '../../../share/builder-config'; import i18n from '../../../../base/i18n'; import { filterAssetWithBundleConfig } from '../utils/bundle'; import { initBundleConfig } from '../asset-handler/bundle/utils'; @@ -45,10 +44,6 @@ class BuildAssetLibrary { private meta: IMetaMap = {}; - // 缓存地址 - private assetMtimeCache: Record = {}; - private assetMtimeCacheFile: string = join(builderConfig.projectTempDir, 'assets-mtime.json'); - // 是否使用缓存开关 public useCache = true; @@ -67,18 +62,6 @@ class BuildAssetLibrary { keepNodeUuid: false, // 序列化后是否保留节点组件的 uuid 数据 }; - async initMtimeCache() { - if (existsSync(this.assetMtimeCacheFile)) { - try { - this.assetMtimeCache = (await readJSON(this.assetMtimeCacheFile)) || {}; - } catch (error) { } - } - } - - async saveMtimeCache() { - await outputJSON(this.assetMtimeCacheFile, this.assetMtimeCache); - } - /** * 资源管理器初始化 */ @@ -88,7 +71,6 @@ class BuildAssetLibrary { this.defaultSerializedOptions.keepNodeUuid = false; this.useCache = true; console.debug(`init custom config: keepNodeUuid: ${this.defaultSerializedOptions.keepNodeUuid}, useCache: ${this.useCache}`); - await this.initMtimeCache(); } /** @@ -302,7 +284,6 @@ class BuildAssetLibrary { await outputJSON(cacheFile, jsonObject, { spaces: 4, }); - this.assetMtimeCache[asset.uuid] = assetManager.queryAssetProperty(asset, 'mtime'); } } catch (error) { unExpectException(error);