refactor(editor): 提取行为树编辑器为独立包并重构编辑器架构 (#216)

* refactor(editor): 提取行为树编辑器为独立包并重构编辑器架构

* feat(editor): 添加插件市场功能

* feat(editor): 重构插件市场以支持版本管理和ZIP打包

* feat(editor): 重构插件发布流程并修复React渲染警告

* fix(plugin): 修复插件发布和市场的路径不一致问题

* feat: 重构插件发布流程并添加插件删除功能

* fix(editor): 完善插件删除功能并修复多个关键问题

* fix(auth): 修复自动登录与手动登录的竞态条件问题

* feat(editor): 重构插件管理流程

* feat(editor): 支持 ZIP 文件直接发布插件

- 新增 PluginSourceParser 解析插件源
- 重构发布流程支持文件夹和 ZIP 两种方式
- 优化发布向导 UI

* feat(editor): 插件市场支持多版本安装

- 插件解压到项目 plugins 目录
- 新增 Tauri 后端安装/卸载命令
- 支持选择任意版本安装
- 修复打包逻辑,保留完整 dist 目录结构

* feat(editor): 个人中心支持多版本管理

- 合并同一插件的不同版本
- 添加版本历史展开/折叠功能
- 禁止有待审核 PR 时更新插件

* fix(editor): 修复 InspectorRegistry 服务注册

- InspectorRegistry 实现 IService 接口
- 注册到 Core.services 供插件使用

* feat(behavior-tree-editor): 完善插件注册和文件操作

- 添加文件创建模板和操作处理器
- 实现右键菜单创建行为树功能
- 修复文件读取权限问题(使用 Tauri 命令)
- 添加 BehaviorTreeEditorPanel 组件
- 修复 rollup 配置支持动态导入

* feat(plugin): 完善插件构建和发布流程

* fix(behavior-tree-editor): 完整恢复编辑器并修复 Toast 集成

* fix(behavior-tree-editor): 修复节点选中、连线跟随和文件加载问题并优化性能

* fix(behavior-tree-editor): 修复端口连接失败问题并优化连线样式

* refactor(behavior-tree-editor): 移除调试面板功能简化代码结构

* refactor(behavior-tree-editor): 清理冗余代码合并重复逻辑

* feat(behavior-tree-editor): 完善编辑器核心功能增强扩展性

* fix(lint): 修复ESLint错误确保CI通过

* refactor(behavior-tree-editor): 优化编辑器工具栏和编译器功能

* refactor(behavior-tree-editor): 清理技术债务,优化代码质量

* fix(editor-app): 修复字符串替换安全问题
This commit is contained in:
YHH
2025-11-18 14:46:51 +08:00
committed by GitHub
parent eac660b1a0
commit bce3a6e253
251 changed files with 26144 additions and 8844 deletions

View File

@@ -1,532 +0,0 @@
import { World, Entity, Scene, createLogger, Time, Core } from '@esengine/ecs-framework';
import {
BehaviorTreeData,
BehaviorNodeData,
BehaviorTreeRuntimeComponent,
BehaviorTreeAssetManager,
BehaviorTreeExecutionSystem,
TaskStatus,
NodeType
} from '@esengine/behavior-tree';
import type { BehaviorTreeNode } from '../stores/behaviorTreeStore';
const logger = createLogger('BehaviorTreeExecutor');
export interface ExecutionStatus {
nodeId: string;
status: 'running' | 'success' | 'failure' | 'idle';
message?: string;
executionOrder?: number;
}
export interface ExecutionLog {
timestamp: number;
message: string;
level: 'info' | 'success' | 'error' | 'warning';
nodeId?: string;
}
export type ExecutionCallback = (
statuses: ExecutionStatus[],
logs: ExecutionLog[],
blackboardVariables?: Record<string, any>
) => void;
/**
* 行为树执行器
*
* 使用新的Runtime架构执行行为树
*/
export class BehaviorTreeExecutor {
private world: World;
private scene: Scene;
private entity: Entity | null = null;
private runtime: BehaviorTreeRuntimeComponent | null = null;
private treeData: BehaviorTreeData | null = null;
private callback: ExecutionCallback | null = null;
private isRunning = false;
private isPaused = false;
private executionLogs: ExecutionLog[] = [];
private lastStatuses: Map<string, 'running' | 'success' | 'failure' | 'idle'> = new Map();
private persistentStatuses: Map<string, 'running' | 'success' | 'failure' | 'idle'> = new Map();
private executionOrders: Map<string, number> = new Map();
private tickCount = 0;
private nodeIdMap: Map<string, string> = new Map();
private blackboardKeys: string[] = [];
private rootNodeId: string = '';
private assetManager: BehaviorTreeAssetManager;
private executionSystem: BehaviorTreeExecutionSystem;
constructor() {
this.world = new World({ name: 'BehaviorTreeWorld' });
this.scene = this.world.createScene('BehaviorTreeScene');
// 尝试获取已存在的 assetManager如果不存在则创建新的
try {
this.assetManager = Core.services.resolve(BehaviorTreeAssetManager);
} catch {
this.assetManager = new BehaviorTreeAssetManager();
Core.services.registerInstance(BehaviorTreeAssetManager, this.assetManager);
}
this.executionSystem = new BehaviorTreeExecutionSystem();
this.scene.addSystem(this.executionSystem);
}
/**
* 从编辑器节点构建行为树数据
*/
buildTree(
nodes: BehaviorTreeNode[],
rootNodeId: string,
blackboard: Record<string, any>,
connections: Array<{ from: string; to: string; fromProperty?: string; toProperty?: string; connectionType: 'node' | 'property' }>,
callback: ExecutionCallback
): void {
this.cleanup();
this.callback = callback;
this.treeData = this.convertToTreeData(nodes, rootNodeId, blackboard, connections);
this.rootNodeId = this.treeData.rootNodeId;
this.assetManager.loadAsset(this.treeData);
this.entity = this.scene.createEntity('BehaviorTreeEntity');
this.runtime = new BehaviorTreeRuntimeComponent();
// 在添加组件之前设置资产ID和autoStart
this.runtime.treeAssetId = this.treeData.id;
this.runtime.autoStart = false;
this.entity.addComponent(this.runtime);
if (this.treeData.blackboardVariables) {
this.blackboardKeys = Array.from(this.treeData.blackboardVariables.keys());
for (const [key, value] of this.treeData.blackboardVariables.entries()) {
this.runtime.setBlackboardValue(key, value);
}
} else {
this.blackboardKeys = [];
}
this.addLog('行为树构建完成', 'info');
}
/**
* 将编辑器节点转换为BehaviorTreeData
*/
private convertToTreeData(
nodes: BehaviorTreeNode[],
rootNodeId: string,
blackboard: Record<string, any>,
connections: Array<{ from: string; to: string; fromProperty?: string; toProperty?: string; connectionType: 'node' | 'property' }>
): BehaviorTreeData {
const rootNode = nodes.find((n) => n.id === rootNodeId);
if (!rootNode) {
throw new Error('未找到根节点');
}
// 如果根节点是编辑器特有的"根节点"且只有一个子节点,使用第一个子节点作为实际根节点
let actualRootId = rootNodeId;
if (rootNode.template.displayName === '根节点' && rootNode.children.length === 1) {
actualRootId = rootNode.children[0]!;
}
const treeData: BehaviorTreeData = {
id: `tree_${Date.now()}`,
name: 'EditorTree',
rootNodeId: actualRootId,
nodes: new Map(),
blackboardVariables: new Map()
};
this.nodeIdMap.clear();
for (const node of nodes) {
// 跳过编辑器的虚拟根节点
if (node.id === rootNodeId && node.template.displayName === '根节点' && rootNode.children.length === 1) {
continue;
}
this.nodeIdMap.set(node.id, node.id);
const nodeData: BehaviorNodeData = {
id: node.id,
name: node.template.displayName,
nodeType: this.convertNodeType(node.template.type),
implementationType: node.template.className || this.getImplementationType(node.template.displayName),
config: { ...node.data },
children: Array.from(node.children)
};
treeData.nodes.set(node.id, nodeData);
}
// 处理属性连接,转换为 bindings
for (const conn of connections) {
if (conn.connectionType === 'property' && conn.toProperty) {
const targetNodeData = treeData.nodes.get(conn.to);
const sourceNode = nodes.find((n) => n.id === conn.from);
if (targetNodeData && sourceNode) {
// 检查源节点是否是黑板变量节点
if (sourceNode.data.nodeType === 'blackboard-variable') {
// 从黑板变量节点获取实际的变量名
const variableName = sourceNode.data.variableName as string;
if (variableName) {
// 初始化 bindings 如果不存在
if (!targetNodeData.bindings) {
targetNodeData.bindings = {};
}
// 添加绑定:属性名 -> 黑板变量名
targetNodeData.bindings[conn.toProperty] = variableName;
}
}
}
}
}
for (const [key, value] of Object.entries(blackboard)) {
treeData.blackboardVariables!.set(key, value);
}
return treeData;
}
/**
* 转换节点类型
*/
private convertNodeType(type: string): NodeType {
if (type === NodeType.Composite) return NodeType.Composite;
if (type === NodeType.Decorator) return NodeType.Decorator;
if (type === NodeType.Action) return NodeType.Action;
if (type === NodeType.Condition) return NodeType.Condition;
return NodeType.Action;
}
/**
* 根据显示名称获取实现类型
*/
private getImplementationType(displayName: string): string {
const typeMap: Record<string, string> = {
'序列': 'Sequence',
'选择': 'Selector',
'并行': 'Parallel',
'并行选择': 'ParallelSelector',
'随机序列': 'RandomSequence',
'随机选择': 'RandomSelector',
'反转': 'Inverter',
'重复': 'Repeater',
'直到成功': 'UntilSuccess',
'直到失败': 'UntilFail',
'总是成功': 'AlwaysSucceed',
'总是失败': 'AlwaysFail',
'条件装饰器': 'Conditional',
'冷却': 'Cooldown',
'超时': 'Timeout',
'等待': 'Wait',
'日志': 'Log',
'设置变量': 'SetBlackboardValue',
'修改变量': 'ModifyBlackboardValue',
'自定义动作': 'ExecuteAction',
'比较变量': 'BlackboardCompare',
'变量存在': 'BlackboardExists',
'随机概率': 'RandomProbability',
'执行条件': 'ExecuteCondition'
};
return typeMap[displayName] || displayName;
}
/**
* 开始执行
*/
start(): void {
if (!this.runtime || !this.treeData) {
logger.error('未构建行为树');
return;
}
this.isRunning = true;
this.isPaused = false;
this.executionLogs = [];
this.lastStatuses.clear();
this.persistentStatuses.clear();
this.tickCount = 0;
this.runtime.resetAllStates();
this.runtime.isRunning = true;
this.addLog('开始执行行为树', 'info');
}
/**
* 暂停执行
*/
pause(): void {
this.isPaused = true;
if (this.runtime) {
this.runtime.isRunning = false;
}
}
/**
* 恢复执行
*/
resume(): void {
this.isPaused = false;
if (this.runtime) {
this.runtime.isRunning = true;
}
}
/**
* 停止执行
*/
stop(): void {
this.isRunning = false;
this.isPaused = false;
if (this.runtime) {
this.runtime.isRunning = false;
this.runtime.resetAllStates();
}
this.addLog('行为树已停止', 'info');
}
/**
* 执行一帧
*/
tick(deltaTime: number): void {
if (!this.isRunning || this.isPaused || !this.runtime) {
return;
}
Time.update(deltaTime);
this.tickCount++;
this.scene.update();
this.collectExecutionStatus();
}
/**
* 收集所有节点的执行状态
*/
private collectExecutionStatus(): void {
if (!this.callback || !this.runtime || !this.treeData) return;
const rootState = this.runtime.getNodeState(this.rootNodeId);
let rootCurrentStatus: 'running' | 'success' | 'failure' | 'idle' = 'idle';
if (rootState) {
switch (rootState.status) {
case TaskStatus.Running:
rootCurrentStatus = 'running';
break;
case TaskStatus.Success:
rootCurrentStatus = 'success';
break;
case TaskStatus.Failure:
rootCurrentStatus = 'failure';
break;
default:
rootCurrentStatus = 'idle';
}
}
const rootLastStatus = this.lastStatuses.get(this.rootNodeId);
if (rootLastStatus &&
(rootLastStatus === 'success' || rootLastStatus === 'failure') &&
rootCurrentStatus === 'running') {
this.persistentStatuses.clear();
this.executionOrders.clear();
}
const statuses: ExecutionStatus[] = [];
for (const [nodeId, nodeData] of this.treeData.nodes.entries()) {
const state = this.runtime.getNodeState(nodeId);
let currentStatus: 'running' | 'success' | 'failure' | 'idle' = 'idle';
if (state) {
switch (state.status) {
case TaskStatus.Success:
currentStatus = 'success';
break;
case TaskStatus.Failure:
currentStatus = 'failure';
break;
case TaskStatus.Running:
currentStatus = 'running';
break;
default:
currentStatus = 'idle';
}
}
const persistentStatus = this.persistentStatuses.get(nodeId) || 'idle';
const lastStatus = this.lastStatuses.get(nodeId);
let displayStatus: 'running' | 'success' | 'failure' | 'idle' = currentStatus;
if (currentStatus === 'running') {
displayStatus = 'running';
this.persistentStatuses.set(nodeId, 'running');
} else if (currentStatus === 'success') {
displayStatus = 'success';
this.persistentStatuses.set(nodeId, 'success');
} else if (currentStatus === 'failure') {
displayStatus = 'failure';
this.persistentStatuses.set(nodeId, 'failure');
} else if (currentStatus === 'idle') {
if (persistentStatus !== 'idle') {
displayStatus = persistentStatus;
} else if (this.executionOrders.has(nodeId)) {
displayStatus = 'success';
this.persistentStatuses.set(nodeId, 'success');
} else {
displayStatus = 'idle';
}
}
// 检测状态变化
const hasStateChanged = lastStatus !== currentStatus;
// 从运行时状态读取执行顺序
if (state?.executionOrder !== undefined && !this.executionOrders.has(nodeId)) {
this.executionOrders.set(nodeId, state.executionOrder);
console.log(`[ExecutionOrder READ] ${nodeData.name} | ID: ${nodeId} | Order: ${state.executionOrder}`);
}
// 记录状态变化日志
if (hasStateChanged && currentStatus !== 'idle') {
this.onNodeStatusChanged(nodeId, nodeData.name, lastStatus || 'idle', currentStatus);
}
this.lastStatuses.set(nodeId, currentStatus);
statuses.push({
nodeId,
status: displayStatus,
executionOrder: this.executionOrders.get(nodeId)
});
}
const currentBlackboardVars = this.getBlackboardVariables();
this.callback(statuses, this.executionLogs, currentBlackboardVars);
}
/**
* 节点状态变化时记录日志
*/
private onNodeStatusChanged(
nodeId: string,
nodeName: string,
oldStatus: string,
newStatus: string
): void {
if (newStatus === 'running') {
this.addLog(`[${nodeName}](${nodeId}) 开始执行`, 'info', nodeId);
} else if (newStatus === 'success') {
this.addLog(`[${nodeName}](${nodeId}) 执行成功`, 'success', nodeId);
} else if (newStatus === 'failure') {
this.addLog(`[${nodeName}](${nodeId}) 执行失败`, 'error', nodeId);
}
}
/**
* 添加日志
*/
private addLog(message: string, level: 'info' | 'success' | 'error' | 'warning', nodeId?: string): void {
this.executionLogs.push({
timestamp: Date.now(),
message,
level,
nodeId
});
if (this.executionLogs.length > 1000) {
this.executionLogs.shift();
}
}
/**
* 获取当前tick计数
*/
getTickCount(): number {
return this.tickCount;
}
/**
* 获取黑板变量
*/
getBlackboardVariables(): Record<string, any> {
if (!this.runtime) return {};
const variables: Record<string, any> = {};
for (const name of this.blackboardKeys) {
variables[name] = this.runtime.getBlackboardValue(name);
}
return variables;
}
/**
* 更新黑板变量
*/
updateBlackboardVariable(key: string, value: any): void {
if (!this.runtime) {
logger.warn('无法更新黑板变量:未构建行为树');
return;
}
this.runtime.setBlackboardValue(key, value);
logger.info(`黑板变量已更新: ${key} = ${JSON.stringify(value)}`);
}
/**
* 清理资源
*/
cleanup(): void {
this.stop();
this.nodeIdMap.clear();
this.lastStatuses.clear();
this.persistentStatuses.clear();
this.blackboardKeys = [];
if (this.entity) {
this.entity.destroy();
this.entity = null;
}
// 卸载旧的行为树资产
if (this.treeData) {
this.assetManager.unloadAsset(this.treeData.id);
}
this.runtime = null;
this.treeData = null;
}
/**
* 检查节点的执行器是否存在
*/
hasExecutor(implementationType: string): boolean {
const registry = this.executionSystem.getExecutorRegistry();
return registry.has(implementationType);
}
/**
* 销毁
*/
destroy(): void {
this.cleanup();
}
}