From 0b4244fd8e1b043a4042c319bff092047a328e33 Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Wed, 24 Sep 2025 10:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 41 +------------------ .../src/Utils/Debug/ComponentDataCollector.ts | 21 ++++------ packages/core/src/Utils/Debug/DebugManager.ts | 40 +++++++++--------- .../src/Utils/Debug/EntityDataCollector.ts | 39 ++++++++---------- .../Utils/Debug/PerformanceDataCollector.ts | 13 +----- .../src/Utils/Debug/SceneDataCollector.ts | 5 +-- .../src/Utils/Debug/SystemDataCollector.ts | 5 +-- 7 files changed, 52 insertions(+), 112 deletions(-) diff --git a/package-lock.json b/package-lock.json index 247befe8..79ddf392 100644 --- a/package-lock.json +++ b/package-lock.json @@ -529,43 +529,6 @@ "@babel/core": "^7.0.0" } }, - "node_modules/@babel/plugin-proposal-nullish-coalescing-operator": { - "version": "7.18.6", - "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-nullish-coalescing-operator/-/plugin-proposal-nullish-coalescing-operator-7.18.6.tgz", - "integrity": "sha512-wQxQzxYeJqHcfppzBDnm1yAY0jSRkUXR2z8RePZYrKwMKgMlE8+Z6LUno+bd6LvbGh8Gltvy74+9pIYkr+XkKA==", - "deprecated": "This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-nullish-coalescing-operator instead.", - "dev": true, - "license": "MIT", - "dependencies": { - "@babel/helper-plugin-utils": "^7.18.6", - "@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3" - }, - "engines": { - "node": ">=6.9.0" - }, - "peerDependencies": { - "@babel/core": "^7.0.0-0" - } - }, - "node_modules/@babel/plugin-proposal-optional-chaining": { - "version": "7.21.0", - "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.21.0.tgz", - "integrity": "sha512-p4zeefM72gpmEe2fkUr/OnOXpWEf8nAgk7ZYVqqfFiyIG7oFfVZcCrU64hWn5xp4tQ9LkV4bTIa5rD0KANpKNA==", - "deprecated": "This proposal has been merged to the ECMAScript standard and thus this plugin is no longer maintained. Please use @babel/plugin-transform-optional-chaining instead.", - "dev": true, - "license": "MIT", - "dependencies": { - "@babel/helper-plugin-utils": "^7.20.2", - "@babel/helper-skip-transparent-expression-wrappers": "^7.20.0", - "@babel/plugin-syntax-optional-chaining": "^7.8.3" - }, - "engines": { - "node": ">=6.9.0" - }, - "peerDependencies": { - "@babel/core": "^7.0.0-0" - } - }, "node_modules/@babel/plugin-proposal-private-property-in-object": { "version": "7.21.0-placeholder-for-preset-env.2", "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-private-property-in-object/-/plugin-proposal-private-property-in-object-7.21.0-placeholder-for-preset-env.2.tgz", @@ -13079,12 +13042,10 @@ }, "packages/core": { "name": "@esengine/ecs-framework", - "version": "2.1.45", + "version": "2.1.47", "license": "MIT", "devDependencies": { "@babel/core": "^7.28.3", - "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6", - "@babel/plugin-proposal-optional-chaining": "^7.21.0", "@babel/plugin-transform-nullish-coalescing-operator": "^7.27.1", "@babel/plugin-transform-optional-chaining": "^7.27.1", "@babel/preset-env": "^7.28.3", diff --git a/packages/core/src/Utils/Debug/ComponentDataCollector.ts b/packages/core/src/Utils/Debug/ComponentDataCollector.ts index b7703913..605df925 100644 --- a/packages/core/src/Utils/Debug/ComponentDataCollector.ts +++ b/packages/core/src/Utils/Debug/ComponentDataCollector.ts @@ -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); } /** diff --git a/packages/core/src/Utils/Debug/DebugManager.ts b/packages/core/src/Utils/Debug/DebugManager.ts index cf0dbb94..f08613bd 100644 --- a/packages/core/src/Utils/Debug/DebugManager.ts +++ b/packages/core/src/Utils/Debug/DebugManager.ts @@ -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; diff --git a/packages/core/src/Utils/Debug/EntityDataCollector.ts b/packages/core/src/Utils/Debug/EntityDataCollector.ts index 7bbfa8ca..88f82ebc 100644 --- a/packages/core/src/Utils/Debug/EntityDataCollector.ts +++ b/packages/core/src/Utils/Debug/EntityDataCollector.ts @@ -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 { + private buildEntityDetailsMap(entityList: { buffer?: Entity[] }, scene?: IScene | null): Record { if (!entityList?.buffer) return {}; const entityDetailsMap: Record = {}; 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 { + public getComponentProperties(entityId: number, componentIndex: number, scene?: IScene | null): Record { 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; diff --git a/packages/core/src/Utils/Debug/PerformanceDataCollector.ts b/packages/core/src/Utils/Debug/PerformanceDataCollector.ts index 48256137..fee335c8 100644 --- a/packages/core/src/Utils/Debug/PerformanceDataCollector.ts +++ b/packages/core/src/Utils/Debug/PerformanceDataCollector.ts @@ -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 } { // 检查性能监视器是否存在 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) { diff --git a/packages/core/src/Utils/Debug/SceneDataCollector.ts b/packages/core/src/Utils/Debug/SceneDataCollector.ts index 53374126..de41360b 100644 --- a/packages/core/src/Utils/Debug/SceneDataCollector.ts +++ b/packages/core/src/Utils/Debug/SceneDataCollector.ts @@ -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', diff --git a/packages/core/src/Utils/Debug/SystemDataCollector.ts b/packages/core/src/Utils/Debug/SystemDataCollector.ts index 98dc4ab2..22edea32 100644 --- a/packages/core/src/Utils/Debug/SystemDataCollector.ts +++ b/packages/core/src/Utils/Debug/SystemDataCollector.ts @@ -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,