Files
esengine/packages/behavior-tree-editor/src/services/BehaviorTreeService.ts
YHH b42a7b4e43 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 构建错误并优化构建性能
2025-12-01 22:28:51 +08:00

98 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
singleton,
type IService,
createLogger,
MessageHub,
IMessageHub,
} from '@esengine/editor-runtime';
import { useBehaviorTreeDataStore } from '../application/state/BehaviorTreeDataStore';
import type { BehaviorTree } from '../domain/models/BehaviorTree';
import { FileSystemService } from './FileSystemService';
import { PluginContext } from '../PluginContext';
const logger = createLogger('BehaviorTreeService');
@singleton()
export class BehaviorTreeService implements IService {
async createNew(): Promise<void> {
useBehaviorTreeDataStore.getState().reset();
}
async loadFromFile(filePath: string): Promise<void> {
try {
const services = PluginContext.getServices();
// 运行时解析 FileSystemService
const fileSystem = services.resolve(FileSystemService);
if (!fileSystem) {
throw new Error('FileSystemService not found. Please ensure the BehaviorTreePlugin is properly installed.');
}
const content = await fileSystem.readBehaviorTreeFile(filePath);
const fileName = filePath.split(/[\\/]/).pop()?.replace('.btree', '') || 'Untitled';
const store = useBehaviorTreeDataStore.getState();
store.importFromJSON(content);
// 在 store 中保存文件信息Panel 挂载时读取
store.setCurrentFile(filePath, fileName);
const messageHub = services.resolve<MessageHub>(IMessageHub);
if (messageHub) {
messageHub.publish('dynamic-panel:open', {
panelId: 'behavior-tree-editor',
title: `Behavior Tree - ${filePath.split(/[\\/]/).pop()}`
});
// 保留事件发布,以防 Panel 已挂载
messageHub.publish('behavior-tree:file-opened', {
filePath,
fileName
});
}
} catch (error) {
logger.error('Failed to load tree:', error);
throw error;
}
}
async saveToFile(filePath: string, metadata?: { name: string; description: string }): Promise<void> {
try {
const services = PluginContext.getServices();
// 运行时解析 FileSystemService
const fileSystem = services.resolve(FileSystemService);
if (!fileSystem) {
throw new Error('FileSystemService not found. Please ensure the BehaviorTreePlugin is properly installed.');
}
const store = useBehaviorTreeDataStore.getState();
// 如果没有提供元数据,使用文件名作为默认名称
const defaultMetadata = {
name: metadata?.name || filePath.split(/[\\/]/).pop()?.replace('.btree', '') || 'Untitled',
description: metadata?.description || ''
};
const content = store.exportToJSON(defaultMetadata);
await fileSystem.writeBehaviorTreeFile(filePath, content);
logger.info('Tree saved successfully:', filePath);
} catch (error) {
logger.error('Failed to save tree:', error);
throw error;
}
}
getCurrentTree(): BehaviorTree {
return useBehaviorTreeDataStore.getState().tree;
}
setTree(tree: BehaviorTree): void {
useBehaviorTreeDataStore.getState().setTree(tree);
}
dispose(): void {
useBehaviorTreeDataStore.getState().reset();
}
}