/
StartupMapLoader.cpp
119 lines (96 loc) · 2.51 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "StartupMapLoader.h"
#include "imodule.h"
#include "imap.h"
#include "igl.h"
#include "irender.h"
#include "iregistry.h"
#include "icommandsystem.h"
#include "igame.h"
#include "iradiant.h"
#include "imru.h"
#include "module/StaticModule.h"
#include "os/path.h"
#include "os/file.h"
#include "registry/registry.h"
namespace map
{
void StartupMapLoader::onRadiantStartup()
{
std::string mapToLoad = "";
const auto& args(
module::GlobalModuleRegistry().getApplicationContext().getCmdLineArgs()
);
for (const std::string& candidate : args)
{
if (os::getExtension(candidate) != "map" &&
os::getExtension(candidate) != "mapx") 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 = GlobalGameManager().getMapPath();
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 if (registry::getValue<bool>(RKEY_LOAD_LAST_MAP))
{
std::string lastMap = GlobalMRU().getLastMapName();
if (!lastMap.empty() && os::fileOrDirExists(lastMap))
{
loadMapSafe(lastMap);
}
}
else
{
GlobalMapModule().createNewMap();
}
}
void StartupMapLoader::loadMapSafe(const std::string& mapToLoad)
{
// Check if we have a valid openGL context, otherwise postpone the load
if (GlobalOpenGL().wxContextValid())
{
GlobalCommandSystem().executeCommand("OpenMap", mapToLoad);
return;
}
// No valid context, subscribe to the extensionsInitialised signal
GlobalRenderSystem().signal_extensionsInitialised().connect([mapToLoad]()
{
GlobalCommandSystem().executeCommand("OpenMap", mapToLoad);
});
}
const std::string& StartupMapLoader::getName() const
{
static std::string _name("StartupMapLoader");
return _name;
}
const StringSet& StartupMapLoader::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_RADIANT_APP);
}
return _dependencies;
}
void StartupMapLoader::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
GlobalRadiant().signal_radiantStarted().connect(
sigc::mem_fun(*this, &StartupMapLoader::onRadiantStartup)
);
}
module::StaticModule<StartupMapLoader> startupMapLoader;
} // namespace map