From b91e00560e4275dda2e977c11a0e7438e1c5a149 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 25 Feb 2016 18:57:06 +0800 Subject: [PATCH 1/5] Fix unexpected asset delete in editor --- cocos2d/core/load-pipeline/CCLoader.js | 3 ++- cocos2d/core/platform/CCAssetLibrary.js | 10 +++------- cocos2d/core/textures/CCTextureCache.js | 4 ++-- cocos2d/tilemap/CCTiledMap.js | 4 ++-- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 23ff6aa2e1..1579ce1825 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -196,7 +196,8 @@ JS.mixin(cc.loader, { // No new resources, complete directly if (totalCount === completedCount) { if (singleRes) { - var result = this._items.map[resources[0]]; + var src = resources[0].src || resources[0]; + var result = this._items.map[src]; completeCallback.call(this, result.error, result.content); } else { diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index a9c86a1e25..10ca3a0341 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -164,10 +164,6 @@ var AssetLibrary = { return callInNextTick(callback, new Error('[AssetLibrary] uuid must be string'), null); } - if (CC_EDITOR) { - Loader.removeItem(uuid); - } - Loader.load(item, function (error, asset) { if (error || !asset) { error = new Error('[AssetLibrary] loading JSON or dependencies failed : ' + JSON.stringify(error)); @@ -207,12 +203,12 @@ var AssetLibrary = { skips: [ Loader.downloader.id ] }; Loader.load(item, function (error, asset) { - if (error || !asset) { + if (error) { error = new Error('[AssetLibrary] loading JSON or dependencies failed : ' + JSON.stringify(error)); } if (thisTick) { callInNextTick(function () { - if (CC_EDITOR || isScene(asset)) { + if (isScene(asset)) { Loader.removeItem(randomUuid); } asset._uuid = ''; @@ -220,7 +216,7 @@ var AssetLibrary = { }); } else { - if (CC_EDITOR || isScene(asset)) { + if (isScene(asset)) { Loader.removeItem(randomUuid); } asset._uuid = ''; diff --git a/cocos2d/core/textures/CCTextureCache.js b/cocos2d/core/textures/CCTextureCache.js index 1357fa87e4..a2b5789c05 100644 --- a/cocos2d/core/textures/CCTextureCache.js +++ b/cocos2d/core/textures/CCTextureCache.js @@ -319,7 +319,7 @@ game.once(game.EVENT_RENDERER_INITED, function () { tex = locTexs[url] = new Texture2D(); tex.url = url; cc.loader.load(url, function (err, texture) { - if (err || !texture) { + if (err) { return cb && cb.call(target, err || new Error('Unknown error')); } @@ -377,7 +377,7 @@ game.once(game.EVENT_RENDERER_INITED, function () { tex = locTexs[url] = new Texture2D(); tex.url = url; cc.loader.load(url, function (err, texture) { - if (err || !texture) { + if (err) { return cb && cb.call(target, err || new Error('Unknown error')); } diff --git a/cocos2d/tilemap/CCTiledMap.js b/cocos2d/tilemap/CCTiledMap.js index 8d44fed52c..dcf40583d8 100644 --- a/cocos2d/tilemap/CCTiledMap.js +++ b/cocos2d/tilemap/CCTiledMap.js @@ -349,8 +349,8 @@ var TiledMap = cc.Class({ _preloadTmx: function(file, cb) { var self = this; - cc.loader.load(file, function (err, content) { - if (err || !content) { + cc.loader.load(file, function (err) { + if (err) { if (cb) cb(err || new Error('Preload TMX failed: unknown error')); return; } From fa2d62605d166b8c516a8577aea82c8ba5806b22 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 11:42:35 +0800 Subject: [PATCH 2/5] Delay removing in callbacks handler --- cocos2d/core/platform/callbacks-invoker.js | 41 ++++++++++------------ 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index ae304688cc..16d1ac26ba 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -120,6 +120,12 @@ CallbacksHandler.prototype.removeAll = function (key) { CallbacksHandler.prototype.remove = function (key, callback, target) { var list = this._callbackTable[key], index, callbackTarget; if (list) { + // Delay removing + if (this._invoking) { + this._toRemove.push([key, callback, target]); + return true; + } + index = list.indexOf(callback); while (index !== -1) { callbackTarget = list[index+1]; @@ -148,6 +154,8 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { */ var CallbacksInvoker = function () { CallbacksHandler.call(this); + this._invoking = false; + this._toRemove = []; }; JS.extend(CallbacksInvoker, CallbacksHandler); @@ -165,11 +173,11 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - var list = this._callbackTable[key]; + this._invoking = true; + var list = this._callbackTable[key], i; if (list) { var endIndex = list.length - 1; - var lastItem = list[endIndex]; - for (var i = 0; i <= endIndex;) { + for (i = 0; i <= endIndex;) { var callingFunc = list[i]; var target = list[i + 1]; var hasTarget = target && typeof target === 'object'; @@ -183,29 +191,16 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { increment = 1; } - // TODO: check in remove - // check last one to see if any one removed - if (list[endIndex] !== lastItem && i + increment <= endIndex) { // 如果变短 - if (list[endIndex - 1] === lastItem) { - // 只支持删一个 - endIndex -= 1; - } - else if (list[endIndex - 2] === lastItem) { - // 只支持删一个 + target - endIndex -= 2; - } - else { - // 只允许在一个回调里面移除一个回调 - return cc.error('Can remove only a callback at a time.'); - } - if (list[i] !== callingFunc) { // 如果删了前面的回调,索引不加 - continue; - } - } - i += increment; } } + this._invoking = false; + + for (i = 0; i < this._toRemove.length; ++i) { + var toRemove = this._toRemove[i]; + this.remove(toRemove[0], toRemove[1], toRemove[2]); + } + this._toRemove.length = 0; }; /** From c478ae6b67484a1f8932df85bceebdcf448a91d2 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 11:41:35 +0800 Subject: [PATCH 3/5] Fix AssetLibrary cache issue and uuid loader this issue --- cocos2d/core/load-pipeline/uuid-loader.js | 4 ++-- cocos2d/core/platform/CCAssetLibrary.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/load-pipeline/uuid-loader.js b/cocos2d/core/load-pipeline/uuid-loader.js index 9c7f1c15b7..09e3903a6b 100644 --- a/cocos2d/core/load-pipeline/uuid-loader.js +++ b/cocos2d/core/load-pipeline/uuid-loader.js @@ -94,7 +94,7 @@ function loadUuid (item, callback) { } var pipeline = this.pipeline; if (depends.length > 0) { - this.pipeline.flowInDeps(depends, function (items) { + pipeline.flowInDeps(depends, function (items) { var item; for (var src in items) { item = items[src]; @@ -113,7 +113,7 @@ function loadUuid (item, callback) { obj[dependProp] = value; } else { - this.getItems().add(dependSrc, function (item) { + pipeline.getItems().add(dependSrc, function (item) { var value = item.isRawAsset ? (item.url || item.src) : item.content; this.obj[this.prop] = value; }, { diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 10ca3a0341..3440530703 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -170,14 +170,14 @@ var AssetLibrary = { } if (thisTick) { callInNextTick(function () { - if (isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(uuid); } callback(error, asset); }); } else { - if (isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(uuid); } callback(error, asset); @@ -208,7 +208,7 @@ var AssetLibrary = { } if (thisTick) { callInNextTick(function () { - if (isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(randomUuid); } asset._uuid = ''; @@ -216,7 +216,7 @@ var AssetLibrary = { }); } else { - if (isScene(asset)) { + if (isScene(asset) || CC_EDITOR) { Loader.removeItem(randomUuid); } asset._uuid = ''; From 21d0a264b57a26bcfc057c8301470af9eb2c0b33 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Fri, 26 Feb 2016 14:53:01 +0800 Subject: [PATCH 4/5] Improve removing callbacks logic and add test case --- cocos2d/core/platform/callbacks-invoker.js | 23 +++++++--- test/qunit/unit/test-callbacks-invoker.js | 52 ++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/cocos2d/core/platform/callbacks-invoker.js b/cocos2d/core/platform/callbacks-invoker.js index 16d1ac26ba..214adc972d 100644 --- a/cocos2d/core/platform/callbacks-invoker.js +++ b/cocos2d/core/platform/callbacks-invoker.js @@ -90,6 +90,11 @@ CallbacksHandler.prototype.has = function (key, callback, target) { * @param {String|Object} key - The event key to be removed or the target to be removed */ CallbacksHandler.prototype.removeAll = function (key) { + // Delay removing + if (this._invoking) { + this._toRemoveAll = key; + return; + } if (typeof key === 'object') { var target = key, list, index, callback; // loop for all event types @@ -121,8 +126,8 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { var list = this._callbackTable[key], index, callbackTarget; if (list) { // Delay removing - if (this._invoking) { - this._toRemove.push([key, callback, target]); + if (this._invoking === key) { + this._toRemove.push([callback, target]); return true; } @@ -154,8 +159,9 @@ CallbacksHandler.prototype.remove = function (key, callback, target) { */ var CallbacksInvoker = function () { CallbacksHandler.call(this); - this._invoking = false; + this._invoking = null; this._toRemove = []; + this._toRemoveAll = null; }; JS.extend(CallbacksInvoker, CallbacksHandler); @@ -173,7 +179,7 @@ if (CC_TEST) { * @param {any} [p5] */ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { - this._invoking = true; + this._invoking = key; var list = this._callbackTable[key], i; if (list) { var endIndex = list.length - 1; @@ -194,13 +200,18 @@ CallbacksInvoker.prototype.invoke = function (key, p1, p2, p3, p4, p5) { i += increment; } } - this._invoking = false; + this._invoking = null; + // Delay removing for (i = 0; i < this._toRemove.length; ++i) { var toRemove = this._toRemove[i]; - this.remove(toRemove[0], toRemove[1], toRemove[2]); + this.remove(this._invoking, toRemove[0], toRemove[1]); } this._toRemove.length = 0; + if (this._toRemoveAll) { + this.removeAll(this._toRemoveAll); + this._toRemoveAll = null; + } }; /** diff --git a/test/qunit/unit/test-callbacks-invoker.js b/test/qunit/unit/test-callbacks-invoker.js index e5679f0f67..9eb52a0bde 100644 --- a/test/qunit/unit/test-callbacks-invoker.js +++ b/test/qunit/unit/test-callbacks-invoker.js @@ -137,6 +137,58 @@ test('remove last with target during invoking', function () { strictEqual(ci.has('eve', cb2, target), false, 'self callback should be removed'); }); +test('remove multiple callbacks during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.remove('eve', cb1); + ci.remove('eve', cb3); + ci.remove('eve', cb3, target); + }).enable(); + var cb3 = new Callback(function () { + ci.remove('eve', cb2, target); + }).enable(); + var target = {}; + ci.add('eve', cb1); + ci.add('eve', cb1, target); + ci.add('eve', cb2, target); + ci.add('eve', cb3); + ci.add('eve', cb3, target); + ci.invoke('eve'); + + cb1.expect(2, 'first callback should be invoked twice'); + cb2.expect(1, 'second callback should be invoked once'); + cb3.expect(2, 'third callback should be invoked twice'); + strictEqual(ci.has('eve', cb1, target), true, 'first callback with target should not be removed'); + strictEqual(ci.has('eve', cb1), false, 'first callback should be removed'); + strictEqual(ci.has('eve', cb2, target), false, 'second callback with target should be removed'); + strictEqual(ci.has('eve', cb3), false, 'third callback should be removed'); + strictEqual(ci.has('eve', cb3, target), false, 'third callback with target should be removed'); +}); + +test('remove all callbacks during invoking', function () { + var ci = new cc._Test.CallbacksInvoker(); + var cb1 = new Callback().enable(); + var cb2 = new Callback(function () { + ci.removeAll('eve'); + }).enable(); + var cb3 = new Callback(function () { + ci.remove('eve', cb2, target); + }).enable(); + var target = {}; + ci.add('eve', cb1); + ci.add('eve', cb1, target); + ci.add('eve', cb2, target); + ci.add('eve', cb3); + ci.add('eve', cb3, target); + ci.invoke('eve'); + + cb1.expect(2, 'first callback should be invoked twice'); + cb2.expect(1, 'second callback should be invoked once'); + cb3.expect(2, 'third callback should be invoked twice'); + strictEqual(ci.has('eve'), false, 'All callbacks should be removed'); +}); + test('CallbacksInvoker support target', function () { var ci = new cc._Test.CallbacksInvoker(); var cb1 = function () { From 0b164bdfe96e18643cfd5b2de1e80e80d8d7481f Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Fri, 26 Feb 2016 14:57:50 +0800 Subject: [PATCH 5/5] fix create particleSystem error --- cocos2d/particle/CCParticleSystem.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cocos2d/particle/CCParticleSystem.js b/cocos2d/particle/CCParticleSystem.js index 465631e07f..472d798037 100644 --- a/cocos2d/particle/CCParticleSystem.js +++ b/cocos2d/particle/CCParticleSystem.js @@ -137,10 +137,10 @@ var properties = { this._file = value; if (value) { this._applyFile(); - cc.engine.repaintInEditMode(); - //if (CC_EDITOR) { - // self.preview = self.preview; - //} + if (CC_EDITOR) { + cc.engine.repaintInEditMode(); + //self.preview = self.preview; + } } else { this.custom = true;