import { LaunchContext, LaunchState, LaunchStateId } from './LaunchState'; import AppConfig from '../../../app/AppConfig'; import { NATIVE } from 'cc/env'; import { math } from 'cc'; export class HotUpdateState implements LaunchState { readonly id = LaunchStateId.HotUpdate; private _context: LaunchContext; async enter(context: LaunchContext): Promise { if (!AppConfig.HotUpdateEnable || !NATIVE) { return LaunchStateId.PreloadLobby; } this._context = context; const i18n = context.kernel.i18n; this.setProgress(0, i18n.t('launch.check_res_version')); const result = await context.kernel.hotUpdate.update( AppConfig.HotUpdateUrl, (progress) => { const percent = progress.percent || progress.percentByFile; const title = progress.stage === 'retrying' ? i18n.t("launch.update_retry", progress.retryCount) : progress.stage === 'finished' ? i18n.t('launch.update_finished') : progress.stage === 'downloading' ? i18n.t('launch.updating_res', progress.downloadedFiles, progress.totalFiles) : i18n.t('launch.check_res_version'); this.setProgress(percent, title); }, ); if (result.restartRequired) { this.setProgress(1, i18n.t('launch.update_finished')); context.kernel.hotUpdate.restart(); return null; } this.setProgress(1, i18n.t('launch.update_finished')); return LaunchStateId.PreloadLobby; } private setProgress(percent: number, title?: string) { if (percent >= 1) { this._context.totalProgress = 0.65; } const progress = math.lerp(0, 0.65, percent); this._context.progress({ state: this.id, progress, title }); } private getProgress(percent: number) { return math.lerp(0, 0.65, percent); } }