From 8bfe957286e4fb01ee6f3548d512cb7983246e5e Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 26 Apr 2016 10:49:47 +0800 Subject: [PATCH 1/3] support loadRes by type --- cocos2d/core/load-pipeline/CCLoader.js | 55 +++++++++++++------ cocos2d/core/load-pipeline/asset-table.js | 65 ++++++++++++++++++----- cocos2d/core/platform/CCAssetLibrary.js | 17 +++--- test/qunit/unit/test-resources-loader.js | 37 +++++++++++-- 4 files changed, 135 insertions(+), 39 deletions(-) diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 50c373877b..814f96f39d 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -238,15 +238,14 @@ JS.mixin(cc.loader, { }, _resources: resources, - _getResUuid: function (url) { - var uuid = resources.getUuid(url); + _getResUuid: function (url, type) { + var uuid = resources.getUuid(url, type); 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); + uuid = resources.getUuid(url, type); } } return uuid; @@ -260,23 +259,37 @@ JS.mixin(cc.loader, { * @method loadRes * @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} 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 sprite frame (project/assets/resources/imgs/cocos.png/cocos) from resources folder - * cc.loader.loadRes('imgs/cocos.png/cocos', function (err, spriteFrame) { + * // load the prefab (project/assets/resources/misc/character/cocos) from resources folder + * cc.loader.loadRes('misc/character/cocos', function (err, prefab) { * if (err) { * cc.error(err.message || err); * return; * } - * cc.log('Result should be a sprite frame: ' + ( spriteFrame instanceof cc.SpriteFrame)); + * cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab)); + * }); + * + * // load the sprite frame (project/assets/resources/imgs/cocos.png/cocos) from resources folder + * cc.loader.loadRes('imgs/cocos', cc.SpriteFrame, function (err, spriteFrame) { + * if (err) { + * cc.error(err.message || err); + * return; + * } + * cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame)); * }); */ - loadRes: function (url, completeCallback) { - var uuid = this._getResUuid(url); + loadRes: function (url, type, completeCallback) { + if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { + completeCallback = type; + type = null; + } + var uuid = this._getResUuid(url, type); if (uuid) { this.load( { @@ -289,7 +302,14 @@ JS.mixin(cc.loader, { } else { callInNextTick(function () { - completeCallback.call(null, new Error('Resources url "' + url + '" does not exist.'), null); + var info; + if (type) { + info = cc.js.getClassName(type) + ' in "' + url + '" does not exist.'; + } + else { + info = 'Resources url "' + url + '" does not exist.'; + } + completeCallback(new Error(info), null); }); } }, @@ -302,14 +322,15 @@ JS.mixin(cc.loader, { * @method loadResAll * @param {String} url - Url of the target folder. * The url is relative to the "resources" folder. + * @param {Function} [type] - Only asset of type will be loaded if this argument is supplied. * @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. * * @example * - * // load the sprite frame (project/assets/resources/imgs/cocos.png/cocos) from resources folder - * cc.loader.loadResAll('imgs/cocos.png', function (err, assets) { + * // load the texture (resources/imgs/cocos.png) and sprite frame (resources/imgs/cocos.png/cocos) + * cc.loader.loadResAll('imgs/cocos', function (err, assets) { * if (err) { * cc.error(err); * return; @@ -318,8 +339,12 @@ JS.mixin(cc.loader, { * var spriteFrame = assets[1]; * }); */ - loadResAll: function (url, completeCallback) { - var uuids = resources.getUuidArray(url); + loadResAll: function (url, type, completeCallback) { + if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { + completeCallback = type; + type = null; + } + var uuids = resources.getUuidArray(url, type); var remain = uuids.length; if (remain > 0) { var results = []; @@ -353,7 +378,7 @@ JS.mixin(cc.loader, { } else { callInNextTick(function () { - completeCallback.call(null, null, []); + completeCallback(null, []); }); } }, diff --git a/cocos2d/core/load-pipeline/asset-table.js b/cocos2d/core/load-pipeline/asset-table.js index 00c3919be1..b346016d89 100644 --- a/cocos2d/core/load-pipeline/asset-table.js +++ b/cocos2d/core/load-pipeline/asset-table.js @@ -32,6 +32,11 @@ function AssetTable () { this._pathToUuid = {}; } +function Entry (uuid, type) { + this.uuid = uuid; + this.type = type; +} + cc.js.mixin(AssetTable.prototype, { ///** @@ -47,30 +52,63 @@ cc.js.mixin(AssetTable.prototype, { // return (path in this._pathToUuid); //}, - getUuid: function (path) { + getUuid: function (path, type) { path = cc.url.normalize(path); if ( !path ) { return ''; } - var uuid = this._pathToUuid[path]; - if (uuid) { - return uuid; + var isChildClassOf = cc.isChildClassOf; + var p2u = this._pathToUuid; + var item = p2u[path]; + if (item && (!type || isChildClassOf(item.type, type))) { + return item.uuid; + } + + var p; + if (type) { + for (p in p2u) { + if (p.startsWith(path)) { + var item = p2u[p]; + if (isChildClassOf(item.type, type)) { + return item.uuid; + } + } + } } else { - return ''; + for (p in p2u) { + if (p.startsWith(path)) { + return p2u[p].uuid; + } + } } + return ''; }, - getUuidArray: function (path) { + getUuidArray: function (path, type) { path = cc.url.normalize(path); if ( !path ) { return []; } var uuids = []; var p2u = this._pathToUuid; - for (var p in p2u) { - if (p.startsWith(path)) { - uuids.push(p2u[p]); + var p; + if (type) { + var isChildClassOf = cc.isChildClassOf; + for (p in p2u) { + if (p.startsWith(path)) { + var item = p2u[p]; + if (isChildClassOf(item.type, type)) { + uuids.push(item.uuid); + } + } + } + } + else { + for (p in p2u) { + if (p.startsWith(path)) { + uuids.push(p2u[p].uuid); + } } } return uuids; @@ -87,15 +125,16 @@ cc.js.mixin(AssetTable.prototype, { /** * @method add - * @param {string} path - the path to load, should NOT include filename extensions. - * @param {string} uuid + * @param {String} path - the path to load, should NOT include filename extensions. + * @param {String} uuid + * @param {Function} type * @private */ - add: function (path, uuid) { + add: function (path, uuid, type) { //// remove extname //// (can not use path.slice because length of extname maybe 0) //path = path.substring(0, path - cc.path.extname(path).length); - this._pathToUuid[path] = uuid; + this._pathToUuid[path] = new Entry(uuid, type); }, _removeByPath: function (path) { delete this._pathToUuid[path]; diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 3aca0db881..7f53922603 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -109,7 +109,7 @@ var AssetLibrary = { _getAssetInfoInRuntime: function (uuid) { var info = _uuidToRawAssets[uuid]; - if (info && info.raw) { + if (info && !cc.isChildClassOf(info.type, cc.Asset)) { return { url: _rawAssetsBase + info.url, raw: true, @@ -295,15 +295,20 @@ var AssetLibrary = { var assets = rawAssets[mountPoint]; for (var uuid in assets) { var info = assets[uuid]; - var url = info.url; - var raw = info.raw; + var url = info[0]; + var typeId = info[1]; + var type = cc.js._getClassById(typeId); + if (!type) { + cc.error('Cannot get', typeId); + continue; + } _uuidToRawAssets[uuid] = { url: mountPoint + '/' + url, - raw: !!raw, + type: type, }; // init resources if (mountPoint === 'assets' && url.startsWith(RES_DIR)) { - if ( !raw ) { + if (cc.isChildClassOf(type, Asset)) { var ext = cc.path.extname(url); if (ext) { // trim base dir and extname @@ -318,7 +323,7 @@ var AssetLibrary = { url = url.slice(RES_DIR.length); } // register - Loader._resources.add(url, uuid); + Loader._resources.add(url, uuid, type); } } } diff --git a/test/qunit/unit/test-resources-loader.js b/test/qunit/unit/test-resources-loader.js index d9b92c73c6..6cc1e1afa9 100644 --- a/test/qunit/unit/test-resources-loader.js +++ b/test/qunit/unit/test-resources-loader.js @@ -8,16 +8,16 @@ rawAssetsBase: cc.path.dirname(cc.path._setEndWithSep(assetDir, false) + '.dummyExtForDirname') + '/', rawAssets: { assets: { - '748321': { url: 'resources/grossini.png', raw: true }, - '1232218': { url: 'resources/grossini.png/grossini', raw: false } + '748321': ['resources/grossini.png', cc.js._getClassId(cc.Texture2D)], + '1232218': ['resources/grossini.png/grossini', cc.js._getClassId(TestSprite)] } } }); - } + cc.loader.releaseAll(); + }, }); asyncTest('load single', function () { - cc.loader.releaseAll(); cc.loader.loadRes('grossini.png', function (err, texture) { ok(texture instanceof cc.Texture2D, 'should be able to load texture with file extname'); clearTimeout(timeoutId); @@ -30,8 +30,20 @@ }, 5000); }); + asyncTest('load single by type', function () { + cc.loader.loadRes('grossini', TestSprite, function (err, sprite) { + ok(sprite instanceof TestSprite, '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 () { - cc.loader.releaseAll(); cc.loader.loadResAll('grossini.png', function (err, results) { ok(Array.isArray(results), 'result should be an array'); var texture = results[0] instanceof cc.Texture2D ? results[0] : results[1]; @@ -49,4 +61,19 @@ }, 5000); }); + asyncTest('loadResAll by type', function () { + cc.loader.loadResAll('grossini', TestSprite, function (err, results) { + ok(Array.isArray(results), 'result should be an array'); + 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); + }); })(); From 612ad35614e91e1618c74513214ff1b0c893345c Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 10 May 2016 18:59:14 +0800 Subject: [PATCH 2/3] loadRes should not support sub asset path to avoid confusing with folder --- cocos2d/core/load-pipeline/CCLoader.js | 5 +- cocos2d/core/load-pipeline/asset-table.js | 152 +++++++++++------- cocos2d/core/platform/CCAssetLibrary.js | 5 +- cocos2d/core/platform/CCClass.js | 2 +- test/qunit/assets/library/12/123201.json | 16 ++ .../assets/resources/grossini/grossini.png | Bin 0 -> 1521 bytes test/qunit/unit/test-prefab.js | 6 + test/qunit/unit/test-resources-loader.js | 62 +++++-- 8 files changed, 175 insertions(+), 73 deletions(-) create mode 100644 test/qunit/assets/library/12/123201.json create mode 100644 test/qunit/assets/resources/grossini/grossini.png diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 814f96f39d..0c648ff93c 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -246,6 +246,9 @@ JS.mixin(cc.loader, { // strip extname url = url.slice(0, - extname.length); uuid = resources.getUuid(url, type); + if (uuid) { + cc.warn('loadRes: should not specify the extname in ' + url + extname); + } } } return uuid; @@ -321,7 +324,7 @@ JS.mixin(cc.loader, { * * @method loadResAll * @param {String} url - Url of the target folder. - * The url is relative to the "resources" 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} 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. diff --git a/cocos2d/core/load-pipeline/asset-table.js b/cocos2d/core/load-pipeline/asset-table.js index b346016d89..f63c4e8ee9 100644 --- a/cocos2d/core/load-pipeline/asset-table.js +++ b/cocos2d/core/load-pipeline/asset-table.js @@ -23,6 +23,11 @@ THE SOFTWARE. ****************************************************************************/ +function Entry (uuid, type) { + this.uuid = uuid; + this.type = type; +} + /** * AssetTable is used to find asset's uuid by url * @class AssetTable @@ -32,9 +37,12 @@ function AssetTable () { this._pathToUuid = {}; } -function Entry (uuid, type) { - this.uuid = uuid; - this.type = type; +function isMatchByWord (path, test) { + if (path.length > test.length) { + var nextAscii = path.charCodeAt(test.length); + return (nextAscii === 46 || nextAscii === 47); // '.' or '/' + } + return true; } cc.js.mixin(AssetTable.prototype, { @@ -54,91 +62,122 @@ cc.js.mixin(AssetTable.prototype, { getUuid: function (path, type) { path = cc.url.normalize(path); - if ( !path ) { - return ''; - } - var isChildClassOf = cc.isChildClassOf; - var p2u = this._pathToUuid; - var item = p2u[path]; - if (item && (!type || isChildClassOf(item.type, type))) { - return item.uuid; - } - - var p; - if (type) { - for (p in p2u) { - if (p.startsWith(path)) { - var item = p2u[p]; - if (isChildClassOf(item.type, type)) { - return item.uuid; + var item = this._pathToUuid[path]; + if (item) { + if (Array.isArray(item)) { + if (type) { + for (var i = 0; i < item.length; i++) { + var entry = item[i]; + if (cc.isChildClassOf(entry.type, type)) { + return entry.uuid; + } } } - } - } - else { - for (p in p2u) { - if (p.startsWith(path)) { - return p2u[p].uuid; + else { + return item[0].uuid; } } + else if (!type || cc.isChildClassOf(item.type, type)) { + return item.uuid; + } } return ''; }, getUuidArray: function (path, type) { path = cc.url.normalize(path); - if ( !path ) { - return []; - } + var path2uuid = this._pathToUuid; var uuids = []; - var p2u = this._pathToUuid; - var p; + var p, i; if (type) { var isChildClassOf = cc.isChildClassOf; - for (p in p2u) { - if (p.startsWith(path)) { - var item = p2u[p]; - if (isChildClassOf(item.type, type)) { - uuids.push(item.uuid); + 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); + } + } + } + else { + if (isChildClassOf(item.type, type)) { + uuids.push(item.uuid); + } } } } } else { - for (p in p2u) { - if (p.startsWith(path)) { - uuids.push(p2u[p].uuid); + 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 { + uuids.push(item.uuid); + } } } } return uuids; }, - /** - * Returns all asset paths in the table. - * @method getAllPaths - * @return {string[]} - */ - getAllPaths: function () { - return Object.keys(this._pathToUuid); - }, + ///** + // * Returns all asset paths in the table. + // * @method getAllPaths + // * @return {string[]} + // */ + //getAllPaths: function () { + // return Object.keys(this._pathToUuid); + //}, /** * @method add * @param {String} path - the path to load, should NOT include filename extensions. * @param {String} uuid * @param {Function} type + * @param {Boolean} isMainAsset * @private */ - add: function (path, uuid, type) { - //// remove extname - //// (can not use path.slice because length of extname maybe 0) - //path = path.substring(0, path - cc.path.extname(path).length); - this._pathToUuid[path] = new Entry(uuid, type); + add: function (path, uuid, type, isMainAsset) { + // remove extname + // (can not use path.slice because length of extname maybe 0) + path = path.substring(0, path.length - cc.path.extname(path).length); + var newEntry = new Entry(uuid, type); + var pathToUuid = this._pathToUuid; + var exists = pathToUuid[path]; + if (exists) { + if (Array.isArray(exists)) { + if (isMainAsset) { + // load main asset first + exists.unshift(newEntry); + } + else { + exists.push(newEntry); + } + } + else { + if (isMainAsset) { + pathToUuid[path] = [newEntry, exists]; + } + else { + pathToUuid[path] = [exists, newEntry]; + } + } + } + else { + pathToUuid[path] = newEntry; + } }, - _removeByPath: function (path) { - delete this._pathToUuid[path]; - } + //_removeByPath: function (path) { + // delete this._pathToUuid[path]; + //} //_removeByUuid: function (uuid) { // for (var path in this._pathToUuid) { // if (this._pathToUuid[path] === uuid) { @@ -147,6 +186,9 @@ cc.js.mixin(AssetTable.prototype, { // } // } //} + reset: function () { + this._pathToUuid = {} + } }); module.exports = AssetTable; diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 7f53922603..932ceec68a 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -288,6 +288,8 @@ var AssetLibrary = { _rawAssetsBase = options.rawAssetsBase; _uuidToRawAssets = {}; + var resources = Loader._resources; + resources.reset(); var rawAssets = options.rawAssets; if (rawAssets) { var RES_DIR = 'resources/'; @@ -322,8 +324,9 @@ var AssetLibrary = { else { url = url.slice(RES_DIR.length); } + var isSubAsset = info[2] === 1; // register - Loader._resources.add(url, uuid, type); + resources.add(url, uuid, type, !isSubAsset); } } } diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index 30f049620b..c9ec547e8d 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -341,7 +341,7 @@ cc.isChildClassOf = function (subclass, superclass) { return true; } // js class - var dunderProto = Object.getPrototypeOf(subclass.prototype); + var dunderProto = subclass.prototype && Object.getPrototypeOf(subclass.prototype); while (dunderProto) { subclass = dunderProto.constructor; if (subclass === superclass) { diff --git a/test/qunit/assets/library/12/123201.json b/test/qunit/assets/library/12/123201.json new file mode 100644 index 0000000000..945e8dc6b4 --- /dev/null +++ b/test/qunit/assets/library/12/123201.json @@ -0,0 +1,16 @@ +{ + "__type__": "TestSprite", + "rawTexture": null, + "texture": { + "__uuid__": "123200" + }, + "rotated": false, + "trim": false, + "trimThreshold": 1, + "trimLeft": 10, + "trimTop": 20, + "width": 89, + "height": 44, + "x": 0, + "y": 49 +} \ No newline at end of file diff --git a/test/qunit/assets/resources/grossini/grossini.png b/test/qunit/assets/resources/grossini/grossini.png new file mode 100644 index 0000000000000000000000000000000000000000..afb316017ef42f03f5bac55370552f14be5c75f1 GIT binary patch literal 1521 zcmZvcdo&XY9LGnScx zwT)X7Av%SdJZI!xIJg{BZX|i^u7CRDp8GrJ`}_RyJHPXL{PE2S@bgqv(o+He0IJ?z z?r0gYGTl0;Aj@7wWNjG~F8X-7%P5O&x0YlX6t8*(Qvd*E=r_p$3X63B0MKvV?k*(*$#hd4l=!xn4DY#{y%mj5TxXk~a(Phx5KnDHlaLS+TlSazCry zuF%3LAlNGS0Np2LCrB=lgG7n`U$19Xc->T7Wl}q*hOU?9w0i&$PHs~r+i$^7DTh~1 ze}NSM^ThHSgh+cZ@(3S}$vk;4Wt)dC3IzSZVx!AOCrprX#s<$Vt#esQVuQgU5zrEX zmACL#u8VPGLfJL$<%@@*Pi^Zq=V_RwTiL`<^W`z$hNt!zW7&G^ey(rOx>b-(C=YA6 z0*lFB7hA?6Hk)F!_~Qg@vcW%sNqhL>BnmXY&{$(y*^7h=i*$$ z!o-iGLq?3M9_hsl6vFfD1H#d4XKzH~m~M(Z)OC7_;h7m(*ysE9RUrf}H?3c{_7nN7 zwz1iHw$bS2IDs{LYyg^>jEUfkhiI-W-dNX6nPW{TX*HGZdFvtN%(h_l$mSx{jz$04 z(zr@Kzw-PUVF60B|3wlmr%%#(=vPV-xj;Z04lD7iu)eI4orjH{C(R%EToiU`( zg;I438+GvpSQB9g$QC@_O-Z%II@+ZJB2*w!lF=o*A{w*EV~&N_-_Ma_o^TGnq&%1# zSL;yxBJ6e+?D?Fj@7%)sE}Ov6-zR5P^1?gISSej6x?210iR{rzk2@uZItyHbCLDoW zF3`a!A)X30r_d+WR%IvHYIVMTJN4u3gy*$8!}}#bv&e=z78Pv40a_`{SSsd3rct%o z8GEPz6XW!cfBJ#9CKQ}zXWA5CIIS&dR>tCkyJ_lkDxjB$O_&y{k2{8^*LqODT$%ja z0oKp4x?v$UD!bNV%l|o!CUI(xZ7j(=uxi6@Lps+T!#W8^aYEcg{Qd^s^aX@Y1(P_b zJe!y}0q8N+W92|P7rAE}+bkhvfWQha-FZDkLmfgS=Z8uB=g9`Kg#JE|MtJXjP&h}# zgE!l_w5|X{=nFt4n^PQg{pm8qC>~v0h3_9w)i_k>TU~`IGURP8#qJQIh-Ql%cRgu! zc59TUlz`biisSV6T883=Xh%%VMl1iRHxHV<`sGx8$5tcBuVJzqNg0LjJwOe`!Rhzk z0T*h|80FBrmPCoGwTbUa;a(?AO*?9k^X+z{K^#@~;4fkQdlAAXDpB%E7W^uO83G7s zf4^MFugTc^TunN&9F#OdghcVSHPob?k!HzaLY{-TDM;f=EaG9@feF}NQx(MSt_hYw z7-sBt!>!*W)y-5EJvcD!A2NM05<>0c#GoNUw+mbS^o``Kw(( za{hlkDYd5z#B@=Hr(w!PJ*|m`;t^O%&HN9^X%1#$|B>(2|8L`}1*|St(s0AH)(dT7 zD<*BwjS}DiT8VA=Ani68nQ5T*km%S@pX*Dn*P8XNosi?U{{R)c?hNpdBU_(umPF!mWr$l^n NZf_4icfMc6|Y#xwu` literal 0 HcmV?d00001 diff --git a/test/qunit/unit/test-prefab.js b/test/qunit/unit/test-prefab.js index 3b0c76623f..b3bb4874ee 100644 --- a/test/qunit/unit/test-prefab.js +++ b/test/qunit/unit/test-prefab.js @@ -137,6 +137,12 @@ notEqual(first.uuid, second.uuid, 'The id of instances should be different'); }); + test('apply node', function () { + var newNode = cc.instantiate(prefab); + var newPrefab = _Scene.PrefabUtils.createAppliedPrefab(newNode); + strictEqual(newPrefab.data._prefab.fileId, prefab.data._prefab.fileId, "fileId should not changed during apply"); + }); + asyncTest('revert prefab', function () { // stub cc.loader.insertPipe({ diff --git a/test/qunit/unit/test-resources-loader.js b/test/qunit/unit/test-resources-loader.js index 6cc1e1afa9..8caa10707f 100644 --- a/test/qunit/unit/test-resources-loader.js +++ b/test/qunit/unit/test-resources-loader.js @@ -8,8 +8,11 @@ rawAssetsBase: cc.path.dirname(cc.path._setEndWithSep(assetDir, false) + '.dummyExtForDirname') + '/', rawAssets: { assets: { - '748321': ['resources/grossini.png', cc.js._getClassId(cc.Texture2D)], - '1232218': ['resources/grossini.png/grossini', cc.js._getClassId(TestSprite)] + '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 } } }); @@ -17,12 +20,32 @@ }, }); - asyncTest('load single', function () { - cc.loader.loadRes('grossini.png', function (err, texture) { - ok(texture instanceof cc.Texture2D, 'should be able to load texture with file extname'); - clearTimeout(timeoutId); - start(); + asyncTest('matching rules - 1', function () { + cc.loader.loadRes('grossini', TestSprite, function (err, sprite) { + ok(sprite._uuid === '1232218' || sprite._uuid === '123200', 'loadRes - checking uuid'); + + cc.loader.loadResAll('grossini', TestSprite, function (err, array) { + strictEqual(array.length, 3, 'loadResAll - checking count'); + ['123200', '1232218', '123201'].forEach(function (uuid) { + ok(array.some(function (item) { + return item._uuid === uuid; + }), 'loadResAll - checking uuid ' + uuid); + }); + start(); + }); }); + }); + + asyncTest('load single', function () { + //cc.loader.loadRes('grossini/grossini.png', function (err, texture) { + // ok(!texture, 'could not load texture with file extname'); + + 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!'); @@ -33,8 +56,12 @@ asyncTest('load single by type', function () { cc.loader.loadRes('grossini', TestSprite, function (err, sprite) { ok(sprite instanceof TestSprite, 'should be able to load asset by type'); - clearTimeout(timeoutId); - start(); + + 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 () { @@ -44,13 +71,18 @@ }); asyncTest('load main asset and sub asset by loadAll', function () { - cc.loader.loadResAll('grossini.png', function (err, results) { + cc.loader.loadResAll('grossini', function (err, results) { ok(Array.isArray(results), 'result should be an array'); - var texture = results[0] instanceof cc.Texture2D ? results[0] : results[1]; - var sprite = results[0] instanceof TestSprite ? results[0] : results[1]; - ok(texture instanceof cc.Texture2D, 'should be able to load texture'); - ok(sprite instanceof TestSprite, 'should be able to load test sprite'); - + ['123200', '1232218', '123201'].forEach(function (uuid) { + ok(results.some(function (item) { + return item._uuid === uuid; + }), 'checking uuid ' + uuid); + }); + ['resources/grossini/grossini.png', 'resources/grossini.png',].forEach(function (url) { + ok(results.some(function (item) { + return item.url && item.url.endsWith(url); + }), 'checking url ' + url); + }); clearTimeout(timeoutId); start(); }); From dfad718bc83e9ffd5c4c7d5f1516f1a859815719 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 11 May 2016 13:52:53 +0800 Subject: [PATCH 3/3] update pr --- cocos2d/core/load-pipeline/CCLoader.js | 10 +++++++ cocos2d/core/load-pipeline/asset-table.js | 34 +---------------------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 0c648ff93c..4fee5d3692 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -341,6 +341,16 @@ JS.mixin(cc.loader, { * var texture = assets[0]; * var spriteFrame = assets[1]; * }); + * + * // load all textures in "resources/imgs/" + * cc.loader.loadResAll('imgs', cc.Texture2D, function (err, textures) { + * if (err) { + * cc.error(err); + * return; + * } + * var texture1 = textures[0]; + * var texture2 = textures[1]; + * }); */ loadResAll: function (url, type, completeCallback) { if (!completeCallback && type && !cc.isChildClassOf(type, cc.RawAsset)) { diff --git a/cocos2d/core/load-pipeline/asset-table.js b/cocos2d/core/load-pipeline/asset-table.js index f63c4e8ee9..5fad501b5c 100644 --- a/cocos2d/core/load-pipeline/asset-table.js +++ b/cocos2d/core/load-pipeline/asset-table.js @@ -46,19 +46,6 @@ function isMatchByWord (path, test) { } cc.js.mixin(AssetTable.prototype, { - - ///** - // * Check if the table contains a specific object.
- // *
- // * Note: All asset paths in Creator use forward slashes, paths using backslashes will not work. - // * - // * @method contains - // * @param {string} path - (not support wildcard) - // * @return {boolean} - // */ - //contains: function (path) { - // return (path in this._pathToUuid); - //}, getUuid: function (path, type) { path = cc.url.normalize(path); @@ -127,15 +114,6 @@ cc.js.mixin(AssetTable.prototype, { } return uuids; }, - - ///** - // * Returns all asset paths in the table. - // * @method getAllPaths - // * @return {string[]} - // */ - //getAllPaths: function () { - // return Object.keys(this._pathToUuid); - //}, /** * @method add @@ -175,17 +153,7 @@ cc.js.mixin(AssetTable.prototype, { pathToUuid[path] = newEntry; } }, - //_removeByPath: function (path) { - // delete this._pathToUuid[path]; - //} - //_removeByUuid: function (uuid) { - // for (var path in this._pathToUuid) { - // if (this._pathToUuid[path] === uuid) { - // delete this._pathToUuid[path]; - // return; - // } - // } - //} + reset: function () { this._pathToUuid = {} }