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 13288cc0d5..fc2de58f2e 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;
@@ -332,6 +324,56 @@ JS.mixin(CCLoader.prototype, {
return key;
},
+ _urlNotFound: function (url, type, completeCallback) {
+ callInNextTick(function () {
+ var info;
+ if (type) {
+ info = JS.getClassName(type) + ' in "' + url + '" does not exist.';
+ }
+ else {
+ info = 'Resources url "' + url + '" does not exist.';
+ }
+ if (completeCallback) {
+ completeCallback(new Error(info), []);
+ }
+ });
+ },
+
+ /**
+ * @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.
*
@@ -341,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.
@@ -365,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) {
@@ -378,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.
@@ -390,19 +434,103 @@ JS.mixin(CCLoader.prototype, {
);
}
else {
- callInNextTick(function () {
- var info;
- if (type) {
- info = JS.getClassName(type) + ' in "' + url + '" does not exist.';
- }
- else {
- info = 'Resources url "' + url + '" does not exist.';
+ self._urlNotFound(url, type, completeCallback);
+ }
+ },
+
+ _loadResUuids: function (uuids, progressCallback, completeCallback, uuidToUrl) {
+ if (uuids.length > 0) {
+ var self = this;
+ var res = uuids.map(function (uuid) {
+ return {
+ type: 'uuid',
+ uuid: uuid
}
+ });
+ this.load(res, progressCallback, function (errors, items) {
if (completeCallback) {
- completeCallback(new Error(info), null);
+ var results = [];
+ var urls = uuidToUrl && {};
+ for (var i = 0; i < res.length; ++i) {
+ var uuid = res[i].uuid;
+ var id = this._getReferenceKey(uuid);
+ var item = items.getContent(id);
+ if (item) {
+ // should not release these assets, even if they are static referenced in the scene.
+ self.setAutoReleaseRecursively(uuid, false);
+ results.push(item);
+ if (urls) {
+ urls[uuidToUrl[uuid]] = item;
+ }
+ }
+ }
+ if (uuidToUrl) {
+ completeCallback(errors, results, urls);
+ }
+ else {
+ completeCallback(errors, results);
+ }
}
});
}
+ else {
+ if (completeCallback) {
+ callInNextTick(function () {
+ if (uuidToUrl) {
+ completeCallback(null, [], {});
+ }
+ else {
+ completeCallback(null, []);
+ }
+ });
+ }
+ }
+ },
+
+ /**
+ * This method is like {{#crossLink "loader/loadRes:method"}}{{/crossLink}} except that it accepts array of url.
+ *
+ * @method loadResArray
+ * @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.
+ * @example
+ *
+ * // load the SpriteFrames from resources folder
+ * var spriteFrames;
+ * var urls = ['misc/characters/character_01', 'misc/weapons/weapons_01'];
+ * cc.loader.loadResArray(urls, cc.SpriteFrame, function (err, assets) {
+ * if (err) {
+ * cc.error(err);
+ * return;
+ * }
+ * spriteFrames = assets;
+ * // ...
+ * });
+ */
+ 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];
+ var uuid = this._getResUuid(url, type);
+ if (uuid) {
+ uuids.push(uuid);
+ }
+ else {
+ this._urlNotFound(url, type, completeCallback);
+ return;
+ }
+ }
+ this._loadResUuids(uuids, progressCallback, completeCallback);
},
/**
@@ -414,9 +542,11 @@ 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. If error occurs, assets will be null.
+ * @param {Object[]} completeCallback.assets - An array of all loaded assets. If nothing to load, assets will be an empty array.
+ * @param {Object} completeCallback.urls - A dictionary which keys are url, values are asset.
*
* @example
*
@@ -440,47 +570,15 @@ 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;
- }
- var self = this;
- var uuids = resources.getUuidArray(url, type);
- var remain = uuids.length;
- if (remain > 0) {
- var res = [];
- for (var i = 0, len = remain; i < len; ++i) {
- var uuid = uuids[i];
- res.push({
- type: 'uuid',
- uuid: uuid
- });
- }
- this.load(res, function (errors, items) {
- var results = [];
- for (var i = 0; i < res.length; ++i) {
- var uuid = res[i].uuid;
- var id = this._getReferenceKey(uuid);
- var item = items.getContent(id);
- if (item) {
- // should not release these assets, even if they are static referenced in the scene.
- self.setAutoReleaseRecursively(uuid, false);
- results.push(item);
- }
- }
- if (completeCallback) {
- completeCallback(errors, results);
- }
- });
- }
- else {
- callInNextTick(function () {
- if (completeCallback) {
- completeCallback(null, []);
- }
- });
- }
+ loadResDir: function (url, type, progressCallback, completeCallback) {
+ var args = this._parseLoadResArgs(type, progressCallback, completeCallback);
+ type = args.type;
+ progressCallback = args.onProgress;
+ completeCallback = args.onComplete;
+
+ var uuidToUrl = {};
+ var uuids = resources.getUuidArray(url, type, uuidToUrl);
+ this._loadResUuids(uuids, progressCallback, completeCallback, uuidToUrl);
},
/**
diff --git a/cocos2d/core/load-pipeline/asset-table.js b/cocos2d/core/load-pipeline/asset-table.js
index 9600cd6aca..e1c22dc0ed 100644
--- a/cocos2d/core/load-pipeline/asset-table.js
+++ b/cocos2d/core/load-pipeline/asset-table.js
@@ -73,46 +73,34 @@ cc.js.mixin(AssetTable.prototype, {
return '';
},
- getUuidArray: function (path, type) {
+ getUuidArray: function (path, type, out_uuidToUrl) {
path = cc.url.normalize(path);
if (path[path.length - 1] === '/') {
path = path.slice(0, -1);
}
var path2uuid = this._pathToUuid;
var uuids = [];
- var p, i;
- if (type) {
- var isChildClassOf = cc.isChildClassOf;
- for (p in path2uuid) {
- if (p.startsWith(path) && isMatchByWord(p, path)) {
- var item = path2uuid[p];
- if (Array.isArray(item)) {
- for (i = 0; i < item.length; i++) {
- var entry = item[i];
- if (isChildClassOf(entry.type, type)) {
- uuids.push(entry.uuid);
+ var isChildClassOf = cc.isChildClassOf;
+ for (var p in path2uuid) {
+ if ((p.startsWith(path) && isMatchByWord(p, path)) || !path) {
+ var item = path2uuid[p];
+ if (Array.isArray(item)) {
+ for (var i = 0; i < item.length; i++) {
+ var entry = item[i];
+ if (!type || isChildClassOf(entry.type, type)) {
+ uuids.push(entry.uuid);
+ if (out_uuidToUrl) {
+ out_uuidToUrl[entry.uuid] = p;
}
}
}
- else {
- if (isChildClassOf(item.type, type)) {
- uuids.push(item.uuid);
- }
- }
}
- }
- }
- else {
- for (p in path2uuid) {
- if (p.startsWith(path) && isMatchByWord(p, path)) {
- var item = path2uuid[p];
- if (Array.isArray(item)) {
- for (i = 0; i < item.length; i++) {
- uuids.push(item[i].uuid);
- }
- }
- else {
+ else {
+ if (!type || isChildClassOf(item.type, type)) {
uuids.push(item.uuid);
+ if (out_uuidToUrl) {
+ out_uuidToUrl[item.uuid] = p;
+ }
}
}
}
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 e9f85158f4..378e292e2c 100644
--- a/test/qunit/unit/test-resources-loader.js
+++ b/test/qunit/unit/test-resources-loader.js
@@ -1,21 +1,25 @@
(function () {
+ var Assets;
+
module('load resources', {
setup: function () {
_resetGame();
- AssetLibrary.init({
+ Assets = {
+ '0000001': ['resources/grossini/grossini.png', cc.js._getClassId(cc.Texture2D)],
+ '123201': ['resources/grossini/grossini', cc.js._getClassId(TestSprite), 1],
+ '0000000': ['resources/grossini.png', cc.js._getClassId(cc.Texture2D)],
+ '1232218': ['resources/grossini', cc.js._getClassId(TestSprite), 1], // sprite in texture
+ '123200': ['resources/grossini', cc.js._getClassId(TestSprite), 1], // sprite in plist
+ };
+ var options = {
libraryPath: assetDir + '/library',
rawAssetsBase: cc.path.dirname(cc.path._setEndWithSep(assetDir, false) + '.dummyExtForDirname') + '/',
rawAssets: {
- assets: {
- '0000001': ['resources/grossini/grossini.png', cc.js._getClassId(cc.Texture2D)],
- '123201': ['resources/grossini/grossini', cc.js._getClassId(TestSprite), 1],
- '0000000': ['resources/grossini.png', cc.js._getClassId(cc.Texture2D)],
- '1232218': ['resources/grossini', cc.js._getClassId(TestSprite), 1], // sprite in texture
- '123200': ['resources/grossini', cc.js._getClassId(TestSprite), 1], // sprite in plist
- }
+ assets: Assets
}
- });
+ };
+ AssetLibrary.init(options);
cc.loader.releaseAll();
},
});
@@ -42,15 +46,9 @@
cc.loader.loadRes('grossini/grossini', function (err, texture) {
ok(texture instanceof cc.Texture2D, 'should be able to load texture without file extname');
- clearTimeout(timeoutId);
start();
});
//});
-
- var timeoutId = setTimeout(function () {
- ok(false, 'time out!');
- start();
- }, 5000);
});
asyncTest('load single by type', function () {
@@ -59,18 +57,12 @@
cc.loader.loadRes('grossini', cc.Texture2D, function (err, texture) {
ok(texture instanceof cc.Texture2D, 'should be able to load asset by type');
- clearTimeout(timeoutId);
start();
});
});
-
- var timeoutId = setTimeout(function () {
- ok(false, 'time out!');
- start();
- }, 5000);
});
- asyncTest('load main asset and sub asset by loadAll', function () {
+ asyncTest('load main asset and sub asset by loadResDir', function () {
cc.loader.loadResDir('grossini', function (err, results) {
ok(Array.isArray(results), 'result should be an array');
['123200', '1232218', '123201'].forEach(function (uuid) {
@@ -83,14 +75,8 @@
return item.url && item.url.endsWith(url);
}), 'checking url ' + url);
});
- clearTimeout(timeoutId);
start();
});
-
- var timeoutId = setTimeout(function () {
- ok(false, 'time out!');
- start();
- }, 5000);
});
asyncTest('loadResDir by type', function () {
@@ -99,13 +85,76 @@
var sprite = results[0];
ok(sprite instanceof TestSprite, 'should be able to load test sprite');
- clearTimeout(timeoutId);
start();
});
-
- var timeoutId = setTimeout(function () {
- ok(false, 'time out!');
- start();
- }, 5000);
});
+
+ asyncTest('load all resources by loadResDir', function () {
+ cc.loader.loadResDir('', function (err, results) {
+ ok(Array.isArray(results), 'result should be an array');
+ var expectCount = Object.keys(Assets).length;
+ strictEqual(results.length, expectCount, 'should load ' + expectCount + ' assets');
+
+ start();
+ });
+ });
+
+ asyncTest('url dict of loadResDir', function () {
+ cc.loader.loadResDir('', cc.Texture2D, function (err, results, urlToRes) {
+ var urls = Object.keys(urlToRes);
+ strictEqual(results.length, urls.length, 'url dict should contains the same count with array');
+
+ var url = urls[0];
+ cc.loader.loadRes(url, cc.Texture2D, function (err, result) {
+ strictEqual(result, urlToRes[url], 'url is correct');
+ start();
+ });
+ });
+ });
+
+ asyncTest('loadResArray', function () {
+ var urls = [
+ 'grossini/grossini',
+ 'grossini'
+ ];
+ cc.loader.loadResArray(urls, TestSprite, function (err, results) {
+ ok(Array.isArray(results), 'result should be an array');
+ var expectCount = urls.length;
+ strictEqual(results.length, expectCount, 'should load ' + expectCount + ' assets');
+
+ 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');
+ });
+
})();