File: Map.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (1588 lines) | stat: -rw-r--r-- 44,358 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
#include "Map.h"

#include "i18n.h"
#include <ostream>
#include <fstream>
#include "itextstream.h"
#include "iscenegraph.h"
#include "icameraview.h"
#include "imodel.h"
#include "igrid.h"
#include "ifilesystem.h"
#include "ifiletypes.h"
#include "iselectiongroup.h"
#include "ifilter.h"
#include "icounter.h"
#include "iradiant.h"
#include "imapresource.h"
#include "imapinfofile.h"
#include "iaasfile.h"
#include "igame.h"
#include "imru.h"
#include "imapformat.h"

#include "registry/registry.h"
#include "entitylib.h"
#include "gamelib.h"
#include "os/path.h"
#include "os/file.h"
#include "time/ScopeTimer.h"

#include "brush/BrushModule.h"
#include "scene/BasicRootNode.h"
#include "scene/PrefabBoundsAccumulator.h"
#include "map/MapFileManager.h"
#include "map/MapPositionManager.h"
#include "map/MapResource.h"
#include "map/algorithm/Import.h"
#include "map/algorithm/Export.h"
#include "scene/Traverse.h"
#include "map/algorithm/MapExporter.h"
#include "model/export/ModelExporter.h"
#include "model/export/ModelScalePreserver.h"
#include "messages/ScopedLongRunningOperation.h"
#include "messages/FileOverwriteConfirmation.h"
#include "messages/FileSaveConfirmation.h"
#include "messages/MapOperationMessage.h"
#include "selection/algorithm/Primitives.h"
#include "selection/algorithm/Group.h"
#include "scene/Group.h"
#include "selection/algorithm/Transformation.h"
#include "module/StaticModule.h"
#include "command/ExecutionNotPossible.h"
#include "MapPropertyInfoFileModule.h"
#include "messages/NotificationMessage.h"

#include <fmt/format.h>

#include "messages/MapFileOperation.h"
#include "scene/ChildPrimitives.h"
#include "scene/merge/GraphComparer.h"
#include "scene/merge/MergeOperation.h"
#include "scene/merge/ThreeWayMergeOperation.h"
#include "scene/merge/MergeLib.h"

namespace map
{

namespace
{
    const char* const MAP_UNNAMED_STRING = N_("unnamed.map");
}

Map::Map() :
    _lastCopyMapName(""),
    _modified(false),
    _saveInProgress(false),
    _shutdownListener(0)
{}

void Map::clearMapResource()
{
    // Map is unnamed or load failed, reset map resource node to empty
    _resource->clear();

    _resource->getRootNode()->getUndoChangeTracker().setSavedChangeCount();

    // Rename the map to "unnamed" in any case to avoid overwriting the failed map
    setMapName(_(MAP_UNNAMED_STRING));
}

void Map::connectToRootNode()
{
    _modifiedStatusListener.disconnect();
    _undoEventListener.disconnect();
    _layerHierarchyChangedListener.disconnect();

    _modifiedStatusListener = _resource->signal_modifiedStatusChanged().connect(
        [this](bool newStatus) { setModified(newStatus); }
    );

    if (!_resource->getRootNode()) return;

    _undoEventListener = _resource->getRootNode()->getUndoSystem().signal_undoEvent().connect(
        sigc::mem_fun(*this, &Map::onUndoEvent)
    );

    // This is a workaround - changing layer hierarchies is not an undoable operation
    // and this by hitting undo or redo the status might be reset to "unmodified" anytime
    _layerHierarchyChangedListener = _resource->getRootNode()->getLayerManager()
        .signal_layerHierarchyChanged().connect(sigc::mem_fun(*this, &Map::onLayerHierarchyChanged));
}

void Map::onLayerHierarchyChanged()
{
    setModified(true);
}

void Map::onUndoEvent(IUndoSystem::EventType type, const std::string& operationName)
{
    switch (type)
    {
    case IUndoSystem::EventType::OperationRecorded:
        OperationMessage::Send(operationName);
        break;

    case IUndoSystem::EventType::OperationUndone:
        _mapPostUndoSignal.emit();
        OperationMessage::Send(fmt::format(_("Undo: {0}"), operationName));
        break;

    case IUndoSystem::EventType::OperationRedone:
        _mapPostRedoSignal.emit();
        OperationMessage::Send(fmt::format(_("Redo: {0}"), operationName));
        break;
    }
}

void Map::loadMapResourceFromPath(const std::string& path)
{
    // Create a MapLocation defining a physical file, and forward the call
    loadMapResourceFromLocation(MapLocation{path, false, ""});
}

void Map::loadMapResourceFromArchive(const std::string& archive, const std::string& archiveRelativePath)
{
    // Create a MapLocation defining an archive file, and forward the call
    loadMapResourceFromLocation(MapLocation{ archive, true, archiveRelativePath });
}

void Map::loadMapResourceFromLocation(const MapLocation& location)
{
    rMessage() << "Loading map from " << location.path <<
        (location.isArchive ? " [" + location.archiveRelativePath + "]" : "") << std::endl;

	// Map loading started
	emitMapEvent(MapLoading);

    // Abort any ongoing merge
    abortMergeOperation();

	_resource = location.isArchive ?
        GlobalMapResourceManager().createFromArchiveFile(location.path, location.archiveRelativePath) :
        GlobalMapResourceManager().createFromPath(location.path);

    assert(_resource);

    try
    {
        util::ScopeTimer timer("map load");

        if (isUnnamed() || !_resource->load())
        {
            clearMapResource();
        }
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
        clearMapResource();
    }

    connectToRootNode();

    // Take the new node and insert it as map root
    GlobalSceneGraph().setRoot(_resource->getRootNode());

	// Traverse the scenegraph and find the worldspawn
	findWorldspawn();

    // Associate the Scenegaph with the global RenderSystem
    // This usually takes a while since all editor textures are loaded - display a dialog to inform the user
    {
        radiant::ScopedLongRunningOperation blocker(_("Loading textures..."));

        assignRenderSystem(GlobalSceneGraph().root());
    }

    // Update layer visibility of all nodes
    scene::UpdateNodeVisibilityWalker updater(_resource->getRootNode()->getLayerManager());
    _resource->getRootNode()->traverse(updater);

    // Map loading finished, emit the signal
    emitMapEvent(MapLoaded);

    OperationMessage::Send(_("Map loaded"));

    rMessage() << "--- LoadMapFile ---\n";
    rMessage() << _mapName << "\n";

    rMessage() << GlobalCounters().getCounter(counterBrushes).get() << " brushes\n";
    rMessage() << GlobalCounters().getCounter(counterPatches).get() << " patches\n";
    rMessage() << GlobalCounters().getCounter(counterEntities).get() << " entities\n";

    // Let the filtersystem update the filtered status of all instances
    GlobalFilterSystem().update();

    // Clear the modified flag
    setModified(false);
}

void Map::assignRenderSystem(const scene::IMapRootNodePtr& root)
{
    root->setRenderSystem(std::dynamic_pointer_cast<RenderSystem>(
        module::GlobalModuleRegistry().getModule(MODULE_RENDERSYSTEM)));
}

void Map::finishMergeOperation()
{
    if (getEditMode() != EditMode::Merge)
    {
        rWarning() << "Not in merge edit mode, cannot finish any operation" << std::endl;
        return;
    }

    if (!_mergeOperation)
    {
        rError() << "Cannot merge, no active operation attached to this map." << std::endl;
        return;
    }

    // Prepare the scene, let the merge nodes know about the upcoming merge
    // and remove them from the scene, to leave it untouched
    for (const auto& mergeActionNode : _mergeActionNodes)
    {
        mergeActionNode->prepareForMerge();

        scene::removeNodeFromParent(mergeActionNode);

        // Clear any references this node holds
        mergeActionNode->clear();
    }

    _mergeActionNodes.clear();

    // At this point the scene should look the same as before the merge
    {
        UndoableCommand cmd("mergeMap");
        _mergeOperation->applyActions();

        cleanupMergeOperation();
    }
    setEditMode(EditMode::Normal);
    emitMapEvent(IMap::MapMergeOperationFinished);
}

void Map::cleanupMergeOperation()
{
    for (const auto& mergeActionNode : _mergeActionNodes)
    {
        // If the node is already removed from the scene, this does nothing
        scene::removeNodeFromParent(mergeActionNode);

        // Clear any references this node holds
        mergeActionNode->clear();
    }

    _mergeOperationListener.disconnect();
    _mergeActionNodes.clear();
    _mergeOperation.reset();
}

void Map::abortMergeOperation()
{
    bool mergeWasActive = _mergeOperation != nullptr;

    // Remove the nodes and switch back to normal without applying the operation
    cleanupMergeOperation();
    setEditMode(EditMode::Normal);

    if (mergeWasActive)
    {
        emitMapEvent(IMap::MapMergeOperationAborted);
    }
}

scene::merge::IMergeOperation::Ptr Map::getActiveMergeOperation()
{
    return _editMode == EditMode::Merge ? _mergeOperation : scene::merge::IMergeOperation::Ptr();
}

void Map::setMapName(const std::string& newName)
{
    bool mapNameChanged = _mapName != newName;

    // Store the name into the member
    _mapName = newName;

    // Update the map resource's root node, if there is one
    if (_resource)
	{
        _resource->rename(newName);
    }

    if (mapNameChanged)
    {
        // Fire the signal to any observers
        signal_mapNameChanged().emit();
    }
}

sigc::signal<void>& Map::signal_mapNameChanged()
{
    return _mapNameChangedSignal;
}

std::string Map::getMapName() const {
    return _mapName;
}

bool Map::isUnnamed() const {
    return _mapName == _(MAP_UNNAMED_STRING);
}

namespace
{

bool pointfileNameMatch(const std::string& candidate, const std::string& mapStem)
{
    // A matching point file either has an identical stem to the map file, or the map file stem
    // with an underscore suffix (e.g. "mapfile_portal_123_456.lin")
    return string::iequals(candidate, mapStem) || string::istarts_with(candidate, mapStem + "_");
}

} // namespace

void Map::forEachPointfile(PointfileFunctor func) const
{
    static const char* LIN_EXT = ".lin";

    const fs::path map(getMapName());
    const fs::path mapDir = map.parent_path();
    const fs::path mapStem = map.stem();

    // Don't bother trying to iterate over a missing map directory, this will
    // just throw an exception.
    if (!fs::is_directory(mapDir))
        return;

    // Iterate over files in the map directory, putting them in a sorted set
    std::set<fs::path> paths;
    for (const auto& entry: fs::directory_iterator(mapDir))
    {
        // Ignore anything which isn't a .lin file
        auto entryPath = entry.path();
        if (entryPath.extension() == LIN_EXT
            && pointfileNameMatch(entryPath.stem().string(), mapStem.string()))
        {
            paths.insert(entryPath);
        }
    }

    // Call functor on paths in order
    for (const fs::path& p: paths)
        func(p);
}

void Map::showPointFile(const fs::path& filePath)
{
    _pointTrace->show(filePath);
}

bool Map::isPointTraceVisible() const
{
    return _pointTrace->isVisible();
}

void Map::onSceneNodeErase(const scene::INodePtr& node)
{
	// Detect when worldspawn is removed from the map
	if (node == _worldSpawnNode)
	{
		_worldSpawnNode.reset();
	}
}

void Map::setWorldspawn(const scene::INodePtr& node)
{
    _worldSpawnNode = node;
}

Map::MapEventSignal Map::signal_mapEvent() const
{
	return _mapEvent;
}

Map::EditMode Map::getEditMode()
{
    return _editMode;
}

void Map::setEditMode(EditMode mode)
{
    _editMode = mode;

    if (_editMode == EditMode::Merge)
    {
        GlobalSelectionSystem().setSelectedAll(false);
        GlobalSelectionSystem().setSelectionMode(selection::SelectionMode::MergeAction);

        if (getRoot())
        {
            getRoot()->getRenderSystem()->setMergeModeEnabled(true);
        }
    }
    else
    {
        GlobalSelectionSystem().setSelectionMode(selection::SelectionMode::Primitive);

        if (getRoot())
        {
            getRoot()->getRenderSystem()->setMergeModeEnabled(false);
        }
    }

    signal_editModeChanged().emit(_editMode);
    SceneChangeNotify();
}

sigc::signal<void, IMap::EditMode>& Map::signal_editModeChanged()
{
    return _mapEditModeChangedSignal;
}

const scene::INodePtr& Map::getWorldspawn()
{
    return _worldSpawnNode;
}

scene::IMapRootNodePtr Map::getRoot()
{
    if (_resource)
	{
        // Try to cast the node onto a root node and return
        return _resource->getRootNode();
    }

    return scene::IMapRootNodePtr();
}

MapFormatPtr Map::getFormat()
{
    return GlobalMapFormatManager().getMapFormatForFilename(_mapName);
}

MapFormatPtr Map::getMapFormatForFilenameSafe(const std::string& filename)
{
    auto candidate = GlobalMapFormatManager().getMapFormatForFilename(filename);

    // Fall back to the format of the current map if the selection is empty (#5808)
    return candidate ? candidate : getFormat();
}

// free all map elements, reinitialize the structures that depend on them
void Map::freeMap()
{
    // Abort any ongoing merge
    abortMergeOperation();

	// Fire the map unloading event,
	// This will de-select stuff, clear the pointfile, etc.
    emitMapEvent(MapUnloading);

	setWorldspawn(scene::INodePtr());

	GlobalSceneGraph().setRoot(scene::IMapRootNodePtr());

    emitMapEvent(MapUnloaded);

    // Reset the resource pointer
    _modifiedStatusListener.disconnect();
    _resource.reset();
}

bool Map::isModified() const
{
    return _modified;
}

void Map::setModified(bool modifiedFlag)
{
    if (_modified != modifiedFlag)
    {
        _modified = modifiedFlag;

        // when the map is modified, let the listeners now
        signal_modifiedChanged().emit();
    }

    // Reset the map save timer
    _mapSaveTimer.restart();
}

sigc::signal<void>& Map::signal_modifiedChanged()
{
    return _mapModifiedChangedSignal;
}

sigc::signal<void>& Map::signal_postUndo()
{
    return _mapPostUndoSignal;
}

sigc::signal<void>& Map::signal_postRedo()
{
    return _mapPostRedoSignal;
}

IUndoSystem& Map::getUndoSystem()
{
    const auto& rootNode = _resource->getRootNode();

    if (!rootNode)
    {
        throw std::runtime_error("No map resource loaded");
    }

    return rootNode->getUndoSystem();
}

// move the view to a certain position
void Map::focusViews(const Vector3& point, const Vector3& angles)
{
    // Set the camera and the views to the given point
    GlobalCameraManager().focusAllCameras(point, angles);

    // ortho views might not be present in headless mode
    if (module::GlobalModuleRegistry().moduleExists(MODULE_ORTHOVIEWMANAGER))
    {
        GlobalOrthoViewManager().setOrigin(point);
    }
}

void Map::focusViewCmd(const cmd::ArgumentList& args)
{
    if (args.size() != 2)
    {
        rWarning() << "Usage: FocusViews <origin:Vector3> <angles:Vector3>" << std::endl;
        return;
    }

    focusViews(args[0].getVector3(), args[1].getVector3());
}

void Map::focusCameraOnSelectionCmd(const cmd::ArgumentList& args)
{
    if (GlobalSelectionSystem().countSelected() == 0)
    {
        throw cmd::ExecutionNotPossible(_("Cannot focus, selection is empty"));
    }

    // Determine the bounds of the current selection
    const auto& workZone = GlobalSelectionSystem().getWorkZone();
    auto originAndAngles = scene::getOriginAndAnglesToLookAtBounds(workZone.bounds);

    // Set the camera and the views to the given point
    GlobalCameraManager().focusAllCameras(originAndAngles.first, originAndAngles.second);
}

scene::INodePtr Map::findWorldspawn()
{
	scene::INodePtr worldspawn;

    // Traverse the scenegraph and search for the worldspawn
	GlobalSceneGraph().root()->foreachNode([&](const scene::INodePtr& node)
	{
		if (Node_isWorldspawn(node))
		{
			worldspawn = node;
			return false; // done traversing
		}

		return true;
	});

	// Set the worldspawn, might be null if nothing was found
	setWorldspawn(worldspawn);

    return worldspawn;
}

scene::INodePtr Map::createWorldspawn()
{
	scene::INodePtr worldspawn(GlobalEntityModule().createEntity(
		GlobalEntityClassManager().findOrInsert("worldspawn", true)));

	// We want the world spawn entity to go for the pole position
	GlobalSceneGraph().root()->addChildNodeToFront(worldspawn);

	return worldspawn;
}

const scene::INodePtr& Map::findOrInsertWorldspawn()
{
	// If we don't know any worldspawn yet, and can't find one either,
	// let's create one afresh
	if (!_worldSpawnNode && findWorldspawn() == nullptr)
	{
		setWorldspawn(createWorldspawn());
	}

    return _worldSpawnNode;
}

void Map::load(const std::string& filename)
{
    setMapName(filename);
    loadMapResourceFromPath(_mapName);
}

bool Map::save(const MapFormatPtr& mapFormat)
{
    if (_saveInProgress) return false; // safeguard

    if (_resource->isReadOnly())
    {
        rError() << "This map is read-only and cannot be saved." << std::endl;
        return false;
    }

    // Check if the map file has been modified in the meantime
    if (_resource->fileOnDiskHasBeenModifiedSinceLastSave() &&
        !radiant::FileOverwriteConfirmation::SendAndReceiveAnswer(
            fmt::format(_("The file {0} has been modified since it was last saved,\nperhaps by another application. "
                "Do you really want to overwrite the file?"), _mapName), _("File modification detected")))
    {
        return false;
    }

    _saveInProgress = true;

    emitMapEvent(MapSaving);

    util::ScopeTimer timer("map save");

    bool success = false;

    // Save the actual map resource
    try
    {
        _resource->save(mapFormat);

        // Clear the modified flag
        setModified(false);

        success = true;
    }
    catch (IMapResource::OperationException & ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }

    emitMapEvent(MapSaved);
    OperationMessage::Send(_("Map saved"));

    _saveInProgress = false;

    // Redraw the views, sometimes the backbuffer containing
    // the previous frame will remain visible
    SceneChangeNotify();

    return success;
}

void Map::createNewMap()
{
    setMapName(_(MAP_UNNAMED_STRING));

	loadMapResourceFromPath(_mapName);

    SceneChangeNotify();

    setModified(false);

    OperationMessage::Send(_("Empty Map created"));
    focusViews(Vector3(0,0,30), Vector3(0,0,0));
}

IMapExporter::Ptr Map::createMapExporter(IMapWriter& writer,
    const scene::IMapRootNodePtr& root, std::ostream& mapStream)
{
    return std::make_shared<MapExporter>(writer, root, mapStream, 0);
}

bool Map::import(const std::string& filename)
{
    bool success = false;

    IMapResourcePtr resource = GlobalMapResourceManager().createFromPath(filename);

    try
    {
        if (resource->load())
        {
            // load() returned TRUE, this means that the resource node
            // is not the NULL node
            const auto& otherRoot = resource->getRootNode();

            // Adjust all new names to fit into the existing map namespace
            algorithm::prepareNamesForImport(getRoot(), otherRoot);

            algorithm::importMap(otherRoot);
            success = true;
        }

        SceneChangeNotify();
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }

    return success;
}

void Map::saveDirect(const std::string& filename, const MapFormatPtr& mapFormat)
{
    if (_saveInProgress) return; // safeguard

    _saveInProgress = true;

	MapFormatPtr format = mapFormat;

	if (!mapFormat)
	{
		format = getMapFormatForFilenameSafe(filename);
	}

    try
    {
        MapResource::saveFile(*format, GlobalSceneGraph().root(), scene::traverse, filename);
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }

    _saveInProgress = false;
}

void Map::saveSelected(const std::string& filename, const MapFormatPtr& mapFormat)
{
    if (_saveInProgress) return; // safeguard

    _saveInProgress = true;

	MapFormatPtr format = mapFormat;

	if (!format)
	{
		format = getMapFormatForFilenameSafe(filename);
	}

    try
    {
        MapResource::saveFile(
            *format,
            GlobalSceneGraph().root(),
            scene::traverseSelected, // TraversalFunc
            filename
        );
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }

    _saveInProgress = false;
}

std::string Map::getSaveConfirmationText() const
{
    std::string primaryText = fmt::format(_("Save changes to map \"{0}\"\nbefore closing?"), _mapName);

    // Display "x seconds" or "x minutes"
    int seconds = static_cast<int>(_mapSaveTimer.getSecondsPassed());
    std::string timeString;
    if (seconds > 120)
    {
        timeString = fmt::format(_("{0:d} minutes"), (seconds / 60));
    }
    else
    {
        timeString = fmt::format(_("{0:d} seconds"), seconds);
    }

    std::string secondaryText = fmt::format(
        _("If you don't save, changes from the last {0}\nwill be lost."), timeString);

    std::string confirmText = fmt::format("{0}\n\n{1}", primaryText, secondaryText);

    return confirmText;
}

bool Map::askForSave(const std::string& title)
{
    if (!isModified())
    {
        // Map is not modified, return positive
        return true;
    }

    // Ask the user
    auto answer = radiant::FileSaveConfirmation::SendAndReceiveAnswer(getSaveConfirmationText(), title);

    if (answer == radiant::FileSaveConfirmation::Action::Cancel)
    {
        return false;
    }

    if (answer == radiant::FileSaveConfirmation::Action::SaveChanges)
    {
        // The user wants to save the map
        if (isUnnamed())
        {
            // Map still unnamed, try to save the map with a new name
            // and take the return value from the other routine.
            return saveAs();
        }
        else
        {
            // Map is named, save it
            save();
        }
    }

    // Default behaviour: allow the action
    return true;
}

bool Map::saveAs()
{
    if (_saveInProgress) return false; // safeguard

    auto fileInfo = MapFileManager::getMapFileSelection(false,
        _("Save Map"), filetype::TYPE_MAP, getMapName());

    if (fileInfo.fullPath.empty())
    {
        // Invalid filename entered, return false
        return false;
    }

    // Remember the old resource, we might need to revert
    auto oldResource = _resource;

    // Create a new resource pointing to the given path...
    _resource = GlobalMapResourceManager().createFromPath(fileInfo.fullPath);

    // ...and import the existing root node into that resource
    _resource->setRootNode(oldResource->getRootNode());

    // Try to save the resource, this might fail
    if (!save(fileInfo.mapFormat))
    {
        // Failure, revert the change
        _resource = oldResource;
        return false;
    }

    connectToRootNode();

    // Resource save was successful, notify about this name change
    rename(fileInfo.fullPath);

    // add an MRU entry on success
    GlobalMRU().insert(fileInfo.fullPath);

    return true;
}

void Map::saveCopyAs()
{
    // Let's see if we can remember a map name from a previous save
    if (_lastCopyMapName.empty())
    {
        _lastCopyMapName = getMapName();
    }

	auto fileInfo = MapFileManager::getMapFileSelection(false,
        _("Save Copy As..."), filetype::TYPE_MAP, _lastCopyMapName);

	if (!fileInfo.fullPath.empty())
	{
        saveCopyAs(fileInfo.fullPath, fileInfo.mapFormat);
    }
}

void Map::saveCopyAs(const std::string& absolutePath, const MapFormatPtr& mapFormat)
{
    if (absolutePath.empty())
    {
        rWarning() << "Map::saveCopyAs: path must not be empty" << std::endl;
        return;
    }

    // Remember the last name
    _lastCopyMapName = absolutePath;

    // Return the result of the actual save method
    saveDirect(absolutePath, mapFormat);
}

void Map::loadPrefabAt(const cmd::ArgumentList& args)
{
    if (args.size() < 2 || args.size() > 4)
    {
        rWarning() << "Usage: " << LOAD_PREFAB_AT_CMD <<
            " <prefabPath:String> <targetCoords:Vector3> [insertAsGroup:0|1] [recalculatePrefabOrigin:0|1]" << std::endl;
        return;
    }

    auto prefabPath = args[0].getString();
    auto targetCoords = args[1].getVector3();
    auto insertAsGroup = args.size() > 2 ? args[2].getBoolean() : false;
    auto recalculatePrefabOrigin = args.size() > 3 ? args[3].getBoolean() : true;

	if (!prefabPath.empty())
	{
        UndoableCommand undo("loadPrefabAt");

        // Deselect everything
        GlobalSelectionSystem().setSelectedAll(false);

        // Now import the prefab (imported items get selected)
		import(prefabPath);

        // Get the selection bounds, snap its origin to the grid
        scene::PrefabBoundsAccumulator accumulator;
        GlobalSelectionSystem().foreachSelected(accumulator);

        if (recalculatePrefabOrigin)
        {
            auto prefabCenter = accumulator.getBounds().getOrigin().getSnapped(GlobalGrid().getGridSize());

            // Switch texture lock on
            bool prevTexLockState = GlobalBrush().textureLockEnabled();
            GlobalBrush().setTextureLock(true);

            // Translate the selection to the given point
            selection::algorithm::translateSelected(targetCoords - prefabCenter);

            // Revert to previous state
            GlobalBrush().setTextureLock(prevTexLockState);
        }

		// Check whether we should group the prefab parts
		if (insertAsGroup && GlobalSelectionSystem().countSelected() > 1)
		{
			try
			{
				selection::groupSelected();
			}
			catch (cmd::ExecutionNotPossible& ex)
			{
				// Ignore grouping errors on prefab insert, just log the message
				rError() << "Error grouping the prefab: " << ex.what() << std::endl;
			}
		}
    }
}

void Map::saveMapCopyAs(const cmd::ArgumentList& args)
{
    if (args.size() == 0 || args[0].getString().empty())
    {
        // Use the overload without arguments, it will ask for a file name
        saveCopyAs();
    }
    else
    {
        // Pass the first argument we got
        saveCopyAs(args[0].getString());
    }
}

void Map::saveAutomaticMapBackup(const cmd::ArgumentList& args)
{
    // Use the saveDirect routine to not change with the _lastCopyMapName member
    saveDirect(args[0].getString());
}

void Map::registerCommands()
{
    GlobalCommandSystem().addCommand("NewMap", Map::newMap);
    GlobalCommandSystem().addCommand("OpenMap", std::bind(&Map::openMapCmd, this, std::placeholders::_1),
        { cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL });
    GlobalCommandSystem().addCommand("OpenMapFromArchive", Map::openMapFromArchive, { cmd::ARGTYPE_STRING, cmd::ARGTYPE_STRING });
    GlobalCommandSystem().addCommand("ImportMap", Map::importMap);
    GlobalCommandSystem().addCommand("StartMergeOperation", std::bind(&Map::startMergeOperationCmd, this, std::placeholders::_1),
        { cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL, cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL });
    GlobalCommandSystem().addCommand("AbortMergeOperation", std::bind(&Map::abortMergeOperationCmd, this, std::placeholders::_1));
    GlobalCommandSystem().addCommand("FinishMergeOperation", std::bind(&Map::finishMergeOperationCmd, this, std::placeholders::_1));
    GlobalCommandSystem().addCommand(LOAD_PREFAB_AT_CMD, std::bind(&Map::loadPrefabAt, this, std::placeholders::_1),
        { cmd::ARGTYPE_STRING, cmd::ARGTYPE_VECTOR3, cmd::ARGTYPE_INT|cmd::ARGTYPE_OPTIONAL, cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL });
    GlobalCommandSystem().addCommand("SaveSelectedAsPrefab", Map::saveSelectedAsPrefab);
    GlobalCommandSystem().addCommand("SaveMap", std::bind(&Map::saveMapCmd, this, std::placeholders::_1));
    GlobalCommandSystem().addCommand("SaveMapAs", Map::saveMapAs);
    GlobalCommandSystem().addCommand("SaveMapCopyAs", std::bind(&Map::saveMapCopyAs, this, std::placeholders::_1), { cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL });
    // Command used by the autosaver to save a copy without messing with the remembered paths
    GlobalCommandSystem().addCommand("SaveAutomaticBackup", std::bind(&Map::saveAutomaticMapBackup, this, std::placeholders::_1), { cmd::ARGTYPE_STRING });
    GlobalCommandSystem().addCommand("ExportMap", std::bind(&Map::exportMap, this, std::placeholders::_1));
    GlobalCommandSystem().addCommand("SaveSelected", Map::exportSelection);
    GlobalCommandSystem().addCommand("FocusViews", std::bind(&Map::focusViewCmd, this, std::placeholders::_1), { cmd::ARGTYPE_VECTOR3, cmd::ARGTYPE_VECTOR3 });
    GlobalCommandSystem().addCommand("FocusCameraViewOnSelection", std::bind(&Map::focusCameraOnSelectionCmd, this, std::placeholders::_1));
    // ExportSelectedAsModel <Path> <ExportFormat> [<ExportOrigin>] [<OriginEntityName>] [<CustomOrigin>] [<SkipCaulk>] [<ReplaceSelectionWithModel>] [<ExportLightsAsObjects>]
	GlobalCommandSystem().addCommand("ExportSelectedAsModel", algorithm::exportSelectedAsModelCmd,
        { cmd::ARGTYPE_STRING, // path
          cmd::ARGTYPE_STRING, // export format
          cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL, // export origin type
          cmd::ARGTYPE_STRING | cmd::ARGTYPE_OPTIONAL, // origin entity name
          cmd::ARGTYPE_VECTOR3 | cmd::ARGTYPE_OPTIONAL, // custom origin
          cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL, // skip caulk
          cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL, // replace selection with model
          cmd::ARGTYPE_INT | cmd::ARGTYPE_OPTIONAL }); // export lights as objects

    // Add undo commands
    GlobalCommandSystem().addCommand("Undo", std::bind(&Map::undoCmd, this, std::placeholders::_1));
    GlobalCommandSystem().addCommand("Redo", std::bind(&Map::redoCmd, this, std::placeholders::_1));
}

void Map::undoCmd(const cmd::ArgumentList& args)
{
    try
    {
        getUndoSystem().undo();
    }
    catch (const std::runtime_error& err)
    {
        throw cmd::ExecutionNotPossible(err.what());
    }
}

void Map::redoCmd(const cmd::ArgumentList& args)
{
    try
    {
        getUndoSystem().redo();
    }
    catch (const std::runtime_error& err)
    {
        throw cmd::ExecutionNotPossible(err.what());
    }
}

// Static command targets
void Map::newMap(const cmd::ArgumentList& args)
{
    if (GlobalMap().askForSave(_("New Map")))
	{
        GlobalMap().freeMap();
        GlobalMap().createNewMap();
    }
}

void Map::openMapCmd(const cmd::ArgumentList& args)
{
    if (!askForSave(_("Open Map"))) return;

    std::string candidate;

    if (!args.empty())
    {
        candidate = args[0].getString();
    }
    else
    {
        // No arguments passed, get the map file name to load
        auto fileInfo = MapFileManager::getMapFileSelection(true, _("Open map"), filetype::TYPE_MAP);
        candidate = fileInfo.fullPath;
    }

    std::string mapToLoad;

    if (os::fileOrDirExists(candidate))
    {
        mapToLoad = candidate;
    }
    else if (!candidate.empty())
    {
        // Try to open this file from the VFS (this will hit physical files
        // in the active project as well as files in registered PK4)
        if (GlobalFileSystem().openTextFile(candidate))
        {
            mapToLoad = candidate;
        }
        else
        {
            // Next, try to look up the map in the regular maps path
            fs::path mapsPath = GlobalGameManager().getMapPath();
            fs::path fullMapPath = mapsPath / candidate;

            if (os::fileOrDirExists(fullMapPath.string()))
            {
                mapToLoad = fullMapPath.string();
            }
            else
            {
                throw cmd::ExecutionFailure(fmt::format(_("File doesn't exist: {0}"), candidate));
            }
        }
    }

    if (!mapToLoad.empty())
	{
        GlobalMRU().insert(mapToLoad);

        freeMap();
        load(mapToLoad);
    }
}

void Map::openMapFromArchive(const cmd::ArgumentList& args)
{
    if (args.size() != 2)
    {
        rWarning() << "Usage: OpenMapFromArchive <pathToPakFile> <pathWithinArchive>" << std::endl;
        return;
    }

    if (!GlobalMap().askForSave(_("Open Map"))) return;

    std::string pathToArchive = args[0].getString();
    std::string relativePath = args[1].getString();

    if (!os::fileOrDirExists(pathToArchive))
    {
        throw cmd::ExecutionFailure(fmt::format(_("File not found: {0}"), pathToArchive));
    }

    if (!pathToArchive.empty())
    {
        GlobalMap().freeMap();
        GlobalMap().setMapName(relativePath);
        GlobalMap().loadMapResourceFromArchive(pathToArchive, relativePath);
    }
}

void Map::importMap(const cmd::ArgumentList& args)
{
    MapFileSelection fileInfo =
        MapFileManager::getMapFileSelection(true, _("Import map"), filetype::TYPE_MAP);

    if (!fileInfo.fullPath.empty())
    {
        UndoableCommand undo("mapImport");
        GlobalMap().import(fileInfo.fullPath);
    }
}

void Map::saveMapAs(const cmd::ArgumentList& args) {
    GlobalMap().saveAs();
}

void Map::saveMapCmd(const cmd::ArgumentList& args)
{
    if (isUnnamed() || (_resource && _resource->isReadOnly()))
    {
        saveAs();
    }
    // greebo: Always let the map be saved, regardless of the modified status.
    else /*if(GlobalMap().isModified())*/
    {
        save();
    }
}

void Map::exportMap(const cmd::ArgumentList& args)
{
    auto fileInfo = MapFileManager::getMapFileSelection(false, _("Export Map"), filetype::TYPE_MAP_EXPORT);

    if (!fileInfo.fullPath.empty())
	{
        if (!fileInfo.mapFormat)
        {
            fileInfo.mapFormat = getMapFormatForFilenameSafe(fileInfo.fullPath);
        }

        emitMapEvent(MapSaving);

        MapResource::saveFile(*fileInfo.mapFormat,
            GlobalSceneGraph().root(),
            scene::traverse,
            fileInfo.fullPath);

        emitMapEvent(MapSaved);
    }
}

void Map::exportSelection(const cmd::ArgumentList& args)
{
    MapFileSelection fileInfo =
        MapFileManager::getMapFileSelection(false, _("Export selection"), filetype::TYPE_MAP);

    if (!fileInfo.fullPath.empty())
	{
		GlobalMap().saveSelected(fileInfo.fullPath, fileInfo.mapFormat);
    }
}

void Map::saveSelectedAsPrefab(const cmd::ArgumentList& args)
{
    MapFileSelection fileInfo =
        MapFileManager::getMapFileSelection(false, _("Save selected as Prefab"), filetype::TYPE_PREFAB);

	if (!fileInfo.fullPath.empty())
    {
        GlobalMap().saveSelected(fileInfo.fullPath, fileInfo.mapFormat);
    }
}

void Map::rename(const std::string& filename)
{
    if (_mapName != filename)
    {
        setMapName(filename);
        SceneChangeNotify();
    }
    else
    {
        _resource->save();
        setModified(false);
    }
}

void Map::exportSelected(std::ostream& out)
{
    exportSelected(out, getFormat());
}

void Map::exportSelected(std::ostream& out, const MapFormatPtr& format)
{
    assert(format);

    // Create our main MapExporter walker for traversal
    auto writer = format->getMapWriter();

    try
    {
        MapExporter exporter(*writer, GlobalSceneGraph().root(), out);
        exporter.disableProgressMessages();

        // Pass the traverseSelected function and start writing selected nodes
        exporter.exportMap(GlobalSceneGraph().root(), scene::traverseSelected);
    }
    catch (FileOperation::OperationCancelled&)
    {
        radiant::NotificationMessage::SendInformation(_("Map export cancelled"));
    }
}

void Map::onMergeActionAdded(const scene::merge::IMergeAction::Ptr& action)
{
    UndoableCommand cmd("insertMergeAction");

    _mergeActionNodes.emplace_back(std::make_shared<scene::RegularMergeActionNode>(action));
    getRoot()->addChildNode(_mergeActionNodes.back());
}

void Map::createMergeActions()
{
    // Group spawnarg actions into one single node if applicable
    std::map<scene::INodePtr, std::vector<scene::merge::IMergeAction::Ptr>> entityChanges;
    std::vector<scene::merge::IMergeAction::Ptr> otherChanges;

    _mergeOperation->foreachAction([&](const scene::merge::IMergeAction::Ptr& action)
    {
        if (scene::merge::actionIsTargetingKeyValue(action))
        {
            auto& actions = entityChanges.try_emplace(action->getAffectedNode()).first->second;
            actions.push_back(action);
        }
        else // regular change, add it to the misc pile
        {
            otherChanges.push_back(action);
        }
    });

    _mergeOperationListener = _mergeOperation->sig_ActionAdded().connect(sigc::mem_fun(this, &Map::onMergeActionAdded));

    UndoableCommand cmd("createMergeOperation");

    // Construct all entity changes...
    for (const auto& pair : entityChanges)
    {
        _mergeActionNodes.emplace_back(std::make_shared<scene::KeyValueMergeActionNode>(pair.second));
        getRoot()->addChildNode(_mergeActionNodes.back());
    }

    // ...and the regular ones
    for (const auto& action : otherChanges)
    {
        _mergeActionNodes.emplace_back(std::make_shared<scene::RegularMergeActionNode>(action));
        getRoot()->addChildNode(_mergeActionNodes.back());
    }
}

void Map::prepareMergeOperation()
{
    if (!getRoot())
    {
        throw cmd::ExecutionNotPossible(_("No map loaded, cannot merge"));
    }

    {
        // Make sure we have a worldspawn in this map
        UndoableCommand cmd("ensureWorldSpawn");
        findOrInsertWorldspawn();
    }

    // Stop any pending merge operation
    abortMergeOperation();
}

void Map::startMergeOperation(const std::string& sourceMap)
{
    if (!os::fileOrDirExists(sourceMap))
    {
        throw cmd::ExecutionFailure(fmt::format(_("File doesn't exist: {0}"), sourceMap));
    }

    prepareMergeOperation();

    auto sourceMapResource = GlobalMapResourceManager().createFromPath(sourceMap);

    try
    {
        if (sourceMapResource->load())
        {
            assignRenderSystem(sourceMapResource->getRootNode());

            // Compare the scenes and get the report
            auto result = scene::merge::GraphComparer::Compare(sourceMapResource->getRootNode(), getRoot());

            // Create the merge actions
            _mergeOperation = scene::merge::MergeOperation::CreateFromComparisonResult(*result);

            if (_mergeOperation->hasActions())
            {
                // Create renderable merge actions
                createMergeActions();

                // Switch to merge mode
                setEditMode(EditMode::Merge);

                emitMapEvent(IMap::MapMergeOperationStarted);
            }
            else
            {
                radiant::NotificationMessage::SendInformation(_("The Merge Operation turns out to be empty, nothing to do."));
            }

            // Dispose of the resource, we don't need it anymore
            sourceMapResource->clear();
        }
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }
}

void Map::startMergeOperation(const std::string& sourceMap, const std::string& baseMap)
{
    prepareMergeOperation();

    auto baseMapResource = GlobalMapResourceManager().createFromPath(baseMap);
    auto sourceMapResource = GlobalMapResourceManager().createFromPath(sourceMap);

    try
    {
        if (sourceMapResource->load() && baseMapResource->load())
        {
            // Assign a rendersystem to let all nodes capture their shaders
            assignRenderSystem(sourceMapResource->getRootNode());
            assignRenderSystem(baseMapResource->getRootNode());

            _mergeOperation = scene::merge::ThreeWayMergeOperation::Create(
                baseMapResource->getRootNode(), sourceMapResource->getRootNode(), getRoot());

            if (_mergeOperation->hasActions())
            {
                // Create renderable merge actions
                createMergeActions();

                // Switch to merge mode
                setEditMode(EditMode::Merge);

                emitMapEvent(IMap::MapMergeOperationStarted);
            }
            else
            {
                radiant::NotificationMessage::SendInformation(_("The Merge Operation turns out to be empty, nothing to do."));
            }

            // Dispose of the resources, we don't need it anymore
            sourceMapResource->clear();
            baseMapResource->clear();
        }
    }
    catch (const IMapResource::OperationException& ex)
    {
        radiant::NotificationMessage::SendError(ex.what());
    }
}

void Map::startMergeOperationCmd(const cmd::ArgumentList& args)
{
    if (!getRoot())
    {
        throw cmd::ExecutionNotPossible(_("No map loaded, cannot merge"));
    }

    std::string sourceCandidate;
    std::string baseCandidate;

    if (!args.empty())
    {
        sourceCandidate = args[0].getString();
    }
    else
    {
        // No arguments passed, get the map file name to load
        auto fileInfo = MapFileManager::getMapFileSelection(true, _("Select Map File to merge"), filetype::TYPE_MAP);

        if (fileInfo.fullPath.empty())
        {
            return; // operation cancelled
        }

        sourceCandidate = fileInfo.fullPath;
    }

    if (!os::fileOrDirExists(sourceCandidate))
    {
        throw cmd::ExecutionFailure(fmt::format(_("File doesn't exist: {0}"), sourceCandidate));
    }

    // Do we have a second argument (base map)
    if (args.size() > 1)
    {
        baseCandidate = args[1].getString();

        if (!os::fileOrDirExists(baseCandidate))
        {
            throw cmd::ExecutionFailure(fmt::format(_("File doesn't exist: {0}"), baseCandidate));
        }
    }

    if (!baseCandidate.empty())
    {
        startMergeOperation(sourceCandidate, baseCandidate);
    }
    else
    {
        startMergeOperation(sourceCandidate);
    }
}

void Map::abortMergeOperationCmd(const cmd::ArgumentList& args)
{
    abortMergeOperation();
}

void Map::finishMergeOperationCmd(const cmd::ArgumentList& args)
{
    finishMergeOperation();
}

void Map::emitMapEvent(MapEvent ev)
{
    try
    {
        signal_mapEvent().emit(ev);
    }
    catch (std::runtime_error & ex)
    {
        radiant::NotificationMessage::SendError(fmt::format(_("Failure running map event {0}:\n{1}"), static_cast<int>(ev), ex.what()));
    }
}

// RegisterableModule implementation
const std::string& Map::getName() const
{
    static std::string _name(MODULE_MAP);
    return _name;
}

const StringSet& Map::getDependencies() const
{
    static StringSet _dependencies
    {
        MODULE_GAMEMANAGER,
        MODULE_SCENEGRAPH,
        MODULE_MAPINFOFILEMANAGER,
        MODULE_FILETYPES,
        MODULE_MAPRESOURCEMANAGER,
        MODULE_COMMANDSYSTEM
    };

    return _dependencies;
}

void Map::initialiseModule(const IApplicationContext& ctx)
{
    // Register for the startup event
	_mapPositionManager.reset(new MapPositionManager);

    GlobalSceneGraph().addSceneObserver(this);

    // Add the Map-related commands to the EventManager
    registerCommands();

    _scaledModelExporter.initialise();
    _modelScalePreserver.reset(new ModelScalePreserver);

    // Construct point trace and connect it to map signals
    _pointTrace.reset(new PointFile());
    signal_mapEvent().connect([this](IMap::MapEvent e)
                              { _pointTrace->onMapEvent(e); });

	MapFileManager::registerFileTypes();

    // Register an info file module to save the map property bag
    GlobalMapInfoFileManager().registerInfoFileModule(
        std::make_shared<MapPropertyInfoFileModule>()
    );

    // Free the map right before all modules are shut down
    module::GlobalModuleRegistry().signal_modulesUninitialising().connect(
        sigc::mem_fun(this, &Map::freeMap)
    );

    _shutdownListener = GlobalRadiantCore().getMessageBus().addListener(
        radiant::IMessage::Type::ApplicationShutdownRequest,
        radiant::TypeListener<radiant::ApplicationShutdownRequest>(
            sigc::mem_fun(this, &Map::handleShutdownRequest)));
}

void Map::shutdownModule()
{
    _undoEventListener.disconnect();

    abortMergeOperation();

    GlobalRadiantCore().getMessageBus().removeListener(_shutdownListener);

    _scaledModelExporter.shutdown();

	GlobalSceneGraph().removeSceneObserver(this);

    _modelScalePreserver.reset();
	_mapPositionManager.reset();
}

void Map::handleShutdownRequest(radiant::ApplicationShutdownRequest& request)
{
    if (!askForSave(_("Exit DarkRadiant")))
    {
        request.deny();
    }

    if (!request.isDenied())
    {
        abortMergeOperation();
    }
}

} // namespace map