From 6f78b3b00f7a903a63106d18db3592a12e9da742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=98=8C=E6=95=A6=E4=BC=9F?= Date: Thu, 10 Apr 2025 10:23:41 +0800 Subject: [PATCH] Remove ccon (#18544) --- cocos/asset/asset-manager/downloader.ts | 29 +-- cocos/serialization/ccon.ts | 63 +---- editor/exports/serialization.ts | 2 - external/deserialize/notepack_decode.ts | 333 ++++++++++++++++++++++++ external/deserialize/notepack_encode.ts | 324 +++++++++++++++++++++++ licenses/LICENSE_notepack.io.txt | 19 ++ tests/core/ccon.test.ts | 34 +-- 7 files changed, 682 insertions(+), 122 deletions(-) create mode 100644 external/deserialize/notepack_decode.ts create mode 100644 external/deserialize/notepack_encode.ts create mode 100644 licenses/LICENSE_notepack.io.txt diff --git a/cocos/asset/asset-manager/downloader.ts b/cocos/asset/asset-manager/downloader.ts index 60a12cbdd7..acd0bf6608 100644 --- a/cocos/asset/asset-manager/downloader.ts +++ b/cocos/asset/asset-manager/downloader.ts @@ -31,7 +31,7 @@ import downloadScript from './download-script'; import { files } from './shared'; import { retry, RetryFunction, urlAppendTimestamp } from './utilities'; import { IConfigOption } from './config'; -import { CCON, parseCCONJson, decodeCCONBinary } from '../../serialization/ccon'; +import { CCON, decodeCCONBinary } from '../../serialization/ccon'; import type { AssetManager } from './asset-manager'; export type DownloadHandler = (url: string, options: Record, onComplete: ((err: Error | null, data?: any) => void)) => void; @@ -68,32 +68,6 @@ const downloadArrayBuffer = (url: string, options: Record, onComple downloadFile(url, options, options.onFileProgress as FileProgressCallback, onComplete); }; -const downloadCCON = (url: string, options: Record, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => { - url = url.replace('.ccon', '.json'); - downloader._downloadJson(url, options, (err, json): void => { - if (err) { - onComplete(err); - return; - } - const cconPreface = parseCCONJson(json); - const chunkPromises = Promise.all(cconPreface.chunks.map((chunk): Promise => new Promise((resolve, reject): void => { - downloader._downloadArrayBuffer(`${path.mainFileName(url)}${chunk}`, {}, (errChunk, chunkBuffer: ArrayBuffer): void => { - if (err) { - reject(err); - } else { - resolve(new Uint8Array(chunkBuffer)); - } - }); - }))); - chunkPromises.then((chunks): void => { - const ccon = new CCON(cconPreface.document, chunks); - onComplete(null, ccon); - }).catch((err: Error): void => { - onComplete(err); - }); - }); -}; - const downloadCCONB = (url: string, options: Record, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => { url = url.replace('.cconb', '.bin'); downloader._downloadArrayBuffer(url, options, (err, arrayBuffer: ArrayBuffer): void => { @@ -305,7 +279,6 @@ export class Downloader { '.ExportJson': downloadJson, '.plist': downloadText, - '.ccon': downloadCCON, '.cconb': downloadCCONB, '.fnt': downloadText, diff --git a/cocos/serialization/ccon.ts b/cocos/serialization/ccon.ts index a789eba9e7..18bb8d5eb4 100644 --- a/cocos/serialization/ccon.ts +++ b/cocos/serialization/ccon.ts @@ -23,6 +23,8 @@ */ import { getError, cclegacy } from '../core'; +import { notepackDecode } from '../../external/deserialize/notepack_decode'; +import { notepackEncode } from '../../external/deserialize/notepack_encode'; const VERSION = 1; @@ -48,37 +50,10 @@ export class CCON { private declare _chunks: Uint8Array[]; } -interface CCONPreface { - version: number; - document: unknown; - chunks: string[]; -} - -export function encodeCCONJson (ccon: CCON, chunkURLs: string[]): unknown { - return { - version: VERSION, - document: ccon.document, - chunks: chunkURLs, - } as unknown; -} - -export function parseCCONJson (json: unknown): { - chunks: string[]; - document: unknown; -} { - const cconPreface = json as CCONPreface; - - return { - chunks: cconPreface.chunks, - document: cconPreface.document, - }; -} - export function encodeCCONBinary (ccon: CCON): Uint8Array { const { document, chunks } = ccon; - const jsonString = JSON.stringify(document); - const jsonBytes = encodeJson(jsonString); + const jsonBytes = new Uint8Array(notepackEncode(document)); const ccobBuilder = new BufferBuilder(); const header = new ArrayBuffer(12); @@ -140,10 +115,9 @@ export function decodeCCONBinary (bytes: Uint8Array): CCON { chunksStart += 4; const jsonData = new Uint8Array(dataView.buffer, chunksStart + dataView.byteOffset, jsonDataLength); chunksStart += jsonDataLength; - const jsonString = decodeJson(jsonData); let json: unknown; try { - json = JSON.parse(jsonString); + json = notepackDecode(jsonData); } catch (err) { throw new InvalidCCONError(err as string); } @@ -183,34 +157,6 @@ interface BufferConstructor { from(buffer: ArrayBuffer, byteOffset?: number, byteLength?: number): Buffer; } -function encodeJson (input: string): Uint8Array { - if (typeof TextEncoder !== 'undefined') { - return new TextEncoder().encode(input); - } else if ('Buffer' in globalThis) { - const { Buffer } = (globalThis as unknown as { Buffer: BufferConstructor }); - const buffer = Buffer.from(input, 'utf8'); - return new Uint8Array( - buffer.buffer, - buffer.byteOffset, - buffer.length, - ); - } else { - throw new Error(getError(13103)); - } -} - -function decodeJson (data: Uint8Array): string { - if (typeof TextDecoder !== 'undefined') { - return new TextDecoder().decode(data); - } else if ('Buffer' in globalThis) { - const { Buffer } = (globalThis as unknown as { Buffer: BufferConstructor }); - // eslint-disable-next-line no-buffer-constructor - return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString(); - } else { - throw new Error(getError(13104)); - } -} - export class InvalidCCONError extends Error { } export class BufferBuilder { @@ -258,6 +204,5 @@ export class BufferBuilder { } } -cclegacy.internal.parseCCONJson = parseCCONJson; cclegacy.internal.decodeCCONBinary = decodeCCONBinary; cclegacy.internal.CCON = CCON; diff --git a/editor/exports/serialization.ts b/editor/exports/serialization.ts index 00b297fa88..fd76cd6faa 100644 --- a/editor/exports/serialization.ts +++ b/editor/exports/serialization.ts @@ -1,11 +1,9 @@ export { CCON, - encodeCCONJson, encodeCCONBinary, BufferBuilder, decodeCCONBinary, - parseCCONJson, } from '../../cocos/serialization/ccon'; export { diff --git a/external/deserialize/notepack_decode.ts b/external/deserialize/notepack_decode.ts new file mode 100644 index 0000000000..5a6afdd6f4 --- /dev/null +++ b/external/deserialize/notepack_decode.ts @@ -0,0 +1,333 @@ +/* + * Copyright (c) 2014 Ion Drive Software Ltd. + * Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +class Decoder { + private _offset = 0; + get offset (): number { + return this._offset; + } + + private _buffer: ArrayBuffer; + private _view: DataView; + constructor (buffer: ArrayBuffer | Uint8Array) { + if (buffer instanceof ArrayBuffer) { + this._buffer = buffer; + this._view = new DataView(this._buffer); + } else if (ArrayBuffer.isView(buffer)) { + this._buffer = buffer.buffer; + this._view = new DataView(this._buffer, buffer.byteOffset, buffer.byteLength); + } else { + throw new Error('Invalid argument'); + } + } + + private _array (length: number): unknown[] { + const value = new Array(length); + for (let i = 0; i < length; i++) { + value[i] = this.parse(); + } + return value; + } + + private _map (length: number): any { + let key = ''; const value = {}; + for (let i = 0; i < length; i++) { + key = this.parse(); + value[key] = this.parse(); + } + return value; + } + + private _str (length: number): string { + const value = utf8Read(this._view, this._offset, length); + this._offset += length; + return value; + } + + private _bin (length: number): ArrayBuffer { + const value = this._buffer.slice(this._offset, this._offset + length); + this._offset += length; + return value; + } + + parse (): any { + const prefix = this._view.getUint8(this._offset++); + let value; let length = 0; let type = 0; let hi = 0; let lo = 0; + + if (prefix < 0xc0) { + // positive fixint + if (prefix < 0x80) { + return prefix; + } + // fixmap + if (prefix < 0x90) { + return this._map(prefix & 0x0f); + } + // fixarray + if (prefix < 0xa0) { + return this._array(prefix & 0x0f); + } + // fixstr + return this._str(prefix & 0x1f); + } + + // negative fixint + if (prefix > 0xdf) { + return (0xff - prefix + 1) * -1; + } + + switch (prefix) { + // nil + case 0xc0: + return null; + // false + case 0xc2: + return false; + // true + case 0xc3: + return true; + + // bin + case 0xc4: + length = this._view.getUint8(this._offset); + this._offset += 1; + return this._bin(length); + case 0xc5: + length = this._view.getUint16(this._offset); + this._offset += 2; + return this._bin(length); + case 0xc6: + length = this._view.getUint32(this._offset); + this._offset += 4; + return this._bin(length); + + // ext + case 0xc7: + length = this._view.getUint8(this._offset); + type = this._view.getInt8(this._offset + 1); + this._offset += 2; + if (type === -1) { + // timestamp 96 + const ns = this._view.getUint32(this._offset); + hi = this._view.getInt32(this._offset + 4); + lo = this._view.getUint32(this._offset + 8); + this._offset += 12; + return new Date((hi * 0x100000000 + lo) * 1e3 + ns / 1e6); + } + return [type, this._bin(length)]; + case 0xc8: + length = this._view.getUint16(this._offset); + type = this._view.getInt8(this._offset + 2); + this._offset += 3; + return [type, this._bin(length)]; + case 0xc9: + length = this._view.getUint32(this._offset); + type = this._view.getInt8(this._offset + 4); + this._offset += 5; + return [type, this._bin(length)]; + + // float + case 0xca: + value = this._view.getFloat32(this._offset); + this._offset += 4; + return value; + case 0xcb: + value = this._view.getFloat64(this._offset); + this._offset += 8; + return value; + + // uint + case 0xcc: + value = this._view.getUint8(this._offset); + this._offset += 1; + return value; + case 0xcd: + value = this._view.getUint16(this._offset); + this._offset += 2; + return value; + case 0xce: + value = this._view.getUint32(this._offset); + this._offset += 4; + return value; + case 0xcf: + hi = this._view.getUint32(this._offset) * 2 ** 32; + lo = this._view.getUint32(this._offset + 4); + this._offset += 8; + return hi + lo; + + // int + case 0xd0: + value = this._view.getInt8(this._offset); + this._offset += 1; + return value; + case 0xd1: + value = this._view.getInt16(this._offset); + this._offset += 2; + return value; + case 0xd2: + value = this._view.getInt32(this._offset); + this._offset += 4; + return value; + case 0xd3: + hi = this._view.getInt32(this._offset) * 2 ** 32; + lo = this._view.getUint32(this._offset + 4); + this._offset += 8; + return hi + lo; + + // fixext + case 0xd4: + type = this._view.getInt8(this._offset); + this._offset += 1; + if (type === 0x00) { + // custom encoding for 'undefined' (kept for backward-compatibility) + this._offset += 1; + return undefined; + } + return [type, this._bin(1)]; + case 0xd5: + type = this._view.getInt8(this._offset); + this._offset += 1; + return [type, this._bin(2)]; + case 0xd6: + type = this._view.getInt8(this._offset); + this._offset += 1; + if (type === -1) { + // timestamp 32 + value = this._view.getUint32(this._offset); + this._offset += 4; + return new Date(value * 1e3); + } + return [type, this._bin(4)]; + case 0xd7: + type = this._view.getInt8(this._offset); + this._offset += 1; + if (type === 0x00) { + // custom date encoding (kept for backward-compatibility) + hi = this._view.getInt32(this._offset) * 2 ** 32; + lo = this._view.getUint32(this._offset + 4); + this._offset += 8; + return new Date(hi + lo); + } + if (type === -1) { + // timestamp 64 + hi = this._view.getUint32(this._offset); + lo = this._view.getUint32(this._offset + 4); + this._offset += 8; + const s = (hi & 0x3) * 0x100000000 + lo; + return new Date(s * 1e3 + (hi >>> 2) / 1e6); + } + return [type, this._bin(8)]; + case 0xd8: + type = this._view.getInt8(this._offset); + this._offset += 1; + return [type, this._bin(16)]; + + // str + case 0xd9: + length = this._view.getUint8(this._offset); + this._offset += 1; + return this._str(length); + case 0xda: + length = this._view.getUint16(this._offset); + this._offset += 2; + return this._str(length); + case 0xdb: + length = this._view.getUint32(this._offset); + this._offset += 4; + return this._str(length); + + // array + case 0xdc: + length = this._view.getUint16(this._offset); + this._offset += 2; + return this._array(length); + case 0xdd: + length = this._view.getUint32(this._offset); + this._offset += 4; + return this._array(length); + + // map + case 0xde: + length = this._view.getUint16(this._offset); + this._offset += 2; + return this._map(length); + case 0xdf: + length = this._view.getUint32(this._offset); + this._offset += 4; + return this._map(length); + default: + } + + throw new Error('Could not parse'); + } +} + +function utf8Read (view: DataView, offset: number, length: number): string { + let string = ''; let chr = 0; + for (let i = offset, end = offset + length; i < end; i++) { + const byte = view.getUint8(i); + if ((byte & 0x80) === 0x00) { + string += String.fromCharCode(byte); + continue; + } + if ((byte & 0xe0) === 0xc0) { + string += String.fromCharCode( + ((byte & 0x1f) << 6) + | (view.getUint8(++i) & 0x3f), + ); + continue; + } + if ((byte & 0xf0) === 0xe0) { + string += String.fromCharCode( + ((byte & 0x0f) << 12) + | ((view.getUint8(++i) & 0x3f) << 6) + | ((view.getUint8(++i) & 0x3f) << 0), + ); + continue; + } + if ((byte & 0xf8) === 0xf0) { + chr = ((byte & 0x07) << 18) + | ((view.getUint8(++i) & 0x3f) << 12) + | ((view.getUint8(++i) & 0x3f) << 6) + | ((view.getUint8(++i) & 0x3f) << 0); + if (chr >= 0x010000) { // surrogate pair + chr -= 0x010000; + string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00); + } else { + string += String.fromCharCode(chr); + } + continue; + } + throw new Error(`Invalid byte ${byte.toString(16)}`); + } + return string; +} + +export function notepackDecode (buffer: ArrayBuffer): any { + const decoder = new Decoder(buffer); + const value = decoder.parse(); + if (decoder.offset !== buffer.byteLength) { + throw new Error(`${buffer.byteLength - decoder.offset} trailing bytes`); + } + return value; +} diff --git a/external/deserialize/notepack_encode.ts b/external/deserialize/notepack_encode.ts new file mode 100644 index 0000000000..25e1b27844 --- /dev/null +++ b/external/deserialize/notepack_encode.ts @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2014 Ion Drive Software Ltd. + * Copyright (c) 2025 Xiamen Yaji Software Co., Ltd. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +const TIMESTAMP32_MAX_SEC = 0x100000000 - 1; // 32-bit unsigned int +const TIMESTAMP64_MAX_SEC = 0x400000000 - 1; // 34-bit unsigned int + +function utf8Write (view, offset, str): void { + let c = 0; + for (let i = 0, l = str.length; i < l; i++) { + c = str.charCodeAt(i); + if (c < 0x80) { + view.setUint8(offset++, c); + } else if (c < 0x800) { + view.setUint8(offset++, 0xc0 | (c >> 6)); + view.setUint8(offset++, 0x80 | (c & 0x3f)); + } else if (c < 0xd800 || c >= 0xe000) { + view.setUint8(offset++, 0xe0 | (c >> 12)); + view.setUint8(offset++, 0x80 | (c >> 6) & 0x3f); + view.setUint8(offset++, 0x80 | (c & 0x3f)); + } else { + i++; + c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); + view.setUint8(offset++, 0xf0 | (c >> 18)); + view.setUint8(offset++, 0x80 | (c >> 12) & 0x3f); + view.setUint8(offset++, 0x80 | (c >> 6) & 0x3f); + view.setUint8(offset++, 0x80 | (c & 0x3f)); + } + } +} + +function utf8Length (str): number { + let c = 0; let length = 0; + for (let i = 0, l = str.length; i < l; i++) { + c = str.charCodeAt(i); + if (c < 0x80) { + length += 1; + } else if (c < 0x800) { + length += 2; + } else if (c < 0xd800 || c >= 0xe000) { + length += 3; + } else { + i++; + length += 4; + } + } + return length; +} + +function _encode (bytes, defers, value: any): number { + const type = typeof value; let i = 0; let l = 0; let hi = 0; let lo = 0; let length = 0; let size = 0; + + if (type === 'string') { + length = utf8Length(value); + + if (length < 0x20) { // fixstr + bytes.push(length | 0xa0); + size = 1; + } else if (length < 0x100) { // str 8 + bytes.push(0xd9, length); + size = 2; + } else if (length < 0x10000) { // str 16 + bytes.push(0xda, length >> 8, length); + size = 3; + } else if (length < 0x100000000) { // str 32 + bytes.push(0xdb, length >> 24, length >> 16, length >> 8, length); + size = 5; + } else { + throw new Error('String too long'); + } + defers.push({ _str: value, _length: length, _offset: bytes.length }); + return size + length; + } + if (typeof value === 'number') { + // TODO: encode to float 32? + if (Math.floor(value) !== value || !Number.isFinite(value)) { // float 64 + bytes.push(0xcb); + defers.push({ _float: value, _length: 8, _offset: bytes.length }); + return 9; + } + + if (value >= 0) { + // positive fixnum + if (value < 0x80) { + bytes.push(value); + return 1; + } + // uint 8 + if (value < 0x100) { + bytes.push(0xcc, value); + return 2; + } + // uint 16 + if (value < 0x10000) { + bytes.push(0xcd, value >> 8, value); + return 3; + } + // uint 32 + if (value < 0x100000000) { + bytes.push(0xce, value >> 24, value >> 16, value >> 8, value); + return 5; + } + // uint 64 + hi = (value / 2 ** 32) >> 0; + lo = value >>> 0; + bytes.push(0xcf, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo); + return 9; + } else { + // negative fixnum + if (value >= -0x20) { + bytes.push(value); + return 1; + } + // int 8 + if (value >= -0x80) { + bytes.push(0xd0, value); + return 2; + } + // int 16 + if (value >= -0x8000) { + bytes.push(0xd1, value >> 8, value); + return 3; + } + // int 32 + if (value >= -0x80000000) { + bytes.push(0xd2, value >> 24, value >> 16, value >> 8, value); + return 5; + } + // int 64 + hi = Math.floor(value / 2 ** 32); + lo = value >>> 0; + bytes.push(0xd3, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo); + return 9; + } + } + if (type === 'object') { + // nil + if (value === null) { + bytes.push(0xc0); + return 1; + } + + if (Array.isArray(value)) { + length = value.length; + + // fixarray + if (length < 0x10) { + bytes.push(length | 0x90); + size = 1; + } else if (length < 0x10000) { // array 16 + bytes.push(0xdc, length >> 8, length); + size = 3; + } else if (length < 0x100000000) { // array 32 + bytes.push(0xdd, length >> 24, length >> 16, length >> 8, length); + size = 5; + } else { + throw new Error('Array too large'); + } + for (i = 0; i < length; i++) { + size += _encode(bytes, defers, value[i]); + } + return size; + } + + if (value instanceof Date) { + const ms = value.getTime(); + const s = Math.floor(ms / 1e3); + const ns = (ms - s * 1e3) * 1e6; + + if (s >= 0 && ns >= 0 && s <= TIMESTAMP64_MAX_SEC) { + if (ns === 0 && s <= TIMESTAMP32_MAX_SEC) { + // timestamp 32 + bytes.push(0xd6, 0xff, s >> 24, s >> 16, s >> 8, s); + return 6; + } else { + // timestamp 64 + hi = s / 0x100000000; + lo = s & 0xffffffff; + bytes.push(0xd7, 0xff, ns >> 22, ns >> 14, ns >> 6, hi, lo >> 24, lo >> 16, lo >> 8, lo); + return 10; + } + } else { + // timestamp 96 + hi = Math.floor(s / 0x100000000); + lo = s >>> 0; + bytes.push(0xc7, 0x0c, 0xff, ns >> 24, ns >> 16, ns >> 8, ns, hi >> 24, hi >> 16, hi >> 8, hi, lo >> 24, lo >> 16, lo >> 8, lo); + return 15; + } + } + + if (value instanceof ArrayBuffer) { + length = value.byteLength; + + // bin 8 + if (length < 0x100) { + bytes.push(0xc4, length); + size = 2; + } else + // bin 16 + if (length < 0x10000) { + bytes.push(0xc5, length >> 8, length); + size = 3; + } else + // bin 32 + if (length < 0x100000000) { + bytes.push(0xc6, length >> 24, length >> 16, length >> 8, length); + size = 5; + } else { + throw new Error('Buffer too large'); + } + defers.push({ _bin: value, _length: length, _offset: bytes.length }); + return size + length; + } + + if (typeof value.toJSON === 'function') { + return _encode(bytes, defers, value.toJSON()); + } + + const keys: string[] = []; let key = ''; + + const allKeys = Object.keys(value as object); + for (i = 0, l = allKeys.length; i < l; i++) { + key = allKeys[i]; + if (value[key] !== undefined && typeof value[key] !== 'function') { + keys.push(key); + } + } + length = keys.length; + + // fixmap + if (length < 0x10) { + bytes.push(length | 0x80); + size = 1; + } else if (length < 0x10000) { // map 16 + bytes.push(0xde, length >> 8, length); + size = 3; + } else if (length < 0x100000000) { // map 32 + bytes.push(0xdf, length >> 24, length >> 16, length >> 8, length); + size = 5; + } else { + throw new Error('Object too large'); + } + + for (i = 0; i < length; i++) { + key = keys[i]; + size += _encode(bytes, defers, key); + size += _encode(bytes, defers, value[key]); + } + return size; + } + // false/true + if (type === 'boolean') { + bytes.push(value ? 0xc3 : 0xc2); + return 1; + } + if (type === 'undefined') { + bytes.push(0xc0); + return 1; + } + // custom types like BigInt (typeof value === 'bigint') + if (typeof value.toJSON === 'function') { + return _encode(bytes, defers, value.toJSON()); + } + throw new Error('Could not encode'); +} + +export function notepackEncode (value: any): ArrayBuffer { + const bytes = []; + const defers: {_offset: number}[] = []; + const size = _encode(bytes, defers, value); + const buf = new ArrayBuffer(size); + const view = new DataView(buf); + + let deferIndex = 0; + let deferWritten = 0; + let nextOffset = -1; + if (defers.length > 0) { + nextOffset = defers[0]._offset; + } + + let defer; let deferLength = 0; let offset = 0; + for (let i = 0, l = bytes.length; i < l; i++) { + view.setUint8(deferWritten + i, bytes[i]); + if (i + 1 !== nextOffset) { continue; } + defer = defers[deferIndex]; + deferLength = defer._length; + offset = deferWritten + nextOffset; + if (defer._bin) { + const bin = new Uint8Array(defer._bin as ArrayBuffer); + for (let j = 0; j < deferLength; j++) { + view.setUint8(offset + j, bin[j]); + } + } else if (defer._str) { + utf8Write(view, offset, defer._str); + } else if (defer._float !== undefined) { + view.setFloat64(offset, defer._float as number); + } + deferIndex++; + deferWritten += deferLength; + if (defers[deferIndex]) { + nextOffset = defers[deferIndex]._offset; + } + } + return buf; +} diff --git a/licenses/LICENSE_notepack.io.txt b/licenses/LICENSE_notepack.io.txt new file mode 100644 index 0000000000..b46b1f8a9a --- /dev/null +++ b/licenses/LICENSE_notepack.io.txt @@ -0,0 +1,19 @@ +Copyright (c) 2014 Ion Drive Software Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/tests/core/ccon.test.ts b/tests/core/ccon.test.ts index 01b89705bf..b77c3e6581 100644 --- a/tests/core/ccon.test.ts +++ b/tests/core/ccon.test.ts @@ -1,39 +1,7 @@ -import { BufferBuilder, CCON, decodeCCONBinary, parseCCONJson, encodeCCONBinary, encodeCCONJson, InvalidCCONError } from "../../cocos/serialization/ccon"; +import { BufferBuilder, CCON, decodeCCONBinary, encodeCCONBinary, InvalidCCONError } from "../../cocos/serialization/ccon"; import { TextEncoder } from 'util'; describe(`CCON`, () => { - describe(`Json`, () => { - function encodeAndDecode (document: unknown, chunks: Uint8Array[], chunkURLs: string[]) { - expect(chunks).toHaveLength(chunkURLs.length); - const ccon = new CCON(document, chunks); - const cconJson = JSON.parse(JSON.stringify(encodeCCONJson(ccon, chunkURLs))); - const decoded = parseCCONJson(cconJson); - expect(decoded.chunks).toStrictEqual(chunkURLs); - expect(decoded.document).toStrictEqual(document); - } - - test('Zero chunks', () => { - encodeAndDecode({}, [], []); - }); - - test('Single chunk', () => { - encodeAndDecode({}, [new Uint8Array(0)], ['.bin']); - encodeAndDecode({}, [new Uint8Array([1, 2, 3])], ['.bin']); - }); - - test('Multiple chunk', () => { - encodeAndDecode({}, [ - new Uint8Array([1, 2, 3]), - new Uint8Array([4]), - new Uint8Array([5, 6, 7, 8]), - ], [ - '.0.bin', - '.1.bin', - '.2.bin', - ]); - }); - }); - describe(`Binary`, () => { function encodeAndDecode (document: unknown, chunks: Uint8Array[], insertFrontBytes = 0, insertBackBytes = 0) { const ccon = new CCON(document, chunks);