/
MapMerging.cpp
2193 lines (1670 loc) · 104 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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"
#include "scene/merge/ThreeWayMergeOperation.h"
#include "scene/merge/SelectionGroupMerger.h"
#include "scene/merge/LayerMerger.h"
namespace test
{
using MapMergeTest = RadiantTest;
using SelectionGroupMergeTest = RadiantTest;
using LayerMergeTest = RadiantTest;
using ThreeWayMergeTest = 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(targetMap));
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 IMergeOperation::Ptr& operation, const std::function<bool(const std::shared_ptr<T>&)>& predicate)
{
std::shared_ptr<T> foundAction;
operation->foreachAction([&](const IMergeAction::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 IMergeOperation::Ptr& operation, const std::function<bool(const std::shared_ptr<T>&)>& predicate)
{
std::size_t count = 0;
operation->foreachAction([&](const IMergeAction::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());
}
TEST_F(MapMergeTest, DeactivatedAddEntityAction)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
auto action = findAction<AddEntityAction>(operation, [](const std::shared_ptr<AddEntityAction>& action)
{
auto sourceEntity = Node_getEntity(action->getSourceNodeToAdd());
return sourceEntity->getKeyValue("name") == "light_3";
});
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_3");
EXPECT_FALSE(entityNode);
action->deactivate();
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_3");
EXPECT_FALSE(entityNode); // must still be missing
}
TEST_F(MapMergeTest, DeactivatedRemoveEntityAction)
{
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";
});
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "info_player_start_1");
EXPECT_TRUE(entityNode);
action->deactivate();
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "info_player_start_1");
EXPECT_TRUE(entityNode); // must still be here
}
TEST_F(MapMergeTest, DeactivatedAddEntityKeyValueAction)
{
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";
});
// 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->deactivate();
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("dist_check_period"), ""); // must still be unchanged
}
TEST_F(MapMergeTest, DeactivatedRemoveEntityKeyValueAction)
{
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();
});
// Check pre-requisites and apply the action
auto entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("break"), "1");
action->deactivate();
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("break"), "1"); // must be unchanged
}
TEST_F(MapMergeTest, DeactivatedChangeEntityKeyValueAction)
{
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";
});
// 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->deactivate();
action->applyChanges();
entityNode = algorithm::getEntityByName(result->getBaseRootNode(), "light_1");
EXPECT_EQ(Node_getEntity(entityNode)->getKeyValue("ai_see"), "0");
}
TEST_F(MapMergeTest, DeactivatedChangePrimitiveActions)
{
auto result = performComparison("maps/fingerprinting.mapx", _context.getTestProjectPath() + "maps/fingerprinting_2.mapx");
auto operation = MergeOperation::CreateFromComparisonResult(*result);
// func_static_1 has 2 additions and 1 removal (== 1 addition, 1 replacement)
operation->foreachAction([&](const IMergeAction::Ptr& action)
{
auto addChildAction = std::dynamic_pointer_cast<AddChildAction>(action);
if (addChildAction && Node_isEntity(addChildAction->getParent()) &&
Node_getEntity(addChildAction->getParent())->getKeyValue("name") == "func_static_1")
{
addChildAction->deactivate();
}
auto removeChildAction = std::dynamic_pointer_cast<RemoveChildAction>(action);
if (removeChildAction && Node_isEntity(removeChildAction->getNodeToRemove()->getParent()) &&
Node_getEntity(removeChildAction->getNodeToRemove()->getParent())->getKeyValue("name") == "func_static_1")
{
removeChildAction->deactivate();
}
});
// func_static_1 has 2 brushes, this should stay the same
// 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), 2);
}
TEST_F(MapMergeTest, GroupDifferenceSameMap)
{
auto result = performComparison("maps/merging_groups_1.mapx", _context.getTestProjectPath() + "maps/merging_groups_1.mapx");
// Make sure we do have groups in the map
std::size_t groupCount = 0;
result->getSourceRootNode()->getSelectionGroupManager().foreachSelectionGroup([&](const selection::ISelectionGroup& group)
{
++groupCount;
});
EXPECT_NE(groupCount, 0) << "We don't have any groups in the test map, this test is useless.";
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Comparing a map with itself shouldn't produce a group diff";
}
TEST_F(MapMergeTest, GroupMemberOrdering)
{
auto originalResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(originalResource->load()) << "Test map not found";
auto changedResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(changedResource->load()) << "Test map not found";
auto result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Unchanged resource should be the same as the original";
// Create a group from 3 defined brushes, in a specific order
auto& originalGroupManager = originalResource->getRootNode()->getSelectionGroupManager();
auto originalGroup = originalGroupManager.createSelectionGroup();
// Find the three defined brushes
auto brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/11");
auto brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/12");
auto brush13 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/13");
originalGroup->addNode(brush11);
originalGroup->addNode(brush12);
originalGroup->addNode(brush13);
// Do the same in the other map, but in a different order
auto& changedGroupManager = changedResource->getRootNode()->getSelectionGroupManager();
auto changedGroup = changedGroupManager.createSelectionGroup();
// Find the three defined brushes
brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/11");
brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/12");
brush13 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/13");
changedGroup->addNode(brush11);
changedGroup->addNode(brush13);
changedGroup->addNode(brush12);
result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Group ordering shouldn't make a difference";
}
// Group links between entities should use the entity's name to check for equivalence
// even if the entity has changed key values or primitives, the link is intact when the name is equal
TEST_F(MapMergeTest, GroupLinksBetweenEntities)
{
auto originalResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(originalResource->load()) << "Test map not found";
auto changedResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(changedResource->load()) << "Test map not found";
auto result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Unchanged resource should be the same as the original";
// Create a group out of two brushes and an entity
auto& originalGroupManager = originalResource->getRootNode()->getSelectionGroupManager();
auto originalGroup = originalGroupManager.createSelectionGroup();
// Find the two defined brushes
auto brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/11");
auto brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/12");
auto funcStatic = algorithm::getEntityByName(originalResource->getRootNode(), "expandable");
originalGroup->addNode(brush11);
originalGroup->addNode(brush12);
originalGroup->addNode(funcStatic);
// Do the same in the other map, but in a different order
auto& changedGroupManager = changedResource->getRootNode()->getSelectionGroupManager();
auto changedGroup = changedGroupManager.createSelectionGroup();
// Find the two defined brushes
brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/11");
brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/12");
funcStatic = algorithm::getEntityByName(changedResource->getRootNode(), "expandable");
// Change a key value on the entity to change its fingerprint
Node_getEntity(funcStatic)->setKeyValue("changedkey", "changedvalue");
changedGroup->addNode(brush11);
changedGroup->addNode(funcStatic);
changedGroup->addNode(brush12);
result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Group ordering shouldn't make a difference and should only look at the names";
}
// Entity with no changed geometry, but with changed group membership in its children
TEST_F(MapMergeTest, GroupDifferenceInMatchingEntity)
{
auto originalResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(originalResource->load()) << "Test map not found";
auto changedResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(changedResource->load()) << "Test map not found";
auto result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Unchanged resource should be the same as the original";
// Create a group from 3 defined brushes in the one map
auto& originalGroupManager = originalResource->getRootNode()->getSelectionGroupManager();
auto originalGroup = originalGroupManager.createSelectionGroup();
auto brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/11");
auto brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/12");
auto brush13 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/13");
originalGroup->addNode(brush11);
originalGroup->addNode(brush12);
originalGroup->addNode(brush13);
// Do the same in the other map, but with one more brush
auto& changedGroupManager = changedResource->getRootNode()->getSelectionGroupManager();
auto changedGroup = changedGroupManager.createSelectionGroup();
// Find the three defined brushes
brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/11");
brush12 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/12");
brush13 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/13");
auto brush14 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(changedResource->getRootNode()), "textures/numbers/14");
changedGroup->addNode(brush11);
changedGroup->addNode(brush12);
changedGroup->addNode(brush13);
changedGroup->addNode(brush14); // one additional brush
result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
// We should get 4 differences: one for each brush (brush11/12/13 and the additional brush14)
EXPECT_EQ(result->selectionGroupDifferences.size(), 4) << "Group difference not detected";
auto groupMemberMismatches = std::count_if(result->selectionGroupDifferences.begin(), result->selectionGroupDifferences.end(),
[&](const ComparisonResult::GroupDifference& diff) { return diff.type == ComparisonResult::GroupDifference::Type::GroupMemberMismatch; });
EXPECT_EQ(groupMemberMismatches, 3);
auto membershipCountMismatches = std::count_if(result->selectionGroupDifferences.begin(), result->selectionGroupDifferences.end(),
[&](const ComparisonResult::GroupDifference& diff) { return diff.type == ComparisonResult::GroupDifference::Type::MembershipCountMismatch; });
EXPECT_EQ(membershipCountMismatches, 1);
}
// Entity with no changed geometry, entity itself has different group membership
TEST_F(MapMergeTest, GroupDifferenceOfMatchingEntity)
{
auto originalResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(originalResource->load()) << "Test map not found";
auto changedResource = GlobalMapResourceManager().createFromPath("maps/merging_groups_1.mapx");
EXPECT_TRUE(changedResource->load()) << "Test map not found";
auto result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
EXPECT_TRUE(result->selectionGroupDifferences.empty()) << "Unchanged resource should be the same as the original";
auto& changedGroupManager = changedResource->getRootNode()->getSelectionGroupManager();
auto changedGroup = changedGroupManager.createSelectionGroup();
// This entity already forms a group with light, add another group on top of that
auto funcStatic = algorithm::getEntityByName(changedResource->getRootNode(), "expandable");
auto brush11 = algorithm::findFirstBrushWithMaterial(algorithm::findWorldspawn(originalResource->getRootNode()), "textures/numbers/11");
changedGroup->addNode(funcStatic);
changedGroup->addNode(brush11);
// Compare the unchanged map to the one with the new group
result = GraphComparer::Compare(changedResource->getRootNode(), originalResource->getRootNode());
// We should get 2 differences: one for the entity node itself and one for brush 11 (both have different group membership count)
EXPECT_EQ(result->selectionGroupDifferences.size(), 2) << "Group difference not detected";
auto membershipCountMismatches = std::count_if(result->selectionGroupDifferences.begin(), result->selectionGroupDifferences.end(),
[&](const ComparisonResult::GroupDifference& diff) { return diff.type == ComparisonResult::GroupDifference::Type::MembershipCountMismatch; });
EXPECT_EQ(membershipCountMismatches, 2);
}
// Entity with mismatching geometry, three of the child primitives have different group memberships
TEST_F(MapMergeTest, GroupDifferenceInMismatchingEntity)
{