HotUpdateState.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { LaunchContext, LaunchState, LaunchStateId } from './LaunchState';
  2. import AppConfig from '../../../app/AppConfig';
  3. import { NATIVE } from 'cc/env';
  4. export class HotUpdateState implements LaunchState {
  5. readonly id = LaunchStateId.HotUpdate;
  6. async enter(context: LaunchContext): Promise<LaunchStateId | null> {
  7. if (!AppConfig.HotUpdateEnable || !NATIVE) {
  8. return LaunchStateId.PreloadLobby;
  9. }
  10. context.progress({ state: this.id, title: '检查资源更新' });
  11. const result = await context.kernel.hotUpdate.update(
  12. AppConfig.HotUpdateUrl,
  13. (progress) => {
  14. const percent = progress.percent || progress.percentByFile;
  15. const title = progress.stage === 'retrying'
  16. ? `资源更新失败,正在重试(${progress.retryCount})`
  17. : progress.stage === 'finished'
  18. ? '资源更新完成'
  19. : progress.stage === 'downloading'
  20. ? `正在更新资源 ${progress.downloadedFiles}/${progress.totalFiles}`
  21. : '检查资源更新';
  22. context.progress({
  23. state: this.id,
  24. title,
  25. progress: 0.25 + percent * 0.4,
  26. });
  27. },
  28. );
  29. if (result.restartRequired) {
  30. context.progress({
  31. state: this.id,
  32. title: '更新完成,正在重启',
  33. progress: 0.65,
  34. });
  35. context.kernel.hotUpdate.restart();
  36. return null;
  37. }
  38. context.progress({ state: this.id, progress: 0.65 });
  39. return LaunchStateId.PreloadLobby;
  40. }
  41. }