mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
@@ -238,15 +238,17 @@ 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);
|
||||
if (uuid) {
|
||||
cc.warn('loadRes: should not specify the extname in ' + url + extname);
|
||||
}
|
||||
}
|
||||
}
|
||||
return uuid;
|
||||
@@ -260,23 +262,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 +305,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);
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -301,15 +324,16 @@ 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.
|
||||
* @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;
|
||||
@@ -317,9 +341,23 @@ 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, 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 +391,7 @@ JS.mixin(cc.loader, {
|
||||
}
|
||||
else {
|
||||
callInNextTick(function () {
|
||||
completeCallback.call(null, null, []);
|
||||
completeCallback(null, []);
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
@@ -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,82 +37,126 @@ function AssetTable () {
|
||||
this._pathToUuid = {};
|
||||
}
|
||||
|
||||
cc.js.mixin(AssetTable.prototype, {
|
||||
function isMatchByWord (path, test) {
|
||||
if (path.length > test.length) {
|
||||
var nextAscii = path.charCodeAt(test.length);
|
||||
return (nextAscii === 46 || nextAscii === 47); // '.' or '/'
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
///**
|
||||
// * Check if the table contains a specific object. <br/>
|
||||
// * <br/>
|
||||
// * 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);
|
||||
//},
|
||||
cc.js.mixin(AssetTable.prototype, {
|
||||
|
||||
getUuid: function (path) {
|
||||
getUuid: function (path, type) {
|
||||
path = cc.url.normalize(path);
|
||||
if ( !path ) {
|
||||
return '';
|
||||
}
|
||||
var uuid = this._pathToUuid[path];
|
||||
if (uuid) {
|
||||
return uuid;
|
||||
}
|
||||
else {
|
||||
return '';
|
||||
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 {
|
||||
return item[0].uuid;
|
||||
}
|
||||
}
|
||||
else if (!type || cc.isChildClassOf(item.type, type)) {
|
||||
return item.uuid;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
},
|
||||
|
||||
getUuidArray: function (path) {
|
||||
getUuidArray: function (path, type) {
|
||||
path = cc.url.normalize(path);
|
||||
if ( !path ) {
|
||||
return [];
|
||||
}
|
||||
var path2uuid = this._pathToUuid;
|
||||
var uuids = [];
|
||||
var p2u = this._pathToUuid;
|
||||
for (var p in p2u) {
|
||||
if (p.startsWith(path)) {
|
||||
uuids.push(p2u[p]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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 {
|
||||
uuids.push(item.uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return uuids;
|
||||
},
|
||||
|
||||
/**
|
||||
* 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 {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) {
|
||||
//// 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;
|
||||
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];
|
||||
|
||||
reset: function () {
|
||||
this._pathToUuid = {}
|
||||
}
|
||||
//_removeByUuid: function (uuid) {
|
||||
// for (var path in this._pathToUuid) {
|
||||
// if (this._pathToUuid[path] === uuid) {
|
||||
// delete this._pathToUuid[path];
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
});
|
||||
|
||||
module.exports = AssetTable;
|
||||
|
||||
@@ -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,
|
||||
@@ -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/';
|
||||
@@ -295,15 +297,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
|
||||
@@ -317,8 +324,9 @@ var AssetLibrary = {
|
||||
else {
|
||||
url = url.slice(RES_DIR.length);
|
||||
}
|
||||
var isSubAsset = info[2] === 1;
|
||||
// register
|
||||
Loader._resources.add(url, uuid);
|
||||
resources.add(url, uuid, type, !isSubAsset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
@@ -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({
|
||||
|
||||
@@ -8,18 +8,81 @@
|
||||
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 }
|
||||
'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
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
cc.loader.releaseAll();
|
||||
},
|
||||
});
|
||||
|
||||
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.releaseAll();
|
||||
cc.loader.loadRes('grossini.png', function (err, texture) {
|
||||
ok(texture instanceof cc.Texture2D, 'should be able to load texture with file extname');
|
||||
//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!');
|
||||
start();
|
||||
}, 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');
|
||||
|
||||
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 () {
|
||||
cc.loader.loadResAll('grossini', function (err, results) {
|
||||
ok(Array.isArray(results), 'result should be an array');
|
||||
['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();
|
||||
});
|
||||
@@ -30,13 +93,10 @@
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
asyncTest('load main asset and sub asset by loadAll', function () {
|
||||
cc.loader.releaseAll();
|
||||
cc.loader.loadResAll('grossini.png', function (err, results) {
|
||||
asyncTest('loadResAll by type', function () {
|
||||
cc.loader.loadResAll('grossini', TestSprite, 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');
|
||||
var sprite = results[0];
|
||||
ok(sprite instanceof TestSprite, 'should be able to load test sprite');
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
@@ -48,5 +108,4 @@
|
||||
start();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user