项目统一改用Logger控制管理
拆分pool类和FluentAPI
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import 'reflect-metadata';
|
||||
import { createLogger } from '@esengine/ecs-framework';
|
||||
|
||||
const logger = createLogger('SyncVarDecorator');
|
||||
|
||||
/**
|
||||
* SyncVar配置选项
|
||||
@@ -151,7 +154,8 @@ export function getSyncVarMetadataForProperty(target: any, propertyKey: string):
|
||||
* public isReady: boolean = false;
|
||||
*
|
||||
* onNameChanged(oldName: string, newName: string) {
|
||||
* console.log(`Name changed: ${oldName} -> ${newName}`);
|
||||
* const logger = createLogger('PlayerComponent');
|
||||
* logger.info(`Name changed: ${oldName} -> ${newName}`);
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
@@ -165,7 +169,7 @@ export function SyncVar(options: SyncVarOptions = {}): PropertyDecorator {
|
||||
// 获取属性类型
|
||||
const type = Reflect.getMetadata('design:type', target, propertyKey);
|
||||
if (!type) {
|
||||
console.warn(`[SyncVar] 无法获取属性 ${propertyKey} 的类型信息`);
|
||||
logger.warn(`无法获取属性 ${propertyKey} 的类型信息`);
|
||||
}
|
||||
|
||||
// 获取现有元数据
|
||||
@@ -174,7 +178,7 @@ export function SyncVar(options: SyncVarOptions = {}): PropertyDecorator {
|
||||
// 检查是否已经存在
|
||||
const existingIndex = existingMetadata.findIndex(m => m.propertyKey === propertyKey);
|
||||
if (existingIndex !== -1) {
|
||||
console.warn(`[SyncVar] 属性 ${propertyKey} 已经被标记为SyncVar,将覆盖配置`);
|
||||
logger.warn(`属性 ${propertyKey} 已经被标记为SyncVar,将覆盖配置`);
|
||||
existingMetadata[existingIndex].options = options;
|
||||
existingMetadata[existingIndex].type = type;
|
||||
} else {
|
||||
@@ -194,7 +198,7 @@ export function SyncVar(options: SyncVarOptions = {}): PropertyDecorator {
|
||||
// 保存元数据
|
||||
setSyncVarMetadata(target.constructor, existingMetadata);
|
||||
|
||||
console.log(`[SyncVar] 注册同步变量: ${target.constructor.name}.${propertyKey}, 字段编号: ${existingMetadata.find(m => m.propertyKey === propertyKey)?.fieldNumber}`);
|
||||
logger.debug(`注册同步变量: ${target.constructor.name}.${propertyKey}, 字段编号: ${existingMetadata.find(m => m.propertyKey === propertyKey)?.fieldNumber}`);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createSyncVarProxy } from './SyncVarProxy';
|
||||
import { getSyncVarMetadata } from './SyncVarDecorator';
|
||||
import { INetworkSyncable } from '../types/NetworkTypes';
|
||||
import { createLogger } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* SyncVar工厂函数
|
||||
@@ -16,6 +17,8 @@ import { INetworkSyncable } from '../types/NetworkTypes';
|
||||
* @param args - 构造函数参数
|
||||
* @returns 带代理的组件实例
|
||||
*/
|
||||
const logger = createLogger('SyncVarFactory');
|
||||
|
||||
export function createNetworkComponent<T extends INetworkSyncable>(
|
||||
ComponentClass: new (...args: any[]) => T,
|
||||
...args: any[]
|
||||
@@ -36,7 +39,7 @@ export function createNetworkComponent<T extends INetworkSyncable>(
|
||||
debugLog: false // 可以根据需要启用调试
|
||||
});
|
||||
|
||||
console.log(`[SyncVarFactory] 为 ${ComponentClass.name} 创建了SyncVar代理,包含 ${metadata.length} 个同步字段`);
|
||||
logger.debug(`为 ${ComponentClass.name} 创建了SyncVar代理,包含 ${metadata.length} 个同步字段`);
|
||||
|
||||
return proxy;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
NetworkComponentType,
|
||||
TypeGuards
|
||||
} from '../types/NetworkTypes';
|
||||
import { createLogger } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* SyncVar变化记录
|
||||
@@ -83,6 +84,7 @@ export interface SyncVarSyncData {
|
||||
*/
|
||||
export class SyncVarManager {
|
||||
private static _instance: SyncVarManager | null = null;
|
||||
private static readonly logger = createLogger('SyncVarManager');
|
||||
|
||||
/**
|
||||
* 组件实例的SyncVar变化监听器
|
||||
@@ -133,7 +135,7 @@ export class SyncVarManager {
|
||||
}
|
||||
|
||||
if (validationErrors.length > 0) {
|
||||
console.error(`[SyncVarManager] 组件 ${component.constructor.name} 的SyncVar配置错误:`, validationErrors);
|
||||
SyncVarManager.logger.error(`组件 ${component.constructor.name} 的SyncVar配置错误:`, validationErrors);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -141,7 +143,7 @@ export class SyncVarManager {
|
||||
this._componentChanges.set(componentId, []);
|
||||
this._lastSyncTimes.set(componentId, new Map());
|
||||
|
||||
console.log(`[SyncVarManager] 初始化组件 ${component.constructor.name} 的SyncVar系统,共 ${metadata.length} 个同步变量`);
|
||||
SyncVarManager.logger.info(`初始化组件 ${component.constructor.name} 的SyncVar系统,共 ${metadata.length} 个同步变量`);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -174,13 +176,13 @@ export class SyncVarManager {
|
||||
const metadata = getSyncVarMetadataForProperty(component, propertyKey);
|
||||
|
||||
if (!metadata) {
|
||||
console.warn(`[SyncVarManager] 属性 ${propertyKey} 不是SyncVar`);
|
||||
SyncVarManager.logger.warn(`属性 ${propertyKey} 不是SyncVar`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查值是否真的发生了变化
|
||||
if (!TypeGuards.isSyncVarValue(oldValue) || !TypeGuards.isSyncVarValue(newValue)) {
|
||||
console.warn(`[SyncVarManager] 无效的SyncVar值类型: ${typeof oldValue}, ${typeof newValue}`);
|
||||
SyncVarManager.logger.warn(`无效的SyncVar值类型: ${typeof oldValue}, ${typeof newValue}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -195,14 +197,14 @@ export class SyncVarManager {
|
||||
|
||||
if (metadata.options.throttleMs && metadata.options.throttleMs > 0) {
|
||||
if (now - lastSyncTime < metadata.options.throttleMs) {
|
||||
console.log(`[SyncVarManager] 属性 ${propertyKey} 变化过于频繁,跳过同步`);
|
||||
SyncVarManager.logger.debug(`属性 ${propertyKey} 变化过于频繁,跳过同步`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查权限
|
||||
if (metadata.options.authorityOnly && !this.hasAuthority(component)) {
|
||||
console.warn(`[SyncVarManager] 属性 ${propertyKey} 需要权限才能修改,但当前没有权限`);
|
||||
SyncVarManager.logger.warn(`属性 ${propertyKey} 需要权限才能修改,但当前没有权限`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -229,7 +231,7 @@ export class SyncVarManager {
|
||||
lastSyncTimes.set(propertyKey, now);
|
||||
}
|
||||
|
||||
console.log(`[SyncVarManager] 记录变化: ${component.constructor.name}.${propertyKey} = ${newValue} (was ${oldValue})`);
|
||||
SyncVarManager.logger.debug(`记录变化: ${component.constructor.name}.${propertyKey} = ${newValue} (was ${oldValue})`);
|
||||
|
||||
// 触发hook回调
|
||||
this.triggerHook(component, metadata, oldValue, newValue);
|
||||
@@ -294,7 +296,7 @@ export class SyncVarManager {
|
||||
data: serializedData
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarManager] 序列化失败 ${change.propertyKey}:`, error);
|
||||
SyncVarManager.logger.error(`序列化失败 ${change.propertyKey}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +324,7 @@ export class SyncVarManager {
|
||||
for (const update of syncData.fieldUpdates) {
|
||||
const meta = metadataMap.get(update.fieldNumber);
|
||||
if (!meta) {
|
||||
console.warn(`[SyncVarManager] 未找到字段编号 ${update.fieldNumber} 的元数据`);
|
||||
SyncVarManager.logger.warn(`未找到字段编号 ${update.fieldNumber} 的元数据`);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -336,9 +338,9 @@ export class SyncVarManager {
|
||||
// 触发hook回调
|
||||
this.triggerHook(component, meta, oldValue, newValue);
|
||||
|
||||
console.log(`[SyncVarManager] 应用同步: ${component.constructor.name}.${meta.propertyKey} = ${newValue}`);
|
||||
SyncVarManager.logger.debug(`应用同步: ${component.constructor.name}.${meta.propertyKey} = ${newValue}`);
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarManager] 反序列化失败 ${meta.propertyKey}:`, error);
|
||||
SyncVarManager.logger.error(`反序列化失败 ${meta.propertyKey}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -421,7 +423,7 @@ export class SyncVarManager {
|
||||
private shouldSync(component: any, metadata: SyncVarMetadata): boolean {
|
||||
// 权限检查:权威字段只有在有权限时才同步
|
||||
if (metadata.options.authorityOnly && !this.hasAuthority(component)) {
|
||||
console.log(`[SyncVarManager] 字段 ${metadata.propertyKey} 是权威字段,但当前没有权限,跳过同步`);
|
||||
SyncVarManager.logger.debug(`字段 ${metadata.propertyKey} 是权威字段,但当前没有权限,跳过同步`);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -457,7 +459,7 @@ export class SyncVarManager {
|
||||
try {
|
||||
hookFunction.call(component, oldValue, newValue);
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarManager] Hook函数执行失败 ${metadata.options.hook}:`, error);
|
||||
SyncVarManager.logger.error(`Hook函数执行失败 ${metadata.options.hook}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -641,7 +643,7 @@ export class SyncVarManager {
|
||||
syncSequence
|
||||
);
|
||||
|
||||
console.log(`[SyncVarManager] 创建SyncVar更新消息: ${component.constructor.name}, ${fieldUpdates.length} 个字段`);
|
||||
SyncVarManager.logger.debug(`创建SyncVar更新消息: ${component.constructor.name}, ${fieldUpdates.length} 个字段`);
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -653,7 +655,7 @@ export class SyncVarManager {
|
||||
*/
|
||||
public applySyncVarUpdateMessage(component: any, message: SyncVarUpdateMessage): void {
|
||||
if (message.componentType !== component.constructor.name) {
|
||||
console.warn(`[SyncVarManager] 组件类型不匹配: 期望 ${component.constructor.name}, 收到 ${message.componentType}`);
|
||||
SyncVarManager.logger.warn(`组件类型不匹配: 期望 ${component.constructor.name}, 收到 ${message.componentType}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -663,7 +665,7 @@ export class SyncVarManager {
|
||||
for (const fieldUpdate of message.fieldUpdates) {
|
||||
const meta = metadataMap.get(fieldUpdate.fieldNumber);
|
||||
if (!meta) {
|
||||
console.warn(`[SyncVarManager] 未找到字段编号 ${fieldUpdate.fieldNumber} 的元数据`);
|
||||
SyncVarManager.logger.warn(`未找到字段编号 ${fieldUpdate.fieldNumber} 的元数据`);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -672,7 +674,7 @@ export class SyncVarManager {
|
||||
if (fieldUpdate.authorityOnly && NetworkEnvironment.isClient && !this.hasAuthority(component)) {
|
||||
// 如果这是来自服务端的更新,则允许应用
|
||||
// 这里简单实现:客户端接受所有权威字段的更新
|
||||
console.log(`[SyncVarManager] 客户端接受权威字段更新: ${fieldUpdate.propertyKey}`);
|
||||
SyncVarManager.logger.debug(`客户端接受权威字段更新: ${fieldUpdate.propertyKey}`);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -684,9 +686,9 @@ export class SyncVarManager {
|
||||
// 触发hook回调
|
||||
this.triggerHook(component, meta, oldValue, fieldUpdate.newValue);
|
||||
|
||||
console.log(`[SyncVarManager] 应用SyncVar消息更新: ${component.constructor.name}.${meta.propertyKey} = ${fieldUpdate.newValue}`);
|
||||
SyncVarManager.logger.debug(`应用SyncVar消息更新: ${component.constructor.name}.${meta.propertyKey} = ${fieldUpdate.newValue}`);
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarManager] 应用SyncVar更新失败 ${meta.propertyKey}:`, error);
|
||||
SyncVarManager.logger.error(`应用SyncVar更新失败 ${meta.propertyKey}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { NetworkConnection } from '../Core/NetworkConnection';
|
||||
import { NetworkIdentityRegistry } from '../Core/NetworkIdentity';
|
||||
import { SyncVarManager } from './SyncVarManager';
|
||||
import { NetworkEnvironment } from '../Core/NetworkEnvironment';
|
||||
import { ComponentRegistry } from '@esengine/ecs-framework';
|
||||
import { ComponentRegistry, createLogger } from '@esengine/ecs-framework';
|
||||
import { NetworkManager } from '../Core/NetworkManager';
|
||||
|
||||
/**
|
||||
@@ -13,6 +13,7 @@ import { NetworkManager } from '../Core/NetworkManager';
|
||||
* 处理接收到的SyncVar更新消息,自动查找目标网络对象并应用更新
|
||||
*/
|
||||
export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessage> {
|
||||
private static readonly logger = createLogger('SyncVarMessageHandler');
|
||||
private _processedMessages: Set<string> = new Set();
|
||||
private _maxProcessedCache: number = 1000;
|
||||
|
||||
@@ -27,7 +28,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
// 生成消息唯一标识符用于去重
|
||||
const messageKey = this.generateMessageKey(message);
|
||||
if (this._processedMessages.has(messageKey)) {
|
||||
console.log(`[SyncVarMessageHandler] 跳过重复消息: ${messageKey}`);
|
||||
SyncVarMessageHandler.logger.debug(` 跳过重复消息: ${messageKey}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -36,27 +37,27 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
|
||||
// 验证消息基本有效性
|
||||
if (!this.validateMessage(message)) {
|
||||
console.error('[SyncVarMessageHandler] 消息验证失败');
|
||||
SyncVarMessageHandler.logger.error(' 消息验证失败');
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找目标网络对象
|
||||
const targetIdentity = NetworkIdentityRegistry.Instance.find(message.networkId);
|
||||
if (!targetIdentity) {
|
||||
console.warn(`[SyncVarMessageHandler] 未找到网络对象: ${message.networkId}`);
|
||||
SyncVarMessageHandler.logger.warn(` 未找到网络对象: ${message.networkId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 权限检查
|
||||
if (!this.checkAuthority(message, connection, targetIdentity)) {
|
||||
console.warn(`[SyncVarMessageHandler] 权限检查失败: ${message.networkId}`);
|
||||
SyncVarMessageHandler.logger.warn(` 权限检查失败: ${message.networkId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找目标组件
|
||||
const targetComponent = this.findTargetComponent(targetIdentity, message.componentType);
|
||||
if (!targetComponent) {
|
||||
console.warn(`[SyncVarMessageHandler] 未找到目标组件: ${message.componentType} on ${message.networkId}`);
|
||||
SyncVarMessageHandler.logger.warn(` 未找到目标组件: ${message.componentType} on ${message.networkId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -74,10 +75,10 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
await this.forwardToOtherClients(message, connection);
|
||||
}
|
||||
|
||||
console.log(`[SyncVarMessageHandler] 成功处理SyncVar更新: ${message.networkId}.${message.componentType}, ${message.fieldUpdates.length}个字段`);
|
||||
SyncVarMessageHandler.logger.debug(` 成功处理SyncVar更新: ${message.networkId}.${message.componentType}, ${message.fieldUpdates.length}个字段`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('[SyncVarMessageHandler] 处理SyncVar更新失败:', error);
|
||||
SyncVarMessageHandler.logger.error(' 处理SyncVar更新失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,12 +107,12 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
*/
|
||||
private validateMessage(message: SyncVarUpdateMessage): boolean {
|
||||
if (!message.networkId || !message.componentType) {
|
||||
console.error('[SyncVarMessageHandler] 消息缺少必要字段');
|
||||
SyncVarMessageHandler.logger.error(' 消息缺少必要字段');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!message.fieldUpdates || message.fieldUpdates.length === 0) {
|
||||
console.error('[SyncVarMessageHandler] 消息没有字段更新');
|
||||
SyncVarMessageHandler.logger.error(' 消息没有字段更新');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -119,7 +120,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
const now = Date.now();
|
||||
const maxAge = 60000; // 1分钟
|
||||
if (message.timestamp > now + 5000 || message.timestamp < now - maxAge) {
|
||||
console.warn(`[SyncVarMessageHandler] 消息时间戳异常: ${message.timestamp}, 当前: ${now}`);
|
||||
SyncVarMessageHandler.logger.warn(` 消息时间戳异常: ${message.timestamp}, 当前: ${now}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -143,7 +144,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
// 非拥有者只能发送非权威字段更新
|
||||
const hasAuthorityOnlyUpdates = message.fieldUpdates.some(update => update.authorityOnly);
|
||||
if (hasAuthorityOnlyUpdates) {
|
||||
console.warn(`[SyncVarMessageHandler] 非拥有者 ${connection.connectionId} 尝试修改权威字段`);
|
||||
SyncVarMessageHandler.logger.warn(` 非拥有者 ${connection.connectionId} 尝试修改权威字段`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -165,7 +166,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
private findTargetComponent(targetIdentity: any, componentType: string): any {
|
||||
const entity = targetIdentity.entity;
|
||||
if (!entity || typeof entity.getComponent !== 'function') {
|
||||
console.error('[SyncVarMessageHandler] NetworkIdentity缺少有效的Entity引用');
|
||||
SyncVarMessageHandler.logger.error(' NetworkIdentity缺少有效的Entity引用');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -179,13 +180,13 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
// 使用Entity的getComponent方法查找组件
|
||||
const component = entity.getComponent(ComponentClass);
|
||||
if (!component) {
|
||||
console.warn(`[SyncVarMessageHandler] Entity ${entity.id} 上未找到组件: ${componentType}`);
|
||||
SyncVarMessageHandler.logger.warn(` Entity ${entity.id} 上未找到组件: ${componentType}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return component;
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarMessageHandler] 查找组件失败: ${componentType}`, error);
|
||||
SyncVarMessageHandler.logger.error(`查找组件失败: ${componentType}`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -197,7 +198,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
const componentClass = ComponentRegistry.getComponentType(componentType);
|
||||
|
||||
if (!componentClass) {
|
||||
console.warn(`[SyncVarMessageHandler] 未找到组件类型: ${componentType}`);
|
||||
SyncVarMessageHandler.logger.warn(` 未找到组件类型: ${componentType}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -213,7 +214,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
try {
|
||||
syncVarManager.applySyncVarUpdateMessage(targetComponent, message);
|
||||
} catch (error) {
|
||||
console.error('[SyncVarMessageHandler] 应用SyncVar更新失败:', error);
|
||||
SyncVarMessageHandler.logger.error(' 应用SyncVar更新失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -230,7 +231,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
const server = NetworkManager.GetServer();
|
||||
|
||||
if (!server || !server.isRunning) {
|
||||
console.warn('[SyncVarMessageHandler] NetworkServer未运行,无法转发消息');
|
||||
SyncVarMessageHandler.logger.warn(' NetworkServer未运行,无法转发消息');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -238,12 +239,12 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
const successCount = await server.broadcastSyncVarMessageExcept(message, senderConnection.connectionId);
|
||||
|
||||
if (successCount > 0) {
|
||||
console.log(`[SyncVarMessageHandler] 成功转发消息给 ${successCount} 个其他客户端 (发送者: ${senderConnection.connectionId})`);
|
||||
SyncVarMessageHandler.logger.debug(` 成功转发消息给 ${successCount} 个其他客户端 (发送者: ${senderConnection.connectionId})`);
|
||||
} else {
|
||||
console.log(`[SyncVarMessageHandler] 没有其他客户端需要转发消息 (发送者: ${senderConnection.connectionId})`);
|
||||
SyncVarMessageHandler.logger.debug(` 没有其他客户端需要转发消息 (发送者: ${senderConnection.connectionId})`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarMessageHandler] 转发消息失败 (发送者: ${senderConnection.connectionId}):`, error);
|
||||
SyncVarMessageHandler.logger.error(`转发消息失败 (发送者: ${senderConnection.connectionId}):`, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +268,7 @@ export class SyncVarMessageHandler implements IMessageHandler<SyncVarUpdateMessa
|
||||
*/
|
||||
public clearProcessedCache(): void {
|
||||
this._processedMessages.clear();
|
||||
console.log('[SyncVarMessageHandler] 已清理消息处理缓存');
|
||||
SyncVarMessageHandler.logger.info('已清理消息处理缓存');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { SyncVarUpdateMessage, SyncVarFieldUpdate } from '../Messaging/MessageTypes';
|
||||
import { createLogger } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* SyncVar优化配置
|
||||
@@ -325,6 +326,7 @@ export class SyncVarDistanceCuller {
|
||||
* SyncVar性能优化器
|
||||
*/
|
||||
export class SyncVarOptimizer {
|
||||
private static readonly logger = createLogger('SyncVarOptimizer');
|
||||
private _config: SyncVarOptimizationConfig;
|
||||
private _messageMerger: SyncVarMessageMerger;
|
||||
private _rateLimiter: SyncVarRateLimiter;
|
||||
@@ -376,7 +378,7 @@ export class SyncVarOptimizer {
|
||||
// 频率限制检查
|
||||
if (!this._rateLimiter.canSend(message.networkId)) {
|
||||
this._stats.messagesBlocked++;
|
||||
console.log(`[SyncVarOptimizer] 消息被频率限制阻止: ${message.networkId}`);
|
||||
SyncVarOptimizer.logger.debug(` 消息被频率限制阻止: ${message.networkId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -385,7 +387,7 @@ export class SyncVarOptimizer {
|
||||
|
||||
if (validObservers.length === 0 && targetObservers.length > 0) {
|
||||
this._stats.messagesBlocked++;
|
||||
console.log(`[SyncVarOptimizer] 消息被距离剔除阻止: ${message.networkId}`);
|
||||
SyncVarOptimizer.logger.debug(` 消息被距离剔除阻止: ${message.networkId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -393,7 +395,7 @@ export class SyncVarOptimizer {
|
||||
this._messageMerger.addMessage(message, (mergedMessage) => {
|
||||
if (mergedMessage !== message) {
|
||||
this._stats.messagesMerged++;
|
||||
console.log(`[SyncVarOptimizer] 消息已合并: ${message.networkId}`);
|
||||
SyncVarOptimizer.logger.debug(` 消息已合并: ${message.networkId}`);
|
||||
}
|
||||
|
||||
onOptimized([mergedMessage], validObservers);
|
||||
@@ -412,7 +414,7 @@ export class SyncVarOptimizer {
|
||||
*/
|
||||
public configure(config: Partial<SyncVarOptimizationConfig>): void {
|
||||
this._config = { ...this._config, ...config };
|
||||
console.log('[SyncVarOptimizer] 配置已更新:', this._config);
|
||||
SyncVarOptimizer.logger.info(' 配置已更新:', this._config);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getSyncVarMetadata, isSyncVar } from './SyncVarDecorator';
|
||||
import { SyncVarManager } from './SyncVarManager';
|
||||
import { INetworkSyncable, SyncVarValue, TypeGuards } from '../types/NetworkTypes';
|
||||
import { createLogger } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* SyncVar代理配置
|
||||
@@ -36,9 +37,11 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
|
||||
// 检查目标是否有SyncVar
|
||||
const metadata = getSyncVarMetadata(target.constructor);
|
||||
const logger = createLogger('SyncVarProxy');
|
||||
|
||||
if (metadata.length === 0) {
|
||||
if (debugLog) {
|
||||
console.log(`[SyncVarProxy] 对象 ${target.constructor.name} 没有SyncVar,返回原对象`);
|
||||
logger.debug(`对象 ${target.constructor.name} 没有SyncVar,返回原对象`);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
@@ -47,7 +50,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
syncVarManager.initializeComponent(target);
|
||||
|
||||
if (debugLog) {
|
||||
console.log(`[SyncVarProxy] 为 ${target.constructor.name} 创建代理,SyncVar字段:`,
|
||||
logger.debug(`为 ${target.constructor.name} 创建代理,SyncVar字段:`,
|
||||
metadata.map(m => m.propertyKey));
|
||||
}
|
||||
|
||||
@@ -81,7 +84,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
const value = Reflect.get(obj, prop);
|
||||
|
||||
if (debugLog && isSyncVar(obj, propertyKey)) {
|
||||
console.log(`[SyncVarProxy] GET ${obj.constructor.name}.${propertyKey} = ${value}`);
|
||||
logger.debug(`GET ${obj.constructor.name}.${propertyKey} = ${value}`);
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -117,7 +120,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
const oldValue = originalValues.get(propertyKey);
|
||||
|
||||
if (debugLog) {
|
||||
console.log(`[SyncVarProxy] SET ${obj.constructor.name}.${propertyKey} = ${newValue} (was ${oldValue})`);
|
||||
logger.debug(`SET ${obj.constructor.name}.${propertyKey} = ${newValue} (was ${oldValue})`);
|
||||
}
|
||||
|
||||
// 设置新值
|
||||
@@ -133,7 +136,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
syncVarManager.recordChange(obj, propertyKey, oldValue, newValue);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarProxy] 记录SyncVar变化失败:`, error);
|
||||
logger.error(`记录SyncVar变化失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +150,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
const propertyKey = prop as string;
|
||||
|
||||
if (typeof prop === 'string' && isSyncVar(obj, propertyKey)) {
|
||||
console.warn(`[SyncVarProxy] 尝试删除SyncVar属性 ${propertyKey},这可能会导致同步问题`);
|
||||
logger.warn(`尝试删除SyncVar属性 ${propertyKey},这可能会导致同步问题`);
|
||||
}
|
||||
|
||||
return Reflect.deleteProperty(obj, prop);
|
||||
@@ -180,7 +183,7 @@ export function createSyncVarProxy<T extends INetworkSyncable>(
|
||||
(proxy as T & { _syncVarProxied: boolean; _syncVarOptions: SyncVarProxyOptions })._syncVarOptions = options;
|
||||
|
||||
if (debugLog) {
|
||||
console.log(`[SyncVarProxy] ${target.constructor.name} 代理创建完成`);
|
||||
logger.debug(`${target.constructor.name} 代理创建完成`);
|
||||
}
|
||||
|
||||
return proxy;
|
||||
@@ -229,7 +232,8 @@ export function destroySyncVarProxy(proxy: INetworkSyncable & { _syncVarProxied?
|
||||
proxy._syncVarProxied = false;
|
||||
proxy._syncVarDestroyed = true;
|
||||
|
||||
console.log(`[SyncVarProxy] ${proxy.constructor?.name || 'Unknown'} 代理已销毁`);
|
||||
const logger = createLogger('SyncVarProxy');
|
||||
logger.debug(`${proxy.constructor?.name || 'Unknown'} 代理已销毁`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@ import { SyncVarManager } from './SyncVarManager';
|
||||
import { NetworkIdentityRegistry, NetworkIdentity } from '../Core/NetworkIdentity';
|
||||
import { SyncVarUpdateMessage } from '../Messaging/MessageTypes';
|
||||
import { NetworkEnvironment } from '../Core/NetworkEnvironment';
|
||||
import { ComponentRegistry } from '@esengine/ecs-framework';
|
||||
import { ComponentRegistry, createLogger } from '@esengine/ecs-framework';
|
||||
import { NetworkComponent } from '../NetworkComponent';
|
||||
|
||||
/**
|
||||
@@ -69,6 +69,7 @@ export class DefaultSyncPriorityCalculator implements ISyncPriorityCalculator {
|
||||
* 支持批处理、优先级排序和性能优化
|
||||
*/
|
||||
export class SyncVarSyncScheduler {
|
||||
private static readonly logger = createLogger('SyncVarSyncScheduler');
|
||||
private static _instance: SyncVarSyncScheduler | null = null;
|
||||
|
||||
private _config: SyncVarSyncConfig;
|
||||
@@ -130,7 +131,7 @@ export class SyncVarSyncScheduler {
|
||||
this.start();
|
||||
}
|
||||
|
||||
console.log('[SyncVarSyncScheduler] 调度器配置已更新:', this._config);
|
||||
SyncVarSyncScheduler.logger.debug('调度器配置已更新:', this._config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +141,7 @@ export class SyncVarSyncScheduler {
|
||||
*/
|
||||
public setPriorityCalculator(calculator: ISyncPriorityCalculator): void {
|
||||
this._priorityCalculator = calculator;
|
||||
console.log('[SyncVarSyncScheduler] 优先级计算器已更新');
|
||||
SyncVarSyncScheduler.logger.debug('优先级计算器已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,7 +151,7 @@ export class SyncVarSyncScheduler {
|
||||
*/
|
||||
public setMessageSendCallback(callback: (message: SyncVarUpdateMessage) => Promise<void>): void {
|
||||
this._messageSendCallback = callback;
|
||||
console.log('[SyncVarSyncScheduler] 消息发送回调已设置');
|
||||
SyncVarSyncScheduler.logger.debug('消息发送回调已设置');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +159,7 @@ export class SyncVarSyncScheduler {
|
||||
*/
|
||||
public start(): void {
|
||||
if (this._isRunning) {
|
||||
console.warn('[SyncVarSyncScheduler] 调度器已经在运行');
|
||||
SyncVarSyncScheduler.logger.warn('调度器已经在运行');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -170,7 +171,7 @@ export class SyncVarSyncScheduler {
|
||||
this.performSyncCycle();
|
||||
}, this._config.syncInterval);
|
||||
|
||||
console.log(`[SyncVarSyncScheduler] 调度器已启动,同步间隔: ${this._config.syncInterval}ms`);
|
||||
SyncVarSyncScheduler.logger.info(`调度器已启动,同步间隔: ${this._config.syncInterval}ms`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +189,7 @@ export class SyncVarSyncScheduler {
|
||||
this._syncTimer = null;
|
||||
}
|
||||
|
||||
console.log('[SyncVarSyncScheduler] 调度器已停止');
|
||||
SyncVarSyncScheduler.logger.info('调度器已停止');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -235,7 +236,7 @@ export class SyncVarSyncScheduler {
|
||||
|
||||
} catch (error) {
|
||||
this._stats.errors++;
|
||||
console.error('[SyncVarSyncScheduler] 同步周期执行失败:', error);
|
||||
SyncVarSyncScheduler.logger.error('同步周期执行失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +286,7 @@ export class SyncVarSyncScheduler {
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarSyncScheduler] 处理网络对象失败: ${identity.networkId}`, error);
|
||||
SyncVarSyncScheduler.logger.error(`处理网络对象失败: ${identity.networkId}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,7 +299,7 @@ export class SyncVarSyncScheduler {
|
||||
private getNetworkComponents(identity: NetworkIdentity): NetworkComponent[] {
|
||||
const entity = identity.entity;
|
||||
if (!entity) {
|
||||
console.warn(`[SyncVarSyncScheduler] NetworkIdentity ${identity.networkId} 缺少Entity引用`);
|
||||
SyncVarSyncScheduler.logger.warn(`NetworkIdentity ${identity.networkId} 缺少Entity引用`);
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -318,7 +319,7 @@ export class SyncVarSyncScheduler {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarSyncScheduler] 获取网络组件失败 (${identity.networkId}):`, error);
|
||||
SyncVarSyncScheduler.logger.error(`获取网络组件失败 (${identity.networkId}):`, error);
|
||||
}
|
||||
|
||||
return networkComponents;
|
||||
@@ -383,7 +384,7 @@ export class SyncVarSyncScheduler {
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[SyncVarSyncScheduler] 处理同步候选对象失败: ${candidate.identity.networkId}`, error);
|
||||
SyncVarSyncScheduler.logger.error(`处理同步候选对象失败: ${candidate.identity.networkId}`, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,7 +399,7 @@ export class SyncVarSyncScheduler {
|
||||
*/
|
||||
private async sendMessageBatch(messages: SyncVarUpdateMessage[]): Promise<void> {
|
||||
if (!this._messageSendCallback) {
|
||||
console.warn('[SyncVarSyncScheduler] 没有设置消息发送回调,消息被丢弃');
|
||||
SyncVarSyncScheduler.logger.warn('没有设置消息发送回调,消息被丢弃');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -407,7 +408,7 @@ export class SyncVarSyncScheduler {
|
||||
await this._messageSendCallback(message);
|
||||
this._stats.totalMessagesSent++;
|
||||
} catch (error) {
|
||||
console.error('[SyncVarSyncScheduler] 发送SyncVar消息失败:', error);
|
||||
SyncVarSyncScheduler.logger.error('发送SyncVar消息失败:', error);
|
||||
this._stats.errors++;
|
||||
}
|
||||
}
|
||||
@@ -458,7 +459,7 @@ export class SyncVarSyncScheduler {
|
||||
lastCycleTime: 0,
|
||||
errors: 0
|
||||
};
|
||||
console.log('[SyncVarSyncScheduler] 统计信息已重置');
|
||||
SyncVarSyncScheduler.logger.debug('统计信息已重置');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user