/
StartupMapLoader.cpp
101 lines (83 loc) · 2.07 KB
/
StartupMapLoader.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
#include "StartupMapLoader.h"
#include "imodule.h"
#include "igl.h"
#include "irender.h"
#include "iregistry.h"
#include "iradiant.h"
#include "Map.h"
#include "ui/mru/MRU.h"
#include "modulesystem/ModuleRegistry.h"
#include "os/path.h"
#include "os/file.h"
namespace map
{
StartupMapLoader::StartupMapLoader()
{
GlobalRadiant().signal_radiantStarted().connect(
sigc::mem_fun(*this, &StartupMapLoader::onRadiantStartup)
);
GlobalRadiant().signal_radiantShutdown().connect(
sigc::mem_fun(*this, &StartupMapLoader::onRadiantShutdown)
);
}
void StartupMapLoader::onRadiantStartup()
{
std::string mapToLoad = "";
const ApplicationContext::ArgumentList& args(
module::ModuleRegistry::Instance().getApplicationContext().getCmdLineArgs()
);
for (const std::string& candidate : args)
{
if (os::getExtension(candidate) != "map") continue;
// We have a map file, check if it exists (and where)
// First, look if we have an absolute map path
if (os::fileOrDirExists(candidate))
{
mapToLoad = candidate;
break;
}
fs::path mapsPath = GlobalRegistry().get(RKEY_MAP_PATH);
fs::path fullMapPath = mapsPath / candidate;
// Next, look in the regular maps path
if (os::fileOrDirExists(fullMapPath.string()))
{
mapToLoad = fullMapPath.string();
break;
}
}
if (!mapToLoad.empty())
{
loadMapSafe(mapToLoad);
}
else
{
std::string lastMap = GlobalMRU().getLastMapName();
if (GlobalMRU().loadLastMap() && !lastMap.empty() && os::fileOrDirExists(lastMap))
{
loadMapSafe(lastMap);
}
else
{
GlobalMap().createNew();
}
}
}
void StartupMapLoader::loadMapSafe(const std::string& mapToLoad)
{
// Check if we have a valid openGL context, otherwise postpone the load
if (GlobalOpenGL().wxContextValid())
{
GlobalMap().load(mapToLoad);
return;
}
// No valid context, subscribe to the extensionsInitialised signal
GlobalRenderSystem().signal_extensionsInitialised().connect([mapToLoad]()
{
GlobalMap().load(mapToLoad);
});
}
void StartupMapLoader::onRadiantShutdown()
{
GlobalMRU().saveRecentFiles();
}
} // namespace map