修复循环依赖问题
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { IComponentDebugData } from '../../Types';
|
||||
import { Core } from '../../Core';
|
||||
import { ComponentPoolManager } from '../../ECS/Core/ComponentPool';
|
||||
import { getComponentInstanceTypeName } from '../../ECS/Decorators';
|
||||
import { IScene } from '../../ECS/IScene';
|
||||
|
||||
/**
|
||||
* 组件数据收集器
|
||||
@@ -12,8 +12,7 @@ export class ComponentDataCollector {
|
||||
/**
|
||||
* 收集组件数据(轻量版,不计算实际内存大小)
|
||||
*/
|
||||
public collectComponentData(): IComponentDebugData {
|
||||
const scene = Core.scene;
|
||||
public collectComponentData(scene?: IScene | null): IComponentDebugData {
|
||||
if (!scene) {
|
||||
return {
|
||||
componentTypes: 0,
|
||||
@@ -73,7 +72,7 @@ export class ComponentDataCollector {
|
||||
const poolSize = poolSizes.get(typeName) || 0;
|
||||
const poolUtilization = poolUtilizations.get(typeName) || 0;
|
||||
// 使用预估的基础内存大小,避免每帧计算
|
||||
const memoryPerInstance = this.getEstimatedComponentSize(typeName);
|
||||
const memoryPerInstance = this.getEstimatedComponentSize(typeName, scene);
|
||||
|
||||
return {
|
||||
typeName,
|
||||
@@ -91,12 +90,11 @@ export class ComponentDataCollector {
|
||||
/**
|
||||
* 获取组件类型的估算内存大小(基于预设值,不进行实际计算)
|
||||
*/
|
||||
private getEstimatedComponentSize(typeName: string): number {
|
||||
private getEstimatedComponentSize(typeName: string, scene?: IScene | null): 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;
|
||||
@@ -170,12 +168,11 @@ export class ComponentDataCollector {
|
||||
* 为内存快照功能提供的详细内存计算
|
||||
* 只在用户主动请求内存快照时调用
|
||||
*/
|
||||
public calculateDetailedComponentMemory(typeName: string): number {
|
||||
const scene = Core.scene;
|
||||
if (!scene) return this.getEstimatedComponentSize(typeName);
|
||||
|
||||
public calculateDetailedComponentMemory(typeName: string, scene?: IScene | null): number {
|
||||
if (!scene) return this.getEstimatedComponentSize(typeName, scene);
|
||||
|
||||
const entityList = (scene as any).entities;
|
||||
if (!entityList?.buffer) return this.getEstimatedComponentSize(typeName);
|
||||
if (!entityList?.buffer) return this.getEstimatedComponentSize(typeName, scene);
|
||||
|
||||
try {
|
||||
// 找到第一个包含此组件的实体,分析组件大小
|
||||
@@ -191,7 +188,7 @@ export class ComponentDataCollector {
|
||||
// 忽略错误,使用估算值
|
||||
}
|
||||
|
||||
return this.getEstimatedComponentSize(typeName);
|
||||
return this.getEstimatedComponentSize(typeName, scene);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@ import { PerformanceDataCollector } from './PerformanceDataCollector';
|
||||
import { ComponentDataCollector } from './ComponentDataCollector';
|
||||
import { SceneDataCollector } from './SceneDataCollector';
|
||||
import { WebSocketManager } from './WebSocketManager';
|
||||
import { Core } from '../../Core';
|
||||
import { Component } from '../../ECS/Component';
|
||||
import { ComponentPoolManager } from '../../ECS/Core/ComponentPool';
|
||||
import { Pool } from '../../Utils/Pool';
|
||||
@@ -13,7 +12,7 @@ import { getComponentInstanceTypeName, getSystemInstanceTypeName } from '../../E
|
||||
|
||||
/**
|
||||
* 调试管理器
|
||||
*
|
||||
*
|
||||
* 整合所有调试数据收集器,负责收集和发送调试数据
|
||||
*/
|
||||
export class DebugManager {
|
||||
@@ -24,15 +23,21 @@ export class DebugManager {
|
||||
private performanceCollector: PerformanceDataCollector;
|
||||
private componentCollector: ComponentDataCollector;
|
||||
private sceneCollector: SceneDataCollector;
|
||||
private sceneProvider: () => any;
|
||||
private performanceMonitorProvider: () => any;
|
||||
|
||||
private frameCounter: number = 0;
|
||||
private lastSendTime: number = 0;
|
||||
private sendInterval: number;
|
||||
private isRunning: boolean = false;
|
||||
|
||||
constructor(core: Core, config: IECSDebugConfig) {
|
||||
constructor(core: any, config: IECSDebugConfig) {
|
||||
this.config = config;
|
||||
|
||||
// 设置提供器函数
|
||||
this.sceneProvider = () => (core as any).scene || (core.constructor as any).scene;
|
||||
this.performanceMonitorProvider = () => core._performanceMonitor;
|
||||
|
||||
// 初始化数据收集器
|
||||
this.entityCollector = new EntityDataCollector();
|
||||
this.systemCollector = new SystemDataCollector();
|
||||
@@ -326,12 +331,12 @@ export class DebugManager {
|
||||
private captureMemorySnapshot(): any {
|
||||
const timestamp = Date.now();
|
||||
|
||||
// 使用专门的内存计算方法收集实体数据
|
||||
const entityData = this.entityCollector.collectEntityDataWithMemory();
|
||||
|
||||
// 收集其他内存统计
|
||||
const baseMemoryInfo = this.collectBaseMemoryInfo();
|
||||
const scene = Core.scene;
|
||||
const scene = this.sceneProvider();
|
||||
|
||||
// 使用专门的内存计算方法收集实体数据
|
||||
const entityData = this.entityCollector.collectEntityDataWithMemory(scene);
|
||||
const componentMemoryStats = scene?.entities ? this.collectComponentMemoryStats(scene.entities) : { totalMemory: 0, componentTypes: 0, totalInstances: 0, breakdown: [] };
|
||||
const systemMemoryStats = this.collectSystemMemoryStats();
|
||||
const poolMemoryStats = this.collectPoolMemoryStats();
|
||||
@@ -536,7 +541,7 @@ export class DebugManager {
|
||||
updateOrder: number;
|
||||
}>;
|
||||
} {
|
||||
const scene = Core.scene;
|
||||
const scene = this.sceneProvider();
|
||||
let totalSystemMemory = 0;
|
||||
const systemBreakdown: Array<{
|
||||
name: string;
|
||||
@@ -710,8 +715,7 @@ export class DebugManager {
|
||||
error?: string;
|
||||
} {
|
||||
try {
|
||||
const coreInstance = Core.Instance as Core & { _performanceMonitor?: unknown };
|
||||
const performanceMonitor = coreInstance._performanceMonitor;
|
||||
const performanceMonitor = this.performanceMonitorProvider();
|
||||
if (!performanceMonitor) {
|
||||
return { enabled: false };
|
||||
}
|
||||
@@ -744,7 +748,7 @@ export class DebugManager {
|
||||
*/
|
||||
public getDebugData(): IECSDebugData {
|
||||
const currentTime = Date.now();
|
||||
const scene = Core.scene;
|
||||
const scene = this.sceneProvider();
|
||||
|
||||
const debugData: IECSDebugData = {
|
||||
timestamp: currentTime,
|
||||
@@ -756,27 +760,25 @@ export class DebugManager {
|
||||
|
||||
// 根据配置收集各种数据
|
||||
if (this.config.channels.entities) {
|
||||
debugData.entities = this.entityCollector.collectEntityData();
|
||||
debugData.entities = this.entityCollector.collectEntityData(scene);
|
||||
}
|
||||
|
||||
if (this.config.channels.systems) {
|
||||
const coreInstance = Core.Instance as Core & { _performanceMonitor?: unknown };
|
||||
const performanceMonitor = coreInstance._performanceMonitor;
|
||||
debugData.systems = this.systemCollector.collectSystemData(performanceMonitor);
|
||||
const performanceMonitor = this.performanceMonitorProvider();
|
||||
debugData.systems = this.systemCollector.collectSystemData(performanceMonitor, scene);
|
||||
}
|
||||
|
||||
if (this.config.channels.performance) {
|
||||
const coreInstance = Core.Instance as Core & { _performanceMonitor?: unknown };
|
||||
const performanceMonitor = coreInstance._performanceMonitor;
|
||||
const performanceMonitor = this.performanceMonitorProvider();
|
||||
debugData.performance = this.performanceCollector.collectPerformanceData(performanceMonitor);
|
||||
}
|
||||
|
||||
if (this.config.channels.components) {
|
||||
debugData.components = this.componentCollector.collectComponentData();
|
||||
debugData.components = this.componentCollector.collectComponentData(scene);
|
||||
}
|
||||
|
||||
if (this.config.channels.scenes) {
|
||||
debugData.scenes = this.sceneCollector.collectSceneData();
|
||||
debugData.scenes = this.sceneCollector.collectSceneData(scene);
|
||||
}
|
||||
|
||||
return debugData;
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { IEntityDebugData } from '../../Types';
|
||||
import { Core } from '../../Core';
|
||||
import { Entity } from '../../ECS/Entity';
|
||||
import { Component } from '../../ECS/Component';
|
||||
import { ComponentTypeManager } from '../../ECS/Utils/ComponentTypeManager';
|
||||
import { getComponentInstanceTypeName, getSystemInstanceTypeName } from '../../ECS/Decorators';
|
||||
import { IScene } from '../../ECS/IScene';
|
||||
|
||||
/**
|
||||
* 实体数据收集器
|
||||
*/
|
||||
export class EntityDataCollector {
|
||||
public collectEntityData(): IEntityDebugData {
|
||||
const scene = Core.scene;
|
||||
public collectEntityData(scene?: IScene | null): IEntityDebugData {
|
||||
if (!scene) {
|
||||
return this.getEmptyEntityDebugData();
|
||||
}
|
||||
@@ -51,7 +50,7 @@ export class EntityDataCollector {
|
||||
}
|
||||
|
||||
|
||||
public getRawEntityList(): Array<{
|
||||
public getRawEntityList(scene?: IScene | null): Array<{
|
||||
id: number;
|
||||
name: string;
|
||||
active: boolean;
|
||||
@@ -65,7 +64,6 @@ export class EntityDataCollector {
|
||||
tag: number;
|
||||
updateOrder: number;
|
||||
}> {
|
||||
const scene = Core.scene;
|
||||
if (!scene) return [];
|
||||
|
||||
const entityList = (scene as any).entities;
|
||||
@@ -88,9 +86,8 @@ export class EntityDataCollector {
|
||||
}
|
||||
|
||||
|
||||
public getEntityDetails(entityId: number): any {
|
||||
public getEntityDetails(entityId: number, scene?: IScene | null): any {
|
||||
try {
|
||||
const scene = Core.scene;
|
||||
if (!scene) return null;
|
||||
|
||||
const entityList = (scene as any).entities;
|
||||
@@ -101,7 +98,7 @@ export class EntityDataCollector {
|
||||
|
||||
const baseDebugInfo = entity.getDebugInfo ?
|
||||
entity.getDebugInfo() :
|
||||
this.buildFallbackEntityInfo(entity);
|
||||
this.buildFallbackEntityInfo(entity, scene);
|
||||
|
||||
const componentDetails = this.extractComponentDetails(entity.components);
|
||||
|
||||
@@ -155,8 +152,7 @@ export class EntityDataCollector {
|
||||
}
|
||||
|
||||
|
||||
public collectEntityDataWithMemory(): IEntityDebugData {
|
||||
const scene = Core.scene;
|
||||
public collectEntityDataWithMemory(scene?: IScene | null): IEntityDebugData {
|
||||
if (!scene) {
|
||||
return this.getEmptyEntityDebugData();
|
||||
}
|
||||
@@ -192,7 +188,7 @@ export class EntityDataCollector {
|
||||
entitiesPerArchetype: archetypeData.distribution,
|
||||
topEntitiesByComponents: archetypeData.topEntities,
|
||||
entityHierarchy: this.buildEntityHierarchyTree(entityList),
|
||||
entityDetailsMap: this.buildEntityDetailsMap(entityList)
|
||||
entityDetailsMap: this.buildEntityDetailsMap(entityList, scene)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -635,20 +631,20 @@ export class EntityDataCollector {
|
||||
/**
|
||||
* 构建实体详情映射
|
||||
*/
|
||||
private buildEntityDetailsMap(entityList: { buffer?: Entity[] }): Record<number, any> {
|
||||
private buildEntityDetailsMap(entityList: { buffer?: Entity[] }, scene?: IScene | null): Record<number, any> {
|
||||
if (!entityList?.buffer) return {};
|
||||
|
||||
const entityDetailsMap: Record<number, any> = {};
|
||||
const entities = entityList.buffer;
|
||||
const batchSize = 100;
|
||||
|
||||
|
||||
for (let i = 0; i < entities.length; i += batchSize) {
|
||||
const batch = entities.slice(i, i + batchSize);
|
||||
|
||||
|
||||
batch.forEach((entity: Entity) => {
|
||||
const baseDebugInfo = entity.getDebugInfo ?
|
||||
entity.getDebugInfo() :
|
||||
this.buildFallbackEntityInfo(entity);
|
||||
const baseDebugInfo = entity.getDebugInfo ?
|
||||
entity.getDebugInfo() :
|
||||
this.buildFallbackEntityInfo(entity, scene);
|
||||
|
||||
const componentCacheStats = (entity as any).getComponentCacheStats ?
|
||||
(entity as any).getComponentCacheStats() : null;
|
||||
@@ -676,8 +672,7 @@ export class EntityDataCollector {
|
||||
/**
|
||||
* 构建实体基础信息
|
||||
*/
|
||||
private buildFallbackEntityInfo(entity: Entity): any {
|
||||
const scene = Core.scene;
|
||||
private buildFallbackEntityInfo(entity: Entity, scene?: IScene | null): any {
|
||||
const sceneInfo = this.getSceneInfo(scene);
|
||||
|
||||
return {
|
||||
@@ -757,9 +752,8 @@ export class EntityDataCollector {
|
||||
/**
|
||||
* 获取组件的完整属性信息(仅在需要时调用)
|
||||
*/
|
||||
public getComponentProperties(entityId: number, componentIndex: number): Record<string, any> {
|
||||
public getComponentProperties(entityId: number, componentIndex: number, scene?: IScene | null): Record<string, any> {
|
||||
try {
|
||||
const scene = Core.scene;
|
||||
if (!scene) return {};
|
||||
|
||||
const entityList = (scene as any).entities;
|
||||
@@ -950,9 +944,8 @@ export class EntityDataCollector {
|
||||
/**
|
||||
* 展开懒加载对象(供调试面板调用)
|
||||
*/
|
||||
public expandLazyObject(entityId: number, componentIndex: number, propertyPath: string): any {
|
||||
public expandLazyObject(entityId: number, componentIndex: number, propertyPath: string, scene?: IScene | null): any {
|
||||
try {
|
||||
const scene = Core.scene;
|
||||
if (!scene) return null;
|
||||
|
||||
const entityList = (scene as any).entities;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { IPerformanceDebugData } from '../../Types';
|
||||
import { Time } from '../Time';
|
||||
import { Core } from '../../Core';
|
||||
|
||||
/**
|
||||
* 性能数据收集器
|
||||
@@ -63,17 +62,7 @@ export class PerformanceDataCollector {
|
||||
private getECSPerformanceData(performanceMonitor: any): { totalExecutionTime: number; systemBreakdown: Array<any> } {
|
||||
// 检查性能监视器是否存在
|
||||
if (!performanceMonitor) {
|
||||
// 尝试从Core实例获取性能监视器
|
||||
try {
|
||||
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: [] };
|
||||
}
|
||||
return { totalExecutionTime: 0, systemBreakdown: [] };
|
||||
}
|
||||
|
||||
if (!performanceMonitor.enabled) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ISceneDebugData } from '../../Types';
|
||||
import { Core } from '../../Core';
|
||||
import { IScene } from '../../ECS/IScene';
|
||||
|
||||
/**
|
||||
* 场景数据收集器
|
||||
@@ -10,8 +10,7 @@ export class SceneDataCollector {
|
||||
/**
|
||||
* 收集场景数据
|
||||
*/
|
||||
public collectSceneData(): ISceneDebugData {
|
||||
const scene = Core.scene;
|
||||
public collectSceneData(scene?: IScene | null): ISceneDebugData {
|
||||
if (!scene) {
|
||||
return {
|
||||
currentSceneName: 'No Scene',
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ISystemDebugData } from '../../Types';
|
||||
import { Core } from '../../Core';
|
||||
import { getSystemInstanceTypeName } from '../../ECS/Decorators';
|
||||
import { IScene } from '../../ECS/IScene';
|
||||
|
||||
/**
|
||||
* 系统数据收集器
|
||||
@@ -9,8 +9,7 @@ export class SystemDataCollector {
|
||||
/**
|
||||
* 收集系统数据
|
||||
*/
|
||||
public collectSystemData(performanceMonitor: any): ISystemDebugData {
|
||||
const scene = Core.scene;
|
||||
public collectSystemData(performanceMonitor: any, scene?: IScene | null): ISystemDebugData {
|
||||
if (!scene) {
|
||||
return {
|
||||
totalSystems: 0,
|
||||
|
||||
Reference in New Issue
Block a user