diff --git a/common/BatchExec.ts b/common/BatchExec.ts index 94f899e..79fd329 100644 --- a/common/BatchExec.ts +++ b/common/BatchExec.ts @@ -5,53 +5,44 @@ import { BehaviorNodeModel, BehaviorTreeModel } from './BehaviorTreeModel'; export default (scriptPath: string, dirPath: string) => { - console.log("script", scriptPath); + console.log("run 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); + const files = fs.readdirSync(filePath); + files.forEach((filename) => { + var filedir = path.join(filePath, filename); + const stats = fs.statSync(filedir); + 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, tree); + 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); + console.log("run script", scriptPath, "done!"); } \ No newline at end of file diff --git a/package.json b/package.json index 76bb8d2..564efa5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "behavior3editor", - "version": "0.1.4", + "version": "0.1.5", "description": "行为树编辑器", "main": "./dist/main.js", "scripts": { diff --git a/sample/scripts/test.js b/sample/scripts/test.js index 76dfa25..7022fd3 100644 --- a/sample/scripts/test.js +++ b/sample/scripts/test.js @@ -5,12 +5,12 @@ */ ({ processTree: (tree) => { - console.log("processTree", tree.name); + console.log(`processTree ${tree.name}`); if (tree.name == "hero") { return tree; } }, - processNode: (node) => { - console.log("processNode", node.id) + processNode: (node, tree) => { + console.log(`processNode ${tree.name}.${node.id}`); } }) \ No newline at end of file