From d9327b3180ba490750231ff8d52650b94bc91b46 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 23 Feb 2016 10:17:02 +0800 Subject: [PATCH 1/2] introduce "missing script" mechanisms to prevent data loss --- cocos2d/ToMerge/wrappers/progress-timer.js | 167 -------------- cocos2d/ToMerge/wrappers/ui/text-field.js | 214 ------------------ cocos2d/core/CCNode.js | 2 +- cocos2d/core/components/index.js | 1 + cocos2d/core/components/missing-script.js | 79 +++++++ cocos2d/core/platform/CCAssetLibrary.js | 21 +- .../unit/test-serialize-missing-script.js | 107 ++++----- 7 files changed, 154 insertions(+), 437 deletions(-) delete mode 100644 cocos2d/ToMerge/wrappers/progress-timer.js delete mode 100644 cocos2d/ToMerge/wrappers/ui/text-field.js create mode 100644 cocos2d/core/components/missing-script.js diff --git a/cocos2d/ToMerge/wrappers/progress-timer.js b/cocos2d/ToMerge/wrappers/progress-timer.js deleted file mode 100644 index 328c05c2b2..0000000000 --- a/cocos2d/ToMerge/wrappers/progress-timer.js +++ /dev/null @@ -1,167 +0,0 @@ -var NodeWrapper = require('./node'); - -var ProgressTimerWrapper = cc.Class({ - name: 'cc.ProgressTimerWrapper', - extends: NodeWrapper, - - ctor: function () { - this._midPoint = [0.5, 0.5]; - this._barChangeRate = [1, 1]; - }, - - properties: { - - _type: { - default: cc.ProgressTimer.Type.RADIAL, - type: cc.ProgressTimer.Type - }, - - type: { - get: function () { - return this.targetN.getType(); - }, - set: function (value) { - if (!isNaN(value)) { - this.targetN.setType(value); - } - else { - cc.error('The new type must not be NaN'); - } - }, - type: cc.ProgressTimer.Type - }, - - _percentage: { - default: 50, - }, - - percentage: { - get: function() { - return this.targetN.getPercentage(); - }, - set: function (value) { - if (!isNaN(value)) { - this.targetN.setPercentage(value); - } - else { - cc.error('The new percentage must not be NaN'); - } - }, - }, - - _midPoint: { - default: [] - }, - - midPoint: { - get: function () { - var pt = this.targetN.getMidpoint(); - return new cc.Vec2(pt.x, pt.y); - }, - set: function (value) { - if ( value instanceof cc.Vec2 ) { - this.targetN.setMidpoint(cc.p(value.x, value.y)); - } - else { - cc.error('The new midPoint must be cc.Vec2'); - } - } - }, - - _barChangeRate: { - default: [] - }, - - barChangeRate: { - get: function () { - var pt = this.targetN.getBarChangeRate(); - return new cc.Vec2(pt.x, pt.y); - }, - set: function (value) { - if ( value instanceof cc.Vec2 ) { - this.targetN.setBarChangeRate(cc.p(value.x, value.y)); - } - else { - cc.error('The new barChangeRate must be cc.Vec2'); - } - } - }, - - _reverseDirection : { - default: false, - }, - - reverseDirection: { - get: function () { - return this.targetN.isReverseDirection(); - }, - set: function (value) { - if (typeof value === 'boolean') { - this.targetN.setReverseProgress(value); - } - else { - cc.error('The new reverseDirection must be Boolean'); - } - } - }, - - _texture: { - default: '', - url: cc.Texture2D - }, - - texture: { - get: function () { - var tex = this.targetN.getSprite().texture; - return (tex && tex.url) || ''; - }, - set: function (value) { - this.targetN.getSprite().texture = value; - }, - url: cc.Texture2D - } - }, - - onBeforeSerialize: function () { - NodeWrapper.prototype.onBeforeSerialize.call(this); - this._type = this.type; - this._percentage = this.percentage; - this._midPoint = [this.midPoint.x, this.midPoint.y]; - this._barChangeRate = [this.barChangeRate.x, this.barChangeRate.y]; - this._reverseDirection = this.reverseDirection; - this._texture = this.texture; - }, - - createNode: function (node) { - node = node || new cc.ProgressTimer(new _ccsg.Sprite()); - - var sp = node.getSprite(); - if (this._texture) { - sp.texture = this._texture; - - if (cc.sys.isNative) { - // jsb Texture will not save url, so we save manually. - sp.texture.url = this._texture; - } - } - - node.setType(this._type); - node.setPercentage(this._percentage); - - if (this._midPoint) { - node.setMidpoint(cc.p(this._midPoint[0], this._midPoint[1])); - } - - if (this._barChangeRate) { - node.setBarChangeRate(cc.p(this._barChangeRate[0], this._barChangeRate[1])); - } - - node.setReverseProgress(this._reverseDirection); - - NodeWrapper.prototype.createNode.call(this, node); - - return node; - } -}); - -cc.ProgressTimerWrapper = module.exports = ProgressTimerWrapper; diff --git a/cocos2d/ToMerge/wrappers/ui/text-field.js b/cocos2d/ToMerge/wrappers/ui/text-field.js deleted file mode 100644 index f237873536..0000000000 --- a/cocos2d/ToMerge/wrappers/ui/text-field.js +++ /dev/null @@ -1,214 +0,0 @@ -var Utils = require('../utils'); - -var WidgetWrapper = require('./widget'); - -var TextFieldWrapper = cc.Class({ - name: 'cc.TextFieldWrapper', - extends: WidgetWrapper, - - properties: { - text: { - get: function () { - return this.targetN.string; - }, - set: function (value) { - if (typeof value === 'string') { - this.targetN.string = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new text must be String'); - } - } - }, - - placeHolder: { - get: function () { - return this.targetN.placeHolder; - }, - set: function (value) { - if (typeof value === 'string') { - this.targetN.placeHolder = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new placeHolder must be String'); - } - } - }, - - fontSize: { - get: function () { - return this.targetN.fontSize; - }, - set: function (value) { - if ( !isNaN(value) ) { - this.targetN.fontSize = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new fontSize must not be NaN'); - } - } - }, - - font: { - default: null, - type: cc.TTFFont, - - notify: function () { - if ( !this.targetN ) return; - - var value = this.font; - if (!value || value instanceof cc.TTFFont) { - Utils.setFontToNode(value, this.targetN); - - this.setDirtyFlag(); - } - else { - cc.error('The new font must be cc.TTFFont'); - } - } - }, - - fontFamily: { - get: function () { - return this.targetN.fontName; - }, - set: function (value) { - if (typeof value === 'string') { - this.targetN.fontName = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new fontFamily must be String'); - } - } - }, - - maxLengthEnabled: { - get: function () { - return this.targetN.maxLengthEnabled; - }, - set: function (value) { - if (typeof value === 'boolean') { - this.targetN.maxLengthEnabled = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new maxLengthEnabled must be boolean'); - } - } - }, - - maxLength: { - get: function () { - return this.targetN.maxLength; - }, - set: function (value) { - if (typeof value === 'number') { - this.targetN.maxLength = value; - - this.setDirtyFlag(); - } - else { - cc.error('The new maxLength must be String'); - } - } - }, - - passwordEnabled: { - get: function () { - return this.targetN.passwordEnabled; - }, - set: function (value) { - if (typeof value === 'boolean') { - var targetN = this.targetN; - targetN.passwordEnabled = value; - - this.text = this.text; - } - else { - cc.error('The new passwordEnabled must be boolean'); - } - } - }, - - _text: { - default: '' - }, - - _placeHolder: { - default: 'Input Something' - }, - - _fontSize: { - default: 16 - }, - - _fontFamily: { - default: null - }, - - _maxLengthEnabled: { - default: false - }, - - _maxLength: { - default: 0 - }, - - _passwordEnabled: { - default: false - } - }, - - onSizeChanged: function () { - WidgetWrapper.prototype.onSizeChanged.call(this); - this.setDirtyFlag(); - }, - - setDirtyFlag: function () { - cc.renderer.childrenOrderDirty = true; - }, - - onBeforeSerialize: function () { - WidgetWrapper.prototype.onBeforeSerialize.call(this); - - this._text = this.text; - this._placeHolder = this.placeHolder; - this._fontSize = this.fontSize; - this._fontFamily = this.fontFamily; - this._maxLengthEnabled = this.maxLengthEnabled; - this._passwordEnabled = this.passwordEnabled; - - if (this.maxLengthEnabled) this._maxLength = this.maxLength; - }, - - createNode: function (node) { - node = node || new ccui.TextField(); - node.ignoreContentAdaptWithSize(false); - - WidgetWrapper.prototype.createNode.call(this, node); - - node.passwordEnabled = this._passwordEnabled; - node.string = this._text; - node.placeHolder = this._placeHolder; - node.fontSize = this._fontSize; - node.fontFamily = this._fontFamily; - node.maxLengthEnabled = this._maxLengthEnabled; - - if (this._maxLengthEnabled) node.maxLength = this._maxLength; - - Utils.setFontToNode(this.font, node); - - return node; - } -}); - -cc.TextFieldWrapper = module.exports = TextFieldWrapper; diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 5459924b23..ede5c20eb5 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -698,7 +698,7 @@ var Node = cc.Class({ for (var c = 0; c < originCount; ++c) { var component = this._components[c]; if (! (component instanceof cc.Component) && CC_EDITOR) { - cc.error('Sorry, the component of entity "%s" which with an index of %s is corrupted! It has been removed.\nSee DevTools for details.', this.name, c); + cc.error('Sorry, the component of "%s" which with an index of %s is corrupted! It has been removed.\nSee DevTools for details.', this.name, c); console.log('Corrupted component value:', component); if (component) { this._removeComponent(component); diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index ee5d42f4cc..5286a8c946 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -1,6 +1,7 @@ require('./CCComponent'); require('./CCComponentInSG'); require('./CCComponentEventHandler'); +require('./missing-script'); module.exports = [ require('./CCSprite'), diff --git a/cocos2d/core/components/missing-script.js b/cocos2d/core/components/missing-script.js new file mode 100644 index 0000000000..24bbabe592 --- /dev/null +++ b/cocos2d/core/components/missing-script.js @@ -0,0 +1,79 @@ +var JS = cc.js; + +/* + * 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', extends: cc.Component, + properties: { + //_scriptUuid: { + // get: function () { + // var id = this._$erialized.__type__; + // if (Editor.UuidUtils.isUuid(id)) { + // return Editor.UuidUtils.decompressUuid(id); + // } + // return ''; + // }, + // set: function (value) { + // if ( !sandbox.compiled ) { + // cc.error('Scripts not yet compiled, please fix script errors and compile first.'); + // return; + // } + // if (value && Editor.UuidUtils.isUuid(value._uuid)) { + // var classId = Editor.UuidUtils.compressUuid(value); + // if (cc.js._getClassById(classId)) { + // this._$erialized.__type__ = classId; + // Editor.sendToWindows('reload:window-scripts', sandbox.compiled); + // } + // else { + // cc.error('Can not find a component in the script which uuid is "%s".', value); + // } + // } + // else { + // cc.error('invalid script'); + // } + // } + //}, + error: { + default: '', + multiline: true, + readonly: true, + serializable: false, + displayName: '(Error)' + }, + // the serialized data for original script object + _$erialized: { + default: null, + visible: false, + editorOnly: true + } + }, + ctor: CC_EDITOR && function () { + this.error = _Scene.Sandbox.compiled ? + Editor.T('COMPONENT.missing_scirpt.error_compiled') : + Editor.T('COMPONENT.missing_scirpt.error_not_compiled') + }, + statics: { + /* + * @param {string} id + * @return {function} constructor + */ + safeFindClass: function (id) { + var cls = JS._getClassById(id); + if (cls) { + return cls; + } + if (id) { + return MissingScript; + } + return null; + } + }, + onLoad: function () { + cc.warn('The referenced script on "%s" is missing!', this.name); + } +}); + +cc._MissingScript = module.exports = MissingScript; diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 51272f533e..a34a9e7254 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -27,6 +27,8 @@ var _tdInfo = new cc.deserialize.Details(); var _cc_loader_loadJson = cc.loader.loadJson.bind(cc.loader); +var SCENE_ID = 'cc.Scene'; + // create a loading context which reserves all relevant parameters function LoadingHandle (readMainCache, writeMainCache, recordAssets, deserializeInfo) { //this.readMainCache = readMainCache; @@ -306,6 +308,15 @@ var AssetLibrary = { loadJson: function (json, callback, dontCache, recordAssets) { var handle = new LoadingHandle(!dontCache, !dontCache, recordAssets); var thisTick = true; + if (typeof json === 'string') { + try { + json = JSON.parse(json); + } + catch (e) { + callInNextTick(callback, e, null); + return handle; + } + } this._deserializeWithDepends(json, '', '', function (p1, p2) { if (thisTick) { callInNextTick(callback, p1, p2); @@ -423,7 +434,7 @@ var AssetLibrary = { /** * @method _deserializeWithDepends - * @param {String|Object} json + * @param {Object} json * @param {String} url * @param {String} uuid * @param {loadCallback} callback @@ -433,9 +444,11 @@ var AssetLibrary = { */ _deserializeWithDepends: function (json, url, uuid, callback, handle, existingAsset) { // deserialize asset - //var isScene = typeof Scene !== 'undefined' && json && json[0] && json[0].__type__ === JS._getClassId(Scene); - //var classFinder = isScene ? cc._MissingScript.safeFindClass : function (id) { - var classFinder = function (id) { + var isScene = json && ( + (json[0] && json[0].__type__ === SCENE_ID) || + (json[1] && json[1].__type__ === SCENE_ID) + ); + var classFinder = isScene ? cc._MissingScript.safeFindClass : function (id) { var cls = JS._getClassById(id); if (cls) { return cls; diff --git a/test/qunit/unit/test-serialize-missing-script.js b/test/qunit/unit/test-serialize-missing-script.js index 004b0a39eb..ddd6e847c0 100644 --- a/test/qunit/unit/test-serialize-missing-script.js +++ b/test/qunit/unit/test-serialize-missing-script.js @@ -1,51 +1,56 @@ -//module('Serialize'); -// -//test('deserialize missing script', function() { -// -// var MissingScript = cc.Class({ -// name: 'MissingScript', -// properties: { -// _$erialized: null -// } -// }); -// MissingScript.safeFindClass = function (id) { -// return cc.js._getClassById(id) || MissingScript; -// }; -// -// var ToMiss = cc.Class({ -// name: 'ToMiss', -// properties: { -// ref: null -// } -// }); -// -// var obj = new ToMiss(); -// obj.ref = new cc.Object(); -// -// var lastData = Editor.serialize(obj); -// delete obj.__id__; -// delete obj.ref.__id__; -// cc.js.unregisterClass(ToMiss); -// -// // deserialize -// -// var missed = cc.deserialize(lastData, null, {classFinder: MissingScript.safeFindClass}); -// -// var expectBackup = { -// "__type__": "ToMiss", -// "ref": obj.ref, -// }; -// deepEqual(missed._$erialized, expectBackup, 'can deserialize missing script'); -// -// // serialize -// -// reSerialized = Editor.serialize(missed, {stringify: false}); -// delete obj.ref.__id__; -// deepEqual(reSerialized, JSON.parse(lastData), 'can serialize missing script as its original data'); -// -// //// re deserialize after fixed, no need to test ;) -// //cc.setClassName('ToMiss', ToMiss); -// //var recovered = cc.deserialize(reSerialized); -// //deepEqual(recovered, obj, 'can deserialize correctly after script fixed'); -// //cc.unregisterClass(ToMiss); -//}); +if (TestEditorExtends) { + + largeModule('Serialize'); + + test('deserialize missing script', function () { + + var MissingScript = cc.Class({ + name: 'MissingScript', + properties: { + _$erialized: null + }, + statics: { + safeFindClass: function (id) { + return cc.js._getClassById(id) || MissingScript; + } + } + }); + + var ToMiss = cc.Class({ + name: 'ToMiss', + properties: { + ref: null + } + }); + + var obj = new ToMiss(); + obj.ref = new cc.Object(); + + var lastData = Editor.serialize(obj); + delete obj.__id__; + delete obj.ref.__id__; + cc.js.unregisterClass(ToMiss); + + // deserialize + + var missed = cc.deserialize(lastData, null, {classFinder: MissingScript.safeFindClass}); + + var expectBackup = { + "__type__": "ToMiss", + "ref": obj.ref, + }; + deepEqual(missed._$erialized, expectBackup, 'can deserialize missing script'); + + // serialize + + reSerialized = Editor.serialize(missed, {stringify: false}); + delete obj.ref.__id__; + deepEqual(reSerialized, JSON.parse(lastData), 'can serialize missing script as its original data'); + + //// re deserialize after fixed, no need to test ;) + //cc.setClassName('ToMiss', ToMiss); + //var recovered = cc.deserialize(reSerialized); + //deepEqual(recovered, obj, 'can deserialize correctly after script fixed'); + //cc.unregisterClass(ToMiss); + }); +} \ No newline at end of file From 8aea4f8a2e4b9658dced1db75095ea7fd84f6c91 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Tue, 23 Feb 2016 13:52:12 +0800 Subject: [PATCH 2/2] refine error message --- cocos2d/core/CCNode.js | 8 +++++--- cocos2d/core/platform/deserialize.js | 10 ++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index ede5c20eb5..bcb6508a9d 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -697,9 +697,11 @@ var Node = cc.Class({ var originCount = this._components.length; for (var c = 0; c < originCount; ++c) { var component = this._components[c]; - if (! (component instanceof cc.Component) && CC_EDITOR) { - cc.error('Sorry, the component of "%s" which with an index of %s is corrupted! It has been removed.\nSee DevTools for details.', this.name, c); - console.log('Corrupted component value:', component); + if ( !(component instanceof cc.Component) ) { + if (CC_EDITOR) { + cc.error('Sorry, the component of "%s" which with an index of %s is corrupted! It has been removed.', this.name, c); + console.log('Corrupted component value:', component); + } if (component) { this._removeComponent(component); } diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 12c98870fd..31fc98e91e 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -404,9 +404,15 @@ var _Deserializer = (function () { // Type Object (including CCClass) - klass = self._classFinder(serialized.__type__); + var type = serialized.__type__; + klass = self._classFinder(type); if (!klass) { - cc.error('[cc.deserialize] unknown type: ' + serialized.__type__); + if (CC_EDITOR && Editor.UuidUtils.isUuid(type)) { + type = Editor.UuidUtils.decompressUuid(type); + cc.warn('Can not find script "%s"', type); + return null; + } + cc.warn('Can not find class "%s"', type); return null; }