diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js
index e6a487e4c9..cd8ab279a3 100644
--- a/cocos2d/core/load-pipeline/CCLoader.js
+++ b/cocos2d/core/load-pipeline/CCLoader.js
@@ -25,13 +25,14 @@
var JS = require('../platform/js');
var Pipeline = require('./pipeline');
-var urlResolver = require('./url-resolver');
var Downloader = require('./downloader');
var Loader = require('./loader');
+var AssetTable = require('./asset-table');
var callInNextTick = require('../platform/utils').callInNextTick;
var downloader = new Downloader();
var loader = new Loader();
+var resources = new AssetTable();
/**
* Loader for resource loading process. It's a singleton object.
@@ -40,7 +41,6 @@ var loader = new Loader();
* @static
*/
cc.loader = new Pipeline([
- urlResolver,
downloader,
loader
]);
@@ -106,35 +106,42 @@ JS.mixin(cc.loader, {
* The only difference is when user pass a single url as resources, the complete callback will set its result directly as the second parameter.
*
* @example
- * cc.loader.load('a.png', function (err, tex) {
- * cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D));
- * });
+ * cc.loader.load('a.png', function (err, tex) {
+ * cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D));
+ * });
*
- * cc.loader.load('http://example.com/a.png', function (err, tex) {
- * cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D));
- * });
+ * cc.loader.load('http://example.com/a.png', function (err, tex) {
+ * cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D));
+ * });
*
- * cc.loader.load({id: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) {
- * cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D));
- * });
+ * cc.loader.load({id: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) {
+ * cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D));
+ * });
*
- * cc.loader.load(['a.png', 'b.json'], function (errors, results) {
- * if (errors) {
- * for (var i = 0; i < errors.length; i++) {
- * cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i]));
- * }
- * }
- * var aTex = results.getContent('a.png');
- * var bJsonObj = results.getContent('b.json');
- * });
+ * cc.loader.load(['a.png', 'b.json'], function (errors, results) {
+ * if (errors) {
+ * for (var i = 0; i < errors.length; i++) {
+ * cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i]));
+ * }
+ * }
+ * var aTex = results.getContent('a.png');
+ * var bJsonObj = results.getContent('b.json');
+ * });
*
* @method load
- * @param {String|Array} resources - Url list in an array.
- * If url starts with "resources://", will load from "resources" folder in your assets.
- * @param {[Function]} progressCallback - Callback invoked when progression change
+ * @param {String|Array} resources - Url list in an array
+ * @param {Function} [progressCallback] - Callback invoked when progression change
* @param {Function} completeCallback - Callback invoked when all resources loaded
*/
load: function(resources, progressCallback, completeCallback) {
+
+ // COMPATIBLE WITH 0.X
+ if (typeof resources === 'string' && resources.startsWith('resources://')) {
+ cc.warn('Sorry, the "resources://" protocol is obsoleted, use cc.loader.loadRes instead please.');
+ this.loadRes(resources.slice('resources://'.length), progressCallback, completeCallback);
+ return;
+ }
+
if (completeCallback === undefined) {
completeCallback = progressCallback;
progressCallback = null;
@@ -230,10 +237,71 @@ JS.mixin(cc.loader, {
}
},
+ _resources: resources,
+ _getResUuid: function (url) {
+ url = cc.url.normalize(url);
+ var uuid = resources.getUuid(url);
+ if ( !uuid ) {
+ var extname = cc.path.extname(url);
+ if (extname) {
+ //cc.warn('Don\'t add extension for "%s".', url);
+ // strip extname
+ url = url.slice(0, - extname.length);
+ uuid = resources.getUuid(url);
+ }
+ }
+ return uuid;
+ },
+
/**
- * Get resource data by id.
- * When you load resources with cc.loader, the url or id passed will be the unique identity of the resource.
- * After loaded, you can acquire them by passing the url or original id to this API.
+ * Load resources from the "resources" folder inside the "assets" folder of your project.
+ *
+ * Note: All asset urls in Creator use forward slashes, urls using backslashes will not work.
+ *
+ * @method loadRes
+ * @param {String} url - Url of the target resource.
+ * The url is relative to the "resources" folder, extensions must be omitted.
+ * @param {Function} [progressCallback] - Callback invoked when progression change. See {{#crossLink "Pipeline/onProgress:method"}}Pipeline.onProgress{{/crossLink}}.
+ * @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.
+ *
+ * @example
+ *
+ * // load the texture (project/assets/resources/imgs/cocos.png) from resources folder with no extension
+ * cc.loader.load('imgs/cocos', function (err, texture) {
+ * if (err) {
+ * cc.error(err.message || err);
+ * return;
+ * }
+ * cc.log('Result should be a texture: ' + (texture instanceof cc.Texture2D));
+ * });
+ */
+ loadRes: function (url, progressCallback, completeCallback) {
+ var uuid = this._getResUuid(url);
+ if (uuid) {
+ this.load(
+ {
+ id: uuid,
+ type: 'uuid',
+ uuid: uuid
+ },
+ progressCallback,
+ completeCallback
+ );
+ }
+ else {
+ callInNextTick(function () {
+ completeCallback.call(null, new Error('Resources url "' + url + '" does not exist.'), null);
+ });
+ }
+ },
+
+ /**
+ * Get resource data by id.
+ * When you load resources with {{#crossLink "loader/load:method"}}{{/crossLink}} or {{#crossLink "loader/loadRes:method"}}{{/crossLink}},
+ * the url will be the unique identity of the resource.
+ * After loaded, you can acquire them by passing the url to this API.
*
* @method getRes
* @param {String} url
@@ -261,6 +329,35 @@ JS.mixin(cc.loader, {
this.removeItem(url);
},
+ /**
+ * Release the loaded cache of asset.
+ *
+ * @method releaseAsset
+ * @param {Asset} asset
+ */
+ releaseAsset: function (asset) {
+ var uuid = asset._uuid;
+ if (uuid) {
+ this.removeItem(uuid);
+ }
+ },
+
+ /**
+ * Release the cache of resource which loaded by {{#crossLink "loader/loadRes:method"}}{{/crossLink}}.
+ *
+ * @method releaseRes
+ * @param {String} url
+ */
+ releaseRes: function (url) {
+ var uuid = this._getResUuid(url);
+ if (uuid) {
+ this.removeItem(uuid);
+ }
+ else {
+ cc.error('Resources url "%s" does not exist.', url);
+ }
+ },
+
/**
* Resource cache of all resources.
*
diff --git a/cocos2d/core/load-pipeline/url-resolver.js b/cocos2d/core/load-pipeline/url-resolver.js
deleted file mode 100644
index 1eabdd3a75..0000000000
--- a/cocos2d/core/load-pipeline/url-resolver.js
+++ /dev/null
@@ -1,101 +0,0 @@
-/****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
-
- http://www.cocos.com
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated engine source code (the "Software"), a limited,
- worldwide, royalty-free, non-assignable, revocable and non-exclusive license
- to use Cocos Creator solely to develop games on your target platforms. You shall
- not use Cocos Creator software for developing other software or tools that's
- used for developing games. You are not granted to publish, distribute,
- sublicense, and/or sell copies of Cocos Creator.
-
- The software or tools in this License Agreement are licensed, not sold.
- Chukong Aipu reserves all rights not expressly granted to you.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
-
-var Pipeline = require('./pipeline');
-var AssetTable = require('./asset-table');
-
-var RESOURCES_PROTOCOL = 'resources://';
-
-Pipeline.UrlResolver = module.exports = {
- id: 'UrlResolver',
- async: true,
- pipeline: null,
-
- handle: function (item, callback) {
- var url = item.url;
- if (typeof url === 'string' && url.startsWith(RESOURCES_PROTOCOL)) {
- url = url.slice(RESOURCES_PROTOCOL.length);
- var uuid = this.resources.getUuid(url);
- if ( !uuid ) {
- var extname = cc.path.extname(url);
- if (extname) {
- //cc.warn('Don\'t add extension for "%s".', item.url);
- // strip extname
- url = url.slice(0, - extname.length);
- uuid = this.resources.getUuid(url);
- }
- }
- if (Array.isArray(uuid)) {
- cc.info('Wildcard NYI');
- if (uuid.length > 0) {
- uuid = uuid[0];
- }
- else {
- cc.info('TODO');
- }
- }
- if (uuid) {
- // load by uuid
- var pipeline = this.pipeline;
- pipeline.flowInDeps(
- [{
- id: uuid,
- type: 'uuid',
- uuid: uuid
- }],
- function (items) {
- var loaded = items[uuid];
- if (loaded) {
- item.content = loaded.content;
- pipeline.flowOut(item);
-
- // skip other pipes
- callback = null;
- }
- else {
- callback(new Error('TODO - what\'s wrong?'), null);
- }
- }
- );
- }
- else {
- callback(new Error('Resources path "' + item.url + '" does not exist.'));
- }
- }
- else {
- callback(null);
- }
- },
-
- resources: new AssetTable(),
-
- // resolve raw url for JSB
- getRawUrl: CC_JSB && function (url) {
- if (typeof url === 'string' && url.startsWith(RESOURCES_PROTOCOL)) {
- return cc.url.raw(url);
- }
- return url;
- }
-};
diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js
index c150ec011d..1379ea5e45 100644
--- a/cocos2d/core/platform/CCAssetLibrary.js
+++ b/cocos2d/core/platform/CCAssetLibrary.js
@@ -26,7 +26,6 @@
var Asset = require('../assets/CCAsset');
var callInNextTick = require('./utils').callInNextTick;
var Loader = require('../load-pipeline/CCLoader');
-var resources = require('../load-pipeline/url-resolver').resources;
/**
* The asset library which managing loading/unloading assets in project.
@@ -319,7 +318,7 @@ var AssetLibrary = {
url = url.slice(RES_DIR.length);
}
// register
- resources.add(url, uuid);
+ Loader._resources.add(url, uuid);
}
}
}
diff --git a/cocos2d/core/platform/url.js b/cocos2d/core/platform/url.js
index 9a97443073..b23432da8f 100644
--- a/cocos2d/core/platform/url.js
+++ b/cocos2d/core/platform/url.js
@@ -47,9 +47,20 @@ cc.url = {
* @readOnly
*/
_builtinRawAssets: '',
+
+ normalize: function (url) {
+ if (url[0] === '.' && url[1] === '/') {
+ url = url.slice(2);
+ }
+ else if (url[0] === '/') {
+ url = url.slice(1);
+ }
+ return url;
+ },
/**
- * Returns the url of raw assets.
+ * Returns the url of raw assets, you will only need this if the raw asset is inside the "resources" folder.
+ *
* @method raw
* @param {String} url
* @return {String}
@@ -61,19 +72,9 @@ cc.url = {
return '';
}
- // normalize
- if (url[0] === '.' && url[1] === '/') {
- url = url.slice(2);
- }
- else if (url[0] === '/') {
- url = url.slice(1);
- }
+ url = this.normalize(url);
- var PROTO = 'resources://';
- if (url.startsWith(PROTO)) {
- url = 'resources/' + url.slice(PROTO.length);
- }
- else if ( !url.startsWith('resources/') ) {
+ if ( !url.startsWith('resources/') ) {
if (CC_EDITOR) {
cc.error('Should not load "%s" from script dynamically, ' +
'unless it is placed in the "resources" folder.', url);
@@ -93,20 +94,12 @@ cc.url = {
* @return {String}
* @example {@link utils/api/engine/docs/cocos2d/core/platform/url/builtinRaw.js}
*/
- builtinRaw: CC_EDITOR && function (url) {0
+ builtinRaw: CC_EDITOR && function (url) {
if ( !this._builtinRawAssets ) {
cc.error('Failed to init builtin asset\'s raw path.');
return '';
}
-
- // normalize
- if (url[0] === '.' && url[1] === '/') {
- url = url.slice(2);
- }
- else if (url[0] === '/') {
- url = url.slice(1);
- }
-
+ url = this.normalize(url);
return this._builtinRawAssets + url;
},
diff --git a/jsb/jsb-etc.js b/jsb/jsb-etc.js
index 52e2ce7d54..35ca6d4a6f 100644
--- a/jsb/jsb-etc.js
+++ b/jsb/jsb-etc.js
@@ -110,7 +110,6 @@ cc.TextureCache.prototype.addImage = function(url, cb, target) {
return this.addImageAsync(url, cb, target);
}
else {
- url = cc.Pipeline.UrlResolver.getRawUrl(url);
if (cb) {
return this._addImage(url, cb);
}
diff --git a/jsb/jsb-label.js b/jsb/jsb-label.js
index 1da58a7e8f..9f0fc4ec26 100644
--- a/jsb/jsb-label.js
+++ b/jsb/jsb-label.js
@@ -121,8 +121,6 @@ jsbLabel.prototype.setContentSize = function (size, height) {
};
jsbLabel.prototype.setFontFileOrFamily = function (fontHandle) {
- fontHandle = cc.Pipeline.UrlResolver.getRawUrl(fontHandle);
-
fontHandle = fontHandle || '';
var extName = cc.path.extname(fontHandle);
//specify font family name directly
diff --git a/jsb/jsb-loader.js b/jsb/jsb-loader.js
index 6022485db9..f68037f04a 100644
--- a/jsb/jsb-loader.js
+++ b/jsb/jsb-loader.js
@@ -38,7 +38,6 @@ function downloadScript (item, callback) {
function downloadText (item, callback) {
var url = item.url;
- url = cc.Pipeline.UrlResolver.getRawUrl(url);
var result = jsb.fileUtils.getStringFromFile(url);
if (typeof result === 'string' && result) {
@@ -106,7 +105,6 @@ cc.loader.addDownloadHandlers({
function loadImage (item, callback) {
var url = item.url;
- url = cc.Pipeline.UrlResolver.getRawUrl(url);
var cachedTex = cc.textureCache.getTextureForKey(url);
if (cachedTex) {
diff --git a/jsb/jsb-particle.js b/jsb/jsb-particle.js
index c9e82dcf12..ad8440628a 100644
--- a/jsb/jsb-particle.js
+++ b/jsb/jsb-particle.js
@@ -94,8 +94,3 @@ for (var mode = 0; mode < funcNames.length; mode++) {
}
}
-var initWithFile = proto.initWithFile;
-proto.initWithFile = function (url) {
- url = cc.Pipeline.UrlResolver.getRawUrl(url);
- initWithFile.call(this, url);
-};
diff --git a/test/qunit/unit/test-resources-loader.js b/test/qunit/unit/test-resources-loader.js
index 171b0ccfc5..93e4aa0547 100644
--- a/test/qunit/unit/test-resources-loader.js
+++ b/test/qunit/unit/test-resources-loader.js
@@ -14,15 +14,15 @@
});
}
});
-
+
asyncTest('load single', function () {
cc.loader.releaseAll();
- cc.loader.load('resources://grossini.png', function (err, texture) {
+ cc.loader.loadRes('grossini.png', 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();