DownloadMgr.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. class DownloadMgr{
  2. constructor() {
  3. console.log('DownloadMgr初始化');
  4. this.fUpdateCb = null;
  5. this.manifestUrl = null;
  6. this._storagePath = null; //存储路径
  7. }
  8. createDownLoad(manifestUrl, path, cb){
  9. if(!cc.sys.isNative) return;
  10. this.manifestUrl = manifestUrl;
  11. this.fUpdateCb = cb;
  12. this.savePath = path || "storagePath_0";
  13. this._storagePath = ((jsb.fileUtils ? jsb.fileUtils.getWritablePath() : '/') + this.savePath);
  14. this.versionCompareHandle = function(versionA, versionB){
  15. let vA = versionA.split('.');
  16. let vB = versionB.split('.');
  17. for(let i=0; i < vA.length; ++i){
  18. let a = parseInt(vA[i]);
  19. let b = parseInt(vB[i] || 0);
  20. if (a === b) {
  21. continue;
  22. }
  23. else {
  24. return a - b;
  25. }
  26. }
  27. if (vB.length > vA.length) {
  28. return -1;
  29. }
  30. else {
  31. return 0;
  32. }
  33. };
  34. this._am = new jsb.AssetsManager('', this._storagePath, this.versionCompareHandle);
  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. return true;
  45. }
  46. });
  47. if (cc.sys.os === cc.sys.OS_ANDROID) {
  48. this._am.setMaxConcurrentTask(2);
  49. }
  50. this._am.setEventCallback(this.updateCb.bind(this));
  51. if (this._am.getState() === jsb.AssetsManager.State.UNINITED) {
  52. // Resolve md5 url
  53. let url = this.manifestUrl.nativeUrl;
  54. if (cc.loader.md5Pipe) {
  55. url = cc.loader.md5Pipe.transformURL(url);
  56. }
  57. this._am.loadLocalManifest(url);
  58. }
  59. this._am.update();
  60. this.iFailDownCount = 0;
  61. }
  62. updateCb(event) {
  63. switch (event.getEventCode()) {
  64. case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
  65. break;
  66. case jsb.EventAssetsManager.UPDATE_PROGRESSION:
  67. break;
  68. case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
  69. case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
  70. break;
  71. case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
  72. break;
  73. case jsb.EventAssetsManager.UPDATE_FINISHED:
  74. break;
  75. case jsb.EventAssetsManager.UPDATE_FAILED:
  76. if (this.iFailDownCount < 10) {
  77. this._am.downloadFailedAssets();
  78. this.iFailDownCount++;
  79. } else {
  80. this.removeTemStorage();
  81. }
  82. break;
  83. case jsb.EventAssetsManager.ERROR_UPDATING:
  84. break;
  85. case jsb.EventAssetsManager.ERROR_DECOMPRESS:
  86. break;
  87. default:
  88. break;
  89. }
  90. if (this.fUpdateCb) {
  91. this.fUpdateCb(event);
  92. }
  93. }
  94. onDestroy() {
  95. this._am.setEventCallback(null);
  96. }
  97. removeTemStorage() {
  98. console.log("DownloadMgr removeTemStorage........");
  99. }
  100. getSearchPaths() {
  101. let newPaths = this._am.getLocalManifest().getSearchPaths();
  102. return newPaths;
  103. }
  104. }
  105. let instance = null;
  106. DownloadMgr.getInstance = function(){
  107. if(!instance){
  108. instance = new DownloadMgr();
  109. }
  110. return instance;
  111. }
  112. export default DownloadMgr.getInstance();