refactor(core): 使用fflate替换msgpack以兼容小游戏环境
This commit is contained in:
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user