大版本优化,主要在Map,JiaZai,GameManager, 优化新内存方案进游戏慢的问题,重新调整了所有的分包加载逻辑顺序

This commit is contained in:
COMPUTER\EDY
2026-09-08 20:40:59 +08:00
parent af740a5b3c
commit 4e0378577d
31 changed files with 2079 additions and 743 deletions
+34 -9
View File
@@ -63,6 +63,19 @@ export default class BlockAssetManager {
private activeRequestId = 0;
private homeWarmupRequestId = 0;
private foregroundLoads = 0;
private entryDownloadHolds = 0;
/** 只停止后台派发;已在途下载继续完成,进关所需文件仍可提优先级。 */
holdBackgroundDownloads(): () => void {
this.entryDownloadHolds++;
let released = false;
return () => {
if (released) return;
released = true;
this.entryDownloadHolds--;
this.pumpDownloadQueue();
};
}
private backgroundRunning = false;
private backgroundTimer: any = null;
private backgroundStarted = false;
@@ -182,7 +195,9 @@ export default class BlockAssetManager {
this.foregroundLoads++;
this.releaseUnused();
try {
const versionStartedAt = Date.now();
await this.initialize();
// console.log(`[进关耗时][${stage}/${currentLabel}/Block版本准备] 独立耗时=${Date.now() - versionStartedAt}ms`);
if (id !== this.requestId) return false;
this.ensureUserCacheFolder();
// console.log(`[Block图片][${stage}检查] ${currentLabel},版本=${this.version},共${paths.size}张`);
@@ -190,13 +205,23 @@ export default class BlockAssetManager {
// 公共图集全部进内存后才加载方块,不能混在同一批并发请求中。
const groups = [names.filter(isGameplayAtlas), names.filter(name => !isGameplayAtlas(name))];
for (const group of groups) {
for (let i = 0; i < group.length; i += 4) {
if (id !== this.requestId) return false;
const results = await Promise.all(group.slice(i, i + 4).map(name =>
this.loadFrame(name).then(() => null, error => error)));
const error = results.find(result => result != null);
if (error) throw error;
}
const groupStartedAt = Date.now();
const label = group === groups[0] ? "公共图集" : "方块图片";
// console.log(`[进关耗时][${stage}/${currentLabel}/${label}] 开始,共${group.length}张,最多4个加载任务`);
let nextIndex = 0;
let failure: Error = null;
const worker = async () => {
while (id === this.requestId && !failure && nextIndex < group.length) {
const path = group[nextIndex++];
try { await this.loadFrame(path); }
catch (error) { failure = error; }
}
};
// 完成一张立即补一张;失败后停止领取,并等已在途任务结束再回滚窗口。
await Promise.all(Array.from({ length: Math.min(4, group.length) }, () => worker()));
// console.log(`[进关耗时][${stage}/${currentLabel}/${label}] ${failure ? "失败" : id !== this.requestId ? "取消" : "完成"},独立耗时=${Date.now() - groupStartedAt}ms`);
if (failure) throw failure;
if (id !== this.requestId) return false;
}
if (id !== this.requestId) return false;
this.committedPaths = paths;
@@ -621,7 +646,7 @@ export default class BlockAssetManager {
this.downloadQueue.sort((a, b) => b.priority - a.priority);
while (this.activeFileDownloads < BlockAssetManager.FOREGROUND_DOWNLOAD_LIMIT) {
const index = this.downloadQueue.findIndex(job => !job.background
|| (this.foregroundLoads === 0
|| (this.foregroundLoads === 0 && this.entryDownloadHolds === 0
&& this.activeBackgroundDownloads < BlockAssetManager.BACKGROUND_CONCURRENCY));
if (index < 0) return;
const job = this.downloadQueue.splice(index, 1)[0];
@@ -752,7 +777,7 @@ export default class BlockAssetManager {
let paused = false;
const worker = async (batch: string[]) => {
while (index < batch.length) {
while (this.foregroundLoads > 0) {
while (this.foregroundLoads > 0 || this.entryDownloadHolds > 0) {
// if (!paused) console.log("[Block图片][后台缓存] 暂停派发,优先等待进关图片,已发出的请求继续完成");
paused = true;
await this.delay(100);