refactor(plugin): 重构插件系统架构,统一类型导入路径 (#296)

* refactor(plugin): 重构插件系统架构,统一类型导入路径

## 主要更改

### 新增 @esengine/plugin-types 包
- 提供打破循环依赖的最小类型定义
- 包含 ServiceToken, createServiceToken, PluginServiceRegistry, IEditorModuleBase

### engine-core 类型统一
- IPlugin<T> 泛型参数改为 T = unknown
- 所有运行时类型(IRuntimeModule, ModuleManifest, SystemContext)在此定义
- 新增 PluginServiceRegistry.ts 导出服务令牌相关类型

### editor-core 类型优化
- 重命名 IPluginLoader.ts 为 EditorModule.ts
- 新增 IEditorPlugin = IPlugin<IEditorModuleLoader> 类型别名
- 移除弃用别名 IPluginLoader, IRuntimeModuleLoader

### 编辑器插件更新
- 所有 9 个编辑器插件使用 IEditorPlugin 类型
- 统一从 editor-core 导入编辑器类型

### 服务令牌规范
- 各模块在 tokens.ts 中定义自己的服务接口和令牌
- 遵循"谁定义接口,谁导出 Token"原则

## 导入规范

| 场景 | 导入来源 |
|------|---------|
| 运行时模块 | @esengine/engine-core |
| 编辑器插件 | @esengine/editor-core |
| 服务令牌 | @esengine/engine-core |

* refactor(plugin): 完善服务令牌规范,统一运行时模块

## 更改内容

### 运行时模块优化
- particle: 使用 PluginServiceRegistry 获取依赖服务
- physics-rapier2d: 通过服务令牌注册/获取物理查询接口
- tilemap: 使用服务令牌获取物理系统依赖
- sprite: 导出服务令牌
- ui: 导出服务令牌
- behavior-tree: 使用服务令牌系统

### 资产系统增强
- IAssetManager 接口扩展
- 加载器使用服务令牌获取依赖

### 运行时核心
- GameRuntime 使用 PluginServiceRegistry
- 导出服务令牌相关类型

### 编辑器服务
- EngineService 适配新的服务令牌系统
- AssetRegistryService 优化

* fix: 修复 editor-app 和 behavior-tree-editor 中的类型引用

- editor-app/PluginLoader.ts: 使用 IPlugin 替代 IPluginLoader
- behavior-tree-editor: 使用 IEditorPlugin 替代 IPluginLoader

* fix(ui): 添加缺失的 ecs-engine-bindgen 依赖

UIRuntimeModule 使用 EngineBridgeToken,需要声明对 ecs-engine-bindgen 的依赖

* fix(type): 解决 ServiceToken 跨包类型兼容性问题

- 在 engine-core 中直接定义 ServiceToken 和 PluginServiceRegistry
  而不是从 plugin-types 重新导出,确保 tsup 生成的类型声明
  以 engine-core 作为类型来源
- 移除 RuntimeResolver.ts 中的硬编码模块 ID 检查,
  改用 module.json 中的 name 配置
- 修复 pnpm-lock.yaml 中的依赖记录

* refactor(arch): 改进架构设计,移除硬编码

- 统一类型导出:editor-core 从 engine-core 导入 IEditorModuleBase
- RuntimeResolver: 将硬编码路径改为配置常量和搜索路径列表
- 添加跨平台安装路径支持(Windows/macOS/Linux)
- 使用 ENGINE_WASM_CONFIG 配置引擎 WASM 文件信息
- IBundlePackOptions 添加 preloadBundles 配置项

* fix(particle): 添加缺失的 ecs-engine-bindgen 依赖

ParticleRuntimeModule 导入了 @esengine/ecs-engine-bindgen 的 tokens,
但 package.json 中未声明该依赖,导致 CI 构建失败。

* fix(physics-rapier2d): 移除不存在的 PhysicsSystemContext 导出

PhysicsRuntimeModule 中不存在该类型,导致 type-check 失败
This commit is contained in:
YHH
2025-12-08 21:10:57 +08:00
committed by GitHub
parent 2476379af1
commit c3b7250f85
86 changed files with 1813 additions and 551 deletions

View File

@@ -17,69 +17,27 @@ import { CapsuleCollider2DComponent } from './components/CapsuleCollider2DCompon
import { PolygonCollider2DComponent } from './components/PolygonCollider2DComponent';
import { Physics2DSystem } from './systems/Physics2DSystem';
import { Physics2DService } from './services/Physics2DService';
import {
Physics2DQueryToken,
Physics2DSystemToken,
Physics2DWorldToken,
PhysicsConfigToken,
type IPhysics2DQuery,
type PhysicsConfig
} from './tokens';
// 注册 Rapier2D 加载器
import './loaders';
/**
* 2D 物理查询接口
* 2D Physics query interface
*
* 用于粒子系统等模块查询物理世界
* Used by particle system and other modules to query physics world
*/
export interface IPhysics2DQuery {
overlapCircle(
center: { x: number; y: number },
radius: number,
collisionMask?: number
): { entityIds: number[]; colliderHandles: number[] };
raycast(
origin: { x: number; y: number },
direction: { x: number; y: number },
maxDistance: number,
collisionMask?: number
): {
entityId: number;
point: { x: number; y: number };
normal: { x: number; y: number };
distance: number;
colliderHandle: number;
} | null;
}
/**
* 物理系统上下文扩展
*/
export interface PhysicsSystemContext extends SystemContext {
/**
* 物理系统实例
*/
physicsSystem?: Physics2DSystem;
/**
* 物理世界实例
*/
physics2DWorld?: any;
/**
* 物理配置
*/
physicsConfig?: any;
/**
* 2D 物理查询接口
* 2D Physics query interface
*
* 供粒子系统等模块使用,用于检测粒子与物理碰撞体的碰撞。
* 实际上是 Physics2DSystem 的引用,因为它实现了 IPhysics2DQuery 接口。
*
* For particle system and other modules to detect collision with physics colliders.
* Actually a reference to Physics2DSystem which implements IPhysics2DQuery interface.
*/
physics2DQuery?: IPhysics2DQuery;
}
// 重新导出 tokens 和类型 | Re-export tokens and types
export {
Physics2DQueryToken,
Physics2DSystemToken,
Physics2DWorldToken,
PhysicsConfigToken,
type IPhysics2DQuery,
type PhysicsConfig
} from './tokens';
/**
* 物理运行时模块
@@ -167,10 +125,11 @@ class PhysicsRuntimeModule implements IRuntimeModule {
* @param context - 系统上下文
*/
createSystems(scene: IScene, context: SystemContext): void {
const physicsContext = context as PhysicsSystemContext;
// 从服务注册表获取配置 | Get config from service registry
const physicsConfig = context.services.get(PhysicsConfigToken);
const physicsSystem = new Physics2DSystem({
physics: physicsContext.physicsConfig,
physics: physicsConfig,
updateOrder: -1000
});
@@ -181,11 +140,10 @@ class PhysicsRuntimeModule implements IRuntimeModule {
physicsSystem.initializeWithRapier(this._rapierModule);
}
physicsContext.physicsSystem = physicsSystem;
physicsContext.physics2DWorld = physicsSystem.world;
// 同时暴露 physics2DQuery供粒子系统等模块使用
// Also expose physics2DQuery for particle system and other modules
physicsContext.physics2DQuery = physicsSystem;
// 注册服务 | Register services
context.services.register(Physics2DSystemToken, physicsSystem);
context.services.register(Physics2DWorldToken, physicsSystem.world);
context.services.register(Physics2DQueryToken, physicsSystem);
}
/**

View File

@@ -28,5 +28,13 @@ export { Physics2DPlugin } from './PhysicsEditorPlugin';
// Runtime plugin (for game builds)
export { PhysicsPlugin } from './PhysicsRuntimeModule';
// Physics query interface (for particle system integration)
export type { IPhysics2DQuery, PhysicsSystemContext } from './PhysicsRuntimeModule';
// Service tokens and interfaces (谁定义接口,谁导出 Token)
export {
Physics2DQueryToken,
Physics2DSystemToken,
Physics2DWorldToken,
PhysicsConfigToken,
type IPhysics2DQuery,
type IPhysics2DWorld,
type PhysicsConfig
} from './tokens';

View File

@@ -3,9 +3,6 @@
*
* This entry point exports only runtime-related code without any editor dependencies.
* Use this for standalone game runtime builds.
*
* 此入口点仅导出运行时相关代码,不包含任何编辑器依赖。
* 用于独立游戏运行时构建。
*/
// Types
@@ -53,4 +50,14 @@ export { Physics2DSystem, type Physics2DSystemConfig } from './systems/Physics2D
export { Physics2DService } from './services/Physics2DService';
// Runtime module and plugin
export { PhysicsRuntimeModule, PhysicsPlugin, type PhysicsSystemContext } from './PhysicsRuntimeModule';
export { PhysicsRuntimeModule, PhysicsPlugin } from './PhysicsRuntimeModule';
// Service tokens
export {
Physics2DQueryToken,
Physics2DSystemToken,
Physics2DWorldToken,
PhysicsConfigToken,
type IPhysics2DQuery,
type PhysicsConfig
} from './tokens';

View File

@@ -7,7 +7,7 @@
*/
// Re-export runtime module and plugin with WASM
export { PhysicsRuntimeModule, PhysicsPlugin, type PhysicsSystemContext } from '../PhysicsRuntimeModule';
export { PhysicsRuntimeModule, PhysicsPlugin } from '../PhysicsRuntimeModule';
// Re-export world and system (they have WASM type dependencies)
export { Physics2DWorld } from '../world/Physics2DWorld';

View File

@@ -0,0 +1,177 @@
/**
* 物理模块服务令牌
* Physics module service tokens
*
* 定义 physics-rapier2d 模块导出的服务令牌和接口。
* 谁定义接口,谁导出 Token。
*
* Defines service tokens and interfaces exported by physics-rapier2d module.
* Who defines the interface, who exports the Token.
*
* @example
* ```typescript
* // 消费方导入 Token | Consumer imports Token
* import { Physics2DQueryToken, type IPhysics2DQuery } from '@esengine/physics-rapier2d';
*
* // 获取服务 | Get service
* const physicsQuery = context.services.get(Physics2DQueryToken);
* if (physicsQuery) {
* physicsQuery.raycast(...);
* }
* ```
*/
import { createServiceToken } from '@esengine/engine-core';
import type { Physics2DSystem } from './systems/Physics2DSystem';
// ============================================================================
// 共享物理接口 | Shared Physics Interfaces
// ============================================================================
/**
* 2D 物理查询接口
* 2D Physics query interface
*
* 跨模块共享的物理查询契约。
* 由 Physics2DWorld 实现,粒子等模块可选依赖。
*
* Cross-module shared physics query contract.
* Implemented by Physics2DWorld, optionally depended by particle and other modules.
*/
export interface IPhysics2DQuery {
/**
* 圆形区域检测
* Circle overlap detection
*
* @param center 圆心 | Circle center
* @param radius 半径 | Radius
* @param collisionMask 碰撞掩码 | Collision mask
* @returns 检测结果 | Detection result
*/
overlapCircle(
center: { x: number; y: number },
radius: number,
collisionMask?: number
): { entityIds: number[]; colliderHandles: number[] };
/**
* 射线检测
* Raycast detection
*
* @param origin 起点 | Origin point
* @param direction 方向(归一化)| Direction (normalized)
* @param maxDistance 最大距离 | Max distance
* @param collisionMask 碰撞掩码 | Collision mask
* @returns 命中结果或 null | Hit result or null
*/
raycast(
origin: { x: number; y: number },
direction: { x: number; y: number },
maxDistance: number,
collisionMask?: number
): {
entityId: number;
point: { x: number; y: number };
normal: { x: number; y: number };
distance: number;
colliderHandle: number;
} | null;
}
/**
* 2D 物理世界接口
* 2D Physics world interface
*
* 跨模块共享的物理世界契约。
* 由 Physics2DWorld 实现tilemap 等模块可选依赖。
*
* Cross-module shared physics world contract.
* Implemented by Physics2DWorld, optionally depended by tilemap and other modules.
*/
export interface IPhysics2DWorld {
/**
* 创建静态碰撞体
* Create static collider
*
* @param entityId 实体 ID | Entity ID
* @param position 碰撞体中心位置 | Collider center position
* @param halfExtents 半宽高 | Half extents
* @param collisionLayer 碰撞层 | Collision layer
* @param collisionMask 碰撞掩码 | Collision mask
* @param friction 摩擦系数 | Friction coefficient
* @param restitution 弹性系数 | Restitution coefficient
* @param isTrigger 是否为触发器 | Whether is trigger
* @returns 碰撞体句柄或 null | Collider handle or null
*/
createStaticCollider(
entityId: number,
position: { x: number; y: number },
halfExtents: { x: number; y: number },
collisionLayer: number,
collisionMask: number,
friction: number,
restitution: number,
isTrigger: boolean
): number | null;
/**
* 移除碰撞体
* Remove collider
*
* @param handle 碰撞体句柄 | Collider handle
*/
removeCollider(handle: number): void;
}
// ============================================================================
// 物理模块特有的接口 | Physics module specific interfaces
// ============================================================================
/**
* 物理配置
* Physics configuration
*/
export interface PhysicsConfig {
gravity?: { x: number; y: number };
timestep?: number;
}
// ============================================================================
// 服务令牌 | Service Tokens
// ============================================================================
/**
* 2D 物理查询服务令牌
* 2D Physics query service token
*
* 用于获取物理查询能力(射线检测、区域检测等)。
* For getting physics query capabilities (raycast, overlap detection, etc.).
*/
export const Physics2DQueryToken = createServiceToken<IPhysics2DQuery>('physics2DQuery');
/**
* 2D 物理世界服务令牌
* 2D Physics world service token
*
* 用于获取物理世界实例(需要底层访问时使用)。
* For getting physics world instance (when low-level access is needed).
*/
export const Physics2DWorldToken = createServiceToken<IPhysics2DWorld>('physics2DWorld');
/**
* 物理系统令牌
* Physics system token
*
* 用于获取完整的物理系统实例。
* For getting the full physics system instance.
*/
export const Physics2DSystemToken = createServiceToken<Physics2DSystem>('physics2DSystem');
/**
* 物理配置令牌
* Physics config token
*
* 用于传入物理配置(如重力、时间步)。
* For passing physics configuration (gravity, timestep, etc.).
*/
export const PhysicsConfigToken = createServiceToken<PhysicsConfig>('physicsConfig');