From b1fadcc04e411fee6091dd307c872e9532b921fa Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Fri, 22 Jul 2016 16:38:21 +0800 Subject: [PATCH 1/4] add ignoreEditorOnly mode for deserialization --- .../components/CCComponentEventHandler.js | 3 +- cocos2d/core/platform/deserialize.js | 40 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cocos2d/core/components/CCComponentEventHandler.js b/cocos2d/core/components/CCComponentEventHandler.js index cbaf23cbe7..85275a290c 100644 --- a/cocos2d/core/components/CCComponentEventHandler.js +++ b/cocos2d/core/components/CCComponentEventHandler.js @@ -80,6 +80,7 @@ cc.Component.EventHandler = cc.Class({ /** * @method emitEvents * @param {Component.EventHandler[]} events + * @param {Any} ...params * @statics */ emitEvents: function(events) { @@ -88,7 +89,7 @@ cc.Component.EventHandler = cc.Class({ var event = events[i]; if (! event instanceof cc.Component.EventHandler) continue; - event.emit(Array.prototype.slice.call(arguments,1)); + event.emit(Array.prototype.slice.call(arguments, 1)); } } }, diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index f9da036042..ff3a003489 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -27,8 +27,6 @@ var JS = require('./js'); var CCObject = require('./CCObject'); var Attr = require('./attribute'); -var ENABLE_TARGET = CC_DEV; - // HELPERS /** @@ -144,13 +142,11 @@ Details.prototype.push = function (obj, propName, uuid) { // IMPLEMENT OF DESERIALIZATION var _Deserializer = (function () { - ///** - // * @param {Boolean} isEditor - if false, "editorOnly" properties will be discarded - // */ - function _Deserializer(jsonObj, result, target, classFinder, customEnv) { + function _Deserializer(jsonObj, result, target, classFinder, customEnv, ignoreEditorOnly) { this._classFinder = classFinder; - if (ENABLE_TARGET) { + if (CC_DEV) { this._target = target; + this._ignoreEditorOnly = ignoreEditorOnly; } this._idList = []; this._idObjList = []; @@ -166,7 +162,7 @@ var _Deserializer = (function () { for (var i = 0; i < refCount; i++) { if (jsonArray[i]) { var mainTarget; - if (ENABLE_TARGET) { + if (CC_DEV) { mainTarget = (i === 0 && target); } this.deserializedList[i] = _deserializeObject(this, jsonArray[i], mainTarget); @@ -243,7 +239,7 @@ var _Deserializer = (function () { this.result.uuidPropList.push(propName); } else { - if (ENABLE_TARGET) { + if (CC_DEV) { obj[propName] = _deserializeObject(this, jsonObj, target && target[propName]); } else { @@ -284,7 +280,7 @@ var _Deserializer = (function () { } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(instance, prop, propName, self._target && instance); } else { @@ -313,7 +309,7 @@ var _Deserializer = (function () { } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(instance, prop, propName, self._target && instance); } else { @@ -338,10 +334,11 @@ var _Deserializer = (function () { // assume all prop in __props__ must have attr var rawType = attrs[propName + DELIMETER + 'rawType']; if (!rawType) { - if (!CC_DEV && attrs[propName + DELIMETER + 'editorOnly']) { + if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV)) + && attrs[propName + DELIMETER + 'editorOnly']) { var mayUsedInPersistRoot = (obj instanceof cc.Node && propName === '_id'); if ( !mayUsedInPersistRoot ) { - continue; // skip editor only if not editor + continue; // skip editor only if in preview } } if (attrs[propName + DELIMETER + 'serializable'] === false) { @@ -356,7 +353,7 @@ var _Deserializer = (function () { } else { if (prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(obj, prop, propName, target && obj); } else { @@ -409,7 +406,7 @@ var _Deserializer = (function () { return null; } - if (ENABLE_TARGET && target) { + if (CC_DEV && target) { // use target if ( !(target instanceof klass) ) { cc.warn('Type of target to deserialize not matched with data: target is %s, data is %s', @@ -441,14 +438,14 @@ var _Deserializer = (function () { // embedded primitive javascript object - obj = (ENABLE_TARGET && target) || {}; + obj = (CC_DEV && target) || {}; _deserializePrimitiveObject(self, obj, serialized); } else { // Array - if (ENABLE_TARGET && target) { + if (CC_DEV && target) { target.length = serialized.length; obj = target; } @@ -459,7 +456,7 @@ var _Deserializer = (function () { for (var i = 0; i < serialized.length; i++) { prop = serialized[i]; if (typeof prop === 'object' && prop) { - if (ENABLE_TARGET) { + if (CC_DEV) { self._deserializeObjField(obj, prop, '' + i, target && obj); } else { @@ -504,8 +501,9 @@ cc.deserialize = function (data, result, options) { var classFinder = (options && options.classFinder) || JS._getClassById; // 启用 createAssetRefs 后,如果有 url 属性则会被统一强制设置为 { uuid: 'xxx' },必须后面再特殊处理 var createAssetRefs = (options && options.createAssetRefs) || cc.sys.platform === cc.sys.EDITOR_CORE; - var target = ENABLE_TARGET && (options && options.target); - var customEnv = (options && options.customEnv); + var target = CC_DEV && (options && options.target); + var customEnv = options && options.customEnv; + var ignoreEditorOnly = options && options.ignoreEditorOnly; if (CC_EDITOR && Buffer.isBuffer(data)) { data = data.toString(); @@ -521,7 +519,7 @@ cc.deserialize = function (data, result, options) { result = new Details(); } cc.game._isCloning = true; - var deserializer = new _Deserializer(data, result, target, classFinder, customEnv); + var deserializer = new _Deserializer(data, result, target, classFinder, customEnv, ignoreEditorOnly); cc.game._isCloning = false; if (createAssetRefs) { From 2cd4b08e681caafa36da3a3d419091e81673521f Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 24 Jul 2016 11:28:25 +0800 Subject: [PATCH 2/4] make missing script behavior customizable --- cocos2d/core/components/missing-script.js | 7 ++++++ cocos2d/core/platform/deserialize.js | 26 ++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cocos2d/core/components/missing-script.js b/cocos2d/core/components/missing-script.js index 82bafac5bf..f91283c8e0 100644 --- a/cocos2d/core/components/missing-script.js +++ b/cocos2d/core/components/missing-script.js @@ -90,6 +90,13 @@ var MissingScript = cc.Class({ return cls; } if (id) { + if (CC_EDITOR && Editor.UuidUtils.isUuid(id)) { + id = Editor.UuidUtils.decompressUuid(id); + cc.warn('Can not find script "%s"', id); + } + else { + cc.warn('Can not find class "%s"', id); + } return MissingScript; } return null; diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index ff3a003489..c935b8d528 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -197,7 +197,7 @@ var _Deserializer = (function () { } } - var _dereference = function (self) { + function _dereference (self) { // 这里不采用遍历反序列化结果的方式,因为反序列化的结果如果引用到复杂的外部库,很容易堆栈溢出。 var deserializedList = self.deserializedList; var idPropList = self._idPropList; @@ -208,7 +208,7 @@ var _Deserializer = (function () { var id = idList[i]; idObjList[i][propName] = deserializedList[id]; } - }; + } if (CC_DEV) { _Deserializer.prototype._callVisitorInEditor = function () { @@ -386,8 +386,8 @@ var _Deserializer = (function () { // * @param {Object} serialized - The obj to deserialize, must be non-nil // * @param {Object} [target=null] // */ - var _deserializeObject = function (self, serialized, target) { - var propName, prop; + function _deserializeObject (self, serialized, target) { + var prop; var obj = null; // the obj to return var klass = null; if (serialized.__type__) { @@ -397,12 +397,16 @@ var _Deserializer = (function () { var type = serialized.__type__; klass = self._classFinder(type); if (!klass) { - if (CC_EDITOR && Editor.UuidUtils.isUuid(type)) { - type = Editor.UuidUtils.decompressUuid(type); - cc.warn('Can not find script "%s"', type); - return null; + var noLog = self._classFinder === JS._getClassById; + if (noLog) { + if (CC_EDITOR && Editor.UuidUtils.isUuid(type)) { + type = Editor.UuidUtils.decompressUuid(type); + cc.warn('Can not find script "%s"', type); + } + else { + cc.warn('Can not find class "%s"', type); + } } - cc.warn('Can not find class "%s"', type); return null; } @@ -473,13 +477,11 @@ var _Deserializer = (function () { } } return obj; - }; + } return _Deserializer; })(); -// FACADE - /** * @module cc */ From 1a7b909ed0fa009870758a3125b78a33d945a752 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Sun, 24 Jul 2016 19:58:31 +0800 Subject: [PATCH 3/4] simplify deserialization visitor --- cocos2d/core/components/missing-script.js | 8 +-- cocos2d/core/platform/deserialize.js | 76 ++++++++--------------- 2 files changed, 26 insertions(+), 58 deletions(-) diff --git a/cocos2d/core/components/missing-script.js b/cocos2d/core/components/missing-script.js index f91283c8e0..07fe2f6d9a 100644 --- a/cocos2d/core/components/missing-script.js +++ b/cocos2d/core/components/missing-script.js @@ -90,13 +90,7 @@ var MissingScript = cc.Class({ return cls; } if (id) { - if (CC_EDITOR && Editor.UuidUtils.isUuid(id)) { - id = Editor.UuidUtils.decompressUuid(id); - cc.warn('Can not find script "%s"', id); - } - else { - cc.warn('Can not find class "%s"', id); - } + cc.deserialize.reportMissingClass(id); return MissingScript; } return null; diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index c935b8d528..0ab799f7ba 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -66,20 +66,16 @@ var Details = function () { if (CC_DEV) { /** - * 用户可以指定一个在反序列化结束时会被触发的回调,该回调会传回反序列化时统计到的所有解析过的字段。 + * 用户可以指定一个在反序列化过程中会被触发的回调,该回调会在反序列化之前调用,并且传回反序列化时解析到的字段。 * NOTE: + * - only available in editor * - 会被传回的字段仅限于非 Asset 类型,并且如果字段值为 null 或 undefined,则可能不会被传回。 - * - 该回调在 DeserializeInfo 第一次调用 reset 时就会被清空。 - * @callback visitorInEditor - * @param {Object[]} objs - * @param {String[]} propNames - * @param {Object} _Deserializer + * @callback visit + * @param {Object} obj + * @param {String} propName * @private */ - this.visitorInEditor = null; - - this.visitObjList = []; - this.visitPropList = []; + this.visit = null; } }; /** @@ -92,18 +88,8 @@ Details.prototype.reset = function () { this.rawProp = ''; //this.rawObjList.length = 0; //this.rawPropList.length = 0; - - if (CC_DEV) { - this.visitorInEditor = null; - this.visitObjList.length = 0; - this.visitPropList.length = 0; - } }; if (CC_DEV) { - Details.prototype.visitLater = function (obj, propName) { - this.visitObjList.push(obj); - this.visitPropList.push(propName); - }; Details.prototype.assignAssetsBy = function (getter) { for (var i = 0, len = this.uuidList.length; i < len; i++) { var uuid = this.uuidList[i]; @@ -190,11 +176,6 @@ var _Deserializer = (function () { // dereference _dereference(this); - - // call visitor after all properties initialized - if (CC_DEV) { - this._callVisitorInEditor(); - } } function _dereference (self) { @@ -210,16 +191,7 @@ var _Deserializer = (function () { } } - if (CC_DEV) { - _Deserializer.prototype._callVisitorInEditor = function () { - var result = this.result; - if (result.visitorInEditor) { - result.visitorInEditor(result.visitObjList, result.visitPropList, this); - } - }; - } - - // 和 _deserializeObject 不同的地方在于会判断 id 和 uuid + // 和 _deserializeObject 不同的地方在于会判断 id 和 uuid _Deserializer.prototype._deserializeObjField = function (obj, jsonObj, propName, target) { var id = jsonObj.__id__; if (typeof id === 'undefined') { @@ -245,8 +217,8 @@ var _Deserializer = (function () { else { obj[propName] = _deserializeObject(this, jsonObj); } - if (CC_DEV && this.result.visitorInEditor) { - this.result.visitLater(obj, propName); + if (CC_DEV && this.result.visit) { + this.result.visit(obj, propName); } } } @@ -260,8 +232,8 @@ var _Deserializer = (function () { this._idObjList.push(obj); this._idPropList.push(propName); } - if (CC_DEV && this.result.visitorInEditor) { - this.result.visitLater(obj, propName); + if (CC_DEV && this.result.visit) { + this.result.visit(obj, propName); } } }; @@ -273,8 +245,8 @@ var _Deserializer = (function () { if (typeof prop !== 'object') { if (propName !== '__type__'/* && k != '__id__'*/) { instance[propName] = prop; - if (CC_DEV && self.result.visitorInEditor) { - self.result.visitLater(instance, propName); + if (CC_DEV && self.result.visit) { + self.result.visit(instance, propName); } } } @@ -399,13 +371,7 @@ var _Deserializer = (function () { if (!klass) { var noLog = self._classFinder === JS._getClassById; if (noLog) { - if (CC_EDITOR && Editor.UuidUtils.isUuid(type)) { - type = Editor.UuidUtils.decompressUuid(type); - cc.warn('Can not find script "%s"', type); - } - else { - cc.warn('Can not find class "%s"', type); - } + cc.deserialize.reportMissingClass(type); } return null; } @@ -469,9 +435,8 @@ var _Deserializer = (function () { } else { obj[i] = prop; - - if (CC_DEV && self.result.visitorInEditor) { - self.result.visitLater(obj, '' + i); + if (CC_DEV && self.result.visit) { + self.result.visit(obj, '' + i); } } } @@ -537,3 +502,12 @@ cc.deserialize = function (data, result, options) { }; cc.deserialize.Details = Details; +cc.deserialize.reportMissingClass = function (id) { + if (CC_EDITOR && Editor.UuidUtils.isUuid(id)) { + id = Editor.UuidUtils.decompressUuid(id); + cc.warn('Can not find script "%s"', id); + } + else { + cc.warn('Can not find class "%s"', id); + } +}; From e9b07c435b9d648b09432c75781ea94af0f96e3f Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Mon, 25 Jul 2016 23:24:17 +0800 Subject: [PATCH 4/4] minor tweaks --- cocos2d/core/components/CCComponent.js | 2 +- cocos2d/core/components/missing-script.js | 26 +++++++++++++++++++---- cocos2d/core/platform/CCObject.js | 10 ++++----- cocos2d/core/platform/deserialize.js | 16 ++++++++------ cocos2d/core/utils/find.js | 12 ++++++++++- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index 8ea95a835d..27a27790aa 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -374,7 +374,7 @@ var Component = cc.Class({ */ enabledInHierarchy: { get: function () { - return this._objFlags & IsOnEnableCalled; + return (this._objFlags & IsOnEnableCalled) > 0; }, visible: false }, diff --git a/cocos2d/core/components/missing-script.js b/cocos2d/core/components/missing-script.js index 07fe2f6d9a..23e947db3e 100644 --- a/cocos2d/core/components/missing-script.js +++ b/cocos2d/core/components/missing-script.js @@ -25,10 +25,23 @@ var JS = cc.js; +/* + * A temp fallback to contain the original serialized data which can not be loaded. + */ +var MissingClass = cc.Class({ + name: 'cc.MissingClass', + properties: { + // the serialized data for original object + _$erialized: { + default: null, + visible: false, + editorOnly: true + } + }, +}); + /* * A temp fallback to contain the original component which can not be loaded. - * Actually, this class will be used whenever a class failed to deserialize, - * regardless of whether it is child class of component. */ var MissingScript = cc.Class({ name: 'cc.MissingScript', @@ -84,14 +97,19 @@ var MissingScript = cc.Class({ * @param {string} id * @return {function} constructor */ - safeFindClass: function (id) { + safeFindClass: function (id, data) { var cls = JS._getClassById(id); if (cls) { return cls; } if (id) { cc.deserialize.reportMissingClass(id); - return MissingScript; + if (data.node && (CC_EDITOR && Editor.UuidUtils.isUuid(id))) { + return MissingScript; + } + else { + return MissingClass; + } } return null; } diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index ac25c74b5f..7abf85d342 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -245,14 +245,14 @@ var deferredDestroyTimer = null; /** * !#en * Destroy this Object, and release all its own references to other objects.
+ * Actual object destruction will delayed until before rendering. *
- * After destroy, this CCObject is not usable any more.
- * You can use cc.isValid(obj) (or obj.isValid if obj is non-nil) to check whether the object is destroyed before - * accessing it. + * After destroy, this CCObject is not usable any more. + * You can use cc.isValid(obj) to check whether the object is destroyed before accessing it. * !#zh * 销毁该对象,并释放所有它对其它对象的引用。
- * 销毁后,CCObject 不再可用。
- * 您可以在访问对象之前使用 cc.isValid(obj)(或 obj.isValid 如果 obj 不为 null)来检查对象是否已被销毁。 + * 销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 + * 实际销毁操作会延迟到当前帧渲染前执行。 * @method destroy * @return {Boolean} whether it is the first time the destroy being called * @example diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 0ab799f7ba..d0ac823e65 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -297,23 +297,25 @@ var _Deserializer = (function () { //--self.stackCounter; } + var RAW_TYPE = Attr.DELIMETER + 'rawType'; + var EDITOR_ONLY = Attr.DELIMETER + 'editorOnly'; + var SERIALIZABLE = Attr.DELIMETER + 'serializable'; + function _deserializeFireClass(self, obj, serialized, klass, target) { - var DELIMETER = Attr.DELIMETER; var props = klass.__props__; var attrs = Attr.getClassAttrs(klass); for (var p = 0; p < props.length; p++) { var propName = props[p]; - // assume all prop in __props__ must have attr - var rawType = attrs[propName + DELIMETER + 'rawType']; + var rawType = attrs[propName + RAW_TYPE]; if (!rawType) { - if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV)) - && attrs[propName + DELIMETER + 'editorOnly']) { + if (((CC_EDITOR && self._ignoreEditorOnly) || (!CC_EDITOR && CC_DEV && !CC_TEST)) + && attrs[propName + EDITOR_ONLY]) { var mayUsedInPersistRoot = (obj instanceof cc.Node && propName === '_id'); if ( !mayUsedInPersistRoot ) { continue; // skip editor only if in preview } } - if (attrs[propName + DELIMETER + 'serializable'] === false) { + if (attrs[propName + SERIALIZABLE] === false) { continue; // skip nonSerialized } var prop = serialized[propName]; @@ -367,7 +369,7 @@ var _Deserializer = (function () { // Type Object (including CCClass) var type = serialized.__type__; - klass = self._classFinder(type); + klass = self._classFinder(type, serialized); if (!klass) { var noLog = self._classFinder === JS._getClassById; if (noLog) { diff --git a/cocos2d/core/utils/find.js b/cocos2d/core/utils/find.js index 986b1b9dff..fbcdd94df7 100644 --- a/cocos2d/core/utils/find.js +++ b/cocos2d/core/utils/find.js @@ -43,11 +43,21 @@ cc.find = module.exports = function (path, referenceNode) { if (!referenceNode) { var scene = cc.director.getScene(); if (!scene) { - cc.warn('Can not get current scene.'); + if (CC_DEV) { + cc.warn('Can not get current scene.'); + } + return null; + } + else if (CC_DEV && !scene.isValid) { + cc.warn('Scene is destroyed'); return null; } referenceNode = scene; } + else if (CC_DEV && !referenceNode.isValid) { + cc.warn('reference node is destroyed'); + return null; + } var match = referenceNode; var startIndex = (path[0] !== '/') ? 0 : 1; // skip first '/'