From 683203919fb8d91d724a3c4dd094bdb3c61d58e9 Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Tue, 28 Oct 2025 14:08:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E4=BD=BF=E7=94=A8fflate?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2msgpack=E4=BB=A5=E5=85=BC=E5=AE=B9=E5=B0=8F?= =?UTF-8?q?=E6=B8=B8=E6=88=8F=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 17 ++++++------- packages/core/package.json | 2 +- packages/core/rollup.config.cjs | 22 +++++++++++----- .../Serialization/IncrementalSerializer.ts | 12 ++++----- .../src/ECS/Serialization/SceneSerializer.ts | 11 +++----- packages/core/src/Utils/BinarySerializer.ts | 25 +++++++++++++++++++ packages/core/src/Utils/index.ts | 3 ++- 7 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 packages/core/src/Utils/BinarySerializer.ts diff --git a/package-lock.json b/package-lock.json index 53b3d27c..33052794 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4929,15 +4929,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/@msgpack/msgpack": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/@msgpack/msgpack/-/msgpack-3.1.2.tgz", - "integrity": "sha512-JEW4DEtBzfe8HvUYecLU9e6+XJnKDlUAIve8FvPzF3Kzs6Xo/KuZkZJsDH0wJXl/qEZbeeE7edxDNY3kMs39hQ==", - "license": "ISC", - "engines": { - "node": ">= 18" - } - }, "node_modules/@napi-rs/wasm-runtime": { "version": "0.2.4", "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-0.2.4.tgz", @@ -12793,6 +12784,12 @@ } } }, + "node_modules/fflate": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", + "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", + "license": "MIT" + }, "node_modules/figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -26310,7 +26307,7 @@ "version": "2.2.9", "license": "MIT", "dependencies": { - "@msgpack/msgpack": "^3.0.0", + "fflate": "^0.8.2", "tslib": "^2.8.1" }, "devDependencies": { diff --git a/packages/core/package.json b/packages/core/package.json index 38f5b860..d74c6df6 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -72,7 +72,7 @@ "directory": "packages/core" }, "dependencies": { - "@msgpack/msgpack": "^3.0.0", + "fflate": "^0.8.2", "tslib": "^2.8.1" } } diff --git a/packages/core/rollup.config.cjs b/packages/core/rollup.config.cjs index 3beb93a9..627bd105 100644 --- a/packages/core/rollup.config.cjs +++ b/packages/core/rollup.config.cjs @@ -17,7 +17,17 @@ const banner = `/** const external = []; -const commonPlugins = [ +const modernPlugins = [ + resolve({ + browser: true, + preferBuiltins: false + }), + commonjs({ + include: /node_modules/ + }) +]; + +const legacyPlugins = [ resolve({ browser: true, preferBuiltins: false @@ -44,7 +54,7 @@ module.exports = [ exports: 'named' }, plugins: [ - ...commonPlugins, + ...modernPlugins, terser({ format: { comments: /^!/ @@ -65,7 +75,7 @@ module.exports = [ unknownGlobalSideEffects: false } }, - + // CommonJS构建 { input: 'bin/index.js', @@ -77,7 +87,7 @@ module.exports = [ exports: 'named' }, plugins: [ - ...commonPlugins, + ...modernPlugins, terser({ format: { comments: /^!/ @@ -96,7 +106,7 @@ module.exports = [ } }, - // UMD构建 - 用于CDN和浏览器直接使用 + // UMD构建 { input: 'bin/index.js', output: { @@ -108,7 +118,7 @@ module.exports = [ exports: 'named' }, plugins: [ - ...commonPlugins, + ...legacyPlugins, terser({ format: { comments: /^!/ diff --git a/packages/core/src/ECS/Serialization/IncrementalSerializer.ts b/packages/core/src/ECS/Serialization/IncrementalSerializer.ts index 4705ac52..a3730c09 100644 --- a/packages/core/src/ECS/Serialization/IncrementalSerializer.ts +++ b/packages/core/src/ECS/Serialization/IncrementalSerializer.ts @@ -11,7 +11,7 @@ import { Component } from '../Component'; import { ComponentSerializer, SerializedComponent } from './ComponentSerializer'; import { SerializedEntity } from './EntitySerializer'; import { ComponentType } from '../Core/ComponentStorage'; -import { encode, decode } from '@msgpack/msgpack'; +import { BinarySerializer } from '../../Utils/BinarySerializer'; /** * 变更操作类型 @@ -147,8 +147,8 @@ export interface IncrementalSerializationOptions { /** * 序列化格式 - * - 'json': JSON格式(可读性好,方便调试) - * - 'binary': MessagePack二进制格式(体积小,性能高) + * - 'json': JSON格式 + * - 'binary': 二进制格式 * 默认 'json' */ format?: IncrementalSerializationFormat; @@ -639,7 +639,7 @@ export class IncrementalSerializer { }; if (opts.format === 'binary') { - return encode(incremental); + return BinarySerializer.encode(incremental); } else { return opts.pretty ? JSON.stringify(incremental, null, 2) @@ -664,11 +664,9 @@ export class IncrementalSerializer { */ public static deserializeIncremental(data: string | Uint8Array): IncrementalSnapshot { if (typeof data === 'string') { - // JSON格式 return JSON.parse(data); } else { - // 二进制格式(MessagePack) - return decode(data) as IncrementalSnapshot; + return BinarySerializer.decode(data) as IncrementalSnapshot; } } diff --git a/packages/core/src/ECS/Serialization/SceneSerializer.ts b/packages/core/src/ECS/Serialization/SceneSerializer.ts index 6397bf97..5a5e2189 100644 --- a/packages/core/src/ECS/Serialization/SceneSerializer.ts +++ b/packages/core/src/ECS/Serialization/SceneSerializer.ts @@ -11,7 +11,7 @@ import { ComponentType, ComponentRegistry } from '../Core/ComponentStorage'; import { EntitySerializer, SerializedEntity } from './EntitySerializer'; import { getComponentTypeName } from '../Decorators'; import { getSerializationMetadata } from './SerializationDecorators'; -import { encode, decode } from '@msgpack/msgpack'; +import { BinarySerializer } from '../../Utils/BinarySerializer'; /** * 场景序列化格式 @@ -200,14 +200,12 @@ export class SceneSerializer { }; } - // 根据格式返回数据 if (opts.format === 'json') { return opts.pretty ? JSON.stringify(serializedScene, null, 2) : JSON.stringify(serializedScene); } else { - // 二进制格式(使用 MessagePack) - return encode(serializedScene); + return BinarySerializer.encode(serializedScene); } } @@ -229,15 +227,12 @@ export class SceneSerializer { ...options }; - // 解析数据 let serializedScene: SerializedScene; try { if (typeof saveData === 'string') { - // JSON格式 serializedScene = JSON.parse(saveData); } else { - // 二进制格式(MessagePack) - serializedScene = decode(saveData) as SerializedScene; + serializedScene = BinarySerializer.decode(saveData) as SerializedScene; } } catch (error) { throw new Error(`Failed to parse save data: ${error}`); diff --git a/packages/core/src/Utils/BinarySerializer.ts b/packages/core/src/Utils/BinarySerializer.ts new file mode 100644 index 00000000..63884004 --- /dev/null +++ b/packages/core/src/Utils/BinarySerializer.ts @@ -0,0 +1,25 @@ +import { strToU8, strFromU8, zlibSync, unzlibSync } from 'fflate'; + +/** + * 二进制序列化器 + * 使用zlib压缩JSON数据 + */ +export class BinarySerializer { + /** + * 将对象编码为压缩的二进制数据 + */ + public static encode(value: any): Uint8Array { + const jsonString = JSON.stringify(value); + const utf8Bytes = strToU8(jsonString); + return zlibSync(utf8Bytes); + } + + /** + * 将压缩的二进制数据解码为对象 + */ + public static decode(bytes: Uint8Array): any { + const decompressed = unzlibSync(bytes); + const jsonString = strFromU8(decompressed); + return JSON.parse(jsonString); + } +} diff --git a/packages/core/src/Utils/index.ts b/packages/core/src/Utils/index.ts index f5eed32e..d9817f6a 100644 --- a/packages/core/src/Utils/index.ts +++ b/packages/core/src/Utils/index.ts @@ -5,4 +5,5 @@ export * from './GlobalManager'; export * from './PerformanceMonitor'; export { Time } from './Time'; export * from './Debug'; -export * from './Logger'; +export * from './Logger'; +export * from './BinarySerializer';