Files
esengine/packages/behavior-tree-editor/src/services/FileSystemService.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

91 lines
3.0 KiB
TypeScript

import { singleton, invoke, type IService } from '@esengine/editor-runtime';
/**
* 文件系统服务
* 封装所有文件读写操作,使用通用后端命令
*/
@singleton()
export class FileSystemService implements IService {
/**
* 读取行为树文件
*/
async readBehaviorTreeFile(filePath: string): Promise<string> {
try {
return await invoke<string>('read_file_content', { path: filePath });
} catch (error) {
throw new Error(`Failed to read file ${filePath}: ${error}`);
}
}
/**
* 写入行为树文件
*/
async writeBehaviorTreeFile(filePath: string, content: string): Promise<void> {
try {
await invoke('write_file_content', { path: filePath, content });
} catch (error) {
throw new Error(`Failed to write file ${filePath}: ${error}`);
}
}
/**
* 读取全局黑板配置
* 业务逻辑在前端,后端只提供通用文件操作
*/
async readGlobalBlackboard(projectPath: string): Promise<string> {
try {
const configPath = `${projectPath}/.ecs/global-blackboard.json`;
const exists = await invoke<boolean>('path_exists', { path: configPath });
if (!exists) {
return JSON.stringify({ version: '1.0', variables: [] });
}
return await invoke<string>('read_file_content', { path: configPath });
} catch (error) {
throw new Error(`Failed to read global blackboard: ${error}`);
}
}
/**
* 写入全局黑板配置
* 业务逻辑在前端,后端只提供通用文件操作
*/
async writeGlobalBlackboard(projectPath: string, content: string): Promise<void> {
try {
const ecsDir = `${projectPath}/.ecs`;
const configPath = `${ecsDir}/global-blackboard.json`;
// 创建 .ecs 目录(如果不存在)
const dirExists = await invoke<boolean>('path_exists', { path: ecsDir });
if (!dirExists) {
await invoke('create_directory', { path: ecsDir });
}
await invoke('write_file_content', { path: configPath, content });
} catch (error) {
throw new Error(`Failed to write global blackboard: ${error}`);
}
}
async writeTextFile(filePath: string, content: string): Promise<void> {
try {
await invoke('write_file_content', { path: filePath, content });
} catch (error) {
throw new Error(`Failed to write text file ${filePath}: ${error}`);
}
}
async writeBinaryFile(filePath: string, data: Uint8Array): Promise<void> {
try {
await invoke('write_binary_file', { filePath, content: Array.from(data) });
} catch (error) {
throw new Error(`Failed to write binary file ${filePath}: ${error}`);
}
}
dispose(): void {
// 文件系统服务无需清理资源
}
}