forked from EasyRPG/Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_config.cpp
492 lines (421 loc) · 12.4 KB
/
game_config.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player 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.
*
* EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "game_config.h"
#include "cmdline_parser.h"
#include "filefinder.h"
#include "input_buttons.h"
#include "keys.h"
#include "output.h"
#include "input.h"
#include <lcf/inireader.h>
#include <cstring>
#ifdef _WIN32
# include <shlobj.h>
#endif
#ifdef USE_LIBRETRO
# include "platform/libretro/ui.h"
#endif
namespace {
std::string config_path;
StringView config_name = "config.ini";
}
void Game_ConfigPlayer::Hide() {
// Game specific settings unsupported
}
void Game_ConfigVideo::Hide() {
// Options that are platform dependent are opt-in
// Implementors must invoke SetOptionVisible() when supported
// Always enabled by default:
// - renderer (name of the renderer)
// - show_fps (Rendering of FPS, engine feature)
vsync.SetOptionVisible(false);
fullscreen.SetOptionVisible(false);
fps_limit.SetOptionVisible(false);
fps_render_window.SetOptionVisible(false);
window_zoom.SetOptionVisible(false);
scaling_mode.SetOptionVisible(false);
stretch.SetOptionVisible(false);
touch_ui.SetOptionVisible(false);
game_resolution.SetOptionVisible(false);
}
void Game_ConfigAudio::Hide() {
// Music and SE volume control are opt-out
// Nothing else available currently
}
void Game_ConfigInput::Hide() {
// These features are handled by our input system but showing them only
// makes sense on hardware with the required sticks and buttons
gamepad_swap_ab_and_xy.SetOptionVisible(false);
gamepad_swap_analog.SetOptionVisible(false);
gamepad_swap_dpad_with_buttons.SetOptionVisible(false);
}
Game_Config Game_Config::Create(CmdlineParser& cp) {
Game_Config cfg;
#if USE_SDL >= 2
cfg.video.scaling_mode.Set(ScalingMode::Bilinear);
#endif
cp.Rewind();
config_path = GetConfigPath(cp);
std::string config_file;
if (!config_path.empty()) {
config_file = FileFinder::MakePath(config_path, config_name);
}
auto cli_config = FileFinder::Root().OpenOrCreateInputStream(config_file);
if (!cli_config) {
config_path.clear();
auto global_config = GetGlobalConfigFileInput();
if (global_config) {
cfg.LoadFromStream(global_config);
} else {
// Game_Config only loads an empty layout
cfg.input.buttons = Input::GetDefaultButtonMappings();
}
} else {
cfg.LoadFromStream(cli_config);
}
cp.Rewind();
cfg.LoadFromArgs(cp);
return cfg;
}
FilesystemView Game_Config::GetGlobalConfigFilesystem() {
// FIXME: Game specific configs?
std::string path;
if (config_path.empty()) {
#ifdef __wii__
path = "/data/easyrpg-player";
#elif defined(__WIIU__)
path = "/vol/external01/data/easyrpg-player"; // temp
#elif defined(__SWITCH__)
path = "/switch/easyrpg-player";
#elif defined(__3DS__)
path = "sdmc:/data/easyrpg-player";
#elif defined(__vita__)
path = "ux0:/data/easyrpg-player";
#elif defined(USE_LIBRETRO)
const char* dir = nullptr;
if (LibretroUi::environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) {
path = FileFinder::MakePath(dir, "easyrpg-player");
}
#elif defined(__ANDROID__)
// Never called, passed as argument on startup
#elif defined(_WIN32)
PWSTR knownPath;
const auto hresult = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &knownPath);
if (SUCCEEDED(hresult)) {
path = Utils::FromWideString(knownPath);
CoTaskMemFree(knownPath);
} else {
Output::Debug("Config: SHGetKnownFolderPath failed");
}
if (!path.empty()) {
path = FileFinder::MakePath(path, FileFinder::MakePath(ORGANIZATION_NAME, APPLICATION_NAME));
}
#else
char* home = getenv("XDG_CONFIG_HOME");
if (home) {
path = home;
} else {
home = getenv("HOME");
if (home) {
path = FileFinder::MakePath(home, ".config");
}
}
if (!path.empty()) {
path = FileFinder::MakePath(path, FileFinder::MakePath(ORGANIZATION_NAME, APPLICATION_NAME));
}
#endif
} else {
path = config_path;
}
auto print_err = [&path]() {
if (path.empty()) {
Output::Warning("Could not determine config path");
} else {
Output::Warning("Could not access config path {}", path);
}
};
if (path.empty()) {
print_err();
return {};
}
if (!FileFinder::Root().MakeDirectory(path, true)) {
print_err();
return {};
}
auto fs = FileFinder::Root().Create(path);
if (!fs) {
print_err();
return {};
}
return fs;
}
Filesystem_Stream::InputStream Game_Config::GetGlobalConfigFileInput() {
auto fs = GetGlobalConfigFilesystem();
if (fs) {
return fs.OpenOrCreateInputStream(config_name, std::ios_base::in);
}
return Filesystem_Stream::InputStream();
}
Filesystem_Stream::OutputStream Game_Config::GetGlobalConfigFileOutput() {
auto fs = GetGlobalConfigFilesystem();
if (fs) {
return fs.OpenOutputStream(config_name, std::ios_base::out);
}
return Filesystem_Stream::OutputStream();
}
std::string Game_Config::GetConfigPath(CmdlineParser& cp) {
std::string path;
while (!cp.Done()) {
CmdlineArg arg;
if (cp.ParseNext(arg, 1, "--config-path", 'c')) {
if (arg.NumValues() > 0) {
path = arg.Value(0);
path = FileFinder::MakeCanonical(path, 0);
}
continue;
}
cp.SkipNext();
}
if (!path.empty()) {
if (!FileFinder::Root().MakeDirectory(path, true)) {
Output::Debug("Could not create global config directory {}", path);
path.clear();
}
}
return path;
}
void Game_Config::LoadFromArgs(CmdlineParser& cp) {
while (!cp.Done()) {
CmdlineArg arg;
long li_value = 0;
std::string str_value;
if (cp.ParseNext(arg, 0, {"--vsync", "--no-vsync"})) {
video.vsync.Set(arg.ArgIsOn());
continue;
}
if (cp.ParseNext(arg, 1, "--fps-limit")) {
if (arg.ParseValue(0, li_value)) {
video.fps_limit.Set(li_value);
}
continue;
}
if (cp.ParseNext(arg, 0, "--no-fps-limit")) {
video.fps_limit.Set(0);
continue;
}
if (cp.ParseNext(arg, 0, {"--show-fps", "--no-show-fps"})) {
video.show_fps.Set(arg.ArgIsOn());
continue;
}
if (cp.ParseNext(arg, 0, {"--fps-render-window", "--no-fps-render-window"})) {
video.fps_render_window.Set(arg.ArgIsOn());
continue;
}
if (cp.ParseNext(arg, 0, "--window")) {
video.fullscreen.Set(false);
continue;
}
if (cp.ParseNext(arg, 0, "--fullscreen")) {
video.fullscreen.Set(true);
continue;
}
if (cp.ParseNext(arg, 1, "--window-zoom")) {
if (arg.ParseValue(0, li_value)) {
video.window_zoom.Set(li_value);
}
continue;
}
if (cp.ParseNext(arg, 0, {"--stretch", "--no-stretch"})) {
video.stretch.Set(arg.ArgIsOn());
continue;
}
if (cp.ParseNext(arg, 1, "--scaling")) {
if (arg.ParseValue(0, str_value)) {
video.scaling_mode.SetFromString(str_value);
}
continue;
}
if (cp.ParseNext(arg, 1, "--game-resolution")) {
if (arg.ParseValue(0, str_value)) {
video.game_resolution.SetFromString(str_value);
}
continue;
}
if (cp.ParseNext(arg, 1, "--autobattle-algo")) {
std::string svalue;
if (arg.ParseValue(0, svalue)) {
player.autobattle_algo.Set(std::move(svalue));
}
continue;
}
if (cp.ParseNext(arg, 1, "--enemyai-algo")) {
std::string svalue;
if (arg.ParseValue(0, svalue)) {
player.enemyai_algo.Set(std::move(svalue));
}
continue;
}
if (cp.ParseNext(arg, 1, "--music-volume")) {
if (arg.ParseValue(0, li_value)) {
audio.music_volume.Set(li_value);
}
continue;
}
if (cp.ParseNext(arg, 1, "--sound-volume")) {
if (arg.ParseValue(0, li_value)) {
audio.music_volume.Set(li_value);
}
continue;
}
cp.SkipNext();
}
}
void Game_Config::LoadFromStream(Filesystem_Stream::InputStream& is) {
lcf::INIReader ini(is);
if (ini.ParseError()) {
Output::Debug("Failed to parse ini config file {}", is.GetName());
return;
}
/** VIDEO SECTION */
video.vsync.FromIni(ini);
video.fullscreen.FromIni(ini);
video.show_fps.FromIni(ini);
video.fps_render_window.FromIni(ini);
video.fps_limit.FromIni(ini);
video.window_zoom.FromIni(ini);
video.scaling_mode.FromIni(ini);
video.stretch.FromIni(ini);
video.touch_ui.FromIni(ini);
video.game_resolution.FromIni(ini);
if (ini.HasValue("Video", "WindowX") && ini.HasValue("Video", "WindowY") && ini.HasValue("Video", "WindowWidth") && ini.HasValue("Video", "WindowHeight")) {
video.window_x.FromIni(ini);
video.window_y.FromIni(ini);
video.window_width.FromIni(ini);
video.window_height.FromIni(ini);
}
/** AUDIO SECTION */
audio.music_volume.FromIni(ini);
audio.sound_volume.FromIni(ini);
/** INPUT SECTION */
input.buttons = Input::GetDefaultButtonMappings();
auto& mappings = input.buttons;
for (int i = 0; i < Input::BUTTON_COUNT; ++i) {
auto button = static_cast<Input::InputButton>(i);
auto name = Input::kInputButtonNames.tag(button);
if (ini.HasValue("input", name)) {
auto values = ini.GetString("input", name, "");
mappings.RemoveAll(button);
auto keys = Utils::Tokenize(values, [](char32_t c) { return c == ','; });
// When it is a protected (important) button with zero mappings keep the default
// For all other buttons having no mapping is fine
bool has_mapping = false;
if (Input::IsProtectedButton(button)) {
// Check for protected (important) buttons if they have more than zero mappings
for (const auto& key: keys) {
Input::Keys::InputKey k;
if (Input::Keys::kInputKeyNames.etag(key.c_str(), k)) {
has_mapping = true;
break;
}
}
// If not, keep the default mapping
if (!has_mapping) {
continue;
}
}
// Load mappings from ini
for (const auto& key: keys) {
Input::Keys::InputKey k;
if (Input::Keys::kInputKeyNames.etag(key.c_str(), k)) {
mappings.Add({button, k});
}
}
}
}
input.gamepad_swap_analog.FromIni(ini);
input.gamepad_swap_dpad_with_buttons.FromIni(ini);
input.gamepad_swap_ab_and_xy.FromIni(ini);
input.speed_modifier_a.FromIni(ini);
input.speed_modifier_b.FromIni(ini);
/** PLAYER SECTION */
player.settings_autosave.FromIni(ini);
player.settings_in_title.FromIni(ini);
player.settings_in_menu.FromIni(ini);
player.show_startup_logos.FromIni(ini);
}
void Game_Config::WriteToStream(Filesystem_Stream::OutputStream& os) const {
/** VIDEO SECTION */
os << "[Video]\n";
video.vsync.ToIni(os);
video.fullscreen.ToIni(os);
video.show_fps.ToIni(os);
video.fps_render_window.ToIni(os);
video.fps_limit.ToIni(os);
video.window_zoom.ToIni(os);
video.scaling_mode.ToIni(os);
video.stretch.ToIni(os);
video.touch_ui.ToIni(os);
video.game_resolution.ToIni(os);
// only preserve when toggling between window and fullscreen is supported
if (video.fullscreen.IsOptionVisible()) {
video.window_x.ToIni(os);
video.window_y.ToIni(os);
video.window_width.ToIni(os);
video.window_height.ToIni(os);
}
os << "\n";
/** AUDIO SECTION */
os << "[Audio]\n";
audio.music_volume.ToIni(os);
audio.sound_volume.ToIni(os);
os << "\n";
/** INPUT SECTION */
os << "[Input]\n";
auto& mappings = Input::GetInputSource()->GetButtonMappings();
for (int i = 0; i < Input::BUTTON_COUNT; ++i) {
auto button = static_cast<Input::InputButton>(i);
auto name = Input::kInputButtonNames.tag(button);
os << name << "=";
std::stringstream ss;
bool first = true;
for (auto ki = mappings.LowerBound(button); ki != mappings.end() && ki->first == button;++ki) {
if (!first) {
os << ",";
}
first = false;
auto key = static_cast<Input::Keys::InputKey>(ki->second);
auto kname = Input::Keys::kInputKeyNames.tag(key);
os << kname;
}
os << "\n";
}
input.gamepad_swap_analog.ToIni(os);
input.gamepad_swap_dpad_with_buttons.ToIni(os);
input.gamepad_swap_ab_and_xy.ToIni(os);
input.speed_modifier_a.ToIni(os);
input.speed_modifier_b.ToIni(os);
os << "\n";
/** PLAYER SECTION */
os << "[Player]\n";
//os << "autobattle-algo=" << player.autobattle_algo.Get() << "\n";
//os << "enemyai-algo=" << player.enemyai_algo.Get() << "\n";
player.settings_autosave.ToIni(os);
player.settings_in_title.ToIni(os);
player.settings_in_menu.ToIni(os);
player.show_startup_logos.ToIni(os);
os << "\n";
}