From 09d432d35479c9a32ef20b922dfea455000aff5d Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Thu, 1 Aug 2019 18:00:49 +0800 Subject: [PATCH] enchance deprecated and add some deprecated in animation module (#5055) --- cocos/animation/deprecated.ts | 39 ++++++++++++ cocos/animation/index.ts | 2 + cocos/deprecated.ts | 112 ++++++++++++++++++++-------------- 3 files changed, 107 insertions(+), 46 deletions(-) create mode 100644 cocos/animation/deprecated.ts diff --git a/cocos/animation/deprecated.ts b/cocos/animation/deprecated.ts new file mode 100644 index 0000000000..7fe68356f8 --- /dev/null +++ b/cocos/animation/deprecated.ts @@ -0,0 +1,39 @@ + +import { deprecatedWrapper } from '../deprecated'; +import { AnimationComponent } from './animation-component'; +import { AnimationClip } from './animation-clip'; +import { error } from '../core/platform/CCDebug'; + +// deprecated +if (CC_DEBUG) { + + /** + * API更名 + */ + deprecatedWrapper({ + oldTarget: AnimationComponent.prototype, + oldPrefix: "AnimationComponent", + newTarget: AnimationComponent.prototype, + newPrefix: "AnimationComponent", + pairs: [ + ['getAnimationState', 'getState'], + ['addClip', 'createState'] + ] + }); + + /** + * API更名,以及参数变更 + */ + deprecatedWrapper({ + oldTarget: AnimationComponent.prototype, + oldPrefix: "AnimationComponent", + pairs: [['removeClip']], + compatible: false, + custom: function (...args: any) { + error("removeClip已经弃用,请使用removeState,并注意两者参数不一致"); + let arg0 = args[0] as AnimationClip; + return AnimationComponent.prototype.removeState.call(this, arg0.name); + } + }); + +} \ No newline at end of file diff --git a/cocos/animation/index.ts b/cocos/animation/index.ts index 78c8da356a..12b6c8a721 100644 --- a/cocos/animation/index.ts +++ b/cocos/animation/index.ts @@ -16,3 +16,5 @@ export * from './skeletal-animation-clip'; export * from './skeletal-animation-state'; export * from './skeletal-animation-component'; export * from './transform-utils'; + +import './deprecated'; \ No newline at end of file diff --git a/cocos/deprecated.ts b/cocos/deprecated.ts index 1435e4635a..902de32fb6 100644 --- a/cocos/deprecated.ts +++ b/cocos/deprecated.ts @@ -2,8 +2,7 @@ * @hidden */ -import { js } from './core/utils/js'; -import { errorID, error, warnID, warn } from './core/platform/CCDebug'; +import { error, warn } from './core/platform/CCDebug'; export interface IWrapOptions { oldTarget: Function | {}; @@ -12,6 +11,7 @@ export interface IWrapOptions { newTarget?: Function | {}; newPrefix?: string; custom?: Function; + compatible?: boolean; } export let deprecatedWrapper: (option: IWrapOptions) => void; @@ -23,61 +23,81 @@ if (CC_DEBUG) { let newPrefix = option.newPrefix; let deprecatedProps = option.pairs; let custom = option.custom; + let compatible = (option.compatible == null || option.compatible == true) ? true : false; - let _t0: {}; - if (typeof oldTarget == 'function') { - _t0 = oldTarget.prototype; - } else { - _t0 = oldTarget; - } + let _t0 = oldTarget; + let _t1 = newTarget; - let _t1: {}; - if (newTarget) { - if (typeof newTarget == 'function') { - _t1 = newTarget.prototype; - } else { - _t1 = newTarget; - } - } - - deprecatedProps.forEach(function (prop: string[]) { - let deprecatedProp = prop[0]; - let newProp: string; - if (prop.length > 1) { - newProp = prop[1]; - } - - let _print = () => { - if (custom != null) { - // custom message - custom(); - } else if (newProp != null && newPrefix != null) { - // remove but provide a new - warn("'%s' is deprecated, please use '%s' instead.", `${oldPrefix}.${deprecatedProp}`, `${newPrefix}.${newProp}`); - } else { - // remove only - error("'%s.%s' is removed", oldPrefix, deprecatedProp); + if (compatible) { + deprecatedProps.forEach(function (prop: string[]) { + let deprecatedProp = prop[0]; + let newProp: string; + if (prop.length > 1) { + newProp = prop[1]; } - }; - Object.defineProperty(_t0, deprecatedProp, { + /** + * default log && return + */ /* eslint-disable-next-line */ - get: function () { - _print(); + let _default = () => { + if (newProp != null && newPrefix != null) { + // remove but provide a new + warn("'%s' is deprecated, please use '%s' instead.", `${oldPrefix}.${deprecatedProp}`, `${newPrefix}.${newProp}`); + } else if (newTarget != null) { + // remove but will provide a new + warn("'%s' is deprecated.", `${oldPrefix}.${deprecatedProp}`); + } else { + // remove only + error("'%s.%s' is removed", oldPrefix, deprecatedProp); + } + if (newProp != null && _t1 != null && _t1[newProp] != null) { return _t1[newProp]; } - }, - set: function (v: any) { - _print(); - if (newProp != null && _t1 != null && _t1[newProp] != null) { - if (typeof _t1[newProp] !== 'function') { - _t1[newProp] = v; + }; + + Object.defineProperty(_t0, deprecatedProp, { + /* eslint-disable-next-line */ + get: function (this) { + if (custom != null) { + return custom.call(this); + } else { + return _default(); + } + }, + set: function (this, v: any) { + if (custom != null) { + custom.call(this); + } else { + _default(); + } + if (newProp != null && _t1 != null && _t1[newProp] != null) { + if (typeof _t1[newProp] !== 'function') { + _t1[newProp] = v; + } } } - } + }); }); - }); + } else { + deprecatedProps.forEach(function (prop: string[]) { + let deprecatedProp = prop[0]; + + Object.defineProperty(_t0, deprecatedProp, { + /* eslint-disable-next-line */ + value: function (this, ...args: any) { + if (custom != null) { + return custom.call(this, + args[0], args[1], args[2], + args[3], args[4], args[5], + args[6], args[7], args[8], + args[9], args[10], args[11]); + } + } + }); + }); + } }; } else { // for compatible