From 4d1d09fe809617327f22bc6a8b93fcbe135f091b Mon Sep 17 00:00:00 2001 From: codetypes Date: Tue, 18 Mar 2025 10:58:17 +0800 Subject: [PATCH] use 'readTree' to parse data file --- public/locales/en.json | 1 + public/locales/zh.json | 1 + sample/node-config.b3-setting | 1 + sample/vars/test-vars.json | 13 +++++++++++++ sample/workdir/monster.json | 11 ++++++----- sample/workdir/subtree1.json | 8 ++++++-- src/components/editor.tsx | 2 +- src/components/inspector.tsx | 20 +++++++++++++++----- src/components/register-node.ts | 14 +++++++------- src/contexts/workspace-context.ts | 9 ++------- src/misc/b3util.ts | 3 +-- src/misc/util.ts | 7 ++++++- 12 files changed, 60 insertions(+), 30 deletions(-) diff --git a/public/locales/en.json b/public/locales/en.json index 7137bd0..d17263d 100644 --- a/public/locales/en.json +++ b/public/locales/en.json @@ -103,6 +103,7 @@ "node.editSubtreeDenied": "Unable to edit the subtree node", "node.id": "Node id", "node.inputVariable": "Input variables", + "node.invalidChildren": "Invalid number of children", "node.invalidGroup": "Node group '{{group}}' is not enabled", "node.invalidValue": "Invalid value", "node.invalidVariableName": "Invalid variable name, should start with a letter or underscore", diff --git a/public/locales/zh.json b/public/locales/zh.json index ce978cf..c23915c 100644 --- a/public/locales/zh.json +++ b/public/locales/zh.json @@ -102,6 +102,7 @@ "node.disabled": "禁用", "node.editSubtreeDenied": "不能编辑子树节点", "node.id": "节点标识", + "node.invalidChildren": "子节点数量不合法", "node.inputVariable": "输入变量", "node.invalidGroup": "节点分组 “{{group}}” 未启用", "node.invalidValue": "无效的值", diff --git a/sample/node-config.b3-setting b/sample/node-config.b3-setting index 966e832..352de2f 100644 --- a/sample/node-config.b3-setting +++ b/sample/node-config.b3-setting @@ -28,6 +28,7 @@ "name": "Clear", "type": "Action", "desc": "清除变量", + "children": 0, "output": [ "清除的变量名" ] diff --git a/sample/vars/test-vars.json b/sample/vars/test-vars.json index 5b592de..ba2d793 100644 --- a/sample/vars/test-vars.json +++ b/sample/vars/test-vars.json @@ -61,6 +61,19 @@ "input": [ "target" ] + }, + { + "id": 8, + "name": "Clear", + "output": [ + "test" + ], + "children": [ + { + "id": 9, + "name": "Idle" + } + ] } ] } diff --git a/sample/workdir/monster.json b/sample/workdir/monster.json index fb6790a..3225067 100644 --- a/sample/workdir/monster.json +++ b/sample/workdir/monster.json @@ -4,18 +4,15 @@ "id": 1, "name": "Sequence", "desc": "怪物测试AI", - "args": {}, "children": [ { "id": 2, "name": "Sequence", "desc": "攻击", - "args": {}, "children": [ { "id": 3, "name": "GetHp", - "args": {}, "output": [ "hp" ], @@ -60,10 +57,14 @@ "name": "Log", "desc": "逃跑", "args": { - "str": "Run!" + "message": "Run!" } } ] }, - "desc": "怪物测试AI" + "desc": "怪物测试AI", + "firstid": 1, + "group": [], + "import": [], + "declvar": [] } \ No newline at end of file diff --git a/sample/workdir/subtree1.json b/sample/workdir/subtree1.json index 08be034..e1d7a4a 100644 --- a/sample/workdir/subtree1.json +++ b/sample/workdir/subtree1.json @@ -9,7 +9,7 @@ "id": 2, "name": "Log", "args": { - "str": "hello" + "message": "hello" } }, { @@ -21,5 +21,9 @@ } ] }, - "desc": "子树1" + "desc": "子树1", + "firstid": 1, + "group": [], + "import": [], + "declvar": [] } \ No newline at end of file diff --git a/src/components/editor.tsx b/src/components/editor.tsx index 02422e4..2dbe65e 100644 --- a/src/components/editor.tsx +++ b/src/components/editor.tsx @@ -568,7 +568,7 @@ export const Editor: FC = ({ onUpdate: updateState, data: editor, . workspace.onEditingNode({ data: b3util.createNode(data, false), editable: !isSubtreeNode(data), - limit_error: !b3util.checkChildrenLimit(data), + limitError: !b3util.checkChildrenLimit(data), }); setItemState(editor.selectedId, "selected", true); } else { diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 4ddacf9..69a70e8 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -775,11 +775,21 @@ const NodeInspector: FC = () => { > - - + + !usingVars![v])) || - (usingVars && data.output?.some((v) => !usingVars![v])) || (usingVars && - nodeDef.args?.some( - (v) => - isExprType(v.type) && - parseExpr(data.args?.[v.name] ?? "").some((vv) => !usingVars![vv]) - )) + (data.input?.some((v) => usingVars?.[v] === undefined) || + data.output?.some((v) => usingVars?.[v] === undefined) || + nodeDef.args?.some( + (v) => + isExprType(v.type) && + parseExpr(data.args?.[v.name] ?? "").some((vv) => usingVars?.[vv] === undefined) + ))) ) { classify = "Error"; color = NODE_COLORS[classify]; diff --git a/src/contexts/workspace-context.ts b/src/contexts/workspace-context.ts index b14d038..92770dd 100644 --- a/src/contexts/workspace-context.ts +++ b/src/contexts/workspace-context.ts @@ -74,10 +74,6 @@ export class EditorStore { this.path = path; this.data = readTree(path); this.data.name = Path.basenameWithoutExt(path); - this.data.firstid = this.data.firstid ?? 1; - this.data.group = this.data.group ?? []; - this.data.import = this.data.import ?? []; - this.data.declvar = this.data.declvar ?? []; this.root = b3util.createTreeData(this.data.root); this.modifiedTime = fs.statSync(path).mtimeMs; this.import = this.data.import.map((v) => ({ path: v, vars: [] })); @@ -102,7 +98,7 @@ export type FileTreeType = { export type EditNode = { data: NodeModel; editable: boolean; - limit_error?: boolean; + limitError?: boolean; }; export type EditNodeDef = { @@ -380,8 +376,7 @@ export const useWorkspace = create((set, get) => ({ const str = fs.readFileSync(scriptPath, "utf8"); const batch = eval(str) as BatchScript; workspace.allFiles.forEach((file) => { - const treeStr = fs.readFileSync(file.path, "utf8"); - let tree: TreeModel | undefined = JSON.parse(treeStr); + let tree: TreeModel | undefined = readTree(file.path); if (batch.processTree && tree) { tree = batch.processTree(tree, file.path); } diff --git a/src/misc/b3util.ts b/src/misc/b3util.ts index 57f1493..5cb02b5 100644 --- a/src/misc/b3util.ts +++ b/src/misc/b3util.ts @@ -652,8 +652,7 @@ export const createTreeData = ( export const createBuildData = (path: string) => { try { - const str = fs.readFileSync(path, "utf8"); - const treeModel: TreeModel = JSON.parse(str); + const treeModel: TreeModel = readTree(path); const data = createTreeData(treeModel.root); refreshTreeDataId(data, treeModel.firstid ?? 1); treeModel.name = Path.basenameWithoutExt(path); diff --git a/src/misc/util.ts b/src/misc/util.ts index eace039..e2a3a45 100644 --- a/src/misc/util.ts +++ b/src/misc/util.ts @@ -8,7 +8,12 @@ export const readJson = (path: string) => { export const readTree = (path: string) => { const str = fs.readFileSync(path, "utf-8"); - return JSON.parse(str) as TreeModel; + const data = JSON.parse(str) as TreeModel; + data.firstid = data.firstid ?? 1; + data.group = data.group || []; + data.import = data.import || []; + data.declvar = data.declvar || []; + return data; }; export const writeJson = (path: string, data: any) => {