GameUpdate.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. const JSB_MD5 = require("jsb_md5");
  2. // var BundleLoaderManager = require("BundleLoaderManager");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. _updating: false,
  7. _canRetry: false,
  8. },
  9. goPlanA(Asset, name, refreshnNode) {
  10. this.bundleName = name;
  11. this.manifestUrl = Asset;
  12. this.refreshnNode = refreshnNode;
  13. this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset/assets/' + this.bundleName);
  14. this.versionCompareHandle = function (versionA, versionB) {
  15. var vA = versionA.split('.');
  16. var vB = versionB.split('.');
  17. for (var i = 0; i < vA.length; ++i) {
  18. var a = parseInt(vA[i]);
  19. var b = parseInt(vB[i] || 0);
  20. if (a === b) {
  21. continue;
  22. } else {
  23. return a - b;
  24. }
  25. }
  26. if (vB.length > vA.length) {
  27. return -1;
  28. }
  29. else {
  30. return 0;
  31. }
  32. };
  33. this._am = new jsb.AssetsManager(this.manifestUrl.nativeUrl, this._storagePath, this.versionCompareHandle);
  34. console.log(this._am._tempVersionPath);
  35. this._am.setVerifyCallback(function (path, asset) {
  36. var compressed = asset.compressed;
  37. var expectedMD5 = asset.md5;
  38. var relativePath = asset.path;
  39. var size = asset.size;
  40. if (compressed) {
  41. return true;
  42. }
  43. else {
  44. var resMD5 = JSB_MD5(jsb.fileUtils.getDataFromFile(path));
  45. return asset.md5 == resMD5;
  46. }
  47. });
  48. if (this.versionLabel) {
  49. this.versionLabel.string = `src:${this._am.getLocalManifest().getVersion()}`;
  50. }
  51. //初始化脚本版本信息
  52. if (cc.sys.os === cc.sys.OS_ANDROID) {
  53. //一些安卓设备不支持同时下载文件过多
  54. this._am.setMaxConcurrentTask(2);
  55. } else {
  56. this._am.setMaxConcurrentTask(2);
  57. }
  58. },
  59. onDestroy() {
  60. if (this._am) {
  61. this._am.setEventCallback(null);
  62. this._am = null;
  63. }
  64. },
  65. showLog(msg) {
  66. cc.log('[HotUpdateModule][showLog]----' + msg);
  67. },
  68. retry() {
  69. if (!this._updating && this._canRetry) {
  70. this._canRetry = false;
  71. this._am.downloadFailedAssets();
  72. }
  73. },
  74. updateCallback(event) {
  75. var updateOver = false;
  76. var failed = false;
  77. switch (event.getEventCode()) {
  78. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  79. this.showLog("没有发现本地manifest文件,跳过了热更新.");
  80. failed = true;
  81. break;
  82. //更新进度
  83. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  84. let percent = event.getPercent();
  85. if (isNaN(percent)) return;
  86. var msg = event.getMessage();
  87. this.disPatchRateEvent(percent, msg);
  88. this.showLog("updateCallback更新进度:" + percent + ', msg: ' + msg);
  89. break;
  90. //下载manifest文件失败,跳过热更新
  91. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  92. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  93. this.showLog("下载manifest文件失败,跳过热更新.");
  94. failed = true;
  95. break;
  96. //已是最新版本
  97. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  98. this.showLog("已是最新版本.");
  99. updateOver = true;
  100. break;
  101. //更新结束
  102. case jsb.EventAssetsManager.UPDATE_FINISHED:
  103. this.showLog("更新结束." + event.getMessage());
  104. this.disPatchRateEvent(1);
  105. updateOver = true;
  106. break;
  107. //更新错误
  108. case jsb.EventAssetsManager.UPDATE_FAILED:
  109. this.showLog("更新错误." + event.getMessage());
  110. this._updating = false;
  111. this._canRetry = true;
  112. this._failCount++;
  113. this.retry();
  114. break;
  115. //更新过程中错误
  116. case jsb.EventAssetsManager.ERROR_UPDATING:
  117. this.showLog('更新过程中错误: ' + event.getAssetId() + ', ' + event.getMessage());
  118. break;
  119. //解压错误
  120. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  121. this.showLog('解压错误');
  122. break;
  123. default:
  124. break;
  125. }
  126. if (failed) {
  127. this._am.setEventCallback(null);
  128. this._updating = false;
  129. this.hotUpdateFinish(false);
  130. }
  131. if (updateOver) {
  132. this.hotUpdateFinish(true);
  133. }
  134. },
  135. hotUpdate() {
  136. if (this._am && !this._updating) {
  137. this._am.setEventCallback(this.updateCallback.bind(this));
  138. if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
  139. var url = this.manifestUrl.nativeUrl;
  140. this._am.loadLocalManifest(url);
  141. }
  142. this._failCount = 0;
  143. this._am.update();
  144. this._updating = true;
  145. }
  146. },
  147. //检测更新状态
  148. checkCallback(event) {
  149. switch (event.getEventCode()) {
  150. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  151. this.showLog("没有发现本地manifest文件,跳过了热更新.");
  152. this.hotUpdateFinish(true);
  153. break;
  154. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  155. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  156. this.showLog("下载manifest文件失败,跳过热更新.");
  157. this.hotUpdateFinish(false);
  158. break;
  159. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  160. this.showLog("已更新.");
  161. this.hotUpdateFinish(true);
  162. this.disPatchRateEvent(1);
  163. break;
  164. case jsb.EventAssetsManager.NEW_VERSION_FOUND: {
  165. //有新版本
  166. this.showLog("有新版本,需要更新");
  167. this._updating = false;
  168. this.hotUpdate();
  169. return;
  170. }
  171. case jsb.EventAssetsManager.UPDATE_PROGRESSION: {
  172. //有新版本
  173. let percent = event.getPercent();
  174. if (isNaN(percent)) return;
  175. var msg = event.getMessage();
  176. this.showLog("checkCallback更新进度:" + percent + ', msg: ' + msg);
  177. return;
  178. }
  179. case jsb.EventAssetsManager.UPDATE_FINISHED:
  180. this.showLog("Manifest已更新.");
  181. this.hotUpdateFinish(true);
  182. this.disPatchRateEvent(1);
  183. break;
  184. default:
  185. console.log('event.getEventCode():' + event.getEventCode());
  186. return;
  187. }
  188. this._am.setEventCallback(null);
  189. this._updating = false;
  190. },
  191. checkUpdate() {
  192. if (!cc.sys.isNative) {
  193. return;
  194. }
  195. if (this._updating) {
  196. cc.log("检测更新中...");
  197. return;
  198. }
  199. if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
  200. var url = this.manifestUrl.nativeUrl;
  201. this._am.loadLocalManifest(url);
  202. }
  203. if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
  204. this.showLog('加载manifest文件失败');
  205. return;
  206. }
  207. this._am.setEventCallback(this.checkCallback.bind(this));
  208. this._am.checkUpdate();
  209. this._updating = true;
  210. this.disPatchRateEvent(0.01);
  211. },
  212. //热更完成
  213. hotUpdateFinish(result) {
  214. cc.log("热更完成");
  215. },
  216. disPatchRateEvent(percent) {
  217. if (percent > 1) {
  218. percent = 1;
  219. }
  220. BundleLoaderManager.updateRefreshnComp(percent, this.refreshnNode);
  221. },
  222. });