HotUpdateState.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. const i18n = context.kernel.i18n;
  11. context.progress({ state: this.id, title: i18n.t('launch.check_res_version') });
  12. const result = await context.kernel.hotUpdate.update(
  13. AppConfig.HotUpdateUrl,
  14. (progress) => {
  15. const percent = progress.percent || progress.percentByFile;
  16. const title = progress.stage === 'retrying'
  17. ? i18n.t("launch.update_retry", progress.retryCount)
  18. : progress.stage === 'finished'
  19. ? i18n.t('launch.update_finished')
  20. : progress.stage === 'downloading'
  21. ? i18n.t('launch.updating_res', progress.downloadedFiles, progress.totalFiles)
  22. : i18n.t('launch.check_res_version');
  23. context.progress({
  24. state: this.id,
  25. title,
  26. progress: percent * 0.4,
  27. });
  28. },
  29. );
  30. if (result.restartRequired) {
  31. context.progress({
  32. state: this.id,
  33. title: i18n.t('launch.update_finished'),
  34. progress: 0.65,
  35. });
  36. context.kernel.hotUpdate.restart();
  37. return null;
  38. }
  39. context.progress({ state: this.id, progress: 0.65 });
  40. return LaunchStateId.PreloadLobby;
  41. }
  42. }