diff --git a/README.md b/README.md index c602797..b7964e4 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ + 行为树->新建 ## 示例项目 -+ 工作区: sample/sample-workspace.json -+ 节点定义: sample/sample-node-config.json -+ 行为树目录: sample/sample-workdir ++ 工作区: sample/workspace.json ++ 节点定义: sample/node-config.json ++ 行为树目录: sample/workdir ++ 批处理脚本: sample/scripts Tips: workspace.json也可以手动编辑,加上isRelative可以让配置中的路径变成相对路径,这样就不需要团队中每个人在使用前都必须先创建工作区 @@ -44,9 +45,9 @@ npm run dist # 编译exe可执行文件 ## 技术栈 + react + ts -+ g6 -+ antd + electron ++ antd ++ g6 ## 示例行为树框架 + lua版本 [behavior3lua](https://github.com/zhandouxiaojiji/behavior3lua) @@ -54,6 +55,9 @@ npm run dist # 编译exe可执行文件 ## TODO + 右键菜单 ++ 面板拖拽新建节点 ++ 未保存的行为树标星号 ++ 异步加载界面 ## About 目前编辑器还处于非常简陋的阶段,有问题可以联系作者(QQ1013299930),本项目将长期维护,望前端大佬们多提点~ \ No newline at end of file diff --git a/common/BatchExec.ts b/common/BatchExec.ts new file mode 100644 index 0000000..94f899e --- /dev/null +++ b/common/BatchExec.ts @@ -0,0 +1,57 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { BehaviorNodeModel, BehaviorTreeModel } from './BehaviorTreeModel'; + + + +export default (scriptPath: string, dirPath: string) => { + console.log("script", scriptPath); + const str = fs.readFileSync(scriptPath, "utf8"); + const script = eval(str); + const batchExec = (filePath: string) => { + fs.readdir(filePath, (err, files) => { + if (err) { + console.warn(err) + } else { + files.forEach((filename) => { + var filedir = path.join(filePath, filename); + fs.stat(filedir, (err, stats) => { + if (err) { + console.warn('获取文件stats失败', err); + } else { + if (stats.isFile() && filename.match(/.\.json$/)) { + const str = fs.readFileSync(filedir, "utf8"); + let tree: BehaviorTreeModel = JSON.parse(str); + if(script.processTree) { + tree = script.processTree(tree); + } + if(tree && script.processNode) { + const processNode = (node: BehaviorNodeModel) => { + script.processNode(node); + if(node.children) { + for(let child of node.children) { + processNode(child); + } + } + } + processNode(tree.root); + } + if (tree) { + fs.writeFileSync( + filedir, + JSON.stringify(tree) + ); + } + + } + if (stats.isDirectory()) { + batchExec(filedir); + } + } + }) + }); + } + }); + } + batchExec(dirPath); +} \ No newline at end of file diff --git a/common/MainEventType.ts b/common/MainEventType.ts index 55865fa..e8b7481 100644 --- a/common/MainEventType.ts +++ b/common/MainEventType.ts @@ -12,4 +12,5 @@ export default { RELOAD_SERVER: "RELOAD_SERVER", UNDO: "UNDO", REDO: "REDO", + BATCH_EXEC: "BATCH_EXEC", }; diff --git a/main-process/AppMenu.ts b/main-process/AppMenu.ts index 2e2bae3..0736959 100644 --- a/main-process/AppMenu.ts +++ b/main-process/AppMenu.ts @@ -344,10 +344,6 @@ export default class AppMenu { return new MenuItem({ label: "开发工具", submenu: [ - { - label: "联调服务器", - submenu: serverItems, - }, { label: "热更", accelerator: "Ctrl+R", @@ -355,6 +351,24 @@ export default class AppMenu { this.webContents.send(MainEventType.RELOAD_SERVER); } }, + { + label: "联调服务器", + submenu: serverItems, + }, + { + label: "处理脚本", + click: () => { + (async () => { + const res = await dialog.showOpenDialog({ + properties: ["openFile"], + filters: [{ name: "Javascript", extensions: ["js"] }], + }); + if (res.filePaths.length > 0) { + this.webContents.send(MainEventType.BATCH_EXEC, res.filePaths[0]); + } + })(); + } + }, { type: 'separator', }, diff --git a/render-process/index.tsx b/render-process/index.tsx index 3e4ac8c..fc174ce 100644 --- a/render-process/index.tsx +++ b/render-process/index.tsx @@ -12,6 +12,7 @@ import "./index.css"; import RegisterNode from "./RegisterNode"; import TreeTabs from "./TreeTabs"; import Explorer from "./Explorer"; +import BatchExec from "../common/BatchExec"; const { Sider, Content } = Layout; @@ -63,6 +64,10 @@ export default class Main extends Component { message.success("服务器已更新"); }) + ipcRenderer.on(MainEventType.BATCH_EXEC, (event: any, path: string)=>{ + BatchExec(path, this.state.workdir); + }) + console.log("workdir", this.settings.curWorkspace.getWorkdir()); this.setState({ workdir: this.settings.curWorkspace.getWorkdir(), diff --git a/sample/scripts/test.js b/sample/scripts/test.js new file mode 100644 index 0000000..76dfa25 --- /dev/null +++ b/sample/scripts/test.js @@ -0,0 +1,16 @@ +/* 批处理脚本 */ +/* + 如果有processTree,则先运行prcessTree,如果有返回,则进行后面的针对每个节点运行processNode + 如果没有定义processTree,则直接针对每个节点运行processNode +*/ +({ + processTree: (tree) => { + console.log("processTree", tree.name); + if (tree.name == "hero") { + return tree; + } + }, + processNode: (node) => { + console.log("processNode", node.id) + } +}) \ No newline at end of file