-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBTParser.cs
More file actions
192 lines (164 loc) · 6.34 KB
/
Copy pathBTParser.cs
File metadata and controls
192 lines (164 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using GBFRDataTools.Entities;
using GBFRDataTools.Entities.Base;
using GBFRDataTools.FSM.BehaviorTree;
using GBFRDataTools.FSM.Components;
using GBFRDataTools.FSM.Components.Actions.UI;
using GBFRDataTools.FSM.Components.Actions.UI.Dialog;
using GBFRDataTools.FSM.Components.Conditions.UI.Pause;
using GBFRDataTools.FSM.Entities;
using MessagePack;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace GBFRDataTools.FSM;
public class BTParser
{
private ILogger? _logger;
/// <summary>
/// Not original, used to specify whether the tree has editor settings such as node names and boundary boxes
/// </summary>
public FsmEditorSettings EditorSettings { get; set; }
private static List<Type> _nodeTypes =
[
typeof(RootNode),
typeof(SelectorNode),
typeof(DecorationNode),
typeof(SequencerNode),
typeof(ActionNode),
typeof(NoFailedSequencerNode),
typeof(RandomSelectorNode),
typeof(RandomSelectorEmNode),
typeof(ReferenceTreeNode),
typeof(FSMNodeForBT),
];
public static Dictionary<string, Type> NodeTypeToType { get; } = [];
public static Dictionary<string, Type> ComponentNameToType { get; } = [];
static BTParser()
{
foreach (Type type in Assembly.GetAssembly(typeof(BehaviorTreeComponent)).GetTypes()
.Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(BehaviorTreeComponent))))
{
ComponentNameToType.Add(type.Name, type);
#if DEBUG
var obj = (BehaviorTreeComponent)Activator.CreateInstance(type);
if (obj.ComponentName != type.Name)
Debug.WriteLine($"ComponentName for {type.Name} does not match");
#endif
}
foreach (Type type in _nodeTypes)
NodeTypeToType.Add(type.Name, type);
}
//public static SortedDictionary<string, Dictionary<string, SortedDictionary<int, EnumString>>> _compToEnums = [];
public List<TreeNode> TreeNodes { get; set; } = [];
public Dictionary<uint, TreeNode> NodeMap { get; set; } = [];
public List<BehaviorTreeComponent> Components { get; set; } = [];
public RootNode RootNode { get; set; }
public bool HasErrors { get; private set; }
public BTParser(ILoggerFactory? loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<BTParser>();
}
public void Parse(byte[] data, bool asMessagePack = false)
{
string json;
if (asMessagePack)
{
json = MessagePackSerializer.ConvertToJson(data);
}
else
json = Encoding.UTF8.GetString(data);
JsonDocument doc = JsonDocument.Parse(json);
foreach (var elem in doc.RootElement.EnumerateObject())
{
if (NodeTypeToType.TryGetValue(elem.Name, out Type? type))
{
TreeNode node = (TreeNode)elem.Value.Deserialize(type, DefaultJsonSerializerOptions.InstanceForRead);
if (node is RootNode rootNode)
{
if (RootNode is not null)
throw new InvalidDataException("Root node was already declared in behavior tree.");
RootNode = rootNode;
}
TreeNodes.Add(node);
if (!NodeMap.TryAdd(node.Guid, node))
{
// em7001_state
_logger?.LogWarning("Duplicate guid node found (guid: {guid}, type: {type})", node.Guid, elem.Name);
}
}
else if (ComponentNameToType.TryGetValue(elem.Name, out Type? componentType))
{
BehaviorTreeComponent comp = (BehaviorTreeComponent)elem.Value.Deserialize(componentType, DefaultJsonSerializerOptions.InstanceForRead);
Components.Add(comp);
}
else if (elem.Name == "SubBehaviorTreeData")
{
// Not supported by the game
_logger?.LogWarning("BehaviorTree has 'SubBehaviorTreeData' which is not supported (not read by the game either)");
continue;
}
else
{
switch (elem.Name)
{
case "Em0006GuardAction":
case "Em0010EscapeAction":
throw new NotSupportedException($"Behavior Tree element '{elem.Name}' not supported (game doesn't support it either)");
}
throw new NotSupportedException($"Behavior Tree element '{elem.Name}' not supported");
}
}
BuildTree();
}
private void BuildTree()
{
CompositeNode root = RootNode;
int idx = 1;
Dive(ref root, ref idx);
foreach (BehaviorTreeComponent component in Components)
{
if (!NodeMap.TryGetValue(component.ParentGuid, out TreeNode? parentNode))
{
_logger?.LogWarning("Node {parentGuid} referenced by component {guid} ({name}) was not found in behavior tree.", component.ParentGuid, component.Guid, component.ComponentName);
continue;
}
if (parentNode is ActionNode actionNode)
{
actionNode.Actions.Add(component);
}
else if (parentNode is DecorationNode decorationNode)
{
decorationNode.BehaviorTreeComponent.Add(component);
}
else
throw new NotSupportedException();
}
}
private void Dive(ref CompositeNode parent, ref int idx)
{
for (int i = 0; i < parent.TailIndexOfChildNodeGuids; i++)
{
TreeNode node = TreeNodes[idx];
parent.Children.Add(node);
idx++;
if (node is ActionNode actionn)
;
if (node is CompositeNode compositeNode && compositeNode.TailIndexOfChildNodeGuids != 0)
{
Dive(ref compositeNode, ref idx);
}
else if (node.TailIndexOfChildNodeGuids != 0)
throw new NotSupportedException();
}
}
}