import { AppKernel } from '../../app/AppKernel'; import { AsyncStateMachine } from '../../framework/state/AsyncStateMachine'; import { CheckVersionState } from './states/CheckVersionState'; import { CompleteState } from './states/CompleteState'; import { EnterLobbyState } from './states/EnterLobbyState'; import { FailedState } from './states/FailedState'; import { HotUpdateState } from './states/HotUpdateState'; import { LaunchContext, LaunchProgress, LaunchState, LaunchStateId, } from './states/LaunchState'; import { PreloadLobbyState } from './states/PreloadLobbyState'; export type { LaunchProgress } from './states/LaunchState'; export type LaunchProgressHandler = (value: LaunchProgress) => void; /** * 启动业务状态机。 * * 负责版本检测、热更新、资源预加载、场景切换和统一失败处理, */ export class LaunchStateMachine { private readonly machine = new AsyncStateMachine(); constructor(private readonly context: LaunchContext) { this.registerStates(); } get current(): LaunchStateId | null { return this.machine.current; } async run(): Promise { try { await this.machine.start(LaunchStateId.CheckVersion); } catch (error) { this.context.error = error; await this.machine.transition(LaunchStateId.Failed); } } private registerStates(): void { const states: LaunchState[] = [ new CheckVersionState(), new HotUpdateState(), new PreloadLobbyState(), new EnterLobbyState(), new CompleteState(), new FailedState(), ]; for (const state of states) { this.machine.register(state.id, () => state.enter(this.context)); } } }