/
SystemPaths.cpp
418 lines (351 loc) · 11.5 KB
/
SystemPaths.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
/*
* Copyright 2012-2013 Arx Libertatis Team (see the AUTHORS file)
*
* This file is part of Arx Libertatis.
*
* Arx Libertatis is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Arx Libertatis is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Arx Libertatis. If not, see <http://www.gnu.org/licenses/>.
*/
#include "io/fs/SystemPaths.h"
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include "io/fs/Filesystem.h"
#include "io/fs/PathConstants.h"
#include "io/log/Logger.h"
#include "platform/Environment.h"
#include "platform/ProgramOptions.h"
namespace fs {
SystemPaths paths;
static path canonical(const fs::path & path) {
return path.is_absolute() ? path : current_path() / path;
}
static std::vector<path> getSearchPaths(const char * input) {
std::vector<path> result;
if(input) {
std::string decoded = expandEnvironmentVariables(input);
typedef boost::tokenizer< boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(env_list_seperators);
tokenizer tokens(decoded, sep);
std::copy(tokens.begin(), tokens.end(), std::back_inserter(result));
} else {
result.push_back(".");
}
return result;
};
static path findUserPath(const char * name, const path & force,
const char * registry, const char * prefix,
const char * suffix, const path & fallback,
bool create) {
// Prefer command-line options
if(!force.empty()) {
path dir = canonical(force);
if(create && !create_directories(dir)) {
LogCritical << "Could not create " << name << " directory " << dir << '.';
return path();
} else {
LogDebug("using " << name << " dir from command-line: " << dir);
return dir;
}
}
// Check system settings (windows registry)
std::string temp;
if(registry && getSystemConfiguration(registry, temp)) {
path dir = canonical(temp);
if(!create) {
return dir;
} else if(create_directories(dir)) {
LogDebug("got " << name << " dir from registry: \"" << temp
<< "\" = " << dir);
return dir;
} else {
LogError << "Could not create " << name << " directory " << dir << '.';
LogDebug("ignoring " << name << " dir from registry: \"" << temp << '"');
}
}
// Search standard locations
bool create_exists = false;
path to_create;
std::vector<path> prefixes = getSearchPaths(prefix);
std::vector<path> suffixes = getSearchPaths(suffix);
if(prefix || suffix) {
BOOST_FOREACH(const path & prefix, prefixes) {
BOOST_FOREACH(const path & suffix, suffixes) {
path dir = canonical(prefix / suffix);
if(is_directory(dir)) {
LogDebug("got " << name << " dir from search: " << prefix
<< " + " << suffix << " = " << dir);
return dir;
} else {
LogDebug("ignoring " << name << " dir from search: " << prefix
<< " + " << suffix << " = " << dir);
}
if(to_create.empty() || (!create_exists && is_directory(prefix))) {
to_create = dir;
create_exists = is_directory(prefix);
}
}
}
}
// Create a new standard diretcory
if(!to_create.empty()) {
if(!create) {
return to_create;
} else if(!create_directories(to_create)) {
LogError << "Could not create " << name << " directory " << to_create << '.';
} else {
LogInfo << "Created new " << name << " directory at " << to_create << '.';
return to_create;
}
}
LogDebug("using fallback " << name << " dir");
return fallback;
}
static bool addSearchPath(std::vector<path> & result, const path & dir,
bool filter) {
if(filter && !is_directory(dir)) {
return false;
}
if(std::find(result.begin(), result.end(), dir) != result.end()) {
return false;
}
result.push_back(dir);
return true;
}
std::vector<path> SystemPaths::getSearchPaths(bool filter) const {
std::vector<path> result;
// Use the user diretcory as the highest-priority data directory
if(!user.empty()) {
result.push_back(user);
}
// Use paths specifed on the command-line
BOOST_FOREACH(const path & dir, addData_) {
path tmp = canonical(dir);
if(addSearchPath(result, tmp, filter)) {
LogDebug("got data dir from command-line: " << dir << " = " << tmp);
}
}
// Skip search for system paths if requested
if(!findData_) {
return result;
}
// Check paths specified in environment variables
path exepath = getExecutablePath();
#if ARX_PLATFORM != ARX_PLATFORM_WIN32
if(!exepath.empty()) {
std::string var = "${" + exepath.basename() + "_PATH}";
std::vector<path> paths = fs::getSearchPaths(var.c_str());
BOOST_FOREACH(const path & p, paths) {
addSearchPath(result, p, filter);
}
}
#endif
// Check system settings (windows registry)
std::string temp;
if(getSystemConfiguration("DataDir", temp)) {
path dir = canonical(temp);
if(addSearchPath(result, dir, filter)) {
LogDebug("got data dir from registry: \"" << temp << "\" = " << dir);
} else {
LogDebug("ignoring data dir from registry: \"" << temp << "\" = " << dir);
}
}
// Search the executable directory
if(!exepath.empty()) {
path dir = canonical(exepath.parent());
bool ignored = false;
if(ignore_exe_dir) {
std::vector<path> ignored_dirs = fs::getSearchPaths(ignore_exe_dir);
ignored = (std::find(ignored_dirs.begin(), ignored_dirs.end(), dir)
!= ignored_dirs.end());
}
if(!ignored && addSearchPath(result, dir, filter)) {
LogDebug("got data dir from exe: " << exepath << " -> " << dir);
} else {
LogDebug("ignoring data dir from exe: " << exepath << " -> " << dir);
}
}
// Search standard locations
std::vector<path> prefixes = fs::getSearchPaths(data_dir_prefixes);
std::vector<path> suffixes = fs::getSearchPaths(data_dir);
if(data_dir_prefixes || data_dir) {
BOOST_FOREACH(const path & prefix, prefixes) {
BOOST_FOREACH(const path & suffix, suffixes) {
path dir = canonical(prefix / suffix);
if(addSearchPath(result, dir, filter)) {
LogDebug("got data dir from search: " << prefix
<< " + " << suffix << " = " << dir);
} else {
LogDebug("ignoring data dir from search: " << prefix
<< " + " << suffix << " = " << dir);
}
}
}
}
return result;
}
static SystemPaths::InitParams cmdLineInitParams;
ExitStatus SystemPaths::init() {
return init(cmdLineInitParams);
}
ExitStatus SystemPaths::init(const InitParams& initParams) {
user = findUserPath("user", initParams.forceUser, "UserDir",
user_dir_prefixes, user_dir, current_path(), !initParams.displaySearchDirs);
config = findUserPath("config", initParams.forceConfig, "ConfigDir",
config_dir_prefixes, config_dir, user, !initParams.displaySearchDirs);
addData_ = initParams.dataDirs;
findData_ = initParams.findData;
data = getSearchPaths(true);
if(initParams.displaySearchDirs) {
fs::paths.list(std::cout,
" - --user-dir (-u) command-line parameter\n",
" - --config-dir (-c) command-line parameter\n",
" - --data-dir (-d) command-line parameters\n"
" only without --no-data-dir (-n): \n");
std::cout << std::endl;
return ExitSuccess;
} else if(fs::paths.user.empty() || fs::paths.config.empty()) {
LogCritical << "Could not select user or config directory.";
return ExitFailure;
}
return RunProgram;
}
path SystemPaths::find(const path & resource) const {
if(resource.is_absolute()) {
return exists(resource) ? resource : path();
}
BOOST_FOREACH(const path & prefix, data) {
path full_path = prefix / resource;
if(fs::exists(full_path)) {
return full_path;
}
}
return path();
}
static void listDirectoriesFor(std::ostream & os, const std::string & regKey,
const char * prefix = NULL,
const char * suffix = NULL) {
#if ARX_PLATFORM == ARX_PLATFORM_WIN32
if(!regKey.empty()) {
os << " - Registry key {HKCU,HKLM}\\Software\\ArxLibertatis\\"
<< regKey << '\n';
std::string temp;
if(getSystemConfiguration(regKey, temp)) {
os << " = " << canonical(temp) << '\n';
}
}
#else
ARX_UNUSED(regKey);
#endif
std::vector<path> prefixes = getSearchPaths(prefix);
std::vector<path> suffixes = getSearchPaths(suffix);
if(prefix || suffix) {
os << " - ";
if(prefix) {
os << '"' << prefix << '"';
}
if(prefix && suffix) {
os << " / ";
}
if(suffix) {
os << '"' << suffix << '"';
}
os << ":\n";
std::set<path> result;
BOOST_FOREACH(const path & prefix, prefixes) {
BOOST_FOREACH(const path & suffix, suffixes) {
path dir = canonical(prefix / suffix);
if(result.find(dir) == result.end()) {
os << " -> " << dir << '\n';
result.insert(dir);
}
}
}
}
}
void SystemPaths::list(std::ostream & os, const std::string & forceUser,
const std::string & forceConfig,
const std::string & forceData) {
os << "\nUser directories (select first existing):\n";
if(!forceUser.empty()) {
os << forceUser;
}
listDirectoriesFor(os, "UserDir", user_dir_prefixes, user_dir);
os << " - Current working directory\n";
os << "selected: ";
if(user.empty()) {
os << "(none)\n";
} else {
os << user << '\n';
}
os << "\nConfig directories (select first existing):\n";
if(!forceConfig.empty()) {
os << forceConfig;
}
listDirectoriesFor(os, std::string(), config_dir_prefixes, config_dir);
os << " - The selected user directory\n";
os << "selected: ";
if(config.empty()) {
os << "(none)\n";
} else {
os << config << '\n';
}
os << "\nData directories (decreasing priority):\n";
os << " - The selected user directory\n";
if(!forceData.empty()) {
os << forceData;
}
path exepath = getExecutablePath();
os << " - Paths specifed in ${" << exepath.basename() << "_PATH}\n";
os << " - The directory containing the game executable\n";
listDirectoriesFor(os, "DataDir", data_dir_prefixes, data_dir);
os << "selected:";
if(data.empty()) {
os << " (none)\n";
} else {
os << '\n';
BOOST_FOREACH(const path & dir, data) {
os << " - " << dir << '\n';
}
}
}
} // namespace fs
void disableDataDir() {
fs::cmdLineInitParams.findData = false;
}
void addDataDir(const std::string & dataDir) {
fs::cmdLineInitParams.dataDirs.push_back(dataDir);
}
void setUserDir(const std::string & userDir) {
fs::cmdLineInitParams.forceUser = userDir;
}
void setConfigDir(const std::string & configDir) {
fs::cmdLineInitParams.forceConfig = configDir;
}
void listSearchDirs() {
fs::cmdLineInitParams.displaySearchDirs = true;
}
ARX_PROGRAM_OPTION("no-data-dir", "n",
"Don't automatically detect data directories", &disableDataDir);
ARX_PROGRAM_OPTION("data-dir", "d",
"Where to find the data files (can be repeated)", &addDataDir, "DIR");
ARX_PROGRAM_OPTION("user-dir", "u",
"Where to store user-specific files", &setUserDir, "DIR");
ARX_PROGRAM_OPTION("config-dir", "c",
"Where to store config files", &setConfigDir, "DIR");
ARX_PROGRAM_OPTION("list-dirs", "l",
"List the searched user and data directories", &listSearchDirs);