fix(core): 移除TextEncoder依赖以兼容小游戏环境
This commit is contained in:
@@ -672,36 +672,6 @@ export class IncrementalSerializer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算增量快照的大小(字节)
|
||||
*
|
||||
* @param incremental 增量快照
|
||||
* @param format 序列化格式,默认为 'json'
|
||||
* @returns 字节数
|
||||
*/
|
||||
public static getIncrementalSize(
|
||||
incremental: IncrementalSnapshot,
|
||||
format: IncrementalSerializationFormat = 'json'
|
||||
): number {
|
||||
const data = this.serializeIncremental(incremental, { format });
|
||||
|
||||
if (typeof data === 'string') {
|
||||
// JSON格式:计算UTF-8编码后的字节数
|
||||
// 使用 Blob 来计算浏览器和 Node.js 环境兼容的字节数
|
||||
if (typeof Blob !== 'undefined') {
|
||||
return new Blob([data]).size;
|
||||
} else if (typeof Buffer !== 'undefined') {
|
||||
return Buffer.byteLength(data, 'utf8');
|
||||
} else {
|
||||
// 回退方案:粗略估算(不精确,但可用)
|
||||
return new TextEncoder().encode(data).length;
|
||||
}
|
||||
} else {
|
||||
// 二进制格式:直接返回Buffer长度
|
||||
return data.length;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取增量快照的统计信息
|
||||
*
|
||||
|
||||
@@ -671,17 +671,6 @@ describe('Incremental Serialization System', () => {
|
||||
});
|
||||
|
||||
describe('Statistics and Utilities', () => {
|
||||
it('应该计算增量快照大小', () => {
|
||||
scene.createIncrementalSnapshot();
|
||||
const entity = scene.createEntity('Entity');
|
||||
entity.addComponent(new PositionComponent(10, 20));
|
||||
|
||||
const incremental = scene.serializeIncremental();
|
||||
const size = IncrementalSerializer.getIncrementalSize(incremental);
|
||||
|
||||
expect(size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('应该提供增量快照统计信息', () => {
|
||||
const entity1 = scene.createEntity('Entity1');
|
||||
entity1.addComponent(new PositionComponent(10, 20));
|
||||
|
||||
Reference in New Issue
Block a user