Skip to content

Commit

Permalink
deprecate @property(cc.Texture2D) value = null; in class defined by…
Browse files Browse the repository at this point in the history
… using ES6 or TypeScript
  • Loading branch information
jareguo committed Sep 18, 2017
1 parent fbd4731 commit 40c6f18
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 15 deletions.
1 change: 1 addition & 0 deletions DebugInfos.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ if (CC_DEBUG) {
" set %s (value) {\n" +
" ...\n" +
" }",
"3656": "The default value of %s.%s must be an empty string. (changed since 1.7)", //getTypeChecker_4
//Prefab: 3700
"3700": "internal error: _prefab is undefined", //_doInstantiate
"3701": "Failed to load prefab asset for node '%s'", //syncWithPrefab
Expand Down
7 changes: 7 additions & 0 deletions cocos2d/core/assets/CCRawAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ Object.defineProperty(cc.RawAsset, 'isRawAssetType', {
// enumerable is false by default
});

// TODO - DELME after 2.0
Object.defineProperty(cc.RawAsset, 'wasRawAssetType', {
value: function (ctor) {
return ctor === cc.Texture2D;
}
});

module.exports = cc.RawAsset;
17 changes: 12 additions & 5 deletions cocos2d/core/platform/CCClassDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ function genProperty (ctor, properties, propName, options, desc, cache) {
cc.warnID(3654, JS.getClassName(ctor), propName);
// prop.default = fullOptions.hasOwnProperty('default') ? fullOptions.default : undefined;
}
if (cc.RawAsset.wasRawAssetType(prop.url) &&
prop._short &&
isDefaultValueSpecified &&
defaultValue == null
) {
cc.warnID(3656, JS.getClassName(ctor), propName);
}
}
prop.default = defaultValue;
}
Expand Down Expand Up @@ -355,8 +362,8 @@ var ccclass = checkCtorArgument(function (ctor, name) {
* @property(cc.Vec2)
* offsets = [];
*
* @property(cc.Texture2D)
* texture = "";
* @property(cc.SpriteFrame)
* frame = null;
* }
*
* // above is equivalent to (上面的代码相当于):
Expand Down Expand Up @@ -396,9 +403,9 @@ var ccclass = checkCtorArgument(function (ctor, name) {
* type: cc.Vec2
* }
*
* texture: {
* default: "",
* url: cc.Texture2D
* frame: {
* default: null,
* type: cc.SpriteFrame
* },
* }
* });
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/platform/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Details.prototype.reset = function () {
//this.rawObjList.length = 0;
//this.rawPropList.length = 0;
};
if (CC_EDITOR) {
if (CC_EDITOR || CC_TEST) {
Details.prototype.assignAssetsBy = function (getter) {
for (var i = 0, len = this.uuidList.length; i < len; i++) {
var uuid = this.uuidList[i];
Expand Down
5 changes: 3 additions & 2 deletions cocos2d/core/platform/preprocess-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function checkUrl (val, className, propName, url) {
if (typeof url !== 'function' || !cc.isChildClassOf(url, cc.RawAsset)) {
return cc.errorID(5504, className, propName);
}
if (cc.isChildClassOf(url, cc.Asset)) {
if (cc.isChildClassOf(url, cc.Asset) && !cc.RawAsset.wasRawAssetType(url)) {
return cc.errorID(5505, className, propName, cc.js.getClassName(url));
}
if (val.type) {
Expand Down Expand Up @@ -168,6 +168,7 @@ function getBaseClassWherePropertyDefined_DEV (propName, cls) {
}

exports.getFullFormOfProperty = function (options) {
// TODO - deprecate `variable: cc.Texture2D,` from 1.7
var isLiteral = options && options.constructor === Object;
if ( !isLiteral ) {
if (Array.isArray(options) && options.length > 0) {
Expand All @@ -179,7 +180,7 @@ exports.getFullFormOfProperty = function (options) {
}
else if (typeof options === 'function') {
var type = options;
if (cc.RawAsset.isRawAssetType(type)) {
if (cc.RawAsset.isRawAssetType(type) || cc.RawAsset.wasRawAssetType(type)) {
return {
default: '',
url: type,
Expand Down
16 changes: 9 additions & 7 deletions cocos2d/core/platform/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ cc.url = {
_builtinRawAssets: '',

normalize: function (url) {
if (url.charCodeAt(0) === 46 && url.charCodeAt(1) === 47) {
// strip './'
url = url.slice(2);
}
else if (url.charCodeAt(0) === 47) {
// strip '/'
url = url.slice(1);
if (url) {
if (url.charCodeAt(0) === 46 && url.charCodeAt(1) === 47) {
// strip './'
url = url.slice(2);
}
else if (url.charCodeAt(0) === 47) {
// strip '/'
url = url.slice(1);
}
}
return url;
},
Expand Down
82 changes: 82 additions & 0 deletions test/qunit/unit-es5/test-migrating-raw-asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
if (TestEditorExtends) {
(function () {

module('Migrating Raw Assets');

test('serialization if declared in the OLD way', function () {
// setup
var MyAsset = cc.Class({
name: 'MyAsset',
properties: {
ref_full: {
url: cc.Texture2D,
default: "",
},
ref_brief: cc.Texture2D,
}
});
var asset = new MyAsset();
asset.ref_full = 'foo';
asset.ref_brief = 'foo';

// serialize
var restore = Editor.Utils.UuidCache.urlToUuid;
Editor.Utils.UuidCache.urlToUuid = function (url) {
return {
foo: '01',
}[url];
};
var serializedAsset = Editor.serialize(asset);
Editor.Utils.UuidCache.urlToUuid = restore;

// deserialize
var deserializedAsset = cc.deserialize(serializedAsset, null, {createAssetRefs: true});

// test
ok(deserializedAsset.ref_full._uuid === '01', 'should support serialization if declared in full form');
ok(deserializedAsset.ref_brief._uuid === '01', 'should support serialization if declared in brief form');

//teardown
cc.js.unregisterClass(MyAsset);
});

test('serialization if declared in the NEW way', function () {
// setup
var MyAsset = cc.Class({
name: 'MyAsset',
properties: {
ref_full: {
type: cc.Texture2D,
default: null,
},
// TODO - uncomment in 2.0
// ref_brief: cc.Texture2D,
}
});
var asset = new MyAsset();
asset.ref_full = new cc.Texture2D();
asset.ref_full._uuid = '01';
// asset.ref_brief = asset.ref_full;

// serialize
var restore = Editor.Utils.UuidCache.urlToUuid;
Editor.Utils.UuidCache.urlToUuid = function (url) {
return {
foo: '01',
}[url];
};
var serializedAsset = Editor.serialize(asset);
Editor.Utils.UuidCache.urlToUuid = restore;

// deserialize
var deserializedAsset = cc.deserialize(serializedAsset, null, {createAssetRefs: true});

// test
ok(deserializedAsset.ref_full._uuid === '01', 'should support serialization if declared in full form');
// ok(deserializedAsset.ref_brief._uuid === '01', 'should support serialization if declared in brief form');

//teardown
cc.js.unregisterClass(MyAsset);
});
})();
}

0 comments on commit 40c6f18

Please sign in to comment.