Files
esengine/src/Utils/Serialization/ProtobufSerializer.ts

381 lines
12 KiB
TypeScript
Raw Normal View History

/**
* Protobuf序列化器
*
* protobuf序列化和反序列化
*/
import { Component } from '../../ECS/Component';
import {
ProtobufRegistry,
ProtoComponentDefinition,
ProtoFieldDefinition,
ProtoFieldType,
isProtoSerializable,
getProtoName
} from './ProtobufDecorators';
import { SerializedData } from './SerializationTypes';
/**
* Protobuf序列化器
*/
export class ProtobufSerializer {
private registry: ProtobufRegistry;
private static instance: ProtobufSerializer;
2025-08-06 17:42:12 +08:00
/** protobuf.js库实例 */
private protobuf: any = null;
private root: any = null;
2025-08-06 17:42:12 +08:00
/** MessageType缓存映射表 */
private messageTypeCache: Map<string, any> = new Map();
/** 是否启用数据验证 */
private enableValidation: boolean = process.env.NODE_ENV === 'development';
private constructor() {
this.registry = ProtobufRegistry.getInstance();
this.initializeProtobuf();
}
2025-08-06 17:42:12 +08:00
/**
*
*/
public setPerformanceOptions(options: {
enableValidation?: boolean;
clearCache?: boolean;
}): void {
if (options.enableValidation !== undefined) {
this.enableValidation = options.enableValidation;
}
if (options.clearCache) {
this.messageTypeCache.clear();
}
}
/**
* protobuf支持
*/
private async initializeProtobuf(): Promise<void> {
try {
// 动态导入protobufjs
this.protobuf = await import('protobufjs');
this.buildProtoDefinitions();
2025-08-06 17:42:12 +08:00
console.log('[ProtobufSerializer] Protobuf支持已启用');
} catch (error) {
2025-08-06 17:42:12 +08:00
throw new Error('[ProtobufSerializer] 无法加载protobufjs: ' + error);
}
}
public static getInstance(): ProtobufSerializer {
if (!ProtobufSerializer.instance) {
ProtobufSerializer.instance = new ProtobufSerializer();
}
return ProtobufSerializer.instance;
}
/**
2025-08-06 17:42:12 +08:00
* protobuf.js库
*
2025-08-06 17:42:12 +08:00
* @param protobufJs protobuf.js库实例
*/
public initialize(protobufJs: any): void {
this.protobuf = protobufJs;
this.buildProtoDefinitions();
console.log('[ProtobufSerializer] Protobuf支持已手动启用');
}
/**
*
*
2025-08-06 17:42:12 +08:00
* @param component
* @returns
2025-08-06 17:42:12 +08:00
* @throws Error protobuf序列化
*/
public serialize(component: Component): SerializedData {
const componentType = component.constructor.name;
// 检查是否支持protobuf序列化
if (!isProtoSerializable(component)) {
2025-08-06 17:42:12 +08:00
throw new Error(`[ProtobufSerializer] 组件 ${componentType} 不支持protobuf序列化请添加@ProtoSerializable装饰器`);
}
const protoName = getProtoName(component);
if (!protoName) {
throw new Error(`[ProtobufSerializer] 组件 ${componentType} 未设置protobuf名称`);
}
const definition = this.registry.getComponentDefinition(protoName);
if (!definition) {
throw new Error(`[ProtobufSerializer] 未找到组件定义: ${protoName}`);
}
// 获取protobuf消息类型
const MessageType = this.getMessageType(protoName);
if (!MessageType) {
throw new Error(`[ProtobufSerializer] 未找到消息类型: ${protoName}`);
}
try {
// 构建protobuf数据对象
const protoData = this.buildProtoData(component, definition);
2025-08-06 17:42:12 +08:00
// 数据验证(仅在开发环境)
if (this.enableValidation) {
const error = MessageType.verify(protoData);
if (error) {
throw new Error(`[ProtobufSerializer] 数据验证失败: ${error}`);
}
}
// 创建消息并编码
const message = MessageType.create(protoData);
const buffer = MessageType.encode(message).finish();
return {
type: 'protobuf',
componentType: componentType,
data: buffer,
size: buffer.length
};
} catch (error) {
2025-08-06 17:42:12 +08:00
throw new Error(`[ProtobufSerializer] 序列化失败: ${componentType} - ${error}`);
}
}
/**
*
*
2025-08-06 17:42:12 +08:00
* @param component
* @param serializedData
* @throws Error
*/
public deserialize(component: Component, serializedData: SerializedData): void {
2025-08-06 17:42:12 +08:00
const protoName = getProtoName(component);
if (!protoName) {
throw new Error(`[ProtobufSerializer] 组件 ${component.constructor.name} 未设置protobuf名称`);
}
const MessageType = this.getMessageType(protoName);
if (!MessageType) {
throw new Error(`[ProtobufSerializer] 未找到消息类型: ${protoName}`);
}
try {
// 解码消息
2025-08-06 17:42:12 +08:00
const message = MessageType.decode(serializedData.data);
const data = MessageType.toObject(message);
// 应用数据到组件
this.applyDataToComponent(component, data);
} catch (error) {
2025-08-06 17:42:12 +08:00
throw new Error(`[ProtobufSerializer] 反序列化失败: ${component.constructor.name} - ${error}`);
}
}
/**
* protobuf序列化
*/
public canSerialize(component: Component): boolean {
if (!this.protobuf) return false;
return isProtoSerializable(component);
}
2025-08-06 17:42:12 +08:00
/**
*
*
* @param components
* @returns
*/
public serializeBatch(components: Component[]): SerializedData[] {
const results: SerializedData[] = [];
// 按组件类型分组,减少重复查找
const componentGroups = new Map<string, Component[]>();
for (const component of components) {
if (!isProtoSerializable(component)) {
throw new Error(`[ProtobufSerializer] 组件 ${component.constructor.name} 不支持protobuf序列化`);
}
const protoName = getProtoName(component);
if (!protoName) {
throw new Error(`[ProtobufSerializer] 组件 ${component.constructor.name} 未设置protobuf名称`);
}
if (!componentGroups.has(protoName)) {
componentGroups.set(protoName, []);
}
componentGroups.get(protoName)!.push(component);
}
// 按组分别序列化
for (const [protoName, groupComponents] of componentGroups) {
const definition = this.registry.getComponentDefinition(protoName);
const MessageType = this.getMessageType(protoName);
if (!definition || !MessageType) {
throw new Error(`[ProtobufSerializer] 组件类型 ${protoName} 未正确注册`);
}
for (const component of groupComponents) {
try {
const protoData = this.buildProtoData(component, definition);
// 数据验证(仅在开发环境)
if (this.enableValidation) {
const error = MessageType.verify(protoData);
if (error) {
throw new Error(`[ProtobufSerializer] 数据验证失败: ${error}`);
}
}
const message = MessageType.create(protoData);
const buffer = MessageType.encode(message).finish();
results.push({
type: 'protobuf',
2025-08-06 17:42:12 +08:00
componentType: component.constructor.name,
data: buffer,
size: buffer.length
});
} catch (error) {
throw new Error(`[ProtobufSerializer] 批量序列化失败: ${component.constructor.name} - ${error}`);
}
}
}
return results;
}
/**
*
*/
public getStats(): {
registeredComponents: number;
protobufAvailable: boolean;
2025-08-06 17:42:12 +08:00
cacheSize: number;
} {
return {
registeredComponents: this.registry.getAllComponents().size,
2025-08-06 17:42:12 +08:00
protobufAvailable: !!this.protobuf,
cacheSize: this.messageTypeCache.size
};
}
/**
* protobuf数据对象
*/
private buildProtoData(component: Component, definition: ProtoComponentDefinition): any {
const data: any = {};
for (const [propertyName, fieldDef] of definition.fields) {
const value = (component as any)[propertyName];
if (value !== undefined && value !== null) {
data[fieldDef.name] = this.convertValueToProtoType(value, fieldDef);
}
}
return data;
}
/**
* protobuf类型
*/
private convertValueToProtoType(value: any, fieldDef: ProtoFieldDefinition): any {
if (fieldDef.repeated && Array.isArray(value)) {
return value.map(v => this.convertSingleValue(v, fieldDef.type));
}
return this.convertSingleValue(value, fieldDef.type);
}
/**
2025-08-06 17:42:12 +08:00
* protobuf类型
*/
private convertSingleValue(value: any, type: ProtoFieldType): any {
switch (type) {
case ProtoFieldType.INT32:
case ProtoFieldType.UINT32:
case ProtoFieldType.SINT32:
case ProtoFieldType.FIXED32:
case ProtoFieldType.SFIXED32:
2025-08-06 17:42:12 +08:00
return typeof value === 'number' ? (value | 0) : (parseInt(value) || 0);
case ProtoFieldType.FLOAT:
case ProtoFieldType.DOUBLE:
2025-08-06 17:42:12 +08:00
return typeof value === 'number' ? value : (parseFloat(value) || 0);
case ProtoFieldType.BOOL:
2025-08-06 17:42:12 +08:00
return typeof value === 'boolean' ? value : Boolean(value);
case ProtoFieldType.STRING:
2025-08-06 17:42:12 +08:00
return typeof value === 'string' ? value : String(value);
default:
return value;
}
}
/**
*
*/
private applyDataToComponent(component: Component, data: any): void {
const protoName = getProtoName(component);
if (!protoName) return;
const definition = this.registry.getComponentDefinition(protoName);
if (!definition) return;
for (const [propertyName, fieldDef] of definition.fields) {
const value = data[fieldDef.name];
if (value !== undefined) {
(component as any)[propertyName] = value;
}
}
}
/**
* protobuf定义
*/
private buildProtoDefinitions(): void {
if (!this.protobuf) return;
try {
const protoDefinition = this.registry.generateProtoDefinition();
this.root = this.protobuf.parse(protoDefinition).root;
2025-08-06 17:42:12 +08:00
// 清空缓存schema已更新
this.messageTypeCache.clear();
} catch (error) {
console.error('[ProtobufSerializer] 构建protobuf定义失败:', error);
}
}
/**
2025-08-06 17:42:12 +08:00
*
*/
private getMessageType(typeName: string): any {
if (!this.root) return null;
2025-08-06 17:42:12 +08:00
// 检查缓存
const fullTypeName = `ecs.${typeName}`;
if (this.messageTypeCache.has(fullTypeName)) {
return this.messageTypeCache.get(fullTypeName);
}
try {
2025-08-06 17:42:12 +08:00
const messageType = this.root.lookupType(fullTypeName);
// 缓存结果
this.messageTypeCache.set(fullTypeName, messageType);
return messageType;
} catch (error) {
2025-08-06 17:42:12 +08:00
console.warn(`[ProtobufSerializer] 未找到消息类型: ${fullTypeName}`);
// 缓存null结果以避免重复查找
this.messageTypeCache.set(fullTypeName, null);
return null;
}
}
}