HotUpdate.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2022-04-15 14:44:04
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-03-19 15:05:36
  6. */
  7. import { _decorator, Component, game, sys } from "cc";
  8. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  9. import { UIID } from "../../common/config/GameUIConfig";
  10. import { Hot, HotOptions } from "./Hot";
  11. import { LoadingViewComp } from "./LoadingViewComp";
  12. import { tips } from "../../common/tips/TipsManager";
  13. const { ccclass, property } = _decorator;
  14. /** 热更新界面控制脚本 */
  15. @ccclass('HotUpdate')
  16. export class HotUpdate extends Component {
  17. /** 热更新业务管理对象 */
  18. private hot = new Hot();
  19. /** 公用加载界面UI做更新提示 */
  20. private lv: LoadingViewComp = null!;
  21. onLoad() {
  22. if (sys.isNative) {
  23. this.lv = this.getComponent(LoadingViewComp)!;
  24. this.lv.data.prompt = oops.language.getLangByID("update_tips_check_update");
  25. this.startHotUpdate();
  26. }
  27. }
  28. /** 开始热更新 */
  29. private startHotUpdate() {
  30. let options = new HotOptions();
  31. options.onVersionInfo = (data: any) => {
  32. // console.log(`【热更新界面】本地版本:${data.local},远程版本:${data.server}`);
  33. };
  34. options.onUpdateProgress = (event: jsb.EventAssetsManager) => {
  35. // 进度提示字
  36. let pc = event.getPercent();
  37. let _total = event.getTotalBytes();
  38. let _have = event.getDownloadedBytes();
  39. let total: string, have: string;
  40. if (_total < 1048576) { // 小于1m,就显示kb
  41. _total = Math.ceil(_total / 1024)
  42. total = _total + 'K'
  43. }
  44. else { // 显示m
  45. total = (_total / (1024 * 1024)).toFixed(1);
  46. total = total + 'M'
  47. }
  48. if (_have < 1048576) { // 小于1m,就显示kb
  49. _have = Math.ceil(_have / 1024)
  50. have = _have + 'K'
  51. }
  52. else { // 显示m
  53. have = (_have / (1024 * 1024)).toFixed(1);
  54. have = have + 'M'
  55. }
  56. if (total == '0K') {
  57. this.lv.data.prompt = oops.language.getLangByID("update_tips_check_update");
  58. }
  59. else {
  60. this.lv.data.prompt = oops.language.getLangByID("update_tips_update") + have + '/' + total + ' (' + parseInt(pc * 100 + "") + '%)';
  61. }
  62. // 进度条
  63. if (!isNaN(event.getPercent())) {
  64. this.lv.data.finished = event.getDownloadedFiles();
  65. this.lv.data.total = event.getTotalFiles();
  66. this.lv.data.progress = (event.getPercent() * 100).toFixed(2);
  67. }
  68. };
  69. options.onNeedToUpdate = (data: any, totalBytes: number) => {
  70. this.lv.data.prompt = oops.language.getLangByID("update_tips_new_version");
  71. let total: string = "";
  72. if (totalBytes < 1048576) { // 小于1m,就显示kb
  73. // totalBytes = Math.ceil(totalBytes / 1024);
  74. // total = total + 'KB';
  75. total = Math.ceil(totalBytes / 1024) + 'KB';
  76. }
  77. else {
  78. total = (totalBytes / (1024 * 1024)).toFixed(1);
  79. total = total + 'MB';
  80. }
  81. // 提示更新
  82. this.checkForceUpdate(() => {
  83. // 非 WIFI 环境提示玩家
  84. this.showUpdateDialog(total, () => {
  85. this.hot.hotUpdate();
  86. })
  87. });
  88. };
  89. options.onNoNeedToUpdate = () => {
  90. this.lv.enter();
  91. };
  92. options.onUpdateFailed = () => {
  93. this.lv.data.prompt = oops.language.getLangByID("update_tips_update_fail");
  94. this.hot.checkUpdate();
  95. };
  96. options.onUpdateSucceed = () => {
  97. this.lv.data.progress = 100;
  98. this.lv.data.prompt = oops.language.getLangByID("update_tips_update_success");
  99. setTimeout(() => {
  100. game.restart();
  101. }, 1000);
  102. };
  103. this.hot.init(options);
  104. }
  105. /** 检查是否强制更新信息 */
  106. private checkForceUpdate(callback: Function) {
  107. let operate: any = {
  108. title: 'common_prompt_title_sys',
  109. content: "update_tips_force",
  110. okWord: 'common_prompt_ok',
  111. cancelWord: 'common_prompt_cancal',
  112. okFunc: () => {
  113. this.hot.clearHotUpdateStorage();
  114. callback();
  115. },
  116. cancelFunc: () => {
  117. game.end();
  118. },
  119. needCancel: true
  120. };
  121. oops.gui.open(UIID.Confirm, operate);
  122. }
  123. /** 非 WIFI 环境提示玩家 */
  124. private showUpdateDialog(size: string, callback: Function) {
  125. if (sys.getNetworkType() == sys.NetworkType.LAN) {
  126. callback();
  127. return;
  128. }
  129. tips.alert(oops.language.getLangByID("update_nowifi_tip") + size, callback);
  130. }
  131. }