feat: export all trees to single json file support
This commit is contained in:
1 parent
f8cefaec6b
commit
637fd4a6f8
2 files changed
+473
-412
No files matched your search
+117
-86
@@ -1,6 +1,7 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { BehaviorNodeTypeModel } from '../common/BehaviorTreeModel';
|
||||
import * as fs from "fs";
|
||||
import { BehaviorNodeTypeModel } from "../common/BehaviorTreeModel";
|
||||
import * as glob from "glob";
|
||||
import * as path from "path";
|
||||
|
||||
export interface WorkspaceModel {
|
||||
isRelative?: boolean;
|
||||
@@ -9,98 +10,128 @@ export interface WorkspaceModel {
|
||||
}
|
||||
|
||||
const unknowNodeType: BehaviorNodeTypeModel = {
|
||||
name: 'unknow',
|
||||
desc: '新建节点',
|
||||
type: 'Action',
|
||||
}
|
||||
name: "unknow",
|
||||
desc: "新建节点",
|
||||
type: "Action",
|
||||
};
|
||||
|
||||
export default class Workspace {
|
||||
private filepath: string;
|
||||
private nodeConfPath: string;
|
||||
private workdir: string;
|
||||
private filepath: string;
|
||||
private nodeConfPath: string;
|
||||
private workdir: string;
|
||||
|
||||
private name2conf: { [name: string]: BehaviorNodeTypeModel } = {};
|
||||
private types: BehaviorNodeTypeModel[] = [];
|
||||
private name2conf: { [name: string]: BehaviorNodeTypeModel } = {};
|
||||
private types: BehaviorNodeTypeModel[] = [];
|
||||
|
||||
constructor(filepath: string) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
load() {
|
||||
if (!this.filepath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const str = fs.readFileSync(this.filepath, 'utf8');
|
||||
const model = JSON.parse(str) as WorkspaceModel;
|
||||
if (model.isRelative) {
|
||||
const root = path.dirname(this.filepath);
|
||||
this.nodeConfPath = root + '/' + model.nodeConfPath;
|
||||
this.workdir = root + '/' + model.workdir;
|
||||
} else {
|
||||
this.nodeConfPath = model.nodeConfPath;
|
||||
this.workdir = model.workdir;
|
||||
}
|
||||
|
||||
this.initNodeConf();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
private initNodeConf() {
|
||||
if (!this.nodeConfPath) {
|
||||
return;
|
||||
constructor(filepath: string) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
const types: BehaviorNodeTypeModel[] = JSON.parse(fs.readFileSync(this.nodeConfPath, 'utf8'));
|
||||
this.name2conf = {};
|
||||
types.forEach(t => {
|
||||
this.name2conf[t.name] = t;
|
||||
})
|
||||
this.types = types;
|
||||
}
|
||||
|
||||
getNodeConfPath() {
|
||||
return this.nodeConfPath;
|
||||
}
|
||||
setNodeConfPath(path: string) {
|
||||
this.nodeConfPath = path;
|
||||
this.initNodeConf();
|
||||
}
|
||||
getWorkdir() {
|
||||
return this.workdir;
|
||||
}
|
||||
setWorkdir(workdir: string) {
|
||||
this.workdir = workdir;
|
||||
}
|
||||
|
||||
save(filepath?: string) {
|
||||
if (filepath) {
|
||||
this.filepath = filepath;
|
||||
load() {
|
||||
if (!this.filepath) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const str = fs.readFileSync(this.filepath, 'utf8');
|
||||
const model = JSON.parse(str) as WorkspaceModel;
|
||||
if (model.isRelative) {
|
||||
const root = path.dirname(this.filepath);
|
||||
this.nodeConfPath = root + '/' + model.nodeConfPath;
|
||||
this.workdir = root + '/' + model.workdir;
|
||||
} else {
|
||||
this.nodeConfPath = model.nodeConfPath;
|
||||
this.workdir = model.workdir;
|
||||
}
|
||||
|
||||
this.initNodeConf();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
if (!this.filepath) {
|
||||
return;
|
||||
|
||||
private initNodeConf() {
|
||||
if (!this.nodeConfPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
const types: BehaviorNodeTypeModel[] = JSON.parse(fs.readFileSync(this.nodeConfPath, 'utf8'));
|
||||
this.name2conf = {};
|
||||
types.forEach(t => {
|
||||
this.name2conf[t.name] = t;
|
||||
})
|
||||
this.types = types;
|
||||
}
|
||||
fs.writeFileSync(this.filepath, JSON.stringify({
|
||||
nodeConfPath: this.nodeConfPath,
|
||||
workdir: this.workdir,
|
||||
}, null, 2))
|
||||
}
|
||||
|
||||
get nodeConf() {
|
||||
return this.types;
|
||||
}
|
||||
getNodeConfPath() {
|
||||
return this.nodeConfPath;
|
||||
}
|
||||
setNodeConfPath(path: string) {
|
||||
this.nodeConfPath = path;
|
||||
this.initNodeConf();
|
||||
}
|
||||
getWorkdir(): string {
|
||||
return this.workdir;
|
||||
}
|
||||
setWorkdir(workdir: string) {
|
||||
this.workdir = workdir;
|
||||
}
|
||||
|
||||
getFilepath() {
|
||||
return this.filepath;
|
||||
}
|
||||
writeAllTrees(outFilePath: string, cb?: (err: string) => void) {
|
||||
if (!fs.existsSync(this.getWorkdir())) {
|
||||
cb && cb("请先打开工作区");
|
||||
return;
|
||||
}
|
||||
glob.glob(path.join(this.getWorkdir(), "**/*.json"), function (err, files) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
cb && cb(err.message);
|
||||
return;
|
||||
}
|
||||
|
||||
setFilepath(filepath: string) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
let result = new Array();
|
||||
files.forEach((file) => {
|
||||
const data = fs.readFileSync(file, "utf8");
|
||||
result.push(JSON.parse(data));
|
||||
});
|
||||
|
||||
getNodeConf(name: string) {
|
||||
return this.name2conf[name] || unknowNodeType;
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(outFilePath, JSON.stringify(result, null, 2));
|
||||
cb && cb(null);
|
||||
});
|
||||
}
|
||||
|
||||
save(filepath?: string) {
|
||||
if (filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
if (!this.filepath) {
|
||||
return;
|
||||
}
|
||||
fs.writeFileSync(
|
||||
this.filepath,
|
||||
JSON.stringify(
|
||||
{
|
||||
nodeConfPath: this.nodeConfPath,
|
||||
workdir: this.workdir,
|
||||
},
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
get nodeConf() {
|
||||
return this.types;
|
||||
}
|
||||
|
||||
getFilepath() {
|
||||
return this.filepath;
|
||||
}
|
||||
|
||||
setFilepath(filepath: string) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
getNodeConf(name: string) {
|
||||
return this.name2conf[name] || unknowNodeType;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user