mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Merge pull request #1364 from pandamicro/master
Always use url as reference key in cc.loader
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
module.exports = {
|
||||
setFontToNode: function(fontAsset, node) {
|
||||
if (fontAsset) {
|
||||
var config = {type:'font', name: fontAsset.fontFamily, id:fontAsset.url, srcs:[fontAsset.url]};
|
||||
var config = {type:'font', name: fontAsset.fontFamily, url:fontAsset.url, srcs:[fontAsset.url]};
|
||||
cc.loader.load(config, function (err, result) {
|
||||
if (err) throw err;
|
||||
|
||||
|
||||
@@ -774,7 +774,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{
|
||||
var info = this._getSceneUuid(sceneName);
|
||||
if (info) {
|
||||
this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
|
||||
cc.loader.load({ id: info.uuid, type: 'uuid' }, function (error, asset) {
|
||||
cc.loader.load({ uuid: info.uuid, type: 'uuid' }, function (error, asset) {
|
||||
if (error) {
|
||||
cc.error('Failed to preload "%s", %s', sceneName, error.message);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ var BitmapFont = cc.Class({
|
||||
_preloadRawFiles: function (done) {
|
||||
// preload fnt file. (texture already loaded in properties)
|
||||
var fntUrl = this.rawUrl;
|
||||
cc.loader.load({ id: fntUrl, ignoreMaxConcurrency: true }, done);
|
||||
cc.loader.load({ url: fntUrl, ignoreMaxConcurrency: true }, done);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -38,6 +38,32 @@ function getXMLHttpRequest () {
|
||||
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('MSXML2.XMLHTTP');
|
||||
}
|
||||
|
||||
// Convert a resources by finding its real url with uuid, otherwise we will use the uuid or raw url as its url
|
||||
// So we gurantee there will be url in result
|
||||
function getResWithUrl (res) {
|
||||
var id, url, result;
|
||||
if (typeof res === 'object') {
|
||||
result = res;
|
||||
if (res.url) {
|
||||
return res.url;
|
||||
}
|
||||
else {
|
||||
id = res.uuid;
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = {};
|
||||
id = res;
|
||||
}
|
||||
url = cc.AssetLibrary._getAssetUrl(id);
|
||||
result.url = url || id;
|
||||
if (url && result.type === 'uuid' && cc.AssetLibrary._isRawAsset(id)) {
|
||||
result.type = null;
|
||||
result.isRawAsset = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
var _sharedResources = [];
|
||||
var _sharedList = [];
|
||||
|
||||
@@ -131,7 +157,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
* 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.loader.load({url: '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));
|
||||
* });
|
||||
*
|
||||
@@ -171,18 +197,13 @@ JS.mixin(CCLoader.prototype, {
|
||||
resources = resources ? [resources] : [];
|
||||
}
|
||||
|
||||
_sharedResources.length = resources.length;
|
||||
_sharedResources.length = 0;
|
||||
for (var i = 0; i < resources.length; ++i) {
|
||||
var url = resources[i].id || resources[i];
|
||||
if (typeof url !== 'string')
|
||||
var res = getResWithUrl(resources[i]);
|
||||
if (!res.url && !res.uuid)
|
||||
continue;
|
||||
var item = this.getItem(url);
|
||||
if (item) {
|
||||
_sharedResources[i] = item;
|
||||
}
|
||||
else {
|
||||
_sharedResources[i] = resources[i];
|
||||
}
|
||||
var item = this.getItem(res.url);
|
||||
_sharedResources.push(item || res);
|
||||
}
|
||||
|
||||
var queue = LoadingItems.create(this, progressCallback, function (errors, items) {
|
||||
@@ -191,7 +212,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
return;
|
||||
|
||||
if (singleRes) {
|
||||
var id = singleRes.id || singleRes;
|
||||
var id = singleRes.url || singleRes.uuid || singleRes;
|
||||
completeCallback.call(self, items.getError(id), items.getContent(id));
|
||||
}
|
||||
else {
|
||||
@@ -221,17 +242,17 @@ JS.mixin(CCLoader.prototype, {
|
||||
|
||||
_sharedList.length = 0;
|
||||
for (var i = 0; i < urlList.length; ++i) {
|
||||
var url = urlList[i].id || urlList[i];
|
||||
if (typeof url !== 'string')
|
||||
var res = getResWithUrl(urlList[i]);
|
||||
if (!res.url && ! res.uuid)
|
||||
continue;
|
||||
var item = this.getItem(url);
|
||||
var item = this.getItem(res.url);
|
||||
if (item) {
|
||||
_sharedList.push(item);
|
||||
// Collect deps to avoid circle reference
|
||||
owner && owner.deps.push(item);
|
||||
}
|
||||
else {
|
||||
_sharedList.push(urlList[i]);
|
||||
_sharedList.push(res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,6 +297,21 @@ JS.mixin(CCLoader.prototype, {
|
||||
}
|
||||
return uuid;
|
||||
},
|
||||
// Find the asset's reference id in loader, asset could be asset object, asset uuid or asset url
|
||||
_getReferenceKey: function (assetOrUrlOrUuid) {
|
||||
var key;
|
||||
if (typeof assetOrUrlOrUuid === 'string') {
|
||||
key = this._getResUuid(assetOrUrlOrUuid) || assetOrUrlOrUuid;
|
||||
}
|
||||
else if (typeof assetOrUrlOrUuid === 'object') {
|
||||
key = assetOrUrlOrUuid._uuid || null;
|
||||
}
|
||||
else if (CC_DEV) {
|
||||
cc.warn('unknown asset type: ' + assetOrUrlOrUuid);
|
||||
}
|
||||
key = cc.AssetLibrary._getAssetUrl(key) || key;
|
||||
return key;
|
||||
},
|
||||
|
||||
/**
|
||||
* Load resources from the "resources" folder inside the "assets" folder of your project.<br>
|
||||
@@ -320,7 +356,6 @@ JS.mixin(CCLoader.prototype, {
|
||||
if (uuid) {
|
||||
this.load(
|
||||
{
|
||||
id: uuid,
|
||||
type: 'uuid',
|
||||
uuid: uuid
|
||||
},
|
||||
@@ -442,12 +477,13 @@ JS.mixin(CCLoader.prototype, {
|
||||
*/
|
||||
getRes: function (url, type) {
|
||||
var item = this._cache[url];
|
||||
if (item && item.alias) {
|
||||
item = this._cache[item.alias];
|
||||
}
|
||||
if (!item) {
|
||||
var uuid = this._getResUuid(url, type);
|
||||
item = this._cache[uuid];
|
||||
var ref = this._getReferenceKey(uuid);
|
||||
item = this._cache[ref];
|
||||
}
|
||||
if (item && item.alias) {
|
||||
item = this._cache[item.alias];
|
||||
}
|
||||
return (item && item.complete) ? item.content : null;
|
||||
},
|
||||
@@ -491,22 +527,10 @@ JS.mixin(CCLoader.prototype, {
|
||||
* @returns {Array}
|
||||
*/
|
||||
getDependsRecursively: function (owner) {
|
||||
var uuid;
|
||||
if (typeof owner === 'string') {
|
||||
uuid = owner;
|
||||
if (!this._cache[uuid]) {
|
||||
// Not found in cache then try to search res,
|
||||
// If res not found, keep the original value
|
||||
uuid = this._getResUuid(uuid) || uuid;
|
||||
}
|
||||
}
|
||||
else if (typeof owner === 'object') {
|
||||
uuid = owner._uuid || null;
|
||||
}
|
||||
|
||||
if (uuid) {
|
||||
var assets = AutoReleaseUtils.getDependsRecursively(uuid);
|
||||
assets.push(uuid);
|
||||
if (owner) {
|
||||
var key = this._getReferenceKey(owner);
|
||||
var assets = AutoReleaseUtils.getDependsRecursively(key);
|
||||
assets.push(key);
|
||||
return assets;
|
||||
}
|
||||
else {
|
||||
@@ -555,7 +579,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
AutoReleaseUtils.autoRelease(this, asset);
|
||||
}
|
||||
else if (asset) {
|
||||
var id = asset._uuid || asset.url || asset;
|
||||
var id = this._getReferenceKey(asset);
|
||||
var item = this.getItem(id);
|
||||
if (item) {
|
||||
var removed = this.removeItem(id);
|
||||
@@ -678,7 +702,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
* @param {Boolean} autoRelease - indicates whether should release automatically
|
||||
*/
|
||||
setAutoRelease: function (assetOrUrlOrUuid, autoRelease) {
|
||||
var key = AutoReleaseUtils.getKey(this, assetOrUrlOrUuid);
|
||||
var key = this._getReferenceKey(assetOrUrlOrUuid);
|
||||
if (key) {
|
||||
this._autoReleaseSetting[key] = !!autoRelease;
|
||||
}
|
||||
@@ -719,7 +743,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
*/
|
||||
setAutoReleaseRecursively: function (assetOrUrlOrUuid, autoRelease) {
|
||||
autoRelease = !!autoRelease;
|
||||
var key = AutoReleaseUtils.getKey(this, assetOrUrlOrUuid);
|
||||
var key = this._getReferenceKey(assetOrUrlOrUuid);
|
||||
if (key) {
|
||||
this._autoReleaseSetting[key] = autoRelease;
|
||||
|
||||
@@ -749,7 +773,7 @@ JS.mixin(CCLoader.prototype, {
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
isAutoRelease: function (assetOrUrl) {
|
||||
var key = AutoReleaseUtils.getKey(this, assetOrUrl);
|
||||
var key = this._getReferenceKey(assetOrUrl);
|
||||
if (key) {
|
||||
return !!this._autoReleaseSetting[key];
|
||||
}
|
||||
|
||||
@@ -22,25 +22,6 @@ function release (loader, key, nextSceneAssets) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
// get asset url or uuid
|
||||
getKey: function (loader, assetOrUrlOrUuid) {
|
||||
if (assetOrUrlOrUuid) {
|
||||
if (typeof assetOrUrlOrUuid === 'string') {
|
||||
// try to convert uuid to url
|
||||
var item = cc.loader.getItem(assetOrUrlOrUuid);
|
||||
return (item && item.url) || assetOrUrlOrUuid;
|
||||
}
|
||||
else if (assetOrUrlOrUuid._uuid) {
|
||||
return assetOrUrlOrUuid._uuid;
|
||||
}
|
||||
else if (CC_DEV) {
|
||||
cc.warn('unknown asset type');
|
||||
}
|
||||
}
|
||||
return '';
|
||||
},
|
||||
|
||||
// do auto release
|
||||
autoRelease: function (loader, oldSceneAssets, nextSceneAssets) {
|
||||
var releaseSettings = loader._autoReleaseSetting;
|
||||
|
||||
@@ -62,18 +62,19 @@ var _parseUrl = function (url) {
|
||||
};
|
||||
|
||||
function isIdValid (id) {
|
||||
var realId = id.id || id;
|
||||
var realId = id.url || id;
|
||||
return (typeof realId === 'string');
|
||||
}
|
||||
function createItem (id, queueId) {
|
||||
var result, urlItem;
|
||||
if (typeof id === 'object' && id.id) {
|
||||
if (!id.type) {
|
||||
if (typeof id === 'object') {
|
||||
if (id.url && !id.type) {
|
||||
id.type = Path.extname(id.url).toLowerCase().substr(1);
|
||||
}
|
||||
urlItem = _parseUrl(id.url);
|
||||
result = {
|
||||
queueId: queueId,
|
||||
id: urlItem.url,
|
||||
url: urlItem.url,
|
||||
urlParam: urlItem.param,
|
||||
error: null,
|
||||
@@ -490,12 +491,12 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
// Queue new items
|
||||
if (isIdValid(url)) {
|
||||
item = createItem(url, this._id);
|
||||
var id = item.id;
|
||||
var key = item.id;
|
||||
// No duplicated url
|
||||
if (!this.map[id]) {
|
||||
this.map[item.id] = item;
|
||||
if (!this.map[key]) {
|
||||
this.map[key] = item;
|
||||
this.totalCount++;
|
||||
LoadingItems.registerDep(owner || this._id, id);
|
||||
LoadingItems.registerDep(owner || this._id, key);
|
||||
accepted.push(item);
|
||||
// console.log('+++++ Appended ' + item.id);
|
||||
}
|
||||
@@ -685,7 +686,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
* !#en Complete an item in the LoadingItems queue, please do not call this method unless you know what's happening.
|
||||
* !#zh 通知 LoadingItems 队列一个 item 对象已完成,请不要调用这个函数,除非你知道自己在做什么。
|
||||
* @method itemComplete
|
||||
* @param {String} item The item id
|
||||
* @param {String} id The item url
|
||||
*/
|
||||
itemComplete: function (id) {
|
||||
var item = this.map[id];
|
||||
@@ -705,7 +706,7 @@ JS.mixin(LoadingItems.prototype, CallbacksInvoker.prototype, {
|
||||
this.completed[id] = item;
|
||||
this.completedCount++;
|
||||
|
||||
LoadingItems.finishDep(id);
|
||||
LoadingItems.finishDep(item.id);
|
||||
if (this.onProgress) {
|
||||
var dep = _queueDeps[this._id];
|
||||
this.onProgress(dep ? dep.completed.length : this.completedCount, dep ? dep.deps.length : this.totalCount, item);
|
||||
|
||||
@@ -252,10 +252,6 @@ JS.mixin(Pipeline.prototype, {
|
||||
if (pipe) {
|
||||
for (i = 0; i < items.length; i++) {
|
||||
item = items[i];
|
||||
var url = cc.AssetLibrary._getAssetUrl(item.uuid);
|
||||
if (url && !item.url) {
|
||||
item.id = item.url = url;
|
||||
}
|
||||
this._cache[item.id] = item;
|
||||
pipe.flowIn(item);
|
||||
}
|
||||
|
||||
@@ -41,9 +41,12 @@ function isSceneObj (json) {
|
||||
}
|
||||
|
||||
function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRuntime, callback) {
|
||||
var uuid = item.id, uuidList = tdInfo.uuidList;
|
||||
var uuid = item.uuid, uuidList = tdInfo.uuidList;
|
||||
var objList, propList, depends;
|
||||
var i, dependUuid;
|
||||
// cache dependencies for auto release
|
||||
var dependKeys = item.dependKeys = [];
|
||||
|
||||
if (deferredLoadRawAssetsInRuntime) {
|
||||
objList = [];
|
||||
propList = [];
|
||||
@@ -58,13 +61,13 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
// skip preloading raw assets
|
||||
var url = info.url;
|
||||
obj[prop] = url;
|
||||
dependKeys.push(url);
|
||||
}
|
||||
else {
|
||||
objList.push(obj);
|
||||
propList.push(prop);
|
||||
// declare depends assets
|
||||
depends.push({
|
||||
id: dependUuid,
|
||||
type: 'uuid',
|
||||
uuid: dependUuid,
|
||||
deferredLoadRaw: true,
|
||||
@@ -80,7 +83,6 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
for (i = 0; i < uuidList.length; i++) {
|
||||
dependUuid = uuidList[i];
|
||||
depends[i] = {
|
||||
id: dependUuid,
|
||||
type: 'uuid',
|
||||
uuid: dependUuid
|
||||
};
|
||||
@@ -107,9 +109,6 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
return callback(null, asset);
|
||||
}
|
||||
|
||||
// cache dependencies for auto release
|
||||
var dependKeys = item.dependKeys = [];
|
||||
|
||||
// Predefine content for dependencies usage
|
||||
item.content = asset;
|
||||
pipeline.flowInDeps(item, depends, function (errors, items) {
|
||||
@@ -122,9 +121,10 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
}
|
||||
for (var i = 0; i < depends.length; i++) {
|
||||
var dependSrc = depends[i].uuid;
|
||||
var dependUrl = depends[i].url;
|
||||
var dependObj = objList[i];
|
||||
var dependProp = propList[i];
|
||||
item = items.map[dependSrc];
|
||||
item = items.map[dependUrl];
|
||||
if (item) {
|
||||
if (item.complete || item.content) {
|
||||
if (item.error) {
|
||||
@@ -133,7 +133,7 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
else {
|
||||
var value = item.isRawAsset ? item.url : item.content;
|
||||
dependObj[dependProp] = value;
|
||||
dependKeys.push(dependSrc || item.url);
|
||||
dependKeys.push(item.id);
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -141,7 +141,7 @@ function loadDepends (pipeline, item, asset, tdInfo, deferredLoadRawAssetsInRunt
|
||||
var loadCallback = function (item) {
|
||||
var value = item.isRawAsset ? item.url : item.content;
|
||||
this.obj[this.prop] = value;
|
||||
dependKeys.push(item.uuid || item.url);
|
||||
dependKeys.push(item.id);
|
||||
};
|
||||
var target = {
|
||||
obj: dependObj,
|
||||
|
||||
@@ -72,7 +72,6 @@ var AssetLibrary = {
|
||||
// var readMainCache = typeof (options && options.readMainCache) !== 'undefined' ? readMainCache : true;
|
||||
// var writeMainCache = typeof (options && options.writeMainCache) !== 'undefined' ? writeMainCache : true;
|
||||
var item = {
|
||||
id: uuid,
|
||||
uuid: uuid,
|
||||
type: 'uuid'
|
||||
};
|
||||
@@ -89,6 +88,7 @@ var AssetLibrary = {
|
||||
Editor.error('Sorry, the scene data of "%s" is corrupted!', uuid);
|
||||
}
|
||||
else {
|
||||
// We know scene is a raw asset, so we can pass uuid to this API directly
|
||||
asset.scene.dependAssets = AutoReleaseUtils.getDependsRecursively(uuid);
|
||||
}
|
||||
}
|
||||
@@ -127,21 +127,27 @@ var AssetLibrary = {
|
||||
}
|
||||
},
|
||||
|
||||
_getAssetInfoInRuntime: function (uuid) {
|
||||
_getAssetInfoInRuntime: function (uuid, result) {
|
||||
if (!result) {
|
||||
result = {url: null, raw: false};
|
||||
}
|
||||
var info = _uuidToRawAsset[uuid];
|
||||
if (info && !cc.isChildClassOf(info.type, cc.Asset)) {
|
||||
return {
|
||||
url: _rawAssetsBase + info.url,
|
||||
raw: true,
|
||||
};
|
||||
result.url = _rawAssetsBase + info.url;
|
||||
result.raw = true;
|
||||
}
|
||||
else {
|
||||
var url = this.getImportedDir(uuid) + '/' + uuid + '.json';
|
||||
return {
|
||||
url: url,
|
||||
raw: false,
|
||||
};
|
||||
result.url = this.getImportedDir(uuid) + '/' + uuid + '.json';
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
_isRawAsset: function (uuid) {
|
||||
var info = _uuidToRawAsset[uuid];
|
||||
if (info) {
|
||||
return !cc.isChildClassOf(info.type, cc.Asset);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
_getAssetUrl: function (uuid) {
|
||||
@@ -208,7 +214,7 @@ var AssetLibrary = {
|
||||
loadJson: function (json, callback) {
|
||||
var randomUuid = '' + ((new Date()).getTime() + Math.random());
|
||||
var item = {
|
||||
id: randomUuid,
|
||||
uuid: randomUuid,
|
||||
type: 'uuid',
|
||||
content: json,
|
||||
skips: [ Loader.downloader.id ]
|
||||
@@ -219,6 +225,7 @@ var AssetLibrary = {
|
||||
}
|
||||
else {
|
||||
if (asset.constructor === cc.SceneAsset) {
|
||||
// We know scene is a raw asset, so we can pass uuid to this API directly
|
||||
asset.scene.dependAssets = AutoReleaseUtils.getDependsRecursively(randomUuid);
|
||||
}
|
||||
if (CC_EDITOR || isScene(asset)) {
|
||||
@@ -360,4 +367,4 @@ AssetLibrary._uuidToAsset = {};
|
||||
// }
|
||||
//};
|
||||
|
||||
cc.AssetLibrary = AssetLibrary;
|
||||
module.exports = cc.AssetLibrary = AssetLibrary;
|
||||
|
||||
+1
-1
@@ -306,7 +306,7 @@ cc.js.mixin(cc.director, {
|
||||
var info = this._getSceneUuid(sceneName);
|
||||
if (info) {
|
||||
this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
|
||||
cc.loader.load({ id: info.uuid, type: 'uuid' }, function (error, asset) {
|
||||
cc.loader.load({ uuid: info.uuid, type: 'uuid' }, function (error, asset) {
|
||||
if (error) {
|
||||
cc.error('Failed to preload "%s", %s', sceneName, error.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user