HotUpdate.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var e =
  5. (this && this.__decorate) ||
  6. function (t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. },
  16. s =
  17. (this && this.__awaiter) ||
  18. function (t, n, r, l) {
  19. return new (r = r || Promise)(function (i, e) {
  20. function s(t) {
  21. try {
  22. a(l.next(t));
  23. } catch (t) {
  24. e(t);
  25. }
  26. }
  27. function o(t) {
  28. try {
  29. a(l.throw(t));
  30. } catch (t) {
  31. e(t);
  32. }
  33. }
  34. function a(t) {
  35. var e;
  36. t.done
  37. ? i(t.value)
  38. : ((e = t.value) instanceof r
  39. ? e
  40. : new r(function (t) {
  41. t(e);
  42. })
  43. ).then(s, o);
  44. }
  45. a((l = l.apply(t, n || [])).next());
  46. });
  47. };
  48. Object.defineProperty(i, "__esModule", {value: !0});
  49. t("ViewConst");
  50. const n = t("TipView"),
  51. r = t("App"),
  52. l = t("GameText"),
  53. {ccclass: o} = cc._decorator;
  54. e = e(
  55. [o],
  56. (e = class extends cc.Component {
  57. constructor() {
  58. super(...arguments),
  59. (this._updating = !1),
  60. (this._canRetry = !1),
  61. (this._storagePath = ""),
  62. (this._updateListener = !1),
  63. (this._am = null),
  64. (this._failCount = 0),
  65. (this.checkPromise = null),
  66. (this.checkResolve = null),
  67. (this.isCheck = !1),
  68. (this.porgressFunc = null);
  69. }
  70. log(t, ...e) {
  71. Log.trace("HotUpdateLog:", t, ...e);
  72. }
  73. versionCompareHandle(t, e) {
  74. return (
  75. this.log(
  76. "JS Custom Version Compare: version A is " + t + ", version B is " + e,
  77. 0 < parseInt(t) - parseInt(e)
  78. ),
  79. parseInt(t) - parseInt(e)
  80. );
  81. }
  82. checkCb(t) {
  83. let e = !1;
  84. switch ((this.log("Code: " + t.getEventCode()), (this.isCheck = !0), t.getEventCode())) {
  85. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  86. this.log("No local manifest file found, hot update skipped.");
  87. break;
  88. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  89. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  90. this.log("Fail to download manifest file, hot update skipped.");
  91. break;
  92. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  93. this.log("Already up to date with the latest remote version.");
  94. break;
  95. case jsb.EventAssetsManager.NEW_VERSION_FOUND:
  96. (e = !0), this.log("New version found, please try to update.");
  97. break;
  98. default:
  99. return;
  100. }
  101. this._am.setEventCallback(null),
  102. (this._updating = !1),
  103. e
  104. ? this.hotUpdate()
  105. : this.checkResolve && (this.checkResolve(!1), (this.checkResolve = this.checkPromise = null));
  106. }
  107. updateCb(a) {
  108. return s(this, void 0, void 0, function* () {
  109. var t,
  110. e,
  111. i = !1,
  112. s = !1;
  113. switch ((Log.trace("updateCb:", a.getEventCode()), a.getEventCode())) {
  114. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  115. this.log("No local manifest file found, hot update skipped."), (s = !0);
  116. break;
  117. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  118. this.log(a.getDownloadedFiles() + " / " + a.getTotalFiles()),
  119. this.log(a.getDownloadedBytes() + " / " + a.getTotalBytes()),
  120. this.log(a.getPercent()),
  121. this.porgressFunc &&
  122. this.porgressFunc(a.getPercent(), a.getDownloadedBytes(), a.getTotalBytes());
  123. var o = a.getMessage();
  124. o && this.log("Updated file: " + o);
  125. break;
  126. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  127. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  128. this.log("Fail to download manifest file, hot update skipped."), (s = !0);
  129. break;
  130. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  131. this.log("Already up to date with the latest remote version."), (s = !0);
  132. break;
  133. case jsb.EventAssetsManager.UPDATE_FINISHED:
  134. this.log("Update finished. " + a.getMessage()), (i = !0);
  135. break;
  136. case jsb.EventAssetsManager.UPDATE_FAILED:
  137. this.log("Update failed. " + a.getMessage()),
  138. (this._updating = !1),
  139. (this._canRetry = !0),
  140. r.default.ViewManager.open(13, {
  141. curState: n.TIPSTATE.SURE,
  142. leftFunc: () => {
  143. cc.audioEngine.stopAll(), cc.game.restart();
  144. },
  145. leftThisObj: this,
  146. tipsStr: l.GameText.getText(lang.hotupdate_failed_tip)
  147. });
  148. break;
  149. case jsb.EventAssetsManager.ERROR_UPDATING:
  150. Log.error("Asset update error: " + a.getAssetId() + ", " + a.getMessage());
  151. break;
  152. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  153. this.log(a.getMessage());
  154. }
  155. s &&
  156. (this._am.setEventCallback(null),
  157. (this._updating = !1),
  158. this._canRetry
  159. ? this.retry()
  160. : (this.checkResolve(!1), (this.checkResolve = this.checkPromise = null))),
  161. i &&
  162. (this._am.setEventCallback(null),
  163. (this._updateListener = null),
  164. (t = jsb.fileUtils.getSearchPaths()),
  165. (e = this._am.getLocalManifest().getSearchPaths()),
  166. this.log(JSON.stringify(e)),
  167. Array.prototype.unshift.apply(t, e),
  168. cc.sys.localStorage.setItem("HotUpdateSearchPaths", JSON.stringify(t)),
  169. jsb.fileUtils.setSearchPaths(t),
  170. cc.sys.localStorage.removeItem("isUPdateing"),
  171. cc.audioEngine.stopAll(),
  172. cc.game.restart());
  173. });
  174. }
  175. loadCustomManifest() {}
  176. retry() {
  177. !this._updating &&
  178. this._canRetry &&
  179. ((this._canRetry = !1), this.log("Retry failed Assets..."), this._am.downloadFailedAssets());
  180. }
  181. timeOut(e) {
  182. return new Promise(t => {
  183. r.default.TimerManager.setTimeOut(
  184. e,
  185. () => {
  186. this.isCheck || t(!1);
  187. },
  188. this
  189. );
  190. });
  191. }
  192. showUpdatePackTip() {
  193. return new Promise(t => {
  194. r.default.ViewManager.open(13, {
  195. curState: n.TIPSTATE.SURE_CANCEL,
  196. leftBtnText: "确定",
  197. leftFunc: () => {
  198. t();
  199. },
  200. rightFunc: () => {
  201. t();
  202. },
  203. rightThisObj: this,
  204. rightBtnText: "取消",
  205. leftThisObj: this,
  206. tipsStr: "检测到新的版本,是否下载更新!"
  207. });
  208. });
  209. }
  210. checkUpdate(t) {
  211. return cc.sys.isNative
  212. ? this.checkPromise ||
  213. ((this.porgressFunc = t),
  214. (this.checkPromise = Promise.race([
  215. new Promise(t => {
  216. if (
  217. ((this.checkResolve = t),
  218. this._am.getState() === jsb.AssetsManager.State.UNINITED &&
  219. (cc.sys.os === cc.sys.OS_ANDROID
  220. ? this._am.loadLocalManifest("@assets/project.manifest")
  221. : this._am.loadLocalManifest(
  222. jsb.fileUtils.fullPathForFilename("project.manifest")
  223. )),
  224. !this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded())
  225. )
  226. return this.log("Failed to load local manifest ..."), void t(!1);
  227. this._am.setEventCallback(this.checkCb.bind(this)),
  228. this._am.checkUpdate(),
  229. (this._updating = !0);
  230. }),
  231. this.timeOut(1e4)
  232. ])),
  233. this.checkPromise)
  234. : Promise.resolve(!1);
  235. }
  236. hotUpdate() {
  237. this._am &&
  238. !this._updating &&
  239. (this._am.setEventCallback(this.updateCb.bind(this)),
  240. cc.sys.localStorage.setItem("isUPdateing", "true"),
  241. (this._failCount = 0),
  242. this._am.update(),
  243. (this._updating = !0));
  244. }
  245. show() {}
  246. onLoad() {
  247. cc.sys.isNative &&
  248. ((this._storagePath =
  249. (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "/") + "blackjack-remote-asset"),
  250. this.log("this._storagePath:", this._storagePath),
  251. (this._am = new jsb.AssetsManager("", this._storagePath, this.versionCompareHandle.bind(this))),
  252. this._am.setVerifyCallback((t, e) => {
  253. var i = e.compressed,
  254. s = e.md5,
  255. o = e.path;
  256. return e.size, !!i || (this.log("Verification passed : " + o + " (" + s + ")"), !0);
  257. }),
  258. this.log("Hot update is ready, please check or directly update."),
  259. cc.sys.os === cc.sys.OS_ANDROID && this.log("Max concurrent tasks count have been limited to 2"));
  260. }
  261. onDestroy() {
  262. this._updateListener && (this._am.setEventCallback(null), (this._updateListener = null));
  263. }
  264. })
  265. );
  266. i.default = e;