-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeGraphData.cpp
More file actions
361 lines (330 loc) · 13.3 KB
/
Copy pathNodeGraphData.cpp
File metadata and controls
361 lines (330 loc) · 13.3 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "deki-nodegraph/NodeGraphData.h"
#include "deki-nodegraph/NodeFactory.h"
#include "DekiComponent.h" // DekiHashString
#include "PrefabMessagePack.h"
#include "DekiLogSystem.h"
#include <string>
using PrefabFormat::PrefabMsgPackParser;
using PrefabFormat::NodeFactory;
namespace {
// Read a msgpack string into a std::string (key or short value).
bool ReadStdString(PrefabMsgPackParser& parser, std::string& out) {
const char* str = nullptr;
uint16_t len = 0;
if (!parser.ReadString(str, len)) return false;
out.assign(str, len);
return true;
}
void DestroyGraph(NodeGraphData::Graph& graph);
// Destroy a partially-built node (own instance, children, and inner graph
// created so far). Recursive: an inner graph's nodes may have inner graphs.
void DestroyPartialNode(NodeGraphData::NodeInstance& node) {
auto& factory = NodeFactory::Instance();
for (auto& child : node.children) {
if (child.instance) factory.Destroy(child.typeId, child.instance);
}
node.children.clear();
if (node.inner) {
DestroyGraph(*node.inner);
delete node.inner;
node.inner = nullptr;
}
if (node.instance) factory.Destroy(node.typeId, node.instance);
node.instance = nullptr;
}
void DestroyGraph(NodeGraphData::Graph& graph) {
for (auto& node : graph.nodes) DestroyPartialNode(node);
graph.nodes.clear();
graph.links.clear();
}
// Parse one child map ({enabled, type, values} — alphabetical, so "type"
// creates the instance before "values" populates it, mirroring ParseNode).
// On success appends to `node.children`; on failure logs and returns false
// (any instance created here is destroyed by the caller via DestroyPartialNode).
bool ParseChild(PrefabMsgPackParser& parser, uint32_t mapSize,
NodeGraphData::NodeInstance& node) {
NodeGraphData::ChildInstance child;
auto& factory = NodeFactory::Instance();
for (uint32_t i = 0; i < mapSize; ++i) {
std::string key;
if (!ReadStdString(parser, key)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read child map key");
if (child.instance) factory.Destroy(child.typeId, child.instance);
return false;
}
if (key == "enabled") {
bool enabled = true;
if (!parser.ReadBool(enabled)) {
DEKI_LOG_ERROR("NodeGraphData: child 'enabled' is not a bool");
if (child.instance) factory.Destroy(child.typeId, child.instance);
return false;
}
child.enabled = enabled;
} else if (key == "type") {
std::string typeName;
if (!ReadStdString(parser, typeName)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read child type name");
return false;
}
child.typeId = DekiHashString(typeName.c_str());
child.instance = factory.Create(child.typeId);
if (!child.instance) {
DEKI_LOG_ERROR("NodeGraphData: unknown child type '%s' (not registered in NodeFactory)",
typeName.c_str());
return false;
}
} else if (key == "values") {
if (!child.instance) {
DEKI_LOG_ERROR("NodeGraphData: child 'values' encountered before 'type' (corrupt asset)");
return false;
}
uint32_t valuesSize = 0;
if (!parser.ReadMapSize(valuesSize)) {
DEKI_LOG_ERROR("NodeGraphData: child 'values' is not a map");
factory.Destroy(child.typeId, child.instance);
return false;
}
if (!factory.Deserialize(child.typeId, child.instance, parser, valuesSize)) {
DEKI_LOG_ERROR("NodeGraphData: failed to deserialize child values in node id %u", node.id);
factory.Destroy(child.typeId, child.instance);
return false;
}
} else {
if (!parser.SkipValue()) {
DEKI_LOG_ERROR("NodeGraphData: failed to skip child key '%s'", key.c_str());
if (child.instance) factory.Destroy(child.typeId, child.instance);
return false;
}
}
}
if (!child.instance) {
DEKI_LOG_ERROR("NodeGraphData: child map missing 'type'");
return false;
}
node.children.push_back(child);
return true;
}
bool ParseGraph(PrefabMsgPackParser& parser, uint32_t mapSize, NodeGraphData::Graph& graph);
// Parse one node map. On success appends to `graph.nodes`; on failure logs and
// returns false (caller destroys everything already created).
bool ParseNode(PrefabMsgPackParser& parser, uint32_t mapSize,
NodeGraphData::Graph& graph) {
NodeGraphData::NodeInstance node;
auto& factory = NodeFactory::Instance();
for (uint32_t i = 0; i < mapSize; ++i) {
std::string key;
if (!ReadStdString(parser, key)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read node map key");
DestroyPartialNode(node);
return false;
}
if (key == "id") {
int32_t id = 0;
if (!parser.ReadInt(id) || id <= 0) {
DEKI_LOG_ERROR("NodeGraphData: invalid node id");
DestroyPartialNode(node);
return false;
}
node.id = (uint32_t)id;
} else if (key == "children") {
uint32_t count = 0;
if (!parser.ReadArraySize(count)) {
DEKI_LOG_ERROR("NodeGraphData: node 'children' is not an array");
DestroyPartialNode(node);
return false;
}
for (uint32_t c = 0; c < count; ++c) {
uint32_t childMapSize = 0;
if (!parser.ReadMapSize(childMapSize)) {
DEKI_LOG_ERROR("NodeGraphData: child %u is not a map", c);
DestroyPartialNode(node);
return false;
}
if (!ParseChild(parser, childMapSize, node)) {
DestroyPartialNode(node);
return false;
}
}
} else if (key == "graph") {
// Inner graph (DEKI_NODE_SUBGRAPH). Parsed exactly like the root,
// to any depth; "graph" sorts before "type", but nothing in here
// depends on the owning node's instance.
uint32_t innerSize = 0;
if (!parser.ReadMapSize(innerSize)) {
DEKI_LOG_ERROR("NodeGraphData: node 'graph' is not a map");
DestroyPartialNode(node);
return false;
}
node.inner = new NodeGraphData::Graph();
if (!ParseGraph(parser, innerSize, *node.inner)) {
DEKI_LOG_ERROR("NodeGraphData: failed to parse inner graph of node id %u", node.id);
DestroyPartialNode(node);
return false;
}
} else if (key == "type") {
std::string typeName;
if (!ReadStdString(parser, typeName)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read node type name");
DestroyPartialNode(node);
return false;
}
node.typeId = DekiHashString(typeName.c_str());
node.instance = factory.Create(node.typeId);
if (!node.instance) {
DEKI_LOG_ERROR("NodeGraphData: unknown node type '%s' (not registered in NodeFactory)",
typeName.c_str());
DestroyPartialNode(node);
return false;
}
} else if (key == "values") {
// Alphabetical key order guarantees "type" precedes "values"; a file
// violating that is corrupt.
if (!node.instance) {
DEKI_LOG_ERROR("NodeGraphData: node 'values' encountered before 'type' (corrupt asset)");
DestroyPartialNode(node);
return false;
}
uint32_t valuesSize = 0;
if (!parser.ReadMapSize(valuesSize)) {
DEKI_LOG_ERROR("NodeGraphData: node 'values' is not a map");
DestroyPartialNode(node);
return false;
}
if (!factory.Deserialize(node.typeId, node.instance, parser, valuesSize)) {
DEKI_LOG_ERROR("NodeGraphData: failed to deserialize values of node id %u", node.id);
DestroyPartialNode(node);
return false;
}
} else {
// x / y (editor layout) and future keys: skip.
if (!parser.SkipValue()) {
DEKI_LOG_ERROR("NodeGraphData: failed to skip node key '%s'", key.c_str());
DestroyPartialNode(node);
return false;
}
}
}
if (node.id == 0 || !node.instance) {
DEKI_LOG_ERROR("NodeGraphData: node map missing 'id' or 'type'");
DestroyPartialNode(node);
return false;
}
graph.nodes.push_back(std::move(node));
return true;
}
// Parse one link map ({"from", "fromPin", "to", "toPin"} in any order).
bool ParseLink(PrefabMsgPackParser& parser, uint32_t mapSize, NodeGraphData::Link& out) {
for (uint32_t i = 0; i < mapSize; ++i) {
std::string key;
if (!ReadStdString(parser, key)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read link map key");
return false;
}
int32_t v = 0;
if (!parser.ReadInt(v)) {
DEKI_LOG_ERROR("NodeGraphData: link key '%s' is not an int", key.c_str());
return false;
}
if (key == "from") out.fromNode = (uint32_t)v;
else if (key == "fromPin") out.fromPin = v;
else if (key == "to") out.toNode = (uint32_t)v;
else if (key == "toPin") out.toPin = v;
else {
DEKI_LOG_ERROR("NodeGraphData: unknown link key '%s'", key.c_str());
return false;
}
}
return true;
}
// Parse one graph map ("nodes" + "links"). Shared by the document root and
// every inner graph; the root additionally carries "nextNodeId", which is an
// editor-side counter and is skipped here like any other unknown key.
bool ParseGraph(PrefabMsgPackParser& parser, uint32_t mapSize, NodeGraphData::Graph& graph) {
for (uint32_t i = 0; i < mapSize; ++i) {
std::string key;
if (!ReadStdString(parser, key)) {
DEKI_LOG_ERROR("NodeGraphData: failed to read graph map key");
return false;
}
if (key == "nodes") {
uint32_t count = 0;
if (!parser.ReadArraySize(count)) {
DEKI_LOG_ERROR("NodeGraphData: 'nodes' is not an array");
return false;
}
for (uint32_t n = 0; n < count; ++n) {
uint32_t nodeMapSize = 0;
if (!parser.ReadMapSize(nodeMapSize)) {
DEKI_LOG_ERROR("NodeGraphData: node %u is not a map", n);
return false;
}
if (!ParseNode(parser, nodeMapSize, graph)) return false;
}
} else if (key == "links") {
uint32_t count = 0;
if (!parser.ReadArraySize(count)) {
DEKI_LOG_ERROR("NodeGraphData: 'links' is not an array");
return false;
}
for (uint32_t l = 0; l < count; ++l) {
uint32_t linkMapSize = 0;
if (!parser.ReadMapSize(linkMapSize)) {
DEKI_LOG_ERROR("NodeGraphData: link %u is not a map", l);
return false;
}
NodeGraphData::Link link;
if (!ParseLink(parser, linkMapSize, link)) return false;
graph.links.push_back(link);
}
} else {
// nextNodeId (editor counter) and future keys: skip.
if (!parser.SkipValue()) {
DEKI_LOG_ERROR("NodeGraphData: failed to skip graph key '%s'", key.c_str());
return false;
}
}
}
return true;
}
} // namespace
NodeGraphData* NodeGraphData::LoadFromMemory(const uint8_t* data, size_t size) {
if (!data || size == 0) {
DEKI_LOG_ERROR("NodeGraphData: empty buffer");
return nullptr;
}
PrefabMsgPackParser parser(data, size);
uint32_t rootSize = 0;
if (!parser.ReadMapSize(rootSize)) {
DEKI_LOG_ERROR("NodeGraphData: root is not a map");
return nullptr;
}
NodeGraphData* graph = new NodeGraphData();
if (!ParseGraph(parser, rootSize, graph->m_Root)) {
delete graph; // destroys any already-created instances
return nullptr;
}
return graph;
}
NodeGraphData::~NodeGraphData() {
DestroyGraph(m_Root);
}
const NodeGraphData::NodeInstance* NodeGraphData::Graph::FindNode(uint32_t id) const {
for (const auto& node : nodes) {
if (node.id == id) return &node;
}
return nullptr;
}
const NodeGraphData::NodeInstance* NodeGraphData::Graph::FindFirstOfType(uint32_t typeId) const {
for (const auto& node : nodes) {
if (node.typeId == typeId) return &node;
}
return nullptr;
}
const NodeGraphData::NodeInstance* NodeGraphData::Graph::Next(uint32_t nodeId, int32_t fromPin) const {
for (const auto& link : links) {
if (link.fromNode == nodeId && link.fromPin == fromPin) {
return FindNode(link.toNode);
}
}
return nullptr;
}