You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/game/MemoryDowngrade.ts

87 lines
3.1 KiB

3 weeks ago
import { LoadScheduler } from '../../mx/module/res/LoadScheduler';
import { BundleLifecycle } from '../../mx/module/res/BundleLifecycle';
import { ResourcePhase, getResourcePhase } from '../../mx/module/res/ResourcePhase';
import { PreloadPredictor } from '../../mx/module/res/PreloadPredictor';
import { getHallWarmLongTailResPakage, ResPkgName } from './ConfigRes';
import { MemoryDiag } from './MemoryDiag';
import { MemoryProfile } from './MemoryProfile';
import { PerfReport } from './PerfReport';
import { ThermalGovernor } from './ThermalGovernor';
/** 内存降档:告警时停/慢 silent 预载;D2 允许战斗改用稳定帧率档,不改战斗逻辑 */
export class MemoryDowngrade {
private static _level = 0;
private static _warnCount = 0;
private static _lastPhase = '';
static get level(): number {
return this._level;
}
static get warnCountInPhase(): number {
return this._warnCount;
}
static onMemoryWarning(): void {
if (!MemoryProfile.current.enableLowMemoryDowngrade) return;
const phase = MemoryDiag.getPhase();
if (phase === this._lastPhase) {
this._warnCount++;
} else {
this._lastPhase = phase;
this._warnCount = 1;
}
if (this._warnCount >= 1) {
this._applyLevel(1);
}
if (this._warnCount >= 2) {
this._applyLevel(2);
}
}
/** 回大厅 / 新局战斗前恢复默认预载能力 */
static reset(): void {
this._level = 0;
this._warnCount = 0;
this._lastPhase = '';
LoadScheduler.getInstance().setSilentPaused(false);
LoadScheduler.getInstance().setSilentSlowdown(false);
ThermalGovernor.clearStress();
}
private static _applyLevel(level: number): void {
if (level <= this._level) return;
this._level = level;
if (level >= 1) {
LoadScheduler.getInstance().setSilentPaused(true);
ThermalGovernor.applyStressLevel(1);
}
if (level >= 2) {
LoadScheduler.getInstance().setSilentSlowdown(true);
ThermalGovernor.applyStressLevel(2);
this._releaseHallLongTailPreload();
this._releaseRetainedBattleRes();
}
PerfReport.reportMemDowngrade(level);
}
/** D3:大厅相位释放长尾弹窗预载占用(打开弹窗时 openPanel 再载) */
private static _releaseHallLongTailPreload(): void {
const phase = getResourcePhase();
if (phase === ResourcePhase.Battle || phase === ResourcePhase.BattleExit) return;
gg.res.invalidatePreloadForPackage(getHallWarmLongTailResPakage());
PreloadPredictor.getInstance().clearHallLongTailPreload();
}
/** 内存告警:放弃跨局 BattleRes 复用钉子,允许卸战斗分包 */
private static _releaseRetainedBattleRes(): void {
const phase = getResourcePhase();
if (phase === ResourcePhase.Battle) return;
gg.res.clearResPkg(ResPkgName.BattleRes);
BundleLifecycle.getInstance().releaseOwner('battleRetain');
BundleLifecycle.getInstance().releaseBattlePhase();
}
}