使用Lerna 和 monorepo管理项目结构

This commit is contained in:
YHH
2025-08-07 13:29:12 +08:00
parent 4479f0fab0
commit ea8523be35
135 changed files with 7058 additions and 372 deletions

View File

@@ -0,0 +1,271 @@
import { IComponentDebugData } from '../../Types';
import { Core } from '../../Core';
/**
* 组件数据收集器
*/
export class ComponentDataCollector {
private static componentSizeCache = new Map<string, number>();
/**
* 收集组件数据(轻量版,不计算实际内存大小)
*/
public collectComponentData(): IComponentDebugData {
const scene = Core.scene;
if (!scene) {
return {
componentTypes: 0,
componentInstances: 0,
componentStats: []
};
}
const entityList = (scene as any).entities;
if (!entityList?.buffer) {
return {
componentTypes: 0,
componentInstances: 0,
componentStats: []
};
}
const componentStats = new Map<string, { count: number; entities: number }>();
let totalInstances = 0;
entityList.buffer.forEach((entity: any) => {
if (entity.components) {
entity.components.forEach((component: any) => {
const typeName = component.constructor.name;
const stats = componentStats.get(typeName) || { count: 0, entities: 0 };
stats.count++;
totalInstances++;
componentStats.set(typeName, stats);
});
}
});
// 获取池利用率信息
let poolUtilizations = new Map<string, number>();
let poolSizes = new Map<string, number>();
try {
const { ComponentPoolManager } = require('../../ECS/Core/ComponentPool');
const poolManager = ComponentPoolManager.getInstance();
const poolStats = poolManager.getPoolStats();
const utilizations = poolManager.getPoolUtilization();
for (const [typeName, stats] of poolStats.entries()) {
poolSizes.set(typeName, stats.maxSize);
}
for (const [typeName, util] of utilizations.entries()) {
poolUtilizations.set(typeName, util.utilization);
}
} catch (error) {
// 如果无法获取池信息,使用默认值
}
return {
componentTypes: componentStats.size,
componentInstances: totalInstances,
componentStats: Array.from(componentStats.entries()).map(([typeName, stats]) => {
const poolSize = poolSizes.get(typeName) || 0;
const poolUtilization = poolUtilizations.get(typeName) || 0;
// 使用预估的基础内存大小,避免每帧计算
const memoryPerInstance = this.getEstimatedComponentSize(typeName);
return {
typeName,
instanceCount: stats.count,
memoryPerInstance: memoryPerInstance,
totalMemory: stats.count * memoryPerInstance,
poolSize: poolSize,
poolUtilization: poolUtilization,
averagePerEntity: stats.count / entityList.buffer.length
};
})
};
}
/**
* 获取组件类型的估算内存大小(基于预设值,不进行实际计算)
*/
private getEstimatedComponentSize(typeName: string): number {
if (ComponentDataCollector.componentSizeCache.has(typeName)) {
return ComponentDataCollector.componentSizeCache.get(typeName)!;
}
const scene = Core.scene;
if (!scene) return 64;
const entityList = (scene as any).entities;
if (!entityList?.buffer) return 64;
let calculatedSize = 64;
try {
for (const entity of entityList.buffer) {
if (entity.components) {
const component = entity.components.find((c: any) => c.constructor.name === typeName);
if (component) {
calculatedSize = this.calculateQuickObjectSize(component);
break;
}
}
}
} catch (error) {
calculatedSize = 64;
}
ComponentDataCollector.componentSizeCache.set(typeName, calculatedSize);
return calculatedSize;
}
private calculateQuickObjectSize(obj: any): number {
if (!obj || typeof obj !== 'object') return 8;
let size = 32;
const visited = new WeakSet();
const calculate = (item: any, depth: number = 0): number => {
if (!item || typeof item !== 'object' || visited.has(item) || depth > 3) {
return 0;
}
visited.add(item);
let itemSize = 0;
try {
const keys = Object.keys(item);
for (let i = 0; i < Math.min(keys.length, 20); i++) {
const key = keys[i];
if (key === 'entity' || key === '_entity' || key === 'constructor') continue;
const value = item[key];
itemSize += key.length * 2;
if (typeof value === 'string') {
itemSize += Math.min(value.length * 2, 200);
} else if (typeof value === 'number') {
itemSize += 8;
} else if (typeof value === 'boolean') {
itemSize += 4;
} else if (typeof value === 'object' && value !== null) {
itemSize += calculate(value, depth + 1);
}
}
} catch (error) {
return 32;
}
return itemSize;
};
size += calculate(obj);
return Math.max(size, 32);
}
/**
* 为内存快照功能提供的详细内存计算
* 只在用户主动请求内存快照时调用
*/
public calculateDetailedComponentMemory(typeName: string): number {
const scene = Core.scene;
if (!scene) return this.getEstimatedComponentSize(typeName);
const entityList = (scene as any).entities;
if (!entityList?.buffer) return this.getEstimatedComponentSize(typeName);
try {
// 找到第一个包含此组件的实体,分析组件大小
for (const entity of entityList.buffer) {
if (entity.components) {
const component = entity.components.find((c: any) => c.constructor.name === typeName);
if (component) {
return this.estimateObjectSize(component);
}
}
}
} catch (error) {
// 忽略错误,使用估算值
}
return this.getEstimatedComponentSize(typeName);
}
/**
* 估算对象内存大小(仅用于内存快照)
* 优化版本:减少递归深度,提高性能
*/
private estimateObjectSize(obj: any, visited = new WeakSet(), depth = 0): number {
if (obj === null || obj === undefined || depth > 10) return 0;
if (visited.has(obj)) return 0;
let size = 0;
const type = typeof obj;
switch (type) {
case 'boolean':
size = 4;
break;
case 'number':
size = 8;
break;
case 'string':
size = 24 + Math.min(obj.length * 2, 1000);
break;
case 'object':
visited.add(obj);
if (Array.isArray(obj)) {
size = 40 + (obj.length * 8);
const maxElements = Math.min(obj.length, 50);
for (let i = 0; i < maxElements; i++) {
size += this.estimateObjectSize(obj[i], visited, depth + 1);
}
} else {
size = 32;
try {
const ownKeys = Object.getOwnPropertyNames(obj);
const maxProps = Math.min(ownKeys.length, 30);
for (let i = 0; i < maxProps; i++) {
const key = ownKeys[i];
if (key === 'constructor' ||
key === '__proto__' ||
key === 'entity' ||
key === '_entity' ||
key.startsWith('_cc_') ||
key.startsWith('__')) {
continue;
}
try {
size += 16 + (key.length * 2);
const value = obj[key];
if (value !== undefined && value !== null) {
size += this.estimateObjectSize(value, visited, depth + 1);
}
} catch (error) {
continue;
}
}
} catch (error) {
size = 128;
}
}
break;
default:
size = 8;
}
return Math.ceil(size / 8) * 8;
}
public static clearCache(): void {
ComponentDataCollector.componentSizeCache.clear();
}
}

View File

@@ -0,0 +1,124 @@
import { Component } from '../../ECS/Component';
/**
* 调试数据格式化工具
*/
export class DebugDataFormatter {
/**
* 格式化属性值
*/
public static formatPropertyValue(value: any, depth: number = 0): any {
// 防止无限递归,限制最大深度
if (depth > 5) {
return value?.toString() || 'null';
}
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
// 对于数组,总是返回完整数组,让前端决定如何显示
return value.map(item => this.formatPropertyValue(item, depth + 1));
} else {
// 通用对象处理:提取所有可枚举属性,不限制数量
try {
const keys = Object.keys(value);
if (keys.length === 0) {
return {};
}
const result: any = {};
keys.forEach(key => {
const propValue = value[key];
// 避免循环引用和函数属性
if (propValue !== value && typeof propValue !== 'function') {
try {
result[key] = this.formatPropertyValue(propValue, depth + 1);
} catch (error) {
// 如果属性访问失败,记录错误信息
result[key] = `[访问失败: ${error instanceof Error ? error.message : String(error)}]`;
}
}
});
return result;
} catch (error) {
return `[对象解析失败: ${error instanceof Error ? error.message : String(error)}]`;
}
}
}
return value;
}
/**
* 提取组件详细信息
*/
public static extractComponentDetails(components: Component[]): Array<{
typeName: string;
properties: Record<string, any>;
}> {
return components.map((component: Component) => {
const componentDetail = {
typeName: component.constructor.name,
properties: {} as Record<string, any>
};
// 安全地提取组件属性
try {
const propertyKeys = Object.keys(component);
propertyKeys.forEach(propertyKey => {
// 跳过私有属性和实体引用,避免循环引用
if (!propertyKey.startsWith('_') && propertyKey !== 'entity') {
const propertyValue = (component as any)[propertyKey];
if (propertyValue !== undefined && propertyValue !== null) {
componentDetail.properties[propertyKey] = this.formatPropertyValue(propertyValue);
}
}
});
} catch (error) {
componentDetail.properties['_extractionError'] = '属性提取失败';
}
return componentDetail;
});
}
/**
* 计算对象大小
*/
public static calculateObjectSize(obj: any, excludeKeys: string[] = []): number {
if (!obj || typeof obj !== 'object') return 0;
let size = 0;
const visited = new WeakSet();
const calculate = (item: any): number => {
if (!item || typeof item !== 'object' || visited.has(item)) return 0;
visited.add(item);
let itemSize = 0;
try {
for (const key in item) {
if (excludeKeys.includes(key)) continue;
const value = item[key];
itemSize += key.length * 2; // key size
if (typeof value === 'string') {
itemSize += value.length * 2;
} else if (typeof value === 'number') {
itemSize += 8;
} else if (typeof value === 'boolean') {
itemSize += 4;
} else if (typeof value === 'object' && value !== null) {
itemSize += calculate(value);
}
}
} catch (error) {
// 忽略无法访问的属性
}
return itemSize;
};
return calculate(obj);
}
}

View File

@@ -0,0 +1,732 @@
import { IECSDebugConfig, IECSDebugData } from '../../Types';
import { EntityDataCollector } from './EntityDataCollector';
import { SystemDataCollector } from './SystemDataCollector';
import { PerformanceDataCollector } from './PerformanceDataCollector';
import { ComponentDataCollector } from './ComponentDataCollector';
import { SceneDataCollector } from './SceneDataCollector';
import { WebSocketManager } from './WebSocketManager';
import { Core } from '../../Core';
/**
* 调试管理器
*
* 整合所有调试数据收集器,负责收集和发送调试数据
*/
export class DebugManager {
private config: IECSDebugConfig;
private webSocketManager: WebSocketManager;
private entityCollector: EntityDataCollector;
private systemCollector: SystemDataCollector;
private performanceCollector: PerformanceDataCollector;
private componentCollector: ComponentDataCollector;
private sceneCollector: SceneDataCollector;
private frameCounter: number = 0;
private lastSendTime: number = 0;
private sendInterval: number;
private isRunning: boolean = false;
constructor(core: Core, config: IECSDebugConfig) {
this.config = config;
// 初始化数据收集器
this.entityCollector = new EntityDataCollector();
this.systemCollector = new SystemDataCollector();
this.performanceCollector = new PerformanceDataCollector();
this.componentCollector = new ComponentDataCollector();
this.sceneCollector = new SceneDataCollector();
// 初始化WebSocket管理器
this.webSocketManager = new WebSocketManager(
config.websocketUrl,
config.autoReconnect !== false
);
// 设置消息处理回调
this.webSocketManager.setMessageHandler(this.handleMessage.bind(this));
// 计算发送间隔(基于帧率)
const debugFrameRate = config.debugFrameRate || 30;
this.sendInterval = 1000 / debugFrameRate;
this.start();
}
/**
* 启动调试管理器
*/
public start(): void {
if (this.isRunning) return;
this.isRunning = true;
this.connectWebSocket();
}
/**
* 停止调试管理器
*/
public stop(): void {
if (!this.isRunning) return;
this.isRunning = false;
this.webSocketManager.disconnect();
}
/**
* 更新配置
*/
public updateConfig(config: IECSDebugConfig): void {
this.config = config;
// 更新发送间隔
const debugFrameRate = config.debugFrameRate || 30;
this.sendInterval = 1000 / debugFrameRate;
// 重新连接WebSocket如果URL变化
if (this.webSocketManager && config.websocketUrl) {
this.webSocketManager.disconnect();
this.webSocketManager = new WebSocketManager(
config.websocketUrl,
config.autoReconnect !== false
);
this.webSocketManager.setMessageHandler(this.handleMessage.bind(this));
this.connectWebSocket();
}
}
/**
* 帧更新回调
*/
public onFrameUpdate(deltaTime: number): void {
if (!this.isRunning || !this.config.enabled) return;
this.frameCounter++;
const currentTime = Date.now();
// 基于配置的帧率发送数据
if (currentTime - this.lastSendTime >= this.sendInterval) {
this.sendDebugData();
this.lastSendTime = currentTime;
}
}
/**
* 场景变更回调
*/
public onSceneChanged(): void {
// 场景变更时立即发送一次数据
if (this.isRunning && this.config.enabled) {
this.sendDebugData();
}
}
/**
* 处理来自调试面板的消息
*/
private handleMessage(message: any): void {
try {
switch (message.type) {
case 'capture_memory_snapshot':
this.handleMemorySnapshotRequest();
break;
case 'config_update':
if (message.config) {
this.updateConfig({ ...this.config, ...message.config });
}
break;
case 'expand_lazy_object':
this.handleExpandLazyObjectRequest(message);
break;
case 'get_component_properties':
this.handleGetComponentPropertiesRequest(message);
break;
case 'get_raw_entity_list':
this.handleGetRawEntityListRequest(message);
break;
case 'get_entity_details':
this.handleGetEntityDetailsRequest(message);
break;
case 'ping':
this.webSocketManager.send({
type: 'pong',
timestamp: Date.now()
});
break;
default:
// 未知消息类型,忽略
break;
}
} catch (error) {
// console.error('[ECS Debug] 处理消息失败:', error);
if (message.requestId) {
this.webSocketManager.send({
type: 'error_response',
requestId: message.requestId,
error: error instanceof Error ? error.message : String(error)
});
}
}
}
/**
* 处理展开懒加载对象请求
*/
private handleExpandLazyObjectRequest(message: any): void {
try {
const { entityId, componentIndex, propertyPath, requestId } = message;
if (entityId === undefined || componentIndex === undefined || !propertyPath) {
this.webSocketManager.send({
type: 'expand_lazy_object_response',
requestId,
error: '缺少必要参数'
});
return;
}
const expandedData = this.entityCollector.expandLazyObject(entityId, componentIndex, propertyPath);
this.webSocketManager.send({
type: 'expand_lazy_object_response',
requestId,
data: expandedData
});
} catch (error) {
this.webSocketManager.send({
type: 'expand_lazy_object_response',
requestId: message.requestId,
error: error instanceof Error ? error.message : String(error)
});
}
}
/**
* 处理获取组件属性请求
*/
private handleGetComponentPropertiesRequest(message: any): void {
try {
const { entityId, componentIndex, requestId } = message;
if (entityId === undefined || componentIndex === undefined) {
this.webSocketManager.send({
type: 'get_component_properties_response',
requestId,
error: '缺少必要参数'
});
return;
}
const properties = this.entityCollector.getComponentProperties(entityId, componentIndex);
this.webSocketManager.send({
type: 'get_component_properties_response',
requestId,
data: properties
});
} catch (error) {
this.webSocketManager.send({
type: 'get_component_properties_response',
requestId: message.requestId,
error: error instanceof Error ? error.message : String(error)
});
}
}
/**
* 处理获取原始实体列表请求
*/
private handleGetRawEntityListRequest(message: any): void {
try {
const { requestId } = message;
const rawEntityList = this.entityCollector.getRawEntityList();
this.webSocketManager.send({
type: 'get_raw_entity_list_response',
requestId,
data: rawEntityList
});
} catch (error) {
this.webSocketManager.send({
type: 'get_raw_entity_list_response',
requestId: message.requestId,
error: error instanceof Error ? error.message : String(error)
});
}
}
/**
* 处理获取实体详情请求
*/
private handleGetEntityDetailsRequest(message: any): void {
try {
const { entityId, requestId } = message;
if (entityId === undefined) {
this.webSocketManager.send({
type: 'get_entity_details_response',
requestId,
error: '缺少实体ID参数'
});
return;
}
const entityDetails = this.entityCollector.getEntityDetails(entityId);
this.webSocketManager.send({
type: 'get_entity_details_response',
requestId,
data: entityDetails
});
} catch (error) {
this.webSocketManager.send({
type: 'get_entity_details_response',
requestId: message.requestId,
error: error instanceof Error ? error.message : String(error)
});
}
}
/**
* 处理内存快照请求
*/
private handleMemorySnapshotRequest(): void {
try {
const memorySnapshot = this.captureMemorySnapshot();
this.webSocketManager.send({
type: 'memory_snapshot_response',
data: memorySnapshot
});
} catch (error) {
this.webSocketManager.send({
type: 'memory_snapshot_error',
error: error instanceof Error ? error.message : '内存快照捕获失败'
});
}
}
/**
* 捕获内存快照
*/
private captureMemorySnapshot(): any {
const timestamp = Date.now();
// 使用专门的内存计算方法收集实体数据
const entityData = this.entityCollector.collectEntityDataWithMemory();
// 收集其他内存统计
const baseMemoryInfo = this.collectBaseMemoryInfo();
const componentMemoryStats = this.collectComponentMemoryStats((Core.scene as any)?.entities);
const systemMemoryStats = this.collectSystemMemoryStats();
const poolMemoryStats = this.collectPoolMemoryStats();
const performanceStats = this.collectPerformanceStats();
// 计算总内存使用量
const totalEntityMemory = entityData.entitiesPerArchetype.reduce((sum, arch) => sum + arch.memory, 0);
return {
timestamp,
version: '2.0',
summary: {
totalEntities: entityData.totalEntities,
totalMemoryUsage: baseMemoryInfo.usedMemory,
totalMemoryLimit: baseMemoryInfo.totalMemory,
memoryUtilization: (baseMemoryInfo.usedMemory / baseMemoryInfo.totalMemory * 100),
gcCollections: baseMemoryInfo.gcCollections,
entityMemory: totalEntityMemory,
componentMemory: componentMemoryStats.totalMemory,
systemMemory: systemMemoryStats.totalMemory,
poolMemory: poolMemoryStats.totalMemory
},
baseMemory: baseMemoryInfo,
entities: {
totalMemory: totalEntityMemory,
entityCount: entityData.totalEntities,
archetypes: entityData.entitiesPerArchetype,
largestEntities: entityData.topEntitiesByComponents
},
components: componentMemoryStats,
systems: systemMemoryStats,
pools: poolMemoryStats,
performance: performanceStats
};
}
/**
* 收集基础内存信息
*/
private collectBaseMemoryInfo(): any {
const memoryInfo: any = {
totalMemory: 0,
usedMemory: 0,
freeMemory: 0,
gcCollections: 0,
heapInfo: null
};
try {
if ((performance as any).memory) {
const perfMemory = (performance as any).memory;
memoryInfo.totalMemory = perfMemory.jsHeapSizeLimit || 512 * 1024 * 1024;
memoryInfo.usedMemory = perfMemory.usedJSHeapSize || 0;
memoryInfo.freeMemory = memoryInfo.totalMemory - memoryInfo.usedMemory;
memoryInfo.heapInfo = {
totalJSHeapSize: perfMemory.totalJSHeapSize || 0,
usedJSHeapSize: perfMemory.usedJSHeapSize || 0,
jsHeapSizeLimit: perfMemory.jsHeapSizeLimit || 0
};
} else {
memoryInfo.totalMemory = 512 * 1024 * 1024;
memoryInfo.freeMemory = 512 * 1024 * 1024;
}
// 尝试获取GC信息
if ((performance as any).measureUserAgentSpecificMemory) {
// 这是一个实验性API可能不可用
(performance as any).measureUserAgentSpecificMemory().then((result: any) => {
memoryInfo.detailedMemory = result;
}).catch(() => {
// 忽略错误
});
}
} catch (error) {
// 使用默认值
}
return memoryInfo;
}
/**
* 收集组件内存统计(仅用于内存快照)
*/
private collectComponentMemoryStats(entityList: any): any {
const componentStats = new Map<string, { count: number; totalMemory: number; instances: any[] }>();
let totalComponentMemory = 0;
// 首先统计组件类型和数量
const componentTypeCounts = new Map<string, number>();
for (const entity of entityList.buffer) {
if (!entity || entity.destroyed || !entity.components) continue;
for (const component of entity.components) {
const typeName = component.constructor.name;
componentTypeCounts.set(typeName, (componentTypeCounts.get(typeName) || 0) + 1);
}
}
// 为每种组件类型计算详细内存(只计算一次,然后乘以数量)
for (const [typeName, count] of componentTypeCounts.entries()) {
const detailedMemoryPerInstance = this.componentCollector.calculateDetailedComponentMemory(typeName);
const totalMemoryForType = detailedMemoryPerInstance * count;
totalComponentMemory += totalMemoryForType;
// 收集该类型组件的实例信息(用于显示最大的几个实例)
const instances: any[] = [];
let instanceCount = 0;
for (const entity of entityList.buffer) {
if (!entity || entity.destroyed || !entity.components) continue;
for (const component of entity.components) {
if (component.constructor.name === typeName) {
instances.push({
entityId: entity.id,
entityName: entity.name || `Entity_${entity.id}`,
memory: detailedMemoryPerInstance // 使用统一的详细计算结果
});
instanceCount++;
// 限制收集的实例数量,避免过多数据
if (instanceCount >= 100) break;
}
}
if (instanceCount >= 100) break;
}
componentStats.set(typeName, {
count: count,
totalMemory: totalMemoryForType,
instances: instances.slice(0, 10) // 只保留前10个实例用于显示
});
}
const componentBreakdown = Array.from(componentStats.entries()).map(([typeName, stats]) => ({
typeName,
instanceCount: stats.count,
totalMemory: stats.totalMemory,
averageMemory: stats.totalMemory / stats.count,
percentage: totalComponentMemory > 0 ? (stats.totalMemory / totalComponentMemory * 100) : 0,
largestInstances: stats.instances.sort((a, b) => b.memory - a.memory).slice(0, 3)
})).sort((a, b) => b.totalMemory - a.totalMemory);
return {
totalMemory: totalComponentMemory,
componentTypes: componentStats.size,
totalInstances: Array.from(componentStats.values()).reduce((sum, stats) => sum + stats.count, 0),
breakdown: componentBreakdown
};
}
private collectSystemMemoryStats(): any {
const scene = Core.scene;
let totalSystemMemory = 0;
const systemBreakdown: any[] = [];
try {
const entityProcessors = (scene as any).entityProcessors;
if (entityProcessors && entityProcessors.processors) {
const systemTypeMemoryCache = new Map<string, number>();
for (const system of entityProcessors.processors) {
const systemTypeName = system.constructor.name;
let systemMemory: number;
if (systemTypeMemoryCache.has(systemTypeName)) {
systemMemory = systemTypeMemoryCache.get(systemTypeName)!;
} else {
systemMemory = this.calculateQuickSystemSize(system);
systemTypeMemoryCache.set(systemTypeName, systemMemory);
}
totalSystemMemory += systemMemory;
systemBreakdown.push({
name: systemTypeName,
memory: systemMemory,
enabled: system.enabled !== false,
updateOrder: system.updateOrder || 0
});
}
}
} catch (error) {
// 忽略错误
}
return {
totalMemory: totalSystemMemory,
systemCount: systemBreakdown.length,
breakdown: systemBreakdown.sort((a, b) => b.memory - a.memory)
};
}
private calculateQuickSystemSize(system: any): number {
if (!system || typeof system !== 'object') return 64;
let size = 128;
try {
const keys = Object.keys(system);
for (let i = 0; i < Math.min(keys.length, 15); i++) {
const key = keys[i];
if (key === 'entities' || key === 'scene' || key === 'constructor') continue;
const value = system[key];
size += key.length * 2;
if (typeof value === 'string') {
size += Math.min(value.length * 2, 100);
} else if (typeof value === 'number') {
size += 8;
} else if (typeof value === 'boolean') {
size += 4;
} else if (Array.isArray(value)) {
size += 40 + Math.min(value.length * 8, 200);
} else if (typeof value === 'object' && value !== null) {
size += 64;
}
}
} catch (error) {
return 128;
}
return Math.max(size, 64);
}
/**
* 收集对象池内存统计
*/
private collectPoolMemoryStats(): any {
let totalPoolMemory = 0;
const poolBreakdown: any[] = [];
try {
// 尝试获取组件池统计
const { ComponentPoolManager } = require('../../ECS/Core/ComponentPool');
const poolManager = ComponentPoolManager.getInstance();
const poolStats = poolManager.getPoolStats();
for (const [typeName, stats] of poolStats.entries()) {
const poolData = stats as any; // 类型断言
const poolMemory = poolData.maxSize * 32; // 估算每个对象32字节
totalPoolMemory += poolMemory;
poolBreakdown.push({
typeName,
maxSize: poolData.maxSize,
currentSize: poolData.currentSize || 0,
estimatedMemory: poolMemory,
utilization: poolData.currentSize ? (poolData.currentSize / poolData.maxSize * 100) : 0
});
}
} catch (error) {
// 如果无法获取池信息,使用默认值
}
try {
// 尝试获取通用对象池统计
const { Pool } = require('../../Utils/Pool');
const poolStats = Pool.getStats();
for (const [typeName, stats] of Object.entries(poolStats)) {
const poolData = stats as any; // 类型断言
totalPoolMemory += poolData.estimatedMemoryUsage;
poolBreakdown.push({
typeName: `Pool_${typeName}`,
maxSize: poolData.maxSize,
currentSize: poolData.size,
estimatedMemory: poolData.estimatedMemoryUsage,
utilization: poolData.size / poolData.maxSize * 100,
hitRate: poolData.hitRate * 100
});
}
} catch (error) {
// 忽略错误
}
return {
totalMemory: totalPoolMemory,
poolCount: poolBreakdown.length,
breakdown: poolBreakdown.sort((a, b) => b.estimatedMemory - a.estimatedMemory)
};
}
/**
* 收集性能统计信息
*/
private collectPerformanceStats(): any {
try {
const performanceMonitor = (Core.Instance as any)._performanceMonitor;
if (!performanceMonitor) {
return { enabled: false };
}
const stats = performanceMonitor.getAllSystemStats();
const warnings = performanceMonitor.getPerformanceWarnings();
return {
enabled: performanceMonitor.enabled,
systemCount: stats.size,
warnings: warnings.slice(0, 10), // 最多10个警告
topSystems: Array.from(stats.entries() as any).map((entry: any) => {
const [name, stat] = entry;
return {
name,
averageTime: stat.averageTime,
maxTime: stat.maxTime,
samples: stat.executionCount
};
}).sort((a: any, b: any) => b.averageTime - a.averageTime).slice(0, 5)
};
} catch (error: any) {
return { enabled: false, error: error.message };
}
}
/**
* 获取内存大小分类
*/
private getMemorySizeCategory(memoryBytes: number): string {
if (memoryBytes < 1024) return '< 1KB';
if (memoryBytes < 10 * 1024) return '1-10KB';
if (memoryBytes < 100 * 1024) return '10-100KB';
if (memoryBytes < 1024 * 1024) return '100KB-1MB';
return '> 1MB';
}
/**
* 获取调试数据
*/
public getDebugData(): IECSDebugData {
const currentTime = Date.now();
const scene = Core.scene;
const debugData: IECSDebugData = {
timestamp: currentTime,
frameworkVersion: '1.0.0', // 可以从package.json读取
isRunning: this.isRunning,
frameworkLoaded: true,
currentScene: scene?.name || 'Unknown'
};
// 根据配置收集各种数据
if (this.config.channels.entities) {
debugData.entities = this.entityCollector.collectEntityData();
}
if (this.config.channels.systems) {
const performanceMonitor = (Core.Instance as any)._performanceMonitor;
debugData.systems = this.systemCollector.collectSystemData(performanceMonitor);
}
if (this.config.channels.performance) {
const performanceMonitor = (Core.Instance as any)._performanceMonitor;
debugData.performance = this.performanceCollector.collectPerformanceData(performanceMonitor);
}
if (this.config.channels.components) {
debugData.components = this.componentCollector.collectComponentData();
}
if (this.config.channels.scenes) {
debugData.scenes = this.sceneCollector.collectSceneData();
}
return debugData;
}
/**
* 连接WebSocket
*/
private async connectWebSocket(): Promise<void> {
try {
await this.webSocketManager.connect();
// console.log('[ECS Debug] 调试管理器已连接到调试服务器');
} catch (error) {
// console.warn('[ECS Debug] 无法连接到调试服务器:', error);
}
}
/**
* 发送调试数据
*/
private sendDebugData(): void {
if (!this.webSocketManager.getConnectionStatus()) {
return;
}
try {
const debugData = this.getDebugData();
// 包装成调试面板期望的消息格式
const message = {
type: 'debug_data',
data: debugData
};
this.webSocketManager.send(message);
} catch (error) {
// console.error('[ECS Debug] 发送调试数据失败:', error);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,234 @@
import { IPerformanceDebugData } from '../../Types';
import { Time } from '../Time';
/**
* 性能数据收集器
*/
export class PerformanceDataCollector {
private frameTimeHistory: number[] = [];
private maxHistoryLength: number = 60;
private lastGCCount: number = 0;
private gcCollections: number = 0;
private lastMemoryCheck: number = 0;
/**
* 收集性能数据
*/
public collectPerformanceData(performanceMonitor: any): IPerformanceDebugData {
const frameTimeSeconds = Time.deltaTime;
const engineFrameTimeMs = frameTimeSeconds * 1000;
const currentFps = frameTimeSeconds > 0 ? Math.round(1 / frameTimeSeconds) : 0;
const ecsPerformanceData = this.getECSPerformanceData(performanceMonitor);
const ecsExecutionTimeMs = ecsPerformanceData.totalExecutionTime;
const ecsPercentage = engineFrameTimeMs > 0 ? (ecsExecutionTimeMs / engineFrameTimeMs * 100) : 0;
let memoryUsage = 0;
if ((performance as any).memory) {
memoryUsage = (performance as any).memory.usedJSHeapSize / 1024 / 1024;
}
// 更新ECS执行时间历史记录
this.frameTimeHistory.push(ecsExecutionTimeMs);
if (this.frameTimeHistory.length > this.maxHistoryLength) {
this.frameTimeHistory.shift();
}
// 计算ECS执行时间统计
const history = this.frameTimeHistory.filter(t => t >= 0);
const averageECSTime = history.length > 0 ? history.reduce((a, b) => a + b, 0) / history.length : ecsExecutionTimeMs;
const minECSTime = history.length > 0 ? Math.min(...history) : ecsExecutionTimeMs;
const maxECSTime = history.length > 0 ? Math.max(...history) : ecsExecutionTimeMs;
return {
frameTime: ecsExecutionTimeMs,
engineFrameTime: engineFrameTimeMs,
ecsPercentage: ecsPercentage,
memoryUsage: memoryUsage,
fps: currentFps,
averageFrameTime: averageECSTime,
minFrameTime: minECSTime,
maxFrameTime: maxECSTime,
frameTimeHistory: [...this.frameTimeHistory],
systemPerformance: this.getSystemPerformance(performanceMonitor),
systemBreakdown: ecsPerformanceData.systemBreakdown,
memoryDetails: this.getMemoryDetails()
};
}
/**
* 获取ECS框架整体性能数据
*/
private getECSPerformanceData(performanceMonitor: any): { totalExecutionTime: number; systemBreakdown: Array<any> } {
// 检查性能监视器是否存在
if (!performanceMonitor) {
// 尝试从Core实例获取性能监视器
try {
const { Core } = require('../../Core');
const coreInstance = Core.Instance;
if (coreInstance && (coreInstance as any)._performanceMonitor) {
performanceMonitor = (coreInstance as any)._performanceMonitor;
} else {
return { totalExecutionTime: 0, systemBreakdown: [] };
}
} catch (error) {
return { totalExecutionTime: 0, systemBreakdown: [] };
}
}
if (!performanceMonitor.enabled) {
// 尝试启用性能监视器
try {
performanceMonitor.enabled = true;
} catch (error) {
// 如果无法启用,返回默认值
}
return { totalExecutionTime: 0, systemBreakdown: [] };
}
try {
let totalTime = 0;
const systemBreakdown = [];
const stats = performanceMonitor.getAllSystemStats();
if (stats.size === 0) {
return { totalExecutionTime: 0, systemBreakdown: [] };
}
// 计算各系统的执行时间
for (const [systemName, stat] of stats.entries()) {
// 使用最近的执行时间而不是平均时间,这样更能反映当前状态
const systemTime = stat.recentTimes && stat.recentTimes.length > 0 ?
stat.recentTimes[stat.recentTimes.length - 1] :
(stat.averageTime || 0);
totalTime += systemTime;
systemBreakdown.push({
systemName: systemName,
executionTime: systemTime,
percentage: 0 // 后面计算
});
}
// 计算各系统占ECS总时间的百分比
systemBreakdown.forEach(system => {
system.percentage = totalTime > 0 ? (system.executionTime / totalTime * 100) : 0;
});
// 按执行时间排序
systemBreakdown.sort((a, b) => b.executionTime - a.executionTime);
return {
totalExecutionTime: totalTime,
systemBreakdown: systemBreakdown
};
} catch (error) {
return { totalExecutionTime: 0, systemBreakdown: [] };
}
}
/**
* 获取系统性能数据
*/
private getSystemPerformance(performanceMonitor: any): Array<any> {
if (!performanceMonitor) {
return [];
}
try {
const stats = performanceMonitor.getAllSystemStats();
const systemData = performanceMonitor.getAllSystemData();
return Array.from(stats.entries() as Iterable<[string, any]>).map(([systemName, stat]) => {
const data = systemData.get(systemName);
return {
systemName: systemName,
averageTime: stat.averageTime || 0,
maxTime: stat.maxTime || 0,
minTime: stat.minTime === Number.MAX_VALUE ? 0 : (stat.minTime || 0),
samples: stat.executionCount || 0,
percentage: 0,
entityCount: data?.entityCount || 0,
lastExecutionTime: data?.executionTime || 0
};
});
} catch (error) {
return [];
}
}
/**
* 获取内存详情
*/
private getMemoryDetails(): any {
const memoryInfo: any = {
entities: 0,
components: 0,
systems: 0,
pooled: 0,
totalMemory: 0,
usedMemory: 0,
freeMemory: 0,
gcCollections: this.updateGCCount()
};
try {
if ((performance as any).memory) {
const perfMemory = (performance as any).memory;
memoryInfo.totalMemory = perfMemory.jsHeapSizeLimit || 512 * 1024 * 1024;
memoryInfo.usedMemory = perfMemory.usedJSHeapSize || 0;
memoryInfo.freeMemory = memoryInfo.totalMemory - memoryInfo.usedMemory;
// 检测GC如果使用的内存突然大幅减少可能发生了GC
if (this.lastMemoryCheck > 0) {
const memoryDrop = this.lastMemoryCheck - memoryInfo.usedMemory;
if (memoryDrop > 1024 * 1024) { // 内存减少超过1MB
this.gcCollections++;
}
}
this.lastMemoryCheck = memoryInfo.usedMemory;
} else {
memoryInfo.totalMemory = 512 * 1024 * 1024;
memoryInfo.freeMemory = 512 * 1024 * 1024;
}
} catch (error) {
return {
totalMemory: 0,
usedMemory: 0,
freeMemory: 0,
entityMemory: 0,
componentMemory: 0,
systemMemory: 0,
pooledMemory: 0,
gcCollections: this.gcCollections
};
}
return memoryInfo;
}
/**
* 更新GC计数
*/
private updateGCCount(): number {
try {
// 尝试使用PerformanceObserver来检测GC
if (typeof PerformanceObserver !== 'undefined') {
// 这是一个简化的GC检测方法
// 实际的GC检测需要更复杂的逻辑
return this.gcCollections;
}
// 如果有其他GC检测API可以在这里添加
if ((performance as any).measureUserAgentSpecificMemory) {
// 实验性API可能不可用
return this.gcCollections;
}
return this.gcCollections;
} catch (error) {
return this.gcCollections;
}
}
}

View File

@@ -0,0 +1,50 @@
import { ISceneDebugData } from '../../Types';
import { Core } from '../../Core';
/**
* 场景数据收集器
*/
export class SceneDataCollector {
private sceneStartTime: number = Date.now();
/**
* 收集场景数据
*/
public collectSceneData(): ISceneDebugData {
const scene = Core.scene;
if (!scene) {
return {
currentSceneName: 'No Scene',
isInitialized: false,
sceneRunTime: 0,
sceneEntityCount: 0,
sceneSystemCount: 0,
sceneMemory: 0,
sceneUptime: 0
};
}
const currentTime = Date.now();
const runTime = (currentTime - this.sceneStartTime) / 1000;
const entityList = (scene as any).entities;
const entityProcessors = (scene as any).entityProcessors;
return {
currentSceneName: (scene as any).name || 'Unnamed Scene',
isInitialized: (scene as any)._didSceneBegin || false,
sceneRunTime: runTime,
sceneEntityCount: entityList?.buffer?.length || 0,
sceneSystemCount: entityProcessors?.processors?.length || 0,
sceneMemory: 0, // TODO: 计算实际场景内存
sceneUptime: runTime
};
}
/**
* 设置场景开始时间
*/
public setSceneStartTime(time: number): void {
this.sceneStartTime = time;
}
}

View File

@@ -0,0 +1,65 @@
import { ISystemDebugData } from '../../Types';
import { Core } from '../../Core';
/**
* 系统数据收集器
*/
export class SystemDataCollector {
/**
* 收集系统数据
*/
public collectSystemData(performanceMonitor: any): ISystemDebugData {
const scene = Core.scene;
if (!scene) {
return {
totalSystems: 0,
systemsInfo: []
};
}
const entityProcessors = (scene as any).entityProcessors;
if (!entityProcessors) {
return {
totalSystems: 0,
systemsInfo: []
};
}
const systems = entityProcessors.processors || [];
// 获取性能监控数据
let systemStats: Map<string, any> = new Map();
let systemData: Map<string, any> = new Map();
if (performanceMonitor) {
try {
systemStats = performanceMonitor.getAllSystemStats();
systemData = performanceMonitor.getAllSystemData();
} catch (error) {
// 忽略错误使用空的Map
}
}
return {
totalSystems: systems.length,
systemsInfo: systems.map((system: any) => {
const systemName = system.systemName || system.constructor.name;
const stats = systemStats.get(systemName);
const data = systemData.get(systemName);
return {
name: systemName,
type: system.constructor.name,
entityCount: system.entities?.length || 0,
executionTime: stats?.averageTime || data?.executionTime || 0,
minExecutionTime: stats?.minTime === Number.MAX_VALUE ? 0 : (stats?.minTime || 0),
maxExecutionTime: stats?.maxTime || 0,
executionTimeHistory: stats?.recentTimes || [],
updateOrder: system.updateOrder || 0,
enabled: system.enabled !== false,
lastUpdateTime: data?.lastUpdateTime || 0
};
})
};
}
}

View File

@@ -0,0 +1,177 @@
/**
* WebSocket连接管理器
*/
export class WebSocketManager {
private ws?: WebSocket;
private isConnected: boolean = false;
private reconnectAttempts: number = 0;
private maxReconnectAttempts: number = 5;
private reconnectInterval: number = 2000;
private url: string;
private autoReconnect: boolean;
private reconnectTimer?: NodeJS.Timeout;
private onOpen?: (event: Event) => void;
private onClose?: (event: CloseEvent) => void;
private onError?: (error: Event | any) => void;
private messageHandler?: (message: any) => void;
constructor(url: string, autoReconnect: boolean = true) {
this.url = url;
this.autoReconnect = autoReconnect;
}
/**
* 设置消息处理回调
*/
public setMessageHandler(handler: (message: any) => void): void {
this.messageHandler = handler;
}
/**
* 连接WebSocket
*/
public connect(): Promise<void> {
return new Promise((resolve, reject) => {
try {
this.ws = new WebSocket(this.url);
this.ws.onopen = (event) => {
this.handleOpen(event);
resolve();
};
this.ws.onclose = (event) => {
this.handleClose(event);
};
this.ws.onerror = (error) => {
this.handleError(error);
reject(error);
};
this.ws.onmessage = (event) => {
this.handleMessage(event);
};
} catch (error) {
this.handleConnectionFailure(error);
reject(error);
}
});
}
/**
* 断开连接
*/
public disconnect(): void {
if (this.ws) {
this.autoReconnect = false; // 主动断开时不自动重连
this.ws.close();
this.ws = undefined;
}
this.isConnected = false;
}
/**
* 发送数据
*/
public send(data: any): void {
if (!this.isConnected || !this.ws) {
return;
}
try {
const message = typeof data === 'string' ? data : JSON.stringify(data);
this.ws.send(message);
} catch (error) {
}
}
/**
* 获取连接状态
*/
public getConnectionStatus(): boolean {
return this.isConnected;
}
/**
* 设置最大重连次数
*/
public setMaxReconnectAttempts(attempts: number): void {
this.maxReconnectAttempts = attempts;
}
/**
* 设置重连间隔
*/
public setReconnectInterval(interval: number): void {
this.reconnectInterval = interval;
}
/**
* 计划重连
*/
private scheduleReconnect(): void {
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
}
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), 30000);
this.reconnectAttempts++;
this.reconnectTimer = setTimeout(() => {
this.connect().catch(error => {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.scheduleReconnect();
}
});
}, delay);
}
/**
* 处理接收到的消息
*/
private handleMessage(event: MessageEvent): void {
try {
const message = JSON.parse(event.data);
// 调用消息处理回调
if (this.messageHandler) {
this.messageHandler(message);
}
} catch (error) {
}
}
private handleOpen(event: Event): void {
this.isConnected = true;
this.reconnectAttempts = 0;
if (this.onOpen) {
this.onOpen(event);
}
}
private handleClose(event: CloseEvent): void {
this.isConnected = false;
if (this.onClose) {
this.onClose(event);
}
if (this.autoReconnect && this.reconnectAttempts < this.maxReconnectAttempts) {
this.scheduleReconnect();
}
}
private handleError(error: Event): void {
if (this.onError) {
this.onError(error);
}
}
private handleConnectionFailure(error: any): void {
if (this.onError) {
this.onError(error);
}
}
}

View File

@@ -0,0 +1,7 @@
export { EntityDataCollector } from './EntityDataCollector';
export { SystemDataCollector } from './SystemDataCollector';
export { PerformanceDataCollector } from './PerformanceDataCollector';
export { ComponentDataCollector } from './ComponentDataCollector';
export { SceneDataCollector } from './SceneDataCollector';
export { WebSocketManager } from './WebSocketManager';
export { DebugManager } from './DebugManager';