import { invoke } from '@tauri-apps/api/core'; /** * 文件过滤器定义 */ interface FileFilter { name: string; extensions: string[]; } /** * Tauri IPC 通信层 */ export class TauriAPI { static async openFolderDialog(title?: string): Promise { return await invoke('open_folder_dialog', { title }); } static async openFileDialog( title?: string, filters?: FileFilter[], multiple?: boolean ): Promise { return await invoke('open_file_dialog', { title, filters, multiple }); } static async saveFileDialog( title?: string, defaultName?: string, filters?: FileFilter[] ): Promise { return await invoke('save_file_dialog', { title, defaultName, filters }); } static async openProjectDialog(): Promise { return await this.openFolderDialog('Select Project Directory'); } static async openProject(path: string): Promise { return await invoke('open_project', { path }); } /** * 保存项目 */ static async saveProject(path: string, data: string): Promise { return await invoke('save_project', { path, data }); } /** * 导出二进制数据 */ static async exportBinary(data: Uint8Array, outputPath: string): Promise { return await invoke('export_binary', { data: Array.from(data), outputPath }); } /** * 扫描目录查找匹配模式的文件 */ static async scanDirectory(path: string, pattern: string): Promise { return await invoke('scan_directory', { path, pattern }); } /** * 读取文件内容 */ static async readFileContent(path: string): Promise { return await invoke('read_file_content', { path }); } /** * 列出目录内容 */ static async listDirectory(path: string): Promise { return await invoke('list_directory', { path }); } /** * 设置项目基础路径,用于 Custom Protocol */ static async setProjectBasePath(path: string): Promise { return await invoke('set_project_base_path', { path }); } /** * 切换开发者工具(仅在debug模式下可用) */ static async toggleDevtools(): Promise { return await invoke('toggle_devtools'); } /** * 打开保存场景对话框 * @param defaultName 默认文件名(可选) * @returns 用户选择的文件路径,取消则返回 null */ static async saveSceneDialog(defaultName?: string): Promise { return await this.saveFileDialog( 'Save ECS Scene', defaultName, [{ name: 'ECS Scene Files', extensions: ['ecs'] }] ); } /** * 打开场景文件选择对话框 * @returns 用户选择的文件路径,取消则返回 null */ static async openSceneDialog(): Promise { const result = await this.openFileDialog( 'Open ECS Scene', [{ name: 'ECS Scene Files', extensions: ['ecs'] }], false ); return result && result[0] ? result[0] : null; } /** * 创建目录 * @param path 目录路径 */ static async createDirectory(path: string): Promise { return await invoke('create_directory', { path }); } /** * 写入文件内容 * @param path 文件路径 * @param content 文件内容 */ static async writeFileContent(path: string, content: string): Promise { return await invoke('write_file_content', { path, content }); } /** * 检查路径是否存在 * @param path 文件或目录路径 * @returns 路径是否存在 */ static async pathExists(path: string): Promise { return await invoke('path_exists', { path }); } /** * 使用系统默认程序打开文件 * @param path 文件路径 */ static async openFileWithSystemApp(path: string): Promise { await invoke('open_file_with_default_app', { filePath: path }); } /** * 在文件管理器中显示文件 * @param path 文件路径 */ static async showInFolder(path: string): Promise { await invoke('show_in_folder', { filePath: path }); } /** * 打开行为树文件选择对话框 * @returns 用户选择的文件路径,取消则返回 null */ static async openBehaviorTreeDialog(): Promise { const result = await this.openFileDialog( 'Select Behavior Tree', [{ name: 'Behavior Tree Files', extensions: ['btree'] }], false ); return result && result[0] ? result[0] : null; } /** * 扫描项目中的所有行为树文件 * @param projectPath 项目路径 * @returns 行为树资产ID列表(相对于 .ecs/behaviors 的路 径,不含扩展名) */ static async scanBehaviorTrees(projectPath: string): Promise { return await invoke('scan_behavior_trees', { projectPath }); } /** * 重命名文件或文件夹 * @param oldPath 原路径 * @param newPath 新路径 */ static async renameFileOrFolder(oldPath: string, newPath: string): Promise { return await invoke('rename_file_or_folder', { oldPath, newPath }); } /** * 删除文件 * @param path 文件路径 */ static async deleteFile(path: string): Promise { return await invoke('delete_file', { path }); } /** * 删除文件夹 * @param path 文件夹路径 */ static async deleteFolder(path: string): Promise { return await invoke('delete_folder', { path }); } /** * 创建文件 * @param path 文件路径 */ static async createFile(path: string): Promise { return await invoke('create_file', { path }); } /** * 读取文件并转换为base64 * @param path 文件路径 * @returns base64编码的文件内容 */ static async readFileAsBase64(path: string): Promise { return await invoke('read_file_as_base64', { filePath: path }); } } export interface DirectoryEntry { name: string; path: string; is_dir: boolean; size?: number; modified?: number; } /** * 项目信息 */ export interface ProjectInfo { name: string; path: string; version: string; } /** * 编辑器配置 */ export interface EditorConfig { theme: string; autoSave: boolean; recentProjects: string[]; }