diff --git a/EngineErrorMap.md b/EngineErrorMap.md index 0df3c2a20f..f6b586d59f 100644 --- a/EngineErrorMap.md +++ b/EngineErrorMap.md @@ -1012,6 +1012,14 @@ Can not find script '%s' Can not find class '%s' +### 5303 + +Failed to deserialize %s, missing _deserialize function. + +### 5304 + +Unable to deserialize version %s data. + ### 5400 '%s' is deprecated, use '%s' instead please. diff --git a/cocos/core/assets/image-asset.ts b/cocos/core/assets/image-asset.ts index 551f091c3c..0ab02f8119 100644 --- a/cocos/core/assets/image-asset.ts +++ b/cocos/core/assets/image-asset.ts @@ -32,7 +32,7 @@ import {ccclass, override} from 'cc.decorator'; import { GFXDevice, GFXFeature } from '../gfx/device'; import { Asset } from './asset'; import { PixelFormat } from './asset-enum'; -import { EDITOR, MINIGAME, ALIPAY, XIAOMI, BYTEDANCE, JSB } from 'internal:constants'; +import { EDITOR, MINIGAME, ALIPAY, XIAOMI, BYTEDANCE, JSB, TEST } from 'internal:constants'; import { legacyCC } from '../global-exports'; import { warnID } from '../platform/debug'; @@ -245,26 +245,28 @@ export class ImageAsset extends Asset { // SERIALIZATION public _serialize () { - let targetExtensions = this._exportedExts; - if (!targetExtensions && this._native) { - targetExtensions = [this._native]; - } - - if (!targetExtensions) { - return ''; - } - - const extensionIndices: string[] = []; - for (const targetExtension of targetExtensions) { - const extensionFormat = targetExtension.split('@'); - const i = ImageAsset.extnames.indexOf(extensionFormat[0]); - let exportedExtensionID = i < 0 ? targetExtension : `${i}`; - if (extensionFormat[1]) { - exportedExtensionID += '@' + extensionFormat[1]; + if (EDITOR || TEST) { + let targetExtensions = this._exportedExts; + if (!targetExtensions && this._native) { + targetExtensions = [this._native]; } - extensionIndices.push(exportedExtensionID); + + if (!targetExtensions) { + return ''; + } + + const extensionIndices: string[] = []; + for (const targetExtension of targetExtensions) { + const extensionFormat = targetExtension.split('@'); + const i = ImageAsset.extnames.indexOf(extensionFormat[0]); + let exportedExtensionID = i < 0 ? targetExtension : `${i}`; + if (extensionFormat[1]) { + exportedExtensionID += '@' + extensionFormat[1]; + } + extensionIndices.push(exportedExtensionID); + } + return { fmt: extensionIndices.join('_'), w: this.width, h: this.height }; } - return { fmt: extensionIndices.join('_'), w: this.width, h: this.height }; } public _deserialize (data: any, handle: any) { diff --git a/cocos/core/assets/sprite-atlas.ts b/cocos/core/assets/sprite-atlas.ts index 135931a363..a08bdbb74a 100644 --- a/cocos/core/assets/sprite-atlas.ts +++ b/cocos/core/assets/sprite-atlas.ts @@ -28,6 +28,7 @@ * @category asset */ +import { EDITOR, TEST } from "internal:constants"; import { ccclass, serializable, editable } from 'cc.decorator'; import * as js from '../utils/js'; import { Asset } from './asset'; @@ -106,21 +107,23 @@ export class SpriteAtlas extends Asset { } public _serialize (exporting?: any) { - const frames: string[] = []; - for (const key of Object.keys(this.spriteFrames)) { - const spriteFrame = this.spriteFrames[key]; - let id = spriteFrame ? spriteFrame._uuid : ''; - if (id && exporting) { - id = EditorExtends.UuidUtils.compressUuid(id, true); + if (EDITOR || TEST) { + const frames: string[] = []; + for (const key of Object.keys(this.spriteFrames)) { + const spriteFrame = this.spriteFrames[key]; + let id = spriteFrame ? spriteFrame._uuid : ''; + if (id && exporting) { + id = EditorExtends.UuidUtils.compressUuid(id, true); + } + frames.push(key); + frames.push(id); } - frames.push(key); - frames.push(id); - } - return { - name: this._name, - spriteFrames: frames, - }; + return { + name: this._name, + spriteFrames: frames, + }; + } } public _deserialize (serializeData: any, handle: any){ diff --git a/cocos/core/assets/sprite-frame.ts b/cocos/core/assets/sprite-frame.ts index b49ad991fe..481a4b74c9 100644 --- a/cocos/core/assets/sprite-frame.ts +++ b/cocos/core/assets/sprite-frame.ts @@ -35,7 +35,7 @@ import { murmurhash2_32_gc } from '../utils/murmurhash2_gc'; import { Asset } from './asset'; import { RenderTexture } from './render-texture'; import { TextureBase } from './texture-base'; -import { EDITOR } from 'internal:constants'; +import { EDITOR, TEST } from 'internal:constants'; import { legacyCC } from '../global-exports'; import { ImageAsset, ImageSource } from './image-asset'; import { Texture2D } from './texture-2d'; @@ -861,47 +861,49 @@ export class SpriteFrame extends Asset { // SERIALIZATION public _serialize (exporting?: any): any { - const rect = this._rect; - const offset = this._offset; - const originalSize = this._originalSize; - let uuid = this._uuid; - let texture; - if (this._texture) { - texture = this._texture._uuid; - } + if (EDITOR || TEST) { + const rect = this._rect; + const offset = this._offset; + const originalSize = this._originalSize; + let uuid = this._uuid; + let texture; + if (this._texture) { + texture = this._texture._uuid; + } - if (uuid && exporting) { - uuid = EditorExtends.UuidUtils.compressUuid(uuid, true); - } - if (texture && exporting) { - texture = EditorExtends.UuidUtils.compressUuid(texture, true); - } + if (uuid && exporting) { + uuid = EditorExtends.UuidUtils.compressUuid(uuid, true); + } + if (texture && exporting) { + texture = EditorExtends.UuidUtils.compressUuid(texture, true); + } - let vertices; - if (this.vertices) { - vertices = { - triangles: this.vertices.triangles, - x: this.vertices.x, - y: this.vertices.y, - u: this.vertices.u, - v: this.vertices.v, + let vertices; + if (this.vertices) { + vertices = { + triangles: this.vertices.triangles, + x: this.vertices.x, + y: this.vertices.y, + u: this.vertices.u, + v: this.vertices.v, + }; + } + + const serialize = { + name: this._name, + atlas: exporting ? undefined : this._atlasUuid, // strip from json if exporting + rect, + offset, + originalSize, + rotated: this._rotated, + capInsets: this._capInsets, + vertices, + texture, }; + + // 为 underfined 的数据则不在序列化文件里显示 + return serialize; } - - const serialize = { - name: this._name, - atlas: exporting ? undefined : this._atlasUuid, // strip from json if exporting - rect, - offset, - originalSize, - rotated: this._rotated, - capInsets: this._capInsets, - vertices, - texture, - }; - - // 为 underfined 的数据则不在序列化文件里显示 - return serialize; } public _deserialize (serializeData: any, handle: any) { diff --git a/cocos/core/assets/texture-2d.ts b/cocos/core/assets/texture-2d.ts index 28316dd7d1..fa3b3600c6 100644 --- a/cocos/core/assets/texture-2d.ts +++ b/cocos/core/assets/texture-2d.ts @@ -28,6 +28,7 @@ * @category asset */ +import { EDITOR, TEST } from "internal:constants"; import { ccclass, type } from 'cc.decorator'; import { GFXTextureType } from '../gfx/define'; import { PixelFormat } from './asset-enum'; @@ -225,18 +226,20 @@ export class Texture2D extends SimpleTexture { } public _serialize (exporting?: any): any { - return { - base: super._serialize(exporting), - mipmaps: this._mipmaps.map((mipmap) => { - if (!mipmap || !mipmap._uuid) { - return null; - } - if (exporting) { - return EditorExtends.UuidUtils.compressUuid(mipmap._uuid, true); - } - return mipmap._uuid; - }), - }; + if (EDITOR || TEST) { + return { + base: super._serialize(exporting), + mipmaps: this._mipmaps.map((mipmap) => { + if (!mipmap || !mipmap._uuid) { + return null; + } + if (exporting) { + return EditorExtends.UuidUtils.compressUuid(mipmap._uuid, true); + } + return mipmap._uuid; + }), + }; + } } public _deserialize (serializedData: any, handle: any) { diff --git a/cocos/core/assets/texture-base.ts b/cocos/core/assets/texture-base.ts index 5bf1dd0477..f34bf63d3b 100644 --- a/cocos/core/assets/texture-base.ts +++ b/cocos/core/assets/texture-base.ts @@ -29,6 +29,7 @@ */ // @ts-check +import { EDITOR, TEST } from 'internal:constants'; import {ccclass, serializable} from 'cc.decorator'; import { GFXDevice } from '../gfx/device'; import { GFXTexture } from '../gfx/texture'; @@ -277,9 +278,11 @@ export class TextureBase extends Asset { * @return */ public _serialize (exporting?: any): any { - return this._minFilter + ',' + this._magFilter + ',' + - this._wrapS + ',' + this._wrapT + ',' + - this._mipFilter + ',' + this._anisotropy; + if (EDITOR || TEST) { + return this._minFilter + ',' + this._magFilter + ',' + + this._wrapS + ',' + this._wrapT + ',' + + this._mipFilter + ',' + this._anisotropy; + } } /** diff --git a/cocos/core/assets/texture-cube.ts b/cocos/core/assets/texture-cube.ts index 6acf6a8f89..24eaa56489 100644 --- a/cocos/core/assets/texture-cube.ts +++ b/cocos/core/assets/texture-cube.ts @@ -27,6 +27,7 @@ * @category asset */ +import { EDITOR, TEST } from "internal:constants"; import { ccclass, serializable } from 'cc.decorator'; import { GFXTextureFlagBit, GFXTextureType } from '../gfx/define'; import { ImageAsset } from './image-asset'; @@ -220,24 +221,26 @@ export class TextureCube extends SimpleTexture { } public _serialize (exporting?: any) { - return { - base: super._serialize(), - mipmaps: this._mipmaps.map((mipmap) => exporting ? { - front: EditorExtends.UuidUtils.compressUuid(mipmap.front._uuid, true), - back: EditorExtends.UuidUtils.compressUuid(mipmap.back._uuid, true), - left: EditorExtends.UuidUtils.compressUuid(mipmap.left._uuid, true), - right: EditorExtends.UuidUtils.compressUuid(mipmap.right._uuid, true), - top: EditorExtends.UuidUtils.compressUuid(mipmap.top._uuid, true), - bottom: EditorExtends.UuidUtils.compressUuid(mipmap.bottom._uuid, true), - } : { - front: mipmap.front._uuid, - back: mipmap.back._uuid, - left: mipmap.left._uuid, - right: mipmap.right._uuid, - top: mipmap.top._uuid, - bottom: mipmap.bottom._uuid, - }), - }; + if (EDITOR || TEST) { + return { + base: super._serialize(), + mipmaps: this._mipmaps.map((mipmap) => exporting ? { + front: EditorExtends.UuidUtils.compressUuid(mipmap.front._uuid, true), + back: EditorExtends.UuidUtils.compressUuid(mipmap.back._uuid, true), + left: EditorExtends.UuidUtils.compressUuid(mipmap.left._uuid, true), + right: EditorExtends.UuidUtils.compressUuid(mipmap.right._uuid, true), + top: EditorExtends.UuidUtils.compressUuid(mipmap.top._uuid, true), + bottom: EditorExtends.UuidUtils.compressUuid(mipmap.bottom._uuid, true), + } : { + front: mipmap.front._uuid, + back: mipmap.back._uuid, + left: mipmap.left._uuid, + right: mipmap.right._uuid, + top: mipmap.top._uuid, + bottom: mipmap.bottom._uuid, + }), + }; + } } public _deserialize (serializedData: ITextureCubeSerializeData, handle: any) { diff --git a/cocos/core/components/missing-script.ts b/cocos/core/components/missing-script.ts index 04da867227..5ed6591241 100644 --- a/cocos/core/components/missing-script.ts +++ b/cocos/core/components/missing-script.ts @@ -36,20 +36,6 @@ import { EDITOR } from 'internal:constants'; import { legacyCC } from '../global-exports'; import { warnID } from '../platform/debug'; -/** - * @en - * A temp fallback to contain the original serialized data which can not be loaded. - * @zh - * 包含无法加载的原始序列化数据的临时回退。 - */ -@ccclass('cc.MissingClass') -class MissingClass { - // the serialized data for original object - @serializable - @editorOnly - public _$erialized = null; -} - /** * @en * A temp fallback to contain the original component which can not be loaded. @@ -74,25 +60,12 @@ export default class MissingScript extends Component { * @param {string} id * @return {function} constructor */ - public static safeFindClass (id: string, data) { + public static safeFindClass (id: string) { const cls = _getClassById(id); if (cls) { return cls; } - if (id) { - legacyCC.deserialize.reportMissingClass(id); - return MissingScript.getMissingWrapper(id, data); - } - return null; - } - public static getMissingWrapper (id, data) { - if (data.node && (/^[0-9a-zA-Z+/]{23}$/.test(id) || BUILTIN_CLASSID_RE.test(id))) { - // is component - return MissingScript; - } - else { - return MissingClass; - } + legacyCC.deserialize.reportMissingClass(id); } @editable diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index 702781c59b..96578f4437 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -52,7 +52,7 @@ import { legacyCC } from '../global-exports'; const DELIMETER = attributeUtils.DELIMETER; const BUILTIN_ENTRIES = ['name', 'extends', 'mixins', 'ctor', '__ctor__', 'properties', 'statics', 'editor', '__ES6__']; -const INVALID_STATICS_DEV = ['name', '__ctors__', '__props__', 'arguments', 'call', 'apply', 'caller', 'length', 'prototype']; +const INVALID_STATICS_DEV = ['name', '__ctors__', '__props__', '__values__', 'arguments', 'call', 'apply', 'caller', 'length', 'prototype']; function pushUnique (array, item) { if (array.indexOf(item) < 0) { diff --git a/cocos/core/data/deserialize-dynamic.ts b/cocos/core/data/deserialize-dynamic.ts index 227fb026c8..5b1a023ecd 100644 --- a/cocos/core/data/deserialize-dynamic.ts +++ b/cocos/core/data/deserialize-dynamic.ts @@ -28,7 +28,7 @@ * @hidden */ -import { EDITOR, TEST, DEV, JSB, PREVIEW, SUPPORT_JIT } from 'internal:constants'; +import { EDITOR, TEST, DEV, BUILD, JSB, PREVIEW, SUPPORT_JIT } from 'internal:constants'; import { legacyCC } from '../global-exports'; import { warnID } from '../platform/debug'; import * as js from '../utils/js'; @@ -402,7 +402,7 @@ class _Deserializer { this.deserializedList = []; this.deserializedData = null; this._classFinder = classFinder; - if (DEV) { + if (!BUILD) { this._target = target; this._ignoreEditorOnly = ignoreEditorOnly; } @@ -461,7 +461,6 @@ class _Deserializer { /* * @param {Object} serialized - The obj to deserialize, must be non-nil - * @param {Boolean} _stillUseUrl * @param {Object} [target=null] - editor only * @param {Object} [owner] - debug only * @param {String} [propName] - debug only @@ -526,7 +525,7 @@ class _Deserializer { } catch (e) { console.error('deserialize ' + klass.name + ' failed, ' + e.stack); - klass = MissingScript.getMissingWrapper(type, serialized); + klass = MissingScript; legacyCC.deserialize.reportMissingClass(type); deserializeByType(); } @@ -715,7 +714,7 @@ _Deserializer.pool.get = function (result, target, classFinder, customEnv, ignor cache.result = result; cache.customEnv = customEnv; cache._classFinder = classFinder; - if (DEV) { + if (!BUILD) { cache._target = target; cache._ignoreEditorOnly = ignoreEditorOnly; } diff --git a/cocos/core/load-pipeline/unpackers.ts b/cocos/core/load-pipeline/unpackers.ts index d87967ce62..0627b9a6c6 100644 --- a/cocos/core/load-pipeline/unpackers.ts +++ b/cocos/core/load-pipeline/unpackers.ts @@ -31,6 +31,7 @@ import { errorID } from "../platform/debug"; import { js } from "../utils/js"; import { Texture2D } from "../assets/texture-2d"; +import { packCustomObjData } from '../data/deserialize'; export class JsonUnpacker { public jsons = {}; @@ -76,10 +77,7 @@ export class TextureUnpacker { retrieve (key) { let content = this.contents[key]; if (content) { - return { - __type__: js._getClassId(Texture2D), - content: content - }; + return packCustomObjData(js._getClassId(Texture2D), content); } else { return null; diff --git a/cocos/core/load-pipeline/uuid-loader.ts b/cocos/core/load-pipeline/uuid-loader.ts index 27bf526515..07a84455ea 100644 --- a/cocos/core/load-pipeline/uuid-loader.ts +++ b/cocos/core/load-pipeline/uuid-loader.ts @@ -53,7 +53,6 @@ function parseDepends (item, asset, tdInfo: Details, deferredLoadRawAssetsInRunt let objList = tdInfo.uuidObjList; let propList = tdInfo.uuidPropList; // @ts-ignore - let stillUseUrl = tdInfo._stillUseUrl; let depends; let i, dependUuid; // cache dependencies for auto release @@ -81,7 +80,6 @@ function parseDepends (item, asset, tdInfo: Details, deferredLoadRawAssetsInRunt deferredLoadRaw: true, _owner: obj, _ownerProp: prop, - _stillUseUrl: stillUseUrl[i] }); } } @@ -97,7 +95,6 @@ function parseDepends (item, asset, tdInfo: Details, deferredLoadRawAssetsInRunt uuid: dependUuid, _owner: objList[i], _ownerProp: propList[i], - _stillUseUrl: stillUseUrl[i] }; } @@ -143,10 +140,6 @@ function loadDepends (pipeline, item, asset, depends, callback) { function loadCallback (item) { let value = item.content; // @ts-ignore - if (this._stillUseUrl) { - value = value ? value.nativeUrl : item.rawUrl; - } - // @ts-ignore this._owner[this._ownerProp] = value; if (item.uuid !== asset._uuid && dependKeys.indexOf(item.id) < 0) { dependKeys.push(item.id); @@ -265,7 +258,7 @@ export function loadUuid (item, callback) { if (res) { return res; } - return legacyCC._MissingScript.getMissingWrapper(type, data); + return legacyCC._MissingScript; }; classFinder.onDereferenced = MissingClass.classFinder.onDereferenced; } diff --git a/cocos/core/utils/js-typed.ts b/cocos/core/utils/js-typed.ts index 204ba031d3..bb8503a233 100644 --- a/cocos/core/utils/js-typed.ts +++ b/cocos/core/utils/js-typed.ts @@ -269,7 +269,8 @@ export function formatStr (msg: string | any, ...subst: any[]) { for (const arg of subst) { const regExpToTest = typeof arg === 'number' ? REGEXP_NUM_OR_STR : REGEXP_STR; if (regExpToTest.test(msg)) { - msg = msg.replace(regExpToTest, arg); + const notReplaceFunction = '' + arg; + msg = msg.replace(regExpToTest, notReplaceFunction); } else { msg += ' ' + arg; diff --git a/scripts/build-engine/src/index.ts b/scripts/build-engine/src/index.ts index d363aa2629..d6587974f0 100644 --- a/scripts/build-engine/src/index.ts +++ b/scripts/build-engine/src/index.ts @@ -320,8 +320,13 @@ async function _doBuild ({ if (doUglify) { // TODO: tree-shaking not clear! rollupPlugins.push(rpTerser({ + // see https://github.com/terser/terser#compress-options compress: { - reduce_funcs: false // reduce_funcs not suitable for ammo.js + reduce_funcs: false, // reduce_funcs not suitable for ammo.js + keep_fargs: false, + unsafe_Function: true, + unsafe_math: true, + unsafe_methods: true, }, mangle: doUglify, keep_fnames: !doUglify,