HotUpdate.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. manifestUrl: {
  5. type: cc.Asset,
  6. default: null
  7. },
  8. _updating: false,
  9. _canRetry: false,
  10. _storagePath: '',
  11. updateFinish: false,
  12. percent: 100
  13. },
  14. checkCb: function (event) {
  15. console.log('checkCb ')
  16. switch (event.getEventCode()) {
  17. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  18. console.log('checkCb No local manifest file found, hot update skipped.')
  19. this.updateFinish = true
  20. break;
  21. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  22. console.log('checkCb ERROR_DOWNLOAD_MANIFEST')
  23. this.updateFinish = true
  24. break
  25. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  26. console.log('checkCb Fail to download manifest file, hot update skipped.')
  27. this.updateFinish = true
  28. break;
  29. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  30. console.log('checkCb ALREADY_UP_TO_DATE ')
  31. this.updateFinish = true
  32. break;
  33. case jsb.EventAssetsManager.NEW_VERSION_FOUND:
  34. this.percent = 0
  35. setTimeout(() => {
  36. this.hotUpdate()
  37. }, 500);
  38. break;
  39. default:
  40. return;
  41. }
  42. // this._am.setEventCallback(null);
  43. // this._checkListener = null;
  44. this._updating = false;
  45. },
  46. updateCb: function (event) {
  47. var needRestart = false;
  48. var failed = false;
  49. switch (event.getEventCode()) {
  50. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  51. failed = true;
  52. this.updateFinish = true
  53. break;
  54. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  55. console.log('checkCb getDownloadedFiles/getTotalFiles ', event.getDownloadedFiles(), "/", event.getTotalFiles())
  56. // console.log('checkCb getDownloadedBytes/getTotalBytes >> ', event.getDownloadedBytes(), "/", event.getTotalBytes())
  57. let progress = 0
  58. if (event.getTotalBytes() != 0) {
  59. progress = (event.getDownloadedBytes() / event.getTotalBytes()).toFixed(2)
  60. }
  61. this.percent = Math.floor(progress * 100)
  62. break;
  63. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  64. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  65. this.updateFinish = true
  66. failed = true;
  67. break;
  68. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  69. this.updateFinish = true
  70. failed = true;
  71. break;
  72. case jsb.EventAssetsManager.UPDATE_FINISHED:
  73. needRestart = true;
  74. break;
  75. case jsb.EventAssetsManager.UPDATE_FAILED:
  76. this._updating = false;
  77. this._canRetry = true;
  78. setTimeout(() => {
  79. this.checkUpdate()
  80. }, 300);
  81. break;
  82. case jsb.EventAssetsManager.ERROR_UPDATING:
  83. break;
  84. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  85. break;
  86. default:
  87. break;
  88. }
  89. if (failed) {
  90. this._am.setEventCallback(null);
  91. this._updateListener = null;
  92. this._updating = false;
  93. }
  94. if (needRestart) {
  95. this._am.setEventCallback(null);
  96. this._updateListener = null;
  97. // Prepend the manifest's search path
  98. var searchPaths = jsb.fileUtils.getSearchPaths();
  99. console.log('MMM searchPaths ', searchPaths)
  100. var newPaths = this._am.getLocalManifest().getSearchPaths();
  101. console.log('MMM newPaths ', newPaths)
  102. Array.prototype.unshift.apply(searchPaths, newPaths);
  103. console.log('MMM searchPaths new ', searchPaths)
  104. // This value will be retrieved and appended to the default search path during game startup,
  105. // please refer to samples/js-tests/main.js for detailed usage.
  106. // !!! Re-add the search paths in main.js is very important, otherwise, new scripts won't take effect.
  107. cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
  108. jsb.fileUtils.setSearchPaths(searchPaths);
  109. cc.audioEngine.stopAll();
  110. cc.game.restart();
  111. }
  112. },
  113. retry: function () {
  114. if (!this._updating && this._canRetry) {
  115. this._canRetry = false;
  116. this._am.downloadFailedAssets();
  117. }
  118. },
  119. checkUpdate: function () {
  120. if (this._updating) {
  121. return;
  122. }
  123. if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
  124. // Resolve md5 url
  125. var url = this.manifestUrl.nativeUrl;
  126. if (cc.loader.md5Pipe) {
  127. url = cc.loader.md5Pipe.transformURL(url);
  128. }
  129. console.log("loadLocalManifest url ", url)
  130. this._am.loadLocalManifest(url);
  131. }
  132. if (!this._am.getLocalManifest() || !this._am.getLocalManifest().isLoaded()) {
  133. return;
  134. }
  135. this._am.setEventCallback(this.checkCb.bind(this));
  136. console.log('checkUpdate ')
  137. this._am.checkUpdate();
  138. this._updating = true;
  139. },
  140. hotUpdate: function () {
  141. if (this._am && !this._updating) {
  142. this._am.setEventCallback(this.updateCb.bind(this));
  143. if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
  144. // Resolve md5 url
  145. var url = this.manifestUrl.nativeUrl;
  146. if (cc.loader.md5Pipe) {
  147. url = cc.loader.md5Pipe.transformURL(url);
  148. }
  149. this._am.loadLocalManifest(url);
  150. }
  151. this._failCount = 0;
  152. this._am.update();
  153. this._updating = true;
  154. }
  155. },
  156. // use this for initialization
  157. onLoad: function () {
  158. // Hot update is only available in Native build
  159. console.log('HotUpdate is onLoad');
  160. if (!cc.sys.isNative) {
  161. return;
  162. }
  163. this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + 'blackjack-remote-asset');
  164. console.log('Storage path for remote asset : ' + this._storagePath);
  165. // Setup your own version compare handler, versionA and B is versions in string
  166. // if the return value greater than 0, versionA is greater than B,
  167. // if the return value equals 0, versionA equals to B,
  168. // if the return value smaller than 0, versionA is smaller than B.
  169. this.versionCompareHandle = function (versionA, versionB) {
  170. console.log("JS Custom Version Compare: version A is " + versionA + ', version B is ' + versionB);
  171. var vA = versionA.split('.');
  172. var vB = versionB.split('.');
  173. for (var i = 0; i < vA.length; ++i) {
  174. var a = parseInt(vA[i]);
  175. var b = parseInt(vB[i] || 0);
  176. if (a === b) {
  177. continue;
  178. }
  179. else {
  180. return a - b;
  181. }
  182. }
  183. if (vB.length > vA.length) {
  184. return -1;
  185. }
  186. else {
  187. return 0;
  188. }
  189. };
  190. // Init with empty manifest url for testing custom manifest
  191. this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHandle);
  192. // Setup the verification callback, but we don't have md5 check function yet, so only print some message
  193. // Return true if the verification passed, otherwise return false
  194. this._am.setVerifyCallback(function (path, asset) {
  195. // When asset is compressed, we don't need to check its md5, because zip file have been deleted.
  196. var compressed = asset.compressed;
  197. // Retrieve the correct md5 value.
  198. var expectedMD5 = asset.md5;
  199. // asset.path is relative path and path is absolute.
  200. var relativePath = asset.path;
  201. // The size of asset file, but this value could be absent.
  202. var size = asset.size;
  203. if (compressed) {
  204. return true;
  205. }
  206. else {
  207. return true;
  208. }
  209. });
  210. if (cc.sys.os === cc.sys.OS_ANDROID) {
  211. // Some Android device may slow down the download process when concurrent tasks is too much.
  212. // The value may not be accurate, please do more test and find what's most suitable for your game.
  213. this._am.setMaxConcurrentTask(2);
  214. }
  215. this.percent = 100;
  216. },
  217. onDestroy: function () {
  218. if (this._updateListener) {
  219. this._am.setEventCallback(null);
  220. this._updateListener = null;
  221. }
  222. }
  223. });