Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/builder/share/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
82 changes: 6 additions & 76 deletions src/core/builder/worker/builder/manager/asset-library.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -45,11 +44,6 @@ class BuildAssetLibrary {

private meta: IMetaMap = {};

// 缓存地址
private cacheTempDir: string = join(builderConfig.projectTempDir, 'asset-db');
private assetMtimeCache: Record<string, number> = {};
private assetMtimeCacheFile: string = join(builderConfig.projectTempDir, 'builder', 'assets-mtime.json');

// 是否使用缓存开关
public useCache = true;

Expand All @@ -68,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);
}

/**
* 资源管理器初始化
*/
Expand All @@ -89,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();
}

/**
Expand Down Expand Up @@ -117,9 +98,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);
}

/**
Expand Down Expand Up @@ -280,7 +260,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) {
Expand All @@ -304,8 +284,6 @@ class BuildAssetLibrary {
await outputJSON(cacheFile, jsonObject, {
spaces: 4,
});
this.assetMtimeCache[asset.uuid] = assetManager.queryAssetProperty(asset, 'mtime');
await this.saveMtimeCache();
}
} catch (error) {
unExpectException(error);
Expand All @@ -319,10 +297,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;
}
Expand Down Expand Up @@ -456,7 +433,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;
Expand Down Expand Up @@ -558,53 +535,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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/builder/worker/builder/manager/build-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

@yanOO1497 yanOO1497 Jun 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你看这个字段名,含义上,这么写没错的,应该是 projectTempDir 这个目录不应该带上 builder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

this.tempDir = join(builderConfig.projectTempDir, platform);
this.projectRoot = builderConfig.projectRoot;
}

Expand Down Expand Up @@ -303,4 +303,4 @@ export class BuildResult implements IBuildResult {
});
}

}
}
Loading