HotUpdateState.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { LaunchContext, LaunchState, LaunchStateId } from './LaunchState';
  2. import AppConfig from '../../../app/AppConfig';
  3. import { NATIVE } from 'cc/env';
  4. import { math } from 'cc';
  5. export class HotUpdateState implements LaunchState {
  6. readonly id = LaunchStateId.HotUpdate;
  7. private _context: LaunchContext;
  8. async enter(context: LaunchContext): Promise<LaunchStateId | null> {
  9. if (!AppConfig.HotUpdateEnable || !NATIVE) {
  10. return LaunchStateId.PreloadLobby;
  11. }
  12. this._context = context;
  13. const i18n = context.kernel.i18n;
  14. this.setProgress(0, i18n.t('launch.check_res_version'));
  15. const result = await context.kernel.hotUpdate.update(
  16. AppConfig.HotUpdateUrl,
  17. (progress) => {
  18. const percent = progress.percent || progress.percentByFile;
  19. const title = progress.stage === 'retrying'
  20. ? i18n.t("launch.update_retry", progress.retryCount)
  21. : progress.stage === 'finished'
  22. ? i18n.t('launch.update_finished')
  23. : progress.stage === 'downloading'
  24. ? i18n.t('launch.updating_res', progress.downloadedFiles, progress.totalFiles)
  25. : i18n.t('launch.check_res_version');
  26. this.setProgress(percent, title);
  27. },
  28. );
  29. if (result.restartRequired) {
  30. this.setProgress(1, i18n.t('launch.update_finished'));
  31. context.kernel.hotUpdate.restart();
  32. return null;
  33. }
  34. this.setProgress(1, i18n.t('launch.update_finished'));
  35. return LaunchStateId.PreloadLobby;
  36. }
  37. private setProgress(percent: number, title?: string) {
  38. if (percent >= 1) {
  39. this._context.totalProgress = 0.65;
  40. }
  41. const progress = math.lerp(0, 0.65, percent);
  42. this._context.progress({
  43. state: this.id,
  44. progress,
  45. title
  46. });
  47. }
  48. private getProgress(percent: number) {
  49. return math.lerp(0, 0.65, percent);
  50. }
  51. }