From f13b9eef94654470ef590cde8afc452a6194daa1 Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Mon, 28 Oct 2019 19:08:01 +0800 Subject: [PATCH 1/8] fix platform detection --- cocos2d/core/platform/CCSys.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index 84755778979..debe238c6a4 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -821,7 +821,8 @@ function initSys () { osVersion = uaResult[2] || ''; osMainVersion = parseInt(osVersion) || 0; } - else if (/(iPhone|iPad|iPod)/.exec(nav.platform)) { + // refer to https://github.com/cocos-creator/engine/pull/5542 , thanks for contribition from @krapnikkk + else if (/(iPhone|iPad|iPod|MacIntel)/.exec(nav.platform)) { iOS = true; osVersion = ''; osMainVersion = 0; From 4e031ffdc0986cf73007c65b33690bedf10b8d15 Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Tue, 29 Oct 2019 14:45:19 +0800 Subject: [PATCH 2/8] adjust api annotation about cc.Asset.url --- cocos2d/core/assets/CCAsset.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/assets/CCAsset.js b/cocos2d/core/assets/CCAsset.js index 13cdb011423..391195e02bd 100644 --- a/cocos2d/core/assets/CCAsset.js +++ b/cocos2d/core/assets/CCAsset.js @@ -65,13 +65,13 @@ cc.Asset = cc.Class({ /** * !#en * Points to the true url of this asset's native object, only valid when asset is loaded and asyncLoadAsset is not enabled. - * Url equals nativeUrl on web(web-mobile, web-desktop) or native(iOS, Android etc) platform. The difference between - * nativeUrl and url is that url may points to temporary path or cached path on mini game platform which has cache mechanism (WeChat etc). + * The difference between nativeUrl and url is that the latter is final path, there is no needs to transform url by md5 and subpackage. + * Besides, url may points to temporary path or cached path on mini game platform which has cache mechanism (WeChat etc). * If you want to make use of the native file on those platforms, you should use url instead of nativeUrl. + * * !#zh - * 资源的原生文件的真实url,只在资源被加载后以及没有启用延迟加载时才有效。在web平台(web-mobile, web-desktop)或者原生平台(iOS,安卓等)上url与 - * nativeUrl是相等的,nativeUrl与url的区别在于,某些带缓存机制的小游戏平台(微信等)上url可能会指向临时文件路径或者缓存路径,如果你需要在这些平台上使用资源的原生文件, - * 请使用url,避免使用nativeUrl + * 资源的原生文件的真实url,只在资源被加载后以及没有启用延迟加载时才有效。 nativeUrl 与 url 的区别在于,url 是资源最终路径,所以 url 不需要再经过 md5 以及子包的路径转换, + * 另外某些带缓存机制的小游戏平台(微信等)上url可能会指向临时文件路径或者缓存路径,如果你需要在这些平台上使用资源的原生文件,请使用url,避免使用nativeUrl * @property url * @type {String} */ From ea8a7b707db90b530e2e4f2da1882d99b298ab4e Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Tue, 29 Oct 2019 21:45:04 +0800 Subject: [PATCH 3/8] release packed json automatically --- cocos2d/core/load-pipeline/pack-downloader.js | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index dcc47a1b670..3fe40778467 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -27,6 +27,9 @@ var Unpackers = require('./unpackers'); var pushToMap = require('../utils/misc').pushToMap; +// two minutes +const timeToRemove = 1000 * 60 * 2; + // when more than one package contains the required asset, // choose to load from the package with the largest state value. var PackState = { @@ -39,6 +42,7 @@ var PackState = { function UnpackerData () { this.unpacker = null; this.state = PackState.Invalid; + this.timer = null; } // {assetUuid: packUuid|[packUuid]} @@ -76,6 +80,7 @@ module.exports = { _loadNewPack: function (uuid, packUuid, callback) { var self = this; var packUrl = cc.AssetLibrary.getLibUrlNoExt(packUuid) + '.json'; + globalUnpackers[packUuid].url = packUrl; cc.loader.load({ url: packUrl, ignoreMaxConcurrency: true }, function (err, packJson) { if (err) { cc.errorID(4916, uuid); @@ -163,8 +168,15 @@ module.exports = { packUuid = this._selectLoadedPack(packUuid); } + var self = this; var unpackerData = globalUnpackers[packUuid]; if (unpackerData && unpackerData.state === PackState.Loaded) { + if (unpackerData.timer) { + clearTimeout(unpackerData.timer); + unpackerData.timer = setTimeout(function () { + self.remove(packUuid); + }, timeToRemove); + } // ensure async var json = unpackerData.unpacker.retrieve(uuid); if (json) { @@ -181,11 +193,23 @@ module.exports = { } unpackerData = globalUnpackers[packUuid] = new UnpackerData(); unpackerData.state = PackState.Downloading; + unpackerData.timer = setTimeout(function () { + self.remove(packUuid); + }, timeToRemove); } this._loadNewPack(uuid, packUuid, callback); } // Return null to let caller know it's loading asynchronously return null; + }, + + remove (packUuid) { + var unpackerData = globalUnpackers[packUuid]; + if (unpackerData) { + cc.loader.release(unpackerData.url); + clearTimeout(unpackerData.timer); + delete globalUnpackers[packUuid]; + } } }; From 450980c79c1a3b493f2f5c5d9dc21c3ce37bb470 Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Tue, 29 Oct 2019 21:49:45 +0800 Subject: [PATCH 4/8] export timeToRemove --- cocos2d/core/load-pipeline/pack-downloader.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index 3fe40778467..f1cd2cc8325 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -27,9 +27,6 @@ var Unpackers = require('./unpackers'); var pushToMap = require('../utils/misc').pushToMap; -// two minutes -const timeToRemove = 1000 * 60 * 2; - // when more than one package contains the required asset, // choose to load from the package with the largest state value. var PackState = { @@ -63,6 +60,9 @@ function error (uuid, packUuid) { } module.exports = { + // two minutes + timeToRemove: 1000 * 60 * 2, + initPacks: function (packs) { packIndices = packs; uuidToPack = {}; @@ -175,7 +175,7 @@ module.exports = { clearTimeout(unpackerData.timer); unpackerData.timer = setTimeout(function () { self.remove(packUuid); - }, timeToRemove); + }, this.timeToRemove); } // ensure async var json = unpackerData.unpacker.retrieve(uuid); @@ -195,7 +195,7 @@ module.exports = { unpackerData.state = PackState.Downloading; unpackerData.timer = setTimeout(function () { self.remove(packUuid); - }, timeToRemove); + }, this.timeToRemove); } this._loadNewPack(uuid, packUuid, callback); } From d0cb26757a035613c78704afbe9c1624200ae57e Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Wed, 30 Oct 2019 20:06:05 +0800 Subject: [PATCH 5/8] no need to create timer, use timestamp instead --- cocos2d/core/load-pipeline/pack-downloader.js | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index f1cd2cc8325..550e2f63638 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -39,7 +39,7 @@ var PackState = { function UnpackerData () { this.unpacker = null; this.state = PackState.Invalid; - this.timer = null; + this.timeStamp = 0; } // {assetUuid: packUuid|[packUuid]} @@ -54,6 +54,10 @@ var packIndices = {}; // We have to cache all packs in global because for now there's no operation context in loader. var globalUnpackers = {}; +var toBeChecked = {}; + +var timer = null; +var checkPeriod = 5000; function error (uuid, packUuid) { return new Error('Can not retrieve ' + uuid + ' from packer ' + packUuid); @@ -61,7 +65,7 @@ function error (uuid, packUuid) { module.exports = { // two minutes - timeToRemove: 1000 * 60 * 2, + timeToRelease: 2 * 60 * 1000, initPacks: function (packs) { packIndices = packs; @@ -80,12 +84,12 @@ module.exports = { _loadNewPack: function (uuid, packUuid, callback) { var self = this; var packUrl = cc.AssetLibrary.getLibUrlNoExt(packUuid) + '.json'; - globalUnpackers[packUuid].url = packUrl; cc.loader.load({ url: packUrl, ignoreMaxConcurrency: true }, function (err, packJson) { if (err) { cc.errorID(4916, uuid); return callback(err); } + globalUnpackers[packUuid].url = packUrl; var res = self._doLoadNewPack(uuid, packUuid, packJson); if (res) { callback(null, res); @@ -125,6 +129,26 @@ module.exports = { } unpackerData.unpacker.load(packIndices[packUuid], packedJson); unpackerData.state = PackState.Loaded; + unpackerData.timeStamp = performance.now(); + toBeChecked[packUuid] = unpackerData; + var self = this; + if (!timer) timer = setInterval(function () { + var now = performance.now(); + var empty = true; + for (var packUuid in toBeChecked) { + var pack = toBeChecked[packUuid]; + if (now - pack.timeStamp > self.timeToRelease) { + self.remove(packUuid); + } + else { + empty = false; + } + } + if (empty) { + clearInterval(timer); + timer = null; + } + }, checkPeriod); } return unpackerData.unpacker.retrieve(uuid); @@ -168,15 +192,10 @@ module.exports = { packUuid = this._selectLoadedPack(packUuid); } - var self = this; var unpackerData = globalUnpackers[packUuid]; if (unpackerData && unpackerData.state === PackState.Loaded) { - if (unpackerData.timer) { - clearTimeout(unpackerData.timer); - unpackerData.timer = setTimeout(function () { - self.remove(packUuid); - }, this.timeToRemove); - } + // update timestamp + unpackerData.timeStamp = performance.now(); // ensure async var json = unpackerData.unpacker.retrieve(uuid); if (json) { @@ -193,9 +212,6 @@ module.exports = { } unpackerData = globalUnpackers[packUuid] = new UnpackerData(); unpackerData.state = PackState.Downloading; - unpackerData.timer = setTimeout(function () { - self.remove(packUuid); - }, this.timeToRemove); } this._loadNewPack(uuid, packUuid, callback); } @@ -207,8 +223,8 @@ module.exports = { var unpackerData = globalUnpackers[packUuid]; if (unpackerData) { cc.loader.release(unpackerData.url); - clearTimeout(unpackerData.timer); delete globalUnpackers[packUuid]; + delete toBeChecked[packUuid]; } } }; From 0ad3413e7a211805f989b52c091c7c651b49f8e9 Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Thu, 31 Oct 2019 11:50:19 +0800 Subject: [PATCH 6/8] change name of property --- cocos2d/core/load-pipeline/pack-downloader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index 550e2f63638..d65ce0da295 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -65,7 +65,7 @@ function error (uuid, packUuid) { module.exports = { // two minutes - timeToRelease: 2 * 60 * 1000, + msToRelease: 2 * 60 * 1000, initPacks: function (packs) { packIndices = packs; @@ -137,7 +137,7 @@ module.exports = { var empty = true; for (var packUuid in toBeChecked) { var pack = toBeChecked[packUuid]; - if (now - pack.timeStamp > self.timeToRelease) { + if (now - pack.timeStamp > self.msToRelease) { self.remove(packUuid); } else { From 857b95dc2f176555885043d8ff8d206f481c858a Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Thu, 31 Oct 2019 17:05:34 +0800 Subject: [PATCH 7/8] optimize performance --- cocos2d/core/load-pipeline/pack-downloader.js | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index d65ce0da295..052c3637256 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -39,7 +39,7 @@ var PackState = { function UnpackerData () { this.unpacker = null; this.state = PackState.Invalid; - this.timeStamp = 0; + this.duration = 0; } // {assetUuid: packUuid|[packUuid]} @@ -54,7 +54,7 @@ var packIndices = {}; // We have to cache all packs in global because for now there's no operation context in loader. var globalUnpackers = {}; -var toBeChecked = {}; +var toBeChecked = []; var timer = null; var checkPeriod = 5000; @@ -129,22 +129,20 @@ module.exports = { } unpackerData.unpacker.load(packIndices[packUuid], packedJson); unpackerData.state = PackState.Loaded; - unpackerData.timeStamp = performance.now(); - toBeChecked[packUuid] = unpackerData; + unpackerData.duration = 0; + toBeChecked.push(packUuid); var self = this; if (!timer) timer = setInterval(function () { - var now = performance.now(); - var empty = true; - for (var packUuid in toBeChecked) { - var pack = toBeChecked[packUuid]; - if (now - pack.timeStamp > self.msToRelease) { - self.remove(packUuid); - } - else { - empty = false; + var maxDuration = self.msToRelease / checkPeriod; + for (var i = toBeChecked.length - 1; i >= 0; i--) { + var id = toBeChecked[i]; + var pack = globalUnpackers[id]; + pack.duration++; + if (pack.duration > maxDuration) { + self.release(id); } } - if (empty) { + if (toBeChecked.length === 0) { clearInterval(timer); timer = null; } @@ -194,8 +192,8 @@ module.exports = { var unpackerData = globalUnpackers[packUuid]; if (unpackerData && unpackerData.state === PackState.Loaded) { - // update timestamp - unpackerData.timeStamp = performance.now(); + // update duration + unpackerData.duration = 0; // ensure async var json = unpackerData.unpacker.retrieve(uuid); if (json) { @@ -219,12 +217,13 @@ module.exports = { return null; }, - remove (packUuid) { + release (packUuid) { var unpackerData = globalUnpackers[packUuid]; if (unpackerData) { cc.loader.release(unpackerData.url); delete globalUnpackers[packUuid]; - delete toBeChecked[packUuid]; + var index = toBeChecked.indexOf(packUuid); + cc.js.array.fastRemoveAt(toBeChecked, index); } } }; From 959fa2a77008838f69ccc79dc2958f1d48e0da8c Mon Sep 17 00:00:00 2001 From: Santy-Wang Date: Fri, 1 Nov 2019 15:57:28 +0800 Subject: [PATCH 8/8] refine code --- cocos2d/core/load-pipeline/pack-downloader.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index 052c3637256..57cae68e888 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -111,6 +111,7 @@ module.exports = { unpackerData.unpacker.load(packIndices[packUuid], packJson); unpackerData.state = PackState.Loaded; } + // can not release subdomain packed json because it would not be reloaded. }, _doLoadNewPack: function (uuid, packUuid, packedJson) { @@ -137,8 +138,7 @@ module.exports = { for (var i = toBeChecked.length - 1; i >= 0; i--) { var id = toBeChecked[i]; var pack = globalUnpackers[id]; - pack.duration++; - if (pack.duration > maxDuration) { + if (++pack.duration > maxDuration) { self.release(id); } } @@ -222,8 +222,7 @@ module.exports = { if (unpackerData) { cc.loader.release(unpackerData.url); delete globalUnpackers[packUuid]; - var index = toBeChecked.indexOf(packUuid); - cc.js.array.fastRemoveAt(toBeChecked, index); + cc.js.array.fastRemove(toBeChecked, packUuid); } } };