/
MapMerging.cpp
659 lines (491 loc) · 25.6 KB
/
MapMerging.cpp
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#include "RadiantTest.h"
#include "icommandsystem.h"
#include "itransformable.h"
#include "ibrush.h"
#include "imapresource.h"
#include "ipatch.h"
#include "icomparablenode.h"
#include "algorithm/Scene.h"
#include "registry/registry.h"
#include "scenelib.h"
#include "scene/merge/GraphComparer.h"
#include "scene/merge/MergeOperation.h"
namespace test
{
using MapMergeTest = RadiantTest;
TEST_F(MapMergeTest, BrushFingerprint)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto originalMaterial = "textures/numbers/1";
auto brush = std::dynamic_pointer_cast<IBrushNode>(algorithm::findFirstBrushWithMaterial(
GlobalMapModule().findOrInsertWorldspawn(), originalMaterial));
auto comparable = std::dynamic_pointer_cast<scene::IComparableNode>(brush);
EXPECT_TRUE(comparable) << "BrushNode is not implementing IComparableNode";
auto originalFingerprint = comparable->getFingerprint();
EXPECT_FALSE(originalFingerprint.empty()); // shouldn't be empty
// Calling it twice shouldn't change the value
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Change the material of this brush, the fingerprint should change now
brush->getIBrush().setShader("textures/somethingelse");
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back
brush->getIBrush().setShader(originalMaterial);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Change the contents flags, fingerprint should change
brush->getIBrush().setDetailFlag(IBrush::Detail);
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back
brush->getIBrush().setDetailFlag(IBrush::Structural);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Get the first face and tinker with it
auto& face = brush->getIBrush().getFace(0);
auto lastFingerprint = comparable->getFingerprint();
// Changing the texture should modify the fingerprint
face.flipTexture(0);
EXPECT_NE(comparable->getFingerprint(), lastFingerprint);
lastFingerprint = comparable->getFingerprint();
// Disable texture lock
registry::setValue(RKEY_ENABLE_TEXTURE_LOCK, false);
// Changing the planes
auto transformable = std::dynamic_pointer_cast<ITransformable>(brush);
transformable->setTranslation(Vector3(10,0,0));
transformable->freezeTransform();
EXPECT_NE(comparable->getFingerprint(), lastFingerprint);
lastFingerprint = comparable->getFingerprint();
}
TEST_F(MapMergeTest, PatchFingerprint)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto originalMaterial = "textures/numbers/1";
auto patch = std::dynamic_pointer_cast<IPatchNode>(algorithm::findFirstPatchWithMaterial(
GlobalMapModule().findOrInsertWorldspawn(), originalMaterial));
auto comparable = std::dynamic_pointer_cast<scene::IComparableNode>(patch);
EXPECT_TRUE(comparable) << "PatchNode is not implementing IComparableNode";
auto originalFingerprint = comparable->getFingerprint();
EXPECT_FALSE(originalFingerprint.empty()); // shouldn't be empty
// Calling it twice shouldn't change the value
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Change the material of this patch, the fingerprint should change now
patch->getPatch().setShader("textures/somethingelse");
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back
patch->getPatch().setShader(originalMaterial);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Change a single control vertex
auto& control = patch->getPatch().ctrlAt(0, 0);
auto lastFingerprint = comparable->getFingerprint();
// Change a 3D coordinate
control.vertex.x() += 0.1;
EXPECT_NE(comparable->getFingerprint(), lastFingerprint);
lastFingerprint = comparable->getFingerprint();
// Change a 2D component
control.texcoord.x() += 0.1;
EXPECT_NE(comparable->getFingerprint(), lastFingerprint);
lastFingerprint = comparable->getFingerprint();
// Append vertices
patch->getPatch().appendPoints(true, false);
EXPECT_NE(comparable->getFingerprint(), lastFingerprint);
}
TEST_F(MapMergeTest, EntityFingerprint)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto entityNode = algorithm::getEntityByName(GlobalMapModule().getRoot(), "func_static_1");
auto comparable = std::dynamic_pointer_cast<scene::IComparableNode>(entityNode);
EXPECT_TRUE(comparable) << "EntityNode is not implementing IComparableNode";
auto entity = std::dynamic_pointer_cast<IEntityNode>(entityNode);
auto originalFingerprint = comparable->getFingerprint();
EXPECT_FALSE(originalFingerprint.empty()); // shouldn't be empty
// Calling it twice shouldn't change the value
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Change a spawnarg slightly
auto oldOrigin = entity->getEntity().getKeyValue("origin");
entity->getEntity().setKeyValue("origin", "96 -32 53");
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back
entity->getEntity().setKeyValue("origin", oldOrigin);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Add a new spawnarg
entity->getEntity().setKeyValue("origin22", "whatever");
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back
entity->getEntity().setKeyValue("origin22", "");
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
// Remove a spawnarg, this alters the order of keyvalues
auto originalValue = entity->getEntity().getKeyValue("dummyspawnarg");
entity->getEntity().setKeyValue("dummyspawnarg", "");
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Change it back, even though the order is different, the fingerprint should be the same
entity->getEntity().setKeyValue("dummyspawnarg", originalValue);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
}
TEST_F(MapMergeTest, EntityFingerprintConsidersChildNodes)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto entityNode = algorithm::getEntityByName(GlobalMapModule().getRoot(), "func_static_1");
auto comparable = std::dynamic_pointer_cast<scene::IComparableNode>(entityNode);
EXPECT_TRUE(comparable) << "EntityNode is not implementing IComparableNode";
auto entity = std::dynamic_pointer_cast<IEntityNode>(entityNode);
auto originalFingerprint = comparable->getFingerprint();
// Add a child patch
auto patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def3);
std::dynamic_pointer_cast<IPatchNode>(patchNode)->getPatch().setDims(3, 3);
scene::addNodeToContainer(patchNode, entityNode);
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Remove the patch again
scene::removeNodeFromParent(patchNode);
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
}
TEST_F(MapMergeTest, EntityFingerprintInsensitiveToChildOrder)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto entityNode = algorithm::getEntityByName(GlobalMapModule().getRoot(), "func_static_1");
auto comparable = std::dynamic_pointer_cast<scene::IComparableNode>(entityNode);
EXPECT_TRUE(comparable) << "EntityNode is not implementing IComparableNode";
auto entity = std::dynamic_pointer_cast<IEntityNode>(entityNode);
auto originalFingerprint = comparable->getFingerprint();
// Find the first brush of this func_static
scene::INodePtr firstChild;
std::size_t numChildren = 0;
entity->foreachNode([&](const scene::INodePtr& node)
{
numChildren++;
if (!firstChild)
{
firstChild = node;
}
return true;
});
EXPECT_TRUE(firstChild) << "func_static doesn't have any child nodes";
EXPECT_GT(numChildren, 1) << "We need to have more than one child node in this func_static";
// Remove it from the parent
scene::removeNodeFromParent(firstChild);
EXPECT_NE(comparable->getFingerprint(), originalFingerprint);
// Adding it back to the entity will change the order of children,
scene::addNodeToContainer(firstChild, entityNode);
// Hash should stay the same
EXPECT_EQ(comparable->getFingerprint(), originalFingerprint);
}
using namespace scene::merge;
inline ComparisonResult::Ptr performComparison(const std::string& targetMap, const std::string& sourceMapPath)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto resource = GlobalMapResourceManager().createFromPath(sourceMapPath);
EXPECT_TRUE(resource->load()) << "Test map not found in path " << sourceMapPath;
return GraphComparer::Compare(resource->getRootNode(), GlobalMapModule().getRoot());
}
inline std::size_t countPrimitiveDifference(const ComparisonResult::EntityDifference& diff,
const ComparisonResult::PrimitiveDifference::Type type)
{
std::size_t count = 0;
for (const auto& difference : diff.differingChildren)
{
if (difference.type == type)
{
++count;
}
}
return count;
}
inline bool hasKeyValueDifference(const ComparisonResult::EntityDifference& diff,
const std::string& key, const std::string& value, ComparisonResult::KeyValueDifference::Type type)
{
for (const auto& difference : diff.differingKeyValues)
{
if (difference.type == type && difference.key == key && difference.value == value)
{
return true;
}
}
return false;
}
inline ComparisonResult::EntityDifference getEntityDifference(const ComparisonResult::Ptr& result, const std::string& name)
{
for (const auto& difference : result->differingEntities)
{
if (difference.entityName == name)
{
return difference;
}
}
return ComparisonResult::EntityDifference();
}
TEST_F(MapMergeTest, DetectMissingEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// The player start has been removed in the changed map
auto diff = getEntityDifference(result, "info_player_start_1");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityMissingInSource);
EXPECT_TRUE(diff.baseNode);
EXPECT_FALSE(diff.sourceNode); // source node is missing, so it must be empty
}
TEST_F(MapMergeTest, DetectAddedEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// light_3 start has been added to the changed map
auto diff = getEntityDifference(result, "light_3");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityMissingInBase);
EXPECT_TRUE(diff.sourceNode);
EXPECT_FALSE(diff.baseNode); // base node is missing, so it must be empty
diff = getEntityDifference(result, "func_static_2");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityMissingInBase);
EXPECT_TRUE(diff.sourceNode);
EXPECT_FALSE(diff.baseNode); // base node is missing, so it must be empty
}
TEST_F(MapMergeTest, DetectAddedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// light_1 has different key values, dist_check_period = 40 has been added
auto diff = getEntityDifference(result, "light_1");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "dist_check_period", "40", ComparisonResult::KeyValueDifference::Type::KeyValueAdded));
}
TEST_F(MapMergeTest, DetectRemovedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// light_1 has different key values, break = 1 has been removed
auto diff = getEntityDifference(result, "light_1");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "break", "1", ComparisonResult::KeyValueDifference::Type::KeyValueRemoved));
}
TEST_F(MapMergeTest, DetectChangedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// light_1 has different key values, "ai_see" has been changed to "1"
auto diff = getEntityDifference(result, "light_1");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "ai_see", "1", ComparisonResult::KeyValueDifference::Type::KeyValueChanged));
// light_2 has a different origin
diff = getEntityDifference(result, "light_2");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_TRUE(hasKeyValueDifference(diff, "origin", "280 160 0", ComparisonResult::KeyValueDifference::Type::KeyValueChanged));
}
TEST_F(MapMergeTest, DetectChildPrimitiveChanges)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
// func_static_30 has changed primitives
auto diff = getEntityDifference(result, "func_static_30");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_EQ(diff.differingChildren.size(), 1);
EXPECT_EQ(diff.differingChildren.front().type, ComparisonResult::PrimitiveDifference::Type::PrimitiveAdded);
EXPECT_EQ(diff.differingChildren.front().node->getNodeType(), scene::INode::Type::Brush);
// func_static_1 has 2 additions and 1 removal (== 1 addition, 1 replacement)
diff = getEntityDifference(result, "func_static_1");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_EQ(countPrimitiveDifference(diff, ComparisonResult::PrimitiveDifference::Type::PrimitiveAdded), 2);
EXPECT_EQ(countPrimitiveDifference(diff, ComparisonResult::PrimitiveDifference::Type::PrimitiveRemoved), 1);
// worldspawn has a couple of changes
diff = getEntityDifference(result, "worldspawn");
EXPECT_EQ(diff.type, ComparisonResult::EntityDifference::Type::EntityPresentButDifferent);
EXPECT_EQ(countPrimitiveDifference(diff, ComparisonResult::PrimitiveDifference::Type::PrimitiveAdded), 3);
EXPECT_EQ(countPrimitiveDifference(diff, ComparisonResult::PrimitiveDifference::Type::PrimitiveRemoved), 3);
}
template<typename T>
std::shared_ptr<T> findAction(const MergeOperation::Ptr& operation, const std::function<bool(const std::shared_ptr<T>&)>& predicate)
{
std::shared_ptr<T> foundAction;
operation->foreachAction([&](const MergeAction::Ptr& action)
{
if (foundAction) return;
auto derivedAction = std::dynamic_pointer_cast<T>(action);
if (!derivedAction || !predicate(derivedAction)) return;
foundAction = derivedAction;
});
return foundAction;
}
template<typename T>
std::size_t countActions(const MergeOperation::Ptr& operation, const std::function<bool(const std::shared_ptr<T>&)>& predicate)
{
std::size_t count = 0;
operation->foreachAction([&](const MergeAction::Ptr& action)
{
auto derivedAction = std::dynamic_pointer_cast<T>(action);
if (!derivedAction || !predicate(derivedAction)) return;
++count;
});
return count;
}
TEST_F(MapMergeTest, MergeActionsForMissingEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
// light_3
auto action = findAction<AddEntityAction>(operation, [](const std::shared_ptr<AddEntityAction>& action)
{
auto sourceEntity = Node_getEntity(action->getSourceNodeToAdd());
return sourceEntity->getKeyValue("name") == "light_3";
});
EXPECT_TRUE(action) << "No merge action found for missing entity";
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_3");
EXPECT_FALSE(entityNode);
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_3");
EXPECT_TRUE(entityNode);
// func_static_2
action = findAction<AddEntityAction>(operation, [](const std::shared_ptr<AddEntityAction>& action)
{
auto sourceEntity = Node_getEntity(action->getSourceNodeToAdd());
return sourceEntity->getKeyValue("name") == "func_static_2";
});
EXPECT_TRUE(action) << "No merge action found for missing entity";
// Check pre-requisites and apply the action
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "func_static_2");
EXPECT_FALSE(entityNode);
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "func_static_2");
EXPECT_TRUE(entityNode);
// Assume the child primitives have been added too
EXPECT_TRUE(scene::hasChildPrimitives(entityNode));
}
TEST_F(MapMergeTest, MergeActionsForRemovedEntities)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto action = findAction<RemoveEntityAction>(operation, [](const std::shared_ptr<RemoveEntityAction>& action)
{
auto entity = Node_getEntity(action->getNodeToRemove());
return entity->getKeyValue("name") == "info_player_start_1";
});
EXPECT_TRUE(action) << "No merge action found for removed entity";
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "info_player_start_1");
EXPECT_TRUE(entityNode);
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "info_player_start_1");
EXPECT_FALSE(entityNode);
}
TEST_F(MapMergeTest, MergeActionsForAddedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto action = findAction<AddEntityKeyValueAction>(operation, [](const std::shared_ptr<AddEntityKeyValueAction>& action)
{
auto entity = Node_getEntity(action->getEntityNode());
return entity->getKeyValue("name") == "light_1" && action->getKey() == "dist_check_period" && action->getValue() == "40";
});
EXPECT_TRUE(action) << "No merge action found for added key value";
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("dist_check_period"), "");
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("dist_check_period"), "40");
}
TEST_F(MapMergeTest, MergeActionsForRemovedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto action = findAction<RemoveEntityKeyValueAction>(operation, [](const std::shared_ptr<RemoveEntityKeyValueAction>& action)
{
auto entity = Node_getEntity(action->getEntityNode());
return entity->getKeyValue("name") == "light_1" && action->getKey() == "break" && action->getValue().empty();
});
EXPECT_TRUE(action) << "No merge action found for removed key value";
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("break"), "1");
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("break"), "");
}
TEST_F(MapMergeTest, MergeActionsForChangedKeyValues)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto action = findAction<ChangeEntityKeyValueAction>(operation, [](const std::shared_ptr<ChangeEntityKeyValueAction>& action)
{
auto entity = Node_getEntity(action->getEntityNode());
return entity->getKeyValue("name") == "light_1" && action->getKey() == "ai_see" && action->getValue() == "1";
});
EXPECT_TRUE(action) << "No merge action found for changed key value";
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("ai_see"), "0");
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("ai_see"), "1");
action = findAction<ChangeEntityKeyValueAction>(operation, [](const std::shared_ptr<ChangeEntityKeyValueAction>& action)
{
auto entity = Node_getEntity(action->getEntityNode());
return entity->getKeyValue("name") == "light_2" && action->getKey() == "origin" && action->getValue() == "280 160 0";
});
EXPECT_TRUE(action) << "No merge action found for changed key value";
// Check pre-requisites and apply the action
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_2");
EXPECT_NE(Node_getEntity(entityNode)->getKeyValue("origin"), "280 160 0");
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_2");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("origin"), "280 160 0");
}
inline std::size_t getChildPrimitiveCount(const scene::INodePtr& node)
{
std::size_t count = 0;
node->foreachNode([&](const scene::INodePtr& node)
{
if (Node_isPrimitive(node))
{
++count;
}
return true;
});
return count;
}
TEST_F(MapMergeTest, MergeActionsForChangedPrimitives)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto addAction = findAction<AddChildAction>(operation, [](const std::shared_ptr<AddChildAction>& action)
{
auto entity = Node_getEntity(action->getParent());
return entity->getKeyValue("name") == "func_static_30" && action->getSourceNodeToAdd()->getNodeType() == scene::INode::Type::Brush;
});
EXPECT_TRUE(addAction) << "No merge action found for added child node";
// func_static_1 has 2 additions and 1 removal (== 1 addition, 1 replacement)
auto addActionCount = countActions<AddChildAction>(operation, [](const std::shared_ptr<AddChildAction>& action)
{
auto entity = Node_getEntity(action->getParent());
return entity->getKeyValue("name") == "func_static_1";
});
auto removeActionCount = countActions<RemoveChildAction>(operation, [](const std::shared_ptr<RemoveChildAction>& action)
{
auto entity = Node_getEntity(action->getNodeToRemove()->getParent());
return entity->getKeyValue("name") == "func_static_1";
});
EXPECT_EQ(addActionCount, 2) << "No merge action found for added child node";
EXPECT_EQ(removeActionCount, 1) << "No merge action found for removed child node";
addActionCount = countActions<AddChildAction>(operation, [](const std::shared_ptr<AddChildAction>& action)
{
auto entity = Node_getEntity(action->getParent());
return entity->isWorldspawn();
});
removeActionCount = countActions<RemoveChildAction>(operation, [](const std::shared_ptr<RemoveChildAction>& action)
{
auto entity = Node_getEntity(action->getNodeToRemove()->getParent());
return entity->isWorldspawn();
});
EXPECT_EQ(addActionCount, 3) << "No merge action found for added child node";
EXPECT_EQ(removeActionCount, 3) << "No merge action found for removed child node";
// func_static_1 should have 3 brushes after execution of the actions
// Check prerequisites and execute action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "func_static_1");
EXPECT_EQ(getChildPrimitiveCount(entityNode), 2);
operation->applyActions();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "func_static_1");
EXPECT_EQ(getChildPrimitiveCount(entityNode), 3);
}
TEST_F(MapMergeTest, ApplyMergeOperation)
{
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/fingerprinting.mapx"));
auto changedMap = _context.getTestProjectPath() + "maps/fingerprinting_2.mapx";
auto resource = GlobalMapResourceManager().createFromPath(changedMap);
EXPECT_TRUE(resource->load()) << "Test map not found in path " << changedMap;
auto result = GraphComparer::Compare(resource->getRootNode(), GlobalMapModule().getRoot());
// The result should have differences
EXPECT_FALSE(result->differingEntities.empty());
auto operation = MergeOperation::CreateFromComparisonResult(*result);
operation->applyActions();
auto resultAfterExecution = GraphComparer::Compare(resource->getRootNode(), GlobalMapModule().getRoot());
// The result should not list any differences anymore
EXPECT_TRUE(resultAfterExecution->differingEntities.empty());
}
}