refactor(core): 使用fflate替换msgpack以兼容小游戏环境
This commit is contained in:
@@ -72,7 +72,7 @@
|
||||
"directory": "packages/core"
|
||||
},
|
||||
"dependencies": {
|
||||
"@msgpack/msgpack": "^3.0.0",
|
||||
"fflate": "^0.8.2",
|
||||
"tslib": "^2.8.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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: /^!/
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
25
packages/core/src/Utils/BinarySerializer.ts
Normal file
25
packages/core/src/Utils/BinarySerializer.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user