forked from Voidious/BerryBots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linuxcfg.cpp
290 lines (253 loc) · 8.79 KB
/
linuxcfg.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
/*
Copyright (C) 2013 - Voidious
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <wx/wx.h>
#include <wx/dirdlg.h>
#include <wx/msgdlg.h>
#include <platformstl/filesystem/readdir_sequence.hpp>
#include "bbconst.h"
#include "ResourcePath.hpp"
#include "filemanager.h"
#include "linuxcfg.h"
LinuxCfg::LinuxCfg() {
aaDisabled_ = false;
}
std::string LinuxCfg::stagesDir() {
return stagesDir_;
}
std::string LinuxCfg::shipsDir() {
return shipsDir_;
}
std::string LinuxCfg::runnersDir() {
return runnersDir_;
}
std::string LinuxCfg::cacheDir() {
return cacheDir_;
}
std::string LinuxCfg::tmpDir() {
return tmpDir_;
}
std::string LinuxCfg::replaysDir() {
return replaysDir_;
}
std::string LinuxCfg::apidocPath() {
std::string apidocPath = resourcePath();
apidocPath.append("apidoc/index.html");
return apidocPath;
}
bool LinuxCfg::aaDisabled() {
return aaDisabled_;
}
bool LinuxCfg::isSaved() {
FileManager fileManager;
return fileManager.fileExists(settingsPath().c_str());
}
bool LinuxCfg::loadSettings() {
bool selectRoot = true;
FileManager fileManager;
if (fileManager.fileExists(settingsPath().c_str())) {
char *settingsFile = fileManager.readFile(settingsPath().c_str());
std::string s(settingsFile);
size_t pos = 0;
size_t i;
while ((i = s.find('\n', pos)) != std::string::npos) {
std::string line = s.substr(pos, i - pos);
if (line.length() > 0) {
size_t j;
if ((j = line.find('=', 0)) != std::string::npos) {
std::string key = line.substr(0, j);
std::string value = line.substr(j + 1, line.length() - j - 1);
loadSetting(key, value);
}
}
pos = i + 1;
}
delete settingsFile;
if (stagesDir_.length() > 0 && fileManager.fileExists(stagesDir_.c_str())
&& shipsDir_.length() > 0
&& fileManager.fileExists(shipsDir_.c_str())) {
selectRoot = false;
}
}
bool loaded = (selectRoot ? chooseNewRootDir() : true);
if (loaded && (samplesVersion_ != SAMPLES_VERSION)) {
copySamplesOrSkip();
samplesVersion_ = SAMPLES_VERSION;
save();
}
return loaded;
}
void LinuxCfg::setRootDir(std::string newRootDir) {
stagesDir_ = shipsDir_ = runnersDir_ = cacheDir_ = tmpDir_ = replaysDir_ =
newRootDir;
stagesDir_.append("/stages");
shipsDir_.append("/bots");
runnersDir_.append("/runners");
cacheDir_.append("/cache");
tmpDir_.append("/.tmp");
replaysDir_.append("/replays");
samplesVersion_ = SAMPLES_VERSION;
copyAllAppFiles(resourcePath(), false);
save();
}
bool LinuxCfg::chooseNewRootDir() {
wxDirDialog dialog(NULL, "Select a BerryBots base directory");
int r = dialog.ShowModal();
if (r == wxID_OK) {
wxString rootDir = dialog.GetPath();
setRootDir(std::string(rootDir.mb_str()));
return true;
} else {
return false;
}
}
void LinuxCfg::save() {
std::stringstream settings;
settings << "stagesDir=" << stagesDir_ << std::endl
<< "shipsDir=" << shipsDir_ << std::endl
<< "runnersDir=" << runnersDir_ << std::endl
<< "cacheDir=" << cacheDir_ << std::endl
<< "tmpDir=" << tmpDir_ << std::endl
<< "replaysDir=" << replaysDir_ << std::endl
<< "samplesVersion=" << samplesVersion_ << std::endl
<< "aaDisabled=" << (aaDisabled_ ? "true" : "false") << std::endl;
FileManager fileManager;
fileManager.writeFile(settingsPath().c_str(), settings.str().c_str());
}
void LinuxCfg::copyAllAppFiles(std::string srcDir, bool forceAll) {
bool overwrite = false;
bool asked = false;
std::string stagesSrc = srcDir;
stagesSrc.append("stages");
copyAppFiles(stagesSrc, stagesDir_, forceAll, overwrite, asked);
std::string shipsSrc = srcDir;
shipsSrc.append("bots");
copyAppFiles(shipsSrc, shipsDir_, forceAll, overwrite, asked);
std::string runnersSrc = srcDir;
runnersSrc.append("runners");
copyAppFiles(runnersSrc, runnersDir_, forceAll, overwrite, asked);
}
void LinuxCfg::copyAppFiles(std::string srcDir, std::string targetDir,
bool force, bool &overwrite, bool &asked) {
FileManager fileManager;
fileManager.createDirectoryIfNecessary(targetDir.c_str());
platformstl::readdir_sequence dir(srcDir.c_str(),
platformstl::readdir_sequence::files
| platformstl::readdir_sequence::directories);
platformstl::readdir_sequence::const_iterator first = dir.begin();
platformstl::readdir_sequence::const_iterator last = dir.end();
while (first != last) {
platformstl::readdir_sequence::const_iterator file = first++;
char *filename = (char *) *file;
char *filePath = fileManager.getFilePath(srcDir.c_str(), filename);
char *targetPath = fileManager.getFilePath(targetDir.c_str(), filename);
if (fileManager.isDirectory(filePath)) {
copyAppFiles(std::string(filePath), std::string(targetPath), force,
overwrite, asked);
} else {
if (fileManager.fileExists(filePath)) {
bool destFileExists = fileManager.fileExists(targetPath);
if (destFileExists && !force && !asked) {
wxMessageDialog overwriteMessage(NULL,
"Sample ships and stages already exist in this directory. OK to overwrite them?",
"Overwrite samples?", wxOK | wxCANCEL | wxICON_QUESTION);
overwriteMessage.SetOKCancelLabels("&OK", "&Skip");
int r = overwriteMessage.ShowModal();
if (r == wxID_OK) {
overwrite = true;
}
asked = true;
}
bool doCopy = (force || overwrite || !destFileExists);
if (doCopy) {
char *fileContents = fileManager.readFile(filePath);
fileManager.writeFile(targetPath, fileContents);
delete fileContents;
}
}
}
delete filePath;
delete targetPath;
}
}
void LinuxCfg::copySamplesOrSkip() {
std::stringstream okToUpdate;
okToUpdate << "BerryBots samples have been updated to version "
<< SAMPLES_VERSION
<< ". OK to update the files in your base directory?";
wxMessageDialog updateDialog(NULL, okToUpdate.str(), "Update samples?",
wxOK | wxCANCEL | wxICON_QUESTION);
updateDialog.SetOKCancelLabels("&OK", "&Skip");
int r = updateDialog.ShowModal();
if (r == wxID_OK) {
std::string srcDir = resourcePath();
copyAllAppFiles(srcDir, true);
std::stringstream samplesUpdated;
samplesUpdated << "BerryBots samples and API docs have been updated to v"
<< SAMPLES_VERSION << ".";
wxMessageDialog updatedDialog(NULL, samplesUpdated.str(),
"Samples updated.", wxOK | wxICON_INFORMATION);
updatedDialog.ShowModal();
}
}
void LinuxCfg::loadSetting(std::string key, std::string value) {
if (key.compare("stagesDir") == 0) {
stagesDir_ = value;
} else if (key.compare("shipsDir") == 0) {
shipsDir_ = value;
} else if (key.compare("runnersDir") == 0) {
runnersDir_ = value;
} else if (key.compare("cacheDir") == 0) {
cacheDir_ = value;
} else if (key.compare("tmpDir") == 0) {
tmpDir_ = value;
} else if (key.compare("replaysDir") == 0) {
replaysDir_ = value;
} else if (key.compare("samplesVersion") == 0) {
samplesVersion_ = value;
} else if (key.compare("aaDisabled") == 0) {
aaDisabled_ = (value.compare("true") == 0);
}
}
std::string LinuxCfg::configDir() {
char *xdgConfigEnv = getenv("XDG_CONFIG_HOME");
if (xdgConfigEnv == NULL || strlen(xdgConfigEnv) == 0) {
return std::string(userHomeDir() + "/.config");
} else {
return std::string(xdgConfigEnv);
}
}
std::string LinuxCfg::userHomeDir() {
char *homeEnv = getenv("HOME");
if (homeEnv == NULL) {
struct passwd *pw = getpwuid(getuid());
return std::string(pw->pw_dir);
} else {
return std::string(homeEnv);
}
}
std::string LinuxCfg::settingsPath() {
std::string settingsPath(configDir());
settingsPath.append("/");
settingsPath.append(SETTINGS_FILENAME);
return settingsPath;
}