-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathN2CBlueprintValidator.cpp
More file actions
331 lines (288 loc) · 9.5 KB
/
N2CBlueprintValidator.cpp
File metadata and controls
331 lines (288 loc) · 9.5 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
// Copyright (c) 2025 Nick McClure (Protospatial). All Rights Reserved.
#include "Utils/Validators/N2CBlueprintValidator.h"
bool FN2CBlueprintValidator::Validate(const FN2CBlueprint& Blueprint, FString& OutError)
{
// Validate required fields
if (!ValidateRequired(Blueprint, OutError))
{
return false;
}
// Validate graphs
if (!ValidateGraphs(Blueprint, OutError))
{
return false;
}
// Validate structs
if (!ValidateStructs(Blueprint, OutError))
{
return false;
}
// Validate enums
if (!ValidateEnums(Blueprint, OutError))
{
return false;
}
return true;
}
bool FN2CBlueprintValidator::ValidateRequired(const FN2CBlueprint& Blueprint, FString& OutError)
{
// Check version
if (Blueprint.Version.Value.IsEmpty() || Blueprint.Version.Value != TEXT("1.0.0"))
{
OutError = TEXT("Invalid or missing version");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Check metadata
if (Blueprint.Metadata.Name.IsEmpty())
{
OutError = TEXT("Missing Blueprint name");
FN2CLogger::Get().LogError(OutError);
return false;
}
if (Blueprint.Metadata.BlueprintClass.IsEmpty())
{
OutError = TEXT("Missing Blueprint class");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Check graphs array
if (Blueprint.Graphs.Num() == 0)
{
OutError = TEXT("No graphs found");
FN2CLogger::Get().LogError(OutError);
return false;
}
return true;
}
bool FN2CBlueprintValidator::ValidateGraphs(const FN2CBlueprint& Blueprint, FString& OutError)
{
// Check that at least one graph has nodes
bool bHasNodes = false;
for (const FN2CGraph& Graph : Blueprint.Graphs)
{
if (Graph.Nodes.Num() > 0)
{
bHasNodes = true;
break;
}
}
if (!bHasNodes)
{
OutError = TEXT("No nodes found in any graph");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Validate each graph
for (const FN2CGraph& Graph : Blueprint.Graphs)
{
if (!ValidateGraph(Graph, OutError))
{
OutError = FString::Printf(TEXT("Invalid graph: %s - %s"), *Graph.Name, *OutError);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
return true;
}
bool FN2CBlueprintValidator::ValidateGraph(const FN2CGraph& Graph, FString& OutError)
{
if (Graph.Name.IsEmpty())
{
OutError = TEXT("Empty graph name");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Check nodes array
if (Graph.Nodes.Num() == 0)
{
OutError = FString::Printf(TEXT("No nodes in graph %s"), *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
// Build set of node IDs for validation
TSet<FString> NodeIds;
for (const FN2CNodeDefinition& Node : Graph.Nodes)
{
FString NodeError;
if (!NodeValidator.Validate(Node, NodeError))
{
OutError = FString::Printf(TEXT("Invalid node %s in graph %s: %s"), *Node.ID, *Graph.Name, *NodeError);
FN2CLogger::Get().LogError(OutError);
return false;
}
// Check for duplicate node IDs
if (NodeIds.Contains(Node.ID))
{
OutError = FString::Printf(TEXT("Duplicate node ID %s in graph %s"), *Node.ID, *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
NodeIds.Add(Node.ID);
}
// Log all node IDs for debugging
FString NodeIdList = TEXT("Valid Node IDs in graph ") + Graph.Name + TEXT(": ");
for (const FString& Id : NodeIds)
{
NodeIdList += Id + TEXT(", ");
}
FN2CLogger::Get().Log(NodeIdList, EN2CLogSeverity::Debug);
// Validate flow references
if (!ValidateFlowReferences(Graph, OutError))
{
return false;
}
FN2CLogger::Get().Log(FString::Printf(TEXT("Graph %s validation successful: %d nodes, %d execution flows, %d data flows"),
*Graph.Name, Graph.Nodes.Num(), Graph.Flows.Execution.Num(), Graph.Flows.Data.Num()), EN2CLogSeverity::Debug);
return true;
}
bool FN2CBlueprintValidator::ValidateFlowReferences(const FN2CGraph& Graph, FString& OutError)
{
// Build lookup maps for validation
TSet<FString> NodeIds;
TMap<FString, TSet<FString>> NodePinIds;
for (const FN2CNodeDefinition& Node : Graph.Nodes)
{
NodeIds.Add(Node.ID);
// Track all pin IDs for this node
TSet<FString>& PinIds = NodePinIds.Add(Node.ID);
for (const FN2CPinDefinition& Pin : Node.InputPins)
{
PinIds.Add(Pin.ID);
}
for (const FN2CPinDefinition& Pin : Node.OutputPins)
{
PinIds.Add(Pin.ID);
}
}
// Validate execution flows
for (const FString& ExecFlow : Graph.Flows.Execution)
{
TArray<FString> FlowNodes;
ExecFlow.ParseIntoArray(FlowNodes, TEXT("->"));
// Each flow must have at least 2 nodes
if (FlowNodes.Num() < 2)
{
OutError = FString::Printf(TEXT("Invalid execution flow %s (needs at least 2 nodes) in graph %s"), *ExecFlow, *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
// Verify all referenced nodes exist
for (const FString& NodeId : FlowNodes)
{
if (!NodeIds.Contains(NodeId))
{
OutError = FString::Printf(TEXT("Execution flow %s references non-existent node %s in graph %s"), *ExecFlow, *NodeId, *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
}
// Check data flows
for (const auto& DataFlow : Graph.Flows.Data)
{
// Validate source pin format (N#.P#)
TArray<FString> SourceParts;
DataFlow.Key.ParseIntoArray(SourceParts, TEXT("."));
if (SourceParts.Num() != 2 || !NodeIds.Contains(SourceParts[0]))
{
OutError = FString::Printf(TEXT("Invalid source pin format %s in graph %s"), *DataFlow.Key, *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
for (const auto& TargetPinName : DataFlow.Value.TargetPins)
{
// Validate target pin format (N#.P#)
TArray<FString> TargetParts;
TargetPinName.ParseIntoArray(TargetParts, TEXT("."));
if (TargetParts.Num() != 2 || !NodeIds.Contains(TargetParts[0]))
{
OutError = FString::Printf(TEXT("Invalid target pin format %s in graph %s"), *TargetPinName, *Graph.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
}
return true;
}
bool FN2CBlueprintValidator::ValidateStructs(const FN2CBlueprint& Blueprint, FString& OutError)
{
for (const FN2CStruct& Struct : Blueprint.Structs)
{
if (!ValidateStruct(Struct, OutError))
{
OutError = FString::Printf(TEXT("Invalid struct: %s - %s"), *Struct.Name, *OutError);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
return true;
}
bool FN2CBlueprintValidator::ValidateStruct(const FN2CStruct& Struct, FString& OutError)
{
// A struct needs a name at minimum
if (Struct.Name.IsEmpty())
{
OutError = TEXT("Missing name");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Validate all members
for (int32 i = 0; i < Struct.Members.Num(); ++i)
{
const FN2CStructMember& Member = Struct.Members[i];
if (Member.Name.IsEmpty())
{
OutError = FString::Printf(TEXT("Member at index %d has no name"), i);
FN2CLogger::Get().LogError(OutError);
return false;
}
// For struct/enum/object/class types, verify we have a type name
if ((Member.Type == EN2CStructMemberType::Struct ||
Member.Type == EN2CStructMemberType::Enum ||
Member.Type == EN2CStructMemberType::Object ||
Member.Type == EN2CStructMemberType::Class) &&
Member.TypeName.IsEmpty())
{
OutError = FString::Printf(TEXT("Member %s requires a type name"), *Member.Name);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
return true;
}
bool FN2CBlueprintValidator::ValidateEnums(const FN2CBlueprint& Blueprint, FString& OutError)
{
for (const FN2CEnum& Enum : Blueprint.Enums)
{
if (!ValidateEnum(Enum, OutError))
{
OutError = FString::Printf(TEXT("Invalid enum: %s - %s"), *Enum.Name, *OutError);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
return true;
}
bool FN2CBlueprintValidator::ValidateEnum(const FN2CEnum& Enum, FString& OutError)
{
// An enum needs a name at minimum
if (Enum.Name.IsEmpty())
{
OutError = TEXT("Missing name");
FN2CLogger::Get().LogError(OutError);
return false;
}
// Validate all values
for (int32 i = 0; i < Enum.Values.Num(); ++i)
{
const FN2CEnumValue& Value = Enum.Values[i];
if (Value.Name.IsEmpty())
{
OutError = FString::Printf(TEXT("Value at index %d has no name"), i);
FN2CLogger::Get().LogError(OutError);
return false;
}
}
return true;
}