LoadManager.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import DownloadMgr from "./DownloadMgr";
  2. class LoadMgr{
  3. constructor() {
  4. console.log('LoadMgr初始化');
  5. this.bNeedRestart = true;
  6. this.downloadProgress = 0;
  7. this.manifestUrl = null;
  8. }
  9. setManifest(manifest){
  10. this.manifestUrl = manifest;
  11. }
  12. startLoading(){
  13. if(!cc.sys.isNative){ //非app模式
  14. this.bNeedRestart = false;
  15. }else{
  16. //开始检测热更新
  17. let savePath = "storagePath_0";
  18. DownloadMgr.createDownLoad(this.manifestUrl, savePath, this.updateCb.bind(this));
  19. }
  20. }
  21. updateCb(event){
  22. let needRestart = false;
  23. let failed = false;
  24. switch (event.getEventCode()) {
  25. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  26. //this.panel.info.string = 'No local manifest file found, hot update skipped.';
  27. failed = true;
  28. break;
  29. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  30. //this.panel.byteProgress.progress = event.getPercent();
  31. //this.panel.fileProgress.progress = event.getPercentByFile();
  32. //this.panel.fileLabel.string = event.getDownloadedFiles() + ' / ' + event.getTotalFiles();
  33. //this.panel.byteLabel.string = event.getDownloadedBytes() + ' / ' + event.getTotalBytes();
  34. if (isNaN(event.getPercentByFile())) {
  35. this.downloadProgress = 0;
  36. } else {
  37. this.downloadProgress = event.getPercentByFile();
  38. }
  39. var msg = event.getMessage();
  40. if (msg) {
  41. //this.panel.info.string = 'Updated file: ' + msg;
  42. // cc.log(event.getPercent()/100 + '% : ' + msg);
  43. }
  44. break;
  45. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  46. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  47. //this.panel.info.string = 'Fail to download manifest file, hot update skipped.';
  48. failed = true;
  49. break;
  50. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  51. //this.panel.info.string = 'Already up to date with the latest remote version.';
  52. failed = true;
  53. break;
  54. case jsb.EventAssetsManager.UPDATE_FINISHED:
  55. //this.panel.info.string = 'Update finished. ' + event.getMessage();
  56. needRestart = true;
  57. break;
  58. case jsb.EventAssetsManager.UPDATE_FAILED:
  59. //this.panel.info.string = 'Update failed. ' + event.getMessage();
  60. //this.panel.retryBtn.active = true;
  61. //this._updating = false;
  62. //this._canRetry = true;
  63. if (DownloadMgr.iFailDownCount >= 10) {
  64. failed = true;
  65. }
  66. break;
  67. case jsb.EventAssetsManager.ERROR_UPDATING:
  68. //this.panel.info.string = 'Asset update error: ' + event.getAssetId() + ', ' + event.getMessage();
  69. break;
  70. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  71. //this.panel.info.string = event.getMessage();
  72. break;
  73. default:
  74. break;
  75. }
  76. if (failed) {
  77. this.bNeedRestart = false;
  78. }
  79. if (needRestart) {
  80. //this._am.setEventCallback(null);
  81. //this._updateListener = null;
  82. // Prepend the manifest's search path
  83. var searchPaths = jsb.fileUtils.getSearchPaths();
  84. //var newPaths = this._am.getLocalManifest().getSearchPaths();
  85. var newPaths = DownloadMgr.getSearchPaths();
  86. console.log(JSON.stringify(newPaths));
  87. Array.prototype.unshift.apply(searchPaths, newPaths);
  88. // This value will be retrieved and appended to the default search path during game startup,
  89. // please refer to samples/js-tests/main.js for detailed usage.
  90. // !!! Re-add the search paths in main.js is very important, otherwise, new scripts won't take effect.
  91. cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
  92. jsb.fileUtils.setSearchPaths(searchPaths);
  93. // cc.audioEngine.stopAll();
  94. setTimeout(() => {
  95. console.log("更新完成,重启游戏");
  96. cc.game.restart();
  97. }, 100);
  98. }
  99. }
  100. }
  101. let instance = null;
  102. LoadMgr.getInstance = function(){
  103. if(!instance){
  104. instance = new LoadMgr();
  105. }
  106. return instance;
  107. }
  108. export default LoadMgr.getInstance();