Merge pull request #363 from jareguo/master

introduce "missing script" mechanisms to prevent data loss
This commit is contained in:
Jare Guo
2016-02-23 14:01:47 +08:00
8 changed files with 166 additions and 441 deletions
-167
View File
@@ -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;
-214
View File
@@ -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;
+5 -3
View File
@@ -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 entity "%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);
}
+1
View File
@@ -1,6 +1,7 @@
require('./CCComponent');
require('./CCComponentInSG');
require('./CCComponentEventHandler');
require('./missing-script');
module.exports = [
require('./CCSprite'),
+79
View File
@@ -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;
+17 -4
View File
@@ -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;
+8 -2
View File
@@ -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;
}
@@ -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);
});
}