Feature/runtime cdn and plugin loader (#240)

* feat(ui): 完善 UI 布局系统和编辑器可视化工具

* refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统

* fix: 修复 CodeQL 警告并提升测试覆盖率

* refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题

* fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤

* docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明

* fix(ci): 修复 type-check 失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖

* fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖

* fix(ci): platform-web 添加缺失的 behavior-tree 依赖

* fix(lint): 移除正则表达式中不必要的转义字符
This commit is contained in:
YHH
2025-11-27 20:42:46 +08:00
committed by GitHub
parent 71869b1a58
commit 107439d70c
367 changed files with 10661 additions and 12473 deletions

View File

@@ -0,0 +1,109 @@
/**
* Plugin API - 为插件提供简洁的访问接口
*
* 使用方式:
* ```typescript
* import { PluginAPI } from '@esengine/editor-runtime';
*
* const scene = PluginAPI.scene;
* const entityStore = PluginAPI.entityStore;
* const messageHub = PluginAPI.messageHub;
* ```
*
* 这个 API 会自动从全局 __ESENGINE__ 获取正确的实例,
* 避免模块实例不一致的问题。
*/
import type { EntityStoreService, MessageHub } from '@esengine/editor-core';
import type { Scene, ServiceContainer } from '@esengine/ecs-framework';
// 内部 API 接口定义
interface IPluginAPIInternal {
getScene(): Scene | null;
getEntityStore(): EntityStoreService;
getMessageHub(): MessageHub;
resolveService<T>(serviceType: any): T;
getCore(): any;
}
// 声明全局类型
declare global {
interface Window {
__ESENGINE__?: {
api?: IPluginAPIInternal;
[key: string]: any;
};
}
}
/**
* 获取内部 API
*/
function getInternalAPI(): IPluginAPIInternal {
const api = window.__ESENGINE__?.api;
if (!api) {
throw new Error('[PluginAPI] 插件 API 未初始化,请确保编辑器已正确启动');
}
return api;
}
/**
* 插件 API
* 提供简洁的属性访问方式,避免模块实例不一致问题
*/
export const PluginAPI = {
/**
* 获取当前场景
* @throws 如果场景未初始化
*/
get scene(): Scene {
const scene = getInternalAPI().getScene();
if (!scene) {
throw new Error('[PluginAPI] 场景未初始化,请先打开或创建一个场景');
}
return scene;
},
/**
* 获取当前场景(可能为 null
*/
get sceneOrNull(): Scene | null {
return getInternalAPI().getScene();
},
/**
* 获取 EntityStoreService
*/
get entityStore(): EntityStoreService {
return getInternalAPI().getEntityStore();
},
/**
* 获取 MessageHub
*/
get messageHub(): MessageHub {
return getInternalAPI().getMessageHub();
},
/**
* 获取服务容器
*/
get services(): ServiceContainer {
return getInternalAPI().getCore().services;
},
/**
* 解析服务
* @param serviceType 服务类型
*/
resolve<T>(serviceType: any): T {
return getInternalAPI().resolveService<T>(serviceType);
},
/**
* 检查 API 是否可用
*/
get isAvailable(): boolean {
return !!window.__ESENGINE__?.api;
},
};