From 4351f7c82cd402134dbd36eb60cd673c720e67f8 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 13 Jan 2017 15:56:58 +0800 Subject: [PATCH] add progress callback for loadRes --- DebugInfos.js | 1 - EngineErrorMap.md | 4 -- cocos2d/core/load-pipeline/CCLoader.js | 90 ++++++++++++++++-------- test/qunit/unit/test-loader.js | 24 ------- test/qunit/unit/test-resources-loader.js | 32 +++++++++ 5 files changed, 93 insertions(+), 58 deletions(-) diff --git a/DebugInfos.js b/DebugInfos.js index 45a3811fe3..1be22e4722 100644 --- a/DebugInfos.js +++ b/DebugInfos.js @@ -370,7 +370,6 @@ if (CC_DEV) { //AutoReleaseUtils: 4800 "4800": "unknown asset type %s", //getKey //Loader: 4900 - "4900": "Sorry, the 'resources://' protocol is obsoleted, use cc.loader.loadRes instead please.", //load "4901": "loadRes: should not specify the extname in %s %s", //_getResUuid "4902": "No need to release non-cached asset.", //setAutoRelease "4903": "Can not get class '%s'", //uuid-loader.loadUuid diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 1e2a7358a4..e4b6606815 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -1308,10 +1308,6 @@ The dom control is not created! unknown asset type -### 4900 - -Sorry, the 'resources://' protocol is obsoleted, use cc.loader.loadRes instead please. - ### 4901 loadRes: should not specify the extname in %s %s diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index bde5247b7e..fbddefa3a7 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -182,14 +182,6 @@ JS.mixin(CCLoader.prototype, { * @param {Function} completeCallback - Callback invoked when all resources loaded */ load: function(resources, progressCallback, completeCallback) { - - // COMPATIBLE WITH 0.X - if (CC_DEV && typeof resources === 'string' && resources.startsWith('resources://')) { - cc.warnID(4900); - this.loadRes(resources.slice('resources://'.length), progressCallback, completeCallback); - return; - } - if (completeCallback === undefined) { completeCallback = progressCallback; progressCallback = this.onProgress || null; @@ -347,6 +339,41 @@ JS.mixin(CCLoader.prototype, { }); }, + /** + * @param {Function} [type] + * @param {Function} [onProgress] + * @param {Function} onComplete + * @returns {Object} arguments + * @returns {Function} arguments.type + * @returns {Function} arguments.onProgress + * @returns {Function} arguments.onComplete + */ + _parseLoadResArgs: function (type, onProgress, onComplete) { + if (onComplete === undefined) { + var isValidType = cc.isChildClassOf(type, cc.RawAsset); + if (onProgress) { + onComplete = onProgress; + if (isValidType) { + onProgress = this.onProgress || null; + } + } + else if (onProgress === undefined && !isValidType) { + onComplete = type; + onProgress = this.onProgress || null; + type = null; + } + if (onProgress !== undefined && !isValidType) { + onProgress = type; + type = null; + } + } + return { + type: type, + onProgress: onProgress, + onComplete: onComplete, + }; + }, + /** * Load resources from the "resources" folder inside the "assets" folder of your project.
*
@@ -356,6 +383,7 @@ JS.mixin(CCLoader.prototype, { * @param {String} url - Url of the target resource. * The url is relative to the "resources" folder, extensions must be omitted. * @param {Function} [type] - Only asset of type will be loaded if this argument is supplied. + * @param {Function} [progressCallback] - Callback invoked when progression change. * @param {Function} completeCallback - Callback invoked when the resource loaded. * @param {Error} completeCallback.error - The error info or null if loaded successfully. * @param {Object} completeCallback.resource - The loaded resource if it can be found otherwise returns null. @@ -380,11 +408,11 @@ JS.mixin(CCLoader.prototype, { * cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame)); * }); */ - loadRes: function (url, type, completeCallback) { - if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { - completeCallback = type; - type = null; - } + loadRes: function (url, type, progressCallback, completeCallback) { + var args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; var self = this; var uuid = self._getResUuid(url, type); if (uuid) { @@ -393,6 +421,7 @@ JS.mixin(CCLoader.prototype, { type: 'uuid', uuid: uuid }, + progressCallback, function (err, asset) { if (asset) { // should not release these assets, even if they are static referenced in the scene. @@ -409,9 +438,8 @@ JS.mixin(CCLoader.prototype, { } }, - _loadResUuids: function (uuids, completeCallback) { - var remain = uuids.length; - if (remain > 0) { + _loadResUuids: function (uuids, progressCallback, completeCallback) { + if (uuids.length > 0) { var self = this; var res = uuids.map(function (uuid) { return { @@ -419,7 +447,7 @@ JS.mixin(CCLoader.prototype, { uuid: uuid } }); - this.load(res, function (errors, items) { + this.load(res, progressCallback, function (errors, items) { var results = []; for (var i = 0; i < res.length; ++i) { var uuid = res[i].uuid; @@ -452,6 +480,7 @@ JS.mixin(CCLoader.prototype, { * @param {String[]} urls - Array of urls of the target resource. * The url is relative to the "resources" folder, extensions must be omitted. * @param {Function} [type] - Only asset of type will be loaded if this argument is supplied. + * @param {Function} [progressCallback] - Callback invoked when progression change. * @param {Function} completeCallback - A callback which is called when all assets have been loaded, or an error occurs. * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called with the error. If all assets are loaded successfully, error will be null. * @param {Array} completeCallback.assets - An array of all loaded assets. If nothing to load, assets will be an empty array. @@ -469,11 +498,12 @@ JS.mixin(CCLoader.prototype, { * // ... * }); */ - loadResArray: function (urls, type, completeCallback) { - if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { - completeCallback = type; - type = null; - } + loadResArray: function (urls, type, progressCallback, completeCallback) { + var args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; + var uuids = []; for (var i = 0; i < urls.length; i++) { var url = urls[i]; @@ -486,7 +516,7 @@ JS.mixin(CCLoader.prototype, { return; } } - this._loadResUuids(uuids, completeCallback); + this._loadResUuids(uuids, progressCallback, completeCallback); }, /** @@ -498,6 +528,7 @@ JS.mixin(CCLoader.prototype, { * @param {String} url - Url of the target folder. * The url is relative to the "resources" folder, extensions must be omitted. * @param {Function} [type] - Only asset of type will be loaded if this argument is supplied. + * @param {Function} [progressCallback] - Callback invoked when progression change. * @param {Function} completeCallback - A callback which is called when all assets have been loaded, or an error occurs. * @param {Error} completeCallback.error - If one of the asset failed, the complete callback is immediately called with the error. If all assets are loaded successfully, error will be null. * @param {Object[]} completeCallback.assets - An array of all loaded assets. If nothing to load, assets will be an empty array. @@ -524,13 +555,14 @@ JS.mixin(CCLoader.prototype, { * var texture2 = textures[1]; * }); */ - loadResDir: function (url, type, completeCallback) { - if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { - completeCallback = type; - type = null; - } + loadResDir: function (url, type, progressCallback, completeCallback) { + var args = this._parseLoadResArgs(type, progressCallback, completeCallback); + type = args.type; + progressCallback = args.onProgress; + completeCallback = args.onComplete; + var uuids = resources.getUuidArray(url, type); - this._loadResUuids(uuids, completeCallback); + this._loadResUuids(uuids, progressCallback, completeCallback); }, /** diff --git a/test/qunit/unit/test-loader.js b/test/qunit/unit/test-loader.js index 1582ef85bc..7f0d89286e 100644 --- a/test/qunit/unit/test-loader.js +++ b/test/qunit/unit/test-loader.js @@ -31,14 +31,8 @@ asyncTest('Load', function () { loader.releaseAll(); strictEqual(Object.keys(loader._cache).length, 0, 'should clear loading items after releaseAll called'); - clearTimeout(timeoutId); start(); }); - - var timeoutId = setTimeout(function () { - ok(false, 'time out!'); - start(); - }, 5000); }); asyncTest('Load single file', function () { @@ -56,14 +50,8 @@ asyncTest('Load single file', function () { ok(texture instanceof cc.Texture2D, 'the single result should be Texture2D'); loader.releaseAll(); - clearTimeout(timeoutId); start(); }); - - var timeoutId = setTimeout(function () { - ok(false, 'time out!'); - start(); - }, 5000); }); asyncTest('Load with dependencies', function () { @@ -134,14 +122,8 @@ asyncTest('Load with dependencies', function () { progressCallback.expect(total, 'should call ' + total + ' times progress callback for ' + total + ' resources'); loader.releaseAll(); - clearTimeout(timeoutId); start(); }); - - var timeoutId = setTimeout(function () { - ok(false, 'time out!'); - start(); - }, 5000); }); asyncTest('Loading font', function () { @@ -174,12 +156,6 @@ asyncTest('Loading font', function () { ok(items.isCompleted(), 'be able to load all resources'); progressCallback.expect(total, 'should call ' + total + ' times progress callback for ' + total + ' resources'); - clearTimeout(timeoutId); start(); }); - - var timeoutId = setTimeout(function () { - ok(false, 'time out!'); - start(); - }, 5000); }); \ No newline at end of file diff --git a/test/qunit/unit/test-resources-loader.js b/test/qunit/unit/test-resources-loader.js index 3c27ff3459..150a6fa88a 100644 --- a/test/qunit/unit/test-resources-loader.js +++ b/test/qunit/unit/test-resources-loader.js @@ -112,4 +112,36 @@ start(); }); }); + + test('parse loadRes arguments', function () { + var args; + function onProgress () {} + function onComplete () {} + + function assert (args, type, onProgress, onComplete, testName) { + strictEqual(args.type, type, 'type of ' + testName + ' should be correct'); + equal(args.onProgress, onProgress, 'progress of ' + testName + ' should be correct'); + equal(args.onComplete, onComplete, 'complete of ' + testName + ' should be correct'); + } + + args = cc.loader._parseLoadResArgs(TestSprite, onProgress, onComplete); + assert(args, TestSprite, onProgress, onComplete, 'case 1'); + args = cc.loader._parseLoadResArgs(TestSprite, onProgress, null); + assert(args, TestSprite, onProgress, null, 'case 2'); + args = cc.loader._parseLoadResArgs(TestSprite, null, onComplete); + assert(args, TestSprite, null, onComplete, 'case 3'); + args = cc.loader._parseLoadResArgs(TestSprite, onComplete); + assert(args, TestSprite, null, onComplete, 'case 4'); + args = cc.loader._parseLoadResArgs(TestSprite); + assert(args, TestSprite, null, null, 'case 5'); + args = cc.loader._parseLoadResArgs(onProgress, onComplete); + assert(args, null, onProgress, onComplete, 'case 6'); + args = cc.loader._parseLoadResArgs(null, onComplete); + assert(args, null, null, onComplete, 'case 7'); + args = cc.loader._parseLoadResArgs(onProgress, null); + assert(args, null, onProgress, null, 'case 8'); + args = cc.loader._parseLoadResArgs(onComplete); + assert(args, null, null, onComplete, 'case 9'); + }); + })();