/
ApplicationContextBase.cpp
332 lines (271 loc) · 7.62 KB
/
ApplicationContextBase.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
#include "ApplicationContextBase.h"
#include <locale>
#include "string/string.h"
#include "debugging/debugging.h"
#include "itextstream.h"
#include "iregistry.h"
#include "os/fs.h"
#include "os/path.h"
#include "os/dir.h"
#include "string/encoding.h"
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif
namespace radiant
{
namespace
{
const std::string PLUGINS_DIR = "plugins/"; ///< name of plugins directory
const std::string MODULES_DIR = "modules/"; ///< name of modules directory
}
/**
* Return the application path of the current Radiant instance.
*/
std::string ApplicationContextBase::getApplicationPath() const
{
return _appPath;
}
std::vector<std::string> ApplicationContextBase::getLibraryPaths() const
{
auto libBasePath = os::standardPathWithSlash(getLibraryBasePath());
#if defined(__APPLE__) && defined(DR_MODULES_NEXT_TO_APP)
// Xcode output goes to the application folder right now
return { libBasePath };
#else
return
{
libBasePath + MODULES_DIR,
libBasePath + PLUGINS_DIR
};
#endif
}
std::string ApplicationContextBase::getLibraryBasePath() const
{
#if defined(__APPLE__)
return _appPath;
#elif defined(POSIX)
# if defined(PKGLIBDIR) && !defined(ENABLE_RELOCATION)
return PKGLIBDIR;
# else
return _appPath + "../lib/darkradiant/";
# endif
#else // !defined(POSIX)
return _appPath;
#endif
}
std::string ApplicationContextBase::getRuntimeDataPath() const
{
#if defined(__APPLE__)
// The Resources are in the Bundle folder Contents/Resources/, whereas the
// application binary is located in Contents/MacOS/
std::string path = getApplicationPath() + "../Resources/";
// When launching the app from Xcode, the Resources/ folder
// is next to the binary
if (!fs::exists(path))
{
path = getApplicationPath() + "Resources/";
}
return path;
#elif defined(POSIX)
# if defined(PKGDATADIR) && !defined(ENABLE_RELOCATION)
return std::string(PKGDATADIR) + "/";
# else
return _appPath + "../share/darkradiant/";
# endif
#else
return getApplicationPath();
#endif
}
std::string ApplicationContextBase::getHTMLPath() const
{
#if defined(POSIX)
#if defined(HTMLDIR) && !defined(ENABLE_RELOCATION)
return std::string(HTMLDIR) + "/";
#else
return _appPath + "../share/doc/darkradiant/";
#endif
#else
// TODO: implement correct path for macOS and Windows
return getRuntimeDataPath();
#endif
}
std::string ApplicationContextBase::getSettingsPath() const
{
return _settingsPath;
}
std::string ApplicationContextBase::getBitmapsPath() const
{
return getRuntimeDataPath() + "bitmaps/";
}
const IApplicationContext::ArgumentList& ApplicationContextBase::getCmdLineArgs() const
{
return _cmdLineArgs;
}
// ============== OS-Specific Implementations go here ===================
// ================ POSIX ====================
#if defined(POSIX) || defined(__APPLE__)
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#ifdef __APPLE__
#include <libproc.h>
#endif
namespace
{
#ifdef __APPLE__
// greebo: In OSX, we use the proc_pidpath() function
// to determine the absolute path of this PID
std::string getExecutablePath(char* argv[])
{
pid_t pid = getpid();
char pathBuf[PROC_PIDPATHINFO_MAXSIZE];
int ret = proc_pidpath(pid, pathBuf, sizeof(pathBuf));
if (ret > 0)
{
// Success
fs::path execPath = std::string(pathBuf);
fs::path appPath = execPath.remove_leaf();
rConsole() << "Application path: " << appPath << std::endl;
return appPath.string();
}
// Error, terminate the app
rConsoleError() << "ApplicationContextBase: could not get app path: "
<< strerror(errno) << std::endl;
throw std::runtime_error("ApplicationContextBase: could not get app path");
}
#else // generic POSIX
const char* LINK_NAME =
#if defined (__linux__)
"/proc/self/exe"
#else // FreeBSD
"/proc/curproc/file"
#endif
;
/// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
std::string getExecutablePath(char* argv[])
{
char buf[PATH_MAX];
// Now read the symbolic link
int ret = readlink(LINK_NAME, buf, PATH_MAX);
if (ret == -1)
{
rMessage() << "getexename: falling back to argv[0]: '" << argv[0] << "'\n";
const char* path = realpath(argv[0], buf);
if (path == nullptr)
{
// In case of an error, leave the handling up to the caller
return std::string();
}
}
/* Ensure proper NUL termination */
buf[ret] = '\0';
/* delete the program name */
*(strrchr(buf, '/')) = '\0';
// NOTE: we build app path with a trailing '/'
// it's a general convention in Radiant to have the slash at the end of directories
if (buf[strlen(buf)-1] != '/')
{
strcat(buf, "/");
}
return std::string(buf);
}
#endif
}
void ApplicationContextBase::initialise(int argc, char* argv[])
{
// Give away unnecessary root privileges.
// Important: must be done before calling gtk_init().
char *loginname;
struct passwd *pw;
seteuid(getuid());
if (geteuid() == 0 &&
(loginname = getlogin()) != 0 &&
(pw = getpwnam(loginname)) != 0)
{
setuid(pw->pw_uid);
}
initArgs(argc, argv);
// Initialise the home directory path
std::string homedir = getenv("HOME");
std::string home = os::standardPathWithSlash(homedir) + ".darkradiant/";
os::makeDirectory(home);
_homePath = home;
_appPath = getExecutablePath(argv);
ASSERT_MESSAGE(!_appPath.empty(), "failed to deduce app path");
// Initialise the relative paths
initPaths();
}
// ================ WIN32 ====================
#elif defined(WIN32)
void ApplicationContextBase::initialise(int argc, char* argv[])
{
initArgs(argc, argv);
// Get application data directory from environment
std::string appData = getenv("APPDATA");
if (appData.empty())
{
throw std::runtime_error(
"Critical: cannot find APPDATA environment variable."
);
}
// Construct DarkRadiant home directory
_homePath = appData + "\\DarkRadiant";
if (!os::makeDirectory(_homePath))
{
rConsoleError() << "ApplicationContextBase: could not create home directory "
<< "'" << _homePath << "'" << std::endl;
}
{
// get path to the editor
wchar_t filename[MAX_PATH+1];
GetModuleFileName(0, filename, MAX_PATH);
wchar_t* last_separator = wcsrchr(filename, '\\');
if (last_separator != 0) {
*(last_separator+1) = '\0';
}
else {
filename[0] = '\0';
}
// convert to std::string
std::wstring wide(filename);
std::string appPathNarrow = string::unicode_to_mb(wide);
// Make sure we have forward slashes
_appPath = os::standardPath(appPathNarrow);
}
// Initialise the relative paths
initPaths();
}
#else
#error "unsupported platform"
#endif
// ============== OS-Specific Implementations end ===================
void ApplicationContextBase::initArgs(int argc, char* argv[])
{
// Store the arguments locally, ignore the first one
for (int i = 1; i < argc; i++) {
_cmdLineArgs.push_back(argv[i]);
}
}
void ApplicationContextBase::initPaths()
{
// Ensure that the homepath ends with a slash
_homePath = os::standardPathWithSlash(_homePath);
_appPath = os::standardPathWithSlash(_appPath);
// Make sure the home/settings folder exists (attempt to create it)
_settingsPath = _homePath;
if (!os::makeDirectory(_settingsPath))
{
rConsoleError() << "ApplicationContextBase: unable to create settings path '"
<< _settingsPath << "'" << std::endl;
}
}
const ErrorHandlingFunction& ApplicationContextBase::getErrorHandlingFunction() const
{
return _errorHandler;
}
void ApplicationContextBase::setErrorHandlingFunction(const ErrorHandlingFunction& function)
{
_errorHandler = function;
}
} // namespace module