Feature/editor optimization (#251)
* refactor: 编辑器/运行时架构拆分与构建系统升级 * feat(core): 层级系统重构与UI变换矩阵修复 * refactor: 移除 ecs-components 聚合包并修复跨包组件查找问题 * fix(physics): 修复跨包组件类引用问题 * feat: 统一运行时架构与浏览器运行支持 * feat(asset): 实现浏览器运行时资产加载系统 * fix: 修复文档、CodeQL安全问题和CI类型检查错误 * fix: 修复文档、CodeQL安全问题和CI类型检查错误 * fix: 修复文档、CodeQL安全问题、CI类型检查和测试错误 * test: 补齐核心模块测试用例,修复CI构建配置 * fix: 修复测试用例中的类型错误和断言问题 * fix: 修复 turbo build:npm 任务的依赖顺序问题 * fix: 修复 CI 构建错误并优化构建性能
This commit is contained in:
166
packages/runtime-core/src/PluginManager.ts
Normal file
166
packages/runtime-core/src/PluginManager.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
/**
|
||||
* Runtime Plugin Manager
|
||||
* 运行时插件管理器
|
||||
*/
|
||||
|
||||
import { ComponentRegistry, ServiceContainer } from '@esengine/ecs-framework';
|
||||
import type { IScene } from '@esengine/ecs-framework';
|
||||
|
||||
export interface SystemContext {
|
||||
isEditor: boolean;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface PluginDescriptor {
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
category?: string;
|
||||
enabledByDefault?: boolean;
|
||||
isEnginePlugin?: boolean;
|
||||
}
|
||||
|
||||
export interface IRuntimeModule {
|
||||
registerComponents?(registry: typeof ComponentRegistry): void;
|
||||
registerServices?(services: ServiceContainer): void;
|
||||
createSystems?(scene: IScene, context: SystemContext): void;
|
||||
onSystemsCreated?(scene: IScene, context: SystemContext): void;
|
||||
onInitialize?(): Promise<void>;
|
||||
onDestroy?(): void;
|
||||
}
|
||||
|
||||
export interface IPlugin {
|
||||
readonly descriptor: PluginDescriptor;
|
||||
readonly runtimeModule?: IRuntimeModule;
|
||||
}
|
||||
|
||||
export class RuntimePluginManager {
|
||||
private _plugins = new Map<string, IPlugin>();
|
||||
private _enabledPlugins = new Set<string>();
|
||||
private _bInitialized = false;
|
||||
|
||||
register(plugin: IPlugin): void {
|
||||
const id = plugin.descriptor.id;
|
||||
if (this._plugins.has(id)) {
|
||||
return;
|
||||
}
|
||||
this._plugins.set(id, plugin);
|
||||
if (plugin.descriptor.enabledByDefault !== false) {
|
||||
this._enabledPlugins.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
enable(pluginId: string): void {
|
||||
this._enabledPlugins.add(pluginId);
|
||||
}
|
||||
|
||||
disable(pluginId: string): void {
|
||||
this._enabledPlugins.delete(pluginId);
|
||||
}
|
||||
|
||||
isEnabled(pluginId: string): boolean {
|
||||
return this._enabledPlugins.has(pluginId);
|
||||
}
|
||||
|
||||
loadConfig(config: { enabledPlugins: string[] }): void {
|
||||
this._enabledPlugins.clear();
|
||||
for (const id of config.enabledPlugins) {
|
||||
this._enabledPlugins.add(id);
|
||||
}
|
||||
// 始终启用引擎核心插件
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (plugin.descriptor.isEnginePlugin) {
|
||||
this._enabledPlugins.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async initializeRuntime(services: ServiceContainer): Promise<void> {
|
||||
if (this._bInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (!this._enabledPlugins.has(id)) continue;
|
||||
const mod = plugin.runtimeModule;
|
||||
if (mod?.registerComponents) {
|
||||
try {
|
||||
mod.registerComponents(ComponentRegistry);
|
||||
} catch (e) {
|
||||
console.error(`[PluginManager] Failed to register components for ${id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (!this._enabledPlugins.has(id)) continue;
|
||||
const mod = plugin.runtimeModule;
|
||||
if (mod?.registerServices) {
|
||||
try {
|
||||
mod.registerServices(services);
|
||||
} catch (e) {
|
||||
console.error(`[PluginManager] Failed to register services for ${id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (!this._enabledPlugins.has(id)) continue;
|
||||
const mod = plugin.runtimeModule;
|
||||
if (mod?.onInitialize) {
|
||||
try {
|
||||
await mod.onInitialize();
|
||||
} catch (e) {
|
||||
console.error(`[PluginManager] Failed to initialize ${id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this._bInitialized = true;
|
||||
}
|
||||
|
||||
createSystemsForScene(scene: IScene, context: SystemContext): void {
|
||||
// Phase 1: 创建系统
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (!this._enabledPlugins.has(id)) continue;
|
||||
const mod = plugin.runtimeModule;
|
||||
if (mod?.createSystems) {
|
||||
try {
|
||||
mod.createSystems(scene, context);
|
||||
} catch (e) {
|
||||
console.error(`[PluginManager] Failed to create systems for ${id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2: 连接跨插件依赖
|
||||
for (const [id, plugin] of this._plugins) {
|
||||
if (!this._enabledPlugins.has(id)) continue;
|
||||
const mod = plugin.runtimeModule;
|
||||
if (mod?.onSystemsCreated) {
|
||||
try {
|
||||
mod.onSystemsCreated(scene, context);
|
||||
} catch (e) {
|
||||
console.error(`[PluginManager] Failed to wire dependencies for ${id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getPlugins(): IPlugin[] {
|
||||
return Array.from(this._plugins.values());
|
||||
}
|
||||
|
||||
getPlugin(id: string): IPlugin | undefined {
|
||||
return this._plugins.get(id);
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this._plugins.clear();
|
||||
this._enabledPlugins.clear();
|
||||
this._bInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
export const runtimePluginManager = new RuntimePluginManager();
|
||||
Reference in New Issue
Block a user