From 9988c1657fbd75a42ffdb1a8186273d85b6ed782 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Mon, 28 Nov 2016 18:04:45 +0800 Subject: [PATCH 1/5] Using URL cache(loader) --- cocos2d/core/load-pipeline/downloader.js | 14 +++++++------- cocos2d/core/load-pipeline/pipeline.js | 16 ++++++++++++++-- cocos2d/core/platform/CCAssetLibrary.js | 9 +++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/cocos2d/core/load-pipeline/downloader.js b/cocos2d/core/load-pipeline/downloader.js index 9173b9909a..229bdb6f5f 100644 --- a/cocos2d/core/load-pipeline/downloader.js +++ b/cocos2d/core/load-pipeline/downloader.js @@ -42,7 +42,7 @@ else { function downloadScript (item, callback, isAsync) { var url = item.url, - d = document, + d = document, s = document.createElement('script'); s.async = isAsync; s.src = urlAppendTimestamp(url); @@ -129,8 +129,8 @@ var FONT_TYPE = { '.svg' : 'svg' }; function _loadFont (name, srcs, type){ - var doc = document, - path = cc.path, + var doc = document, + path = cc.path, fontStyle = document.createElement('style'); fontStyle.type = 'text/css'; doc.body.appendChild(fontStyle); @@ -167,8 +167,8 @@ function _loadFont (name, srcs, type){ } function downloadFont (item, callback) { var url = item.url, - type = item.type, - name = item.name, + type = item.type, + name = item.name, srcs = item.srcs; if (name && srcs) { if (srcs.indexOf(url) === -1) { @@ -194,7 +194,7 @@ function downloadFont (item, callback) { var reusedArray = []; function downloadUuid (item, callback) { - var uuid = item.id; + var uuid = item.uuid; var self = this; cc.AssetLibrary.queryAssetInfo(uuid, function (error, url, isRawAsset) { if (error) { @@ -308,7 +308,7 @@ var ID = 'Downloader'; * // This will match all url with `.scene` extension or all url with `scene` type * 'scene' : function (url, callback) {} * }); - * + * * @method Downloader * @param {Object} extMap Custom supported types with corresponded handler */ diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index 3dfe9c29b0..5cf34ea064 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -252,6 +252,10 @@ 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 && url !== item.url) { + item.id = item.url = url; + } this._cache[item.id] = item; pipe.flowIn(item); } @@ -361,11 +365,19 @@ JS.mixin(Pipeline.prototype, { getItem: function (id) { var item = this._cache[id]; + var url; + if (!item) { + url = cc.AssetLibrary._getAssetUrl(id); + item = this._cache[url]; + } + if (!item) return item; - if (item.alias) - item = this._cache[item.alias]; + if (item.alias) { + url = cc.AssetLibrary._getAssetUrl(item.alias); + item = this._cache[url]; + } return item; }, diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index f5c99a50ed..36a65aea51 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -73,6 +73,7 @@ var AssetLibrary = { // var writeMainCache = typeof (options && options.writeMainCache) !== 'undefined' ? writeMainCache : true; var item = { id: uuid, + uuid: uuid, type: 'uuid' }; if (options && options.existingAsset) { @@ -143,6 +144,14 @@ var AssetLibrary = { } }, + _getAssetUrl: function (uuid) { + var info = _uuidToRawAsset[uuid]; + if (info) { + return _rawAssetsBase + info.url; + } + return null; + }, + /** * @method queryAssetInfo * @param {String} uuid From 7b2806a51baad629b8b92cfc5101f486687f9a08 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Thu, 8 Dec 2016 15:13:29 +0800 Subject: [PATCH 2/5] remove getAssetUrl --- cocos2d/core/load-pipeline/pipeline.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index 5cf34ea064..b089e8852a 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -72,7 +72,7 @@ function syncFlow (item) { var pipeId = this.id; var itemState = item.states[pipeId]; var next = this.next; - + if (item.error || itemState === ItemState.WORKING || itemState === ItemState.ERROR) { return; } @@ -374,10 +374,9 @@ JS.mixin(Pipeline.prototype, { if (!item) return item; - if (item.alias) { - url = cc.AssetLibrary._getAssetUrl(item.alias); - item = this._cache[url]; - } + // downloader.js downloadUuid + if (item.alias) + item = this._cache[item.alias]; return item; }, @@ -421,4 +420,4 @@ JS.mixin(Pipeline.prototype, { } }); -cc.Pipeline = module.exports = Pipeline; \ No newline at end of file +cc.Pipeline = module.exports = Pipeline; From 891b55e351c6cf394ac31dc3869cb4b09224c046 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Thu, 8 Dec 2016 15:31:05 +0800 Subject: [PATCH 3/5] remove alias id --- cocos2d/core/load-pipeline/pipeline.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index b089e8852a..4b660dc052 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -365,12 +365,6 @@ JS.mixin(Pipeline.prototype, { getItem: function (id) { var item = this._cache[id]; - var url; - if (!item) { - url = cc.AssetLibrary._getAssetUrl(id); - item = this._cache[url]; - } - if (!item) return item; From f3cc24b4309115157461360541c509c56bd951df Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Thu, 8 Dec 2016 16:15:21 +0800 Subject: [PATCH 4/5] url is error --- cocos2d/core/load-pipeline/loading-items.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/load-pipeline/loading-items.js b/cocos2d/core/load-pipeline/loading-items.js index 266cc323a3..7c89f27317 100644 --- a/cocos2d/core/load-pipeline/loading-items.js +++ b/cocos2d/core/load-pipeline/loading-items.js @@ -52,7 +52,7 @@ function createItem (id, queueId) { } result = { queueId: queueId, - url: id.url || id.id, + url: id.url, error: null, content: null, complete: false, From ec0b965d107ea1f560109ecf36178b9c80d780b5 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Thu, 8 Dec 2016 16:22:52 +0800 Subject: [PATCH 5/5] To determine whether the URL can be rewritten --- cocos2d/core/load-pipeline/pipeline.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/load-pipeline/pipeline.js b/cocos2d/core/load-pipeline/pipeline.js index 4b660dc052..c33738456d 100644 --- a/cocos2d/core/load-pipeline/pipeline.js +++ b/cocos2d/core/load-pipeline/pipeline.js @@ -253,7 +253,7 @@ JS.mixin(Pipeline.prototype, { for (i = 0; i < items.length; i++) { item = items[i]; var url = cc.AssetLibrary._getAssetUrl(item.uuid); - if (url && url !== item.url) { + if (url && !item.url) { item.id = item.url = url; } this._cache[item.id] = item;