Repository navigation
Expand file tree
/
Copy pathOptions.cpp
More file actions
executable file
·353 lines (326 loc) · 7.66 KB
/
Copy pathOptions.cpp
File metadata and controls
executable file
·353 lines (326 loc) · 7.66 KB
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
/*
* Copyright 2010-2012 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Options.h"
#include <SDL_mixer.h>
#include <map>
#include <sstream>
#include <fstream>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include "Exception.h"
#include "CrossPlatform.h"
namespace OpenXcom
{
namespace Options
{
std::string _version = "0.4";
std::string _dataFolder = "";
std::vector<std::string> _dataList;
std::string _userFolder = "";
std::string _configFolder = "";
std::vector<std::string> _userList;
std::map<std::string, std::string> _options;
/**
* Creates a default set of options based on the system.
*/
void createDefault()
{
#ifdef DINGOO
setInt("displayWidth", 320);
setInt("displayHeight", 200);
setBool("fullscreen", true);
setInt("keyboardMode", KEYBOARD_OFF);
#else
setInt("displayWidth", 640);
setInt("displayHeight", 400);
setBool("fullscreen", false);
setInt("keyboardMode", KEYBOARD_ON);
#endif
setBool("debug", false);
setBool("mute", false);
setInt("soundVolume", MIX_MAX_VOLUME);
setInt("musicVolume", MIX_MAX_VOLUME);
setString("language", "");
setInt("battleScrollSpeed", 24); // 8, 16, 24, 32, 40
setInt("battleScrollType", SCROLL_AUTO);
setInt("battleFireSpeed", 20); // 30, 25, 20, 15, 10, 5
setInt("battleXcomSpeed", 40); // 60, 50, 40, 30, 20, 10
setInt("battleAlienSpeed", 40); // 60, 50, 40, 30, 20, 10
setBool("battleAltGrenade", false); // set to true if you want to play with the alternative grenade handling
setBool("battlePreviewPath", false);
setBool("battleRangeBasedAccuracy", false);
setBool("fpsCounter", false);
setBool("craftLaunchAlways", false);
setBool("globeSeasons", false);
setInt("audioSampleRate", 22050);
setInt("audioBitDepth", 16);
}
/**
* Loads options from a set of command line arguments,
* in the format "-option value".
* @param argc Number of arguments.
* @param args Array of argument strings.
*/
void loadArgs(int argc, char** args)
{
for (int i = 1; i < argc; ++i)
{
if (argc > i + 1)
{
std::string arg = args[i];
if (arg[0] == '-' && argc > i + 1)
{
std::map<std::string, std::string>::iterator it = _options.find(arg.substr(1, arg.length()-1));
if (it != _options.end())
{
it->second = args[i+1];
}
else if (arg == "-data")
{
_dataFolder = CrossPlatform::endPath(args[i+1]);
}
else if (arg == "-user")
{
_userFolder = CrossPlatform::endPath(args[i+1]);
}
}
}
}
if (_userFolder != "")
{
load();
}
}
/**
* Handles the initialization of setting up default options
* and finding and loading any existing ones.
* @param argc Number of arguments.
* @param args Array of argument strings.
*/
void init(int argc, char** args)
{
std::cout << "Loading options..." << std::endl;
createDefault();
loadArgs(argc, args);
if (_dataFolder == "")
{
_dataList = CrossPlatform::findDataFolders();
// Missing data folder is handled in StartState
}
if (_userFolder == "")
{
std::vector<std::string> user = CrossPlatform::findUserFolders();
_configFolder = CrossPlatform::findConfigFolder();
// Look for an existing user folder
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
{
if (CrossPlatform::folderExists(*i))
{
_userFolder = *i;
break;
}
}
// Set up folders
if (_userFolder == "")
{
for (std::vector<std::string>::iterator i = user.begin(); i != user.end(); ++i)
{
if (CrossPlatform::createFolder(*i))
{
_userFolder = *i;
break;
}
}
}
if (_configFolder == "")
{
_configFolder = _userFolder;
}
// Load existing options
if (CrossPlatform::folderExists(_configFolder))
{
load();
}
// Create config folder and save options
else
{
CrossPlatform::createFolder(_configFolder);
save();
}
}
std::cout << "Data folder is: " << _dataFolder << std::endl;
std::cout << "User folder is: " << _userFolder << std::endl;
std::cout << "Config folder is: " << _configFolder << std::endl;
std::cout << "Options loaded successfully." << std::endl;
}
/**
* Loads options from a YAML file.
* @param filename YAML filename.
*/
void load(const std::string &filename)
{
std::string s = _configFolder + filename + ".cfg";
std::ifstream fin(s.c_str());
if (!fin)
{
//throw Exception("Failed to load options");
return;
}
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
for (YAML::Iterator i = doc.begin(); i != doc.end(); ++i)
{
std::string key, value;
i.first() >> key;
i.second() >> value;
_options[key] = value;
}
fin.close();
}
/**
* Saves options to a YAML file.
* @param filename YAML filename.
*/
void save(const std::string &filename)
{
std::string s = _configFolder + filename + ".cfg";
std::ofstream sav(s.c_str());
if (!sav)
{
//throw Exception("Failed to save options");
return;
}
YAML::Emitter out;
out << YAML::BeginDoc;
out << _options;
sav << out.c_str();
sav.close();
}
/**
* Returns the game's version in x.x format.
* @return String with version number.
*/
std::string getVersion()
{
return _version;
}
/**
* Returns the game's current Data folder where resources
* and X-Com files are loaded from.
* @return Full path to Data folder.
*/
std::string getDataFolder()
{
return _dataFolder;
}
/**
* Changes the game's current Data folder where resources
* and X-Com files are loaded from.
* @param folder Full path to Data folder.
*/
void setDataFolder(const std::string &folder)
{
_dataFolder = folder;
}
/**
* Returns the game's list of possible Data folders.
* @return List of Data paths.
*/
std::vector<std::string> *getDataList()
{
return &_dataList;
}
/**
* Returns the game's User folder where settings
* and saves are stored in.
* @return Full path to User folder.
*/
std::string getUserFolder()
{
return _userFolder;
}
/**
* Returns an option in string format.
* @param id Option ID.
* @return Option value.
*/
std::string getString(const std::string& id)
{
return _options[id];
}
/**
* Returns an option in integer format.
* @param id Option ID.
* @return Option value.
*/
int getInt(const std::string& id)
{
std::stringstream ss;
int value;
ss << std::dec << _options[id];
ss >> std::dec >> value;
return value;
}
/**
* Returns an option in boolean format.
* @param id Option ID.
* @return Option value.
*/
bool getBool(const std::string& id)
{
std::stringstream ss;
bool value;
ss << std::boolalpha << _options[id];
ss >> std::boolalpha >> value;
return value;
}
/**
* Changes an option in string format.
* @param id Option ID.
* @param value New option value.
*/
void setString(const std::string& id, const std::string& value)
{
_options[id] = value;
}
/**
* Changes an option in integer format.
* @param id Option ID.
* @param value New option value.
*/
void setInt(const std::string& id, int value)
{
std::stringstream ss;
ss << std::dec << value;
_options[id] = ss.str();
}
/**
* Changes an option in boolean format.
* @param id Option ID.
* @param value New option value.
*/
void setBool(const std::string& id, bool value)
{
std::stringstream ss;
ss << std::boolalpha << value;
_options[id] = ss.str();
}
}
}