src/core/config.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "config.h"
7
8#include "core/version.h"
9#include "util/formatting.h"
10#include "util/string.h"
11#include "util/vfs.h"
12
13#include <sys/stat.h>
14
15#ifdef _WIN32
16#include <windows.h>
17#include <shlwapi.h>
18#include <shlobj.h>
19#include <strsafe.h>
20#endif
21
22#ifdef PSP2
23#include <psp2/io/stat.h>
24#endif
25
26#ifdef _3DS
27#include "platform/3ds/3ds-vfs.h"
28#endif
29
30#define SECTION_NAME_MAX 128
31
32static const char* _lookupValue(const struct mCoreConfig* config, const char* key) {
33 const char* value;
34 if (config->port) {
35 value = ConfigurationGetValue(&config->overridesTable, config->port, key);
36 if (value) {
37 return value;
38 }
39 }
40 value = ConfigurationGetValue(&config->overridesTable, 0, key);
41 if (value) {
42 return value;
43 }
44 if (config->port) {
45 value = ConfigurationGetValue(&config->configTable, config->port, key);
46 if (value) {
47 return value;
48 }
49 }
50 value = ConfigurationGetValue(&config->configTable, 0, key);
51 if (value) {
52 return value;
53 }
54 if (config->port) {
55 value = ConfigurationGetValue(&config->defaultsTable, config->port, key);
56 if (value) {
57 return value;
58 }
59 }
60 return ConfigurationGetValue(&config->defaultsTable, 0, key);
61}
62
63static bool _lookupCharValue(const struct mCoreConfig* config, const char* key, char** out) {
64 const char* value = _lookupValue(config, key);
65 if (!value) {
66 return false;
67 }
68 if (*out) {
69 free(*out);
70 }
71 *out = strdup(value);
72 return true;
73}
74
75static bool _lookupIntValue(const struct mCoreConfig* config, const char* key, int* out) {
76 const char* charValue = _lookupValue(config, key);
77 if (!charValue) {
78 return false;
79 }
80 char* end;
81 long value = strtol(charValue, &end, 10);
82 if (*end) {
83 return false;
84 }
85 *out = value;
86 return true;
87}
88
89static bool _lookupUIntValue(const struct mCoreConfig* config, const char* key, unsigned* out) {
90 const char* charValue = _lookupValue(config, key);
91 if (!charValue) {
92 return false;
93 }
94 char* end;
95 unsigned long value = strtoul(charValue, &end, 10);
96 if (*end) {
97 return false;
98 }
99 *out = value;
100 return true;
101}
102
103static bool _lookupFloatValue(const struct mCoreConfig* config, const char* key, float* out) {
104 const char* charValue = _lookupValue(config, key);
105 if (!charValue) {
106 return false;
107 }
108 char* end;
109 float value = strtof_u(charValue, &end);
110 if (*end) {
111 return false;
112 }
113 *out = value;
114 return true;
115}
116
117void mCoreConfigInit(struct mCoreConfig* config, const char* port) {
118 ConfigurationInit(&config->configTable);
119 ConfigurationInit(&config->defaultsTable);
120 ConfigurationInit(&config->overridesTable);
121 if (port) {
122 config->port = malloc(strlen("ports.") + strlen(port) + 1);
123 snprintf(config->port, strlen("ports.") + strlen(port) + 1, "ports.%s", port);
124 } else {
125 config->port = 0;
126 }
127}
128
129void mCoreConfigDeinit(struct mCoreConfig* config) {
130 ConfigurationDeinit(&config->configTable);
131 ConfigurationDeinit(&config->defaultsTable);
132 ConfigurationDeinit(&config->overridesTable);
133 free(config->port);
134}
135
136#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
137bool mCoreConfigLoad(struct mCoreConfig* config) {
138 char path[PATH_MAX];
139 mCoreConfigDirectory(path, PATH_MAX);
140 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
141 return mCoreConfigLoadPath(config, path);
142}
143
144bool mCoreConfigSave(const struct mCoreConfig* config) {
145 char path[PATH_MAX];
146 mCoreConfigDirectory(path, PATH_MAX);
147 strncat(path, PATH_SEP "config.ini", PATH_MAX - strlen(path));
148 return mCoreConfigSavePath(config, path);
149}
150
151bool mCoreConfigLoadPath(struct mCoreConfig* config, const char* path) {
152 return ConfigurationRead(&config->configTable, path);
153}
154
155bool mCoreConfigSavePath(const struct mCoreConfig* config, const char* path) {
156 return ConfigurationWrite(&config->configTable, path);
157}
158
159void mCoreConfigMakePortable(const struct mCoreConfig* config) {
160 struct VFile* portable = 0;
161#ifdef _WIN32
162 char out[MAX_PATH];
163 wchar_t wpath[MAX_PATH];
164 wchar_t wprojectName[MAX_PATH];
165 MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
166 HMODULE hModule = GetModuleHandleW(NULL);
167 GetModuleFileNameW(hModule, wpath, MAX_PATH);
168 PathRemoveFileSpecW(wpath);
169 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, MAX_PATH, 0, 0);
170 StringCchCatA(out, MAX_PATH, "\\portable.ini");
171 portable = VFileOpen(out, O_WRONLY | O_CREAT);
172#elif defined(PSP2) || defined(_3DS) || defined(GEKKO)
173 // Already portable
174#else
175 char out[PATH_MAX];
176 getcwd(out, PATH_MAX);
177 strncat(out, PATH_SEP "portable.ini", PATH_MAX - strlen(out));
178 portable = VFileOpen(out, O_WRONLY | O_CREAT);
179#endif
180 if (portable) {
181 portable->close(portable);
182 mCoreConfigSave(config);
183 }
184}
185
186void mCoreConfigDirectory(char* out, size_t outLength) {
187 struct VFile* portable;
188#ifdef _WIN32
189 wchar_t wpath[MAX_PATH];
190 wchar_t wprojectName[MAX_PATH];
191 MultiByteToWideChar(CP_UTF8, 0, projectName, -1, wprojectName, MAX_PATH);
192 HMODULE hModule = GetModuleHandleW(NULL);
193 GetModuleFileNameW(hModule, wpath, MAX_PATH);
194 PathRemoveFileSpecW(wpath);
195 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
196 StringCchCatA(out, outLength, "\\portable.ini");
197 portable = VFileOpen(out, O_RDONLY);
198 if (portable) {
199 portable->close(portable);
200 } else {
201 wchar_t* home;
202 SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &home);
203 StringCchPrintfW(wpath, MAX_PATH, L"%ws\\%ws", home, wprojectName);
204 CoTaskMemFree(home);
205 CreateDirectoryW(wpath, NULL);
206 }
207 WideCharToMultiByte(CP_UTF8, 0, wpath, -1, out, outLength, 0, 0);
208#elif defined(PSP2)
209 UNUSED(portable);
210 snprintf(out, outLength, "ux0:/%s", projectName);
211 sceIoMkdir(out, 0777);
212#elif defined(GEKKO)
213 UNUSED(portable);
214 snprintf(out, outLength, "/%s", projectName);
215 mkdir(out, 0777);
216#elif defined(_3DS)
217 UNUSED(portable);
218 snprintf(out, outLength, "/%s", projectName);
219 FSUSER_CreateDirectory(sdmcArchive, fsMakePath(PATH_ASCII, out), 0);
220#else
221 getcwd(out, outLength);
222 strncat(out, PATH_SEP "portable.ini", outLength - strlen(out));
223 portable = VFileOpen(out, O_RDONLY);
224 if (portable) {
225 getcwd(out, outLength);
226 portable->close(portable);
227 return;
228 }
229
230 char* home = getenv("HOME");
231 snprintf(out, outLength, "%s/.config", home);
232 mkdir(out, 0755);
233 snprintf(out, outLength, "%s/.config/%s", home, binaryName);
234 mkdir(out, 0755);
235#endif
236}
237#endif
238
239const char* mCoreConfigGetValue(const struct mCoreConfig* config, const char* key) {
240 return _lookupValue(config, key);
241}
242
243bool mCoreConfigGetIntValue(const struct mCoreConfig* config, const char* key, int* value) {
244 return _lookupIntValue(config, key, value);
245}
246
247bool mCoreConfigGetUIntValue(const struct mCoreConfig* config, const char* key, unsigned* value) {
248 return _lookupUIntValue(config, key, value);
249}
250
251bool mCoreConfigGetFloatValue(const struct mCoreConfig* config, const char* key, float* value) {
252 return _lookupFloatValue(config, key, value);
253}
254
255void mCoreConfigSetValue(struct mCoreConfig* config, const char* key, const char* value) {
256 ConfigurationSetValue(&config->configTable, config->port, key, value);
257}
258
259void mCoreConfigSetIntValue(struct mCoreConfig* config, const char* key, int value) {
260 ConfigurationSetIntValue(&config->configTable, config->port, key, value);
261}
262
263void mCoreConfigSetUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
264 ConfigurationSetUIntValue(&config->configTable, config->port, key, value);
265}
266
267void mCoreConfigSetFloatValue(struct mCoreConfig* config, const char* key, float value) {
268 ConfigurationSetFloatValue(&config->configTable, config->port, key, value);
269}
270
271void mCoreConfigSetDefaultValue(struct mCoreConfig* config, const char* key, const char* value) {
272 ConfigurationSetValue(&config->defaultsTable, config->port, key, value);
273}
274
275void mCoreConfigSetDefaultIntValue(struct mCoreConfig* config, const char* key, int value) {
276 ConfigurationSetIntValue(&config->defaultsTable, config->port, key, value);
277}
278
279void mCoreConfigSetDefaultUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
280 ConfigurationSetUIntValue(&config->defaultsTable, config->port, key, value);
281}
282
283void mCoreConfigSetDefaultFloatValue(struct mCoreConfig* config, const char* key, float value) {
284 ConfigurationSetFloatValue(&config->defaultsTable, config->port, key, value);
285}
286
287void mCoreConfigSetOverrideValue(struct mCoreConfig* config, const char* key, const char* value) {
288 ConfigurationSetValue(&config->overridesTable, config->port, key, value);
289}
290
291void mCoreConfigSetOverrideIntValue(struct mCoreConfig* config, const char* key, int value) {
292 ConfigurationSetIntValue(&config->overridesTable, config->port, key, value);
293}
294
295void mCoreConfigSetOverrideUIntValue(struct mCoreConfig* config, const char* key, unsigned value) {
296 ConfigurationSetUIntValue(&config->overridesTable, config->port, key, value);
297}
298
299void mCoreConfigSetOverrideFloatValue(struct mCoreConfig* config, const char* key, float value) {
300 ConfigurationSetFloatValue(&config->overridesTable, config->port, key, value);
301}
302
303void mCoreConfigMap(const struct mCoreConfig* config, struct mCoreOptions* opts) {
304 _lookupCharValue(config, "bios", &opts->bios);
305 _lookupCharValue(config, "shader", &opts->shader);
306 _lookupIntValue(config, "logLevel", &opts->logLevel);
307 _lookupIntValue(config, "frameskip", &opts->frameskip);
308 _lookupIntValue(config, "volume", &opts->volume);
309 _lookupIntValue(config, "rewindBufferCapacity", &opts->rewindBufferCapacity);
310 _lookupIntValue(config, "rewindBufferInterval", &opts->rewindBufferInterval);
311 _lookupFloatValue(config, "fpsTarget", &opts->fpsTarget);
312 unsigned audioBuffers;
313 if (_lookupUIntValue(config, "audioBuffers", &audioBuffers)) {
314 opts->audioBuffers = audioBuffers;
315 }
316 _lookupUIntValue(config, "sampleRate", &opts->sampleRate);
317
318 int fakeBool;
319 if (_lookupIntValue(config, "useBios", &fakeBool)) {
320 opts->useBios = fakeBool;
321 }
322 if (_lookupIntValue(config, "audioSync", &fakeBool)) {
323 opts->audioSync = fakeBool;
324 }
325 if (_lookupIntValue(config, "videoSync", &fakeBool)) {
326 opts->videoSync = fakeBool;
327 }
328 if (_lookupIntValue(config, "lockAspectRatio", &fakeBool)) {
329 opts->lockAspectRatio = fakeBool;
330 }
331 if (_lookupIntValue(config, "resampleVideo", &fakeBool)) {
332 opts->resampleVideo = fakeBool;
333 }
334 if (_lookupIntValue(config, "suspendScreensaver", &fakeBool)) {
335 opts->suspendScreensaver = fakeBool;
336 }
337 if (_lookupIntValue(config, "mute", &fakeBool)) {
338 opts->mute = fakeBool;
339 }
340 if (_lookupIntValue(config, "skipBios", &fakeBool)) {
341 opts->skipBios = fakeBool;
342 }
343 if (_lookupIntValue(config, "rewindEnable", &fakeBool)) {
344 opts->rewindEnable = fakeBool;
345 }
346
347 _lookupIntValue(config, "fullscreen", &opts->fullscreen);
348 _lookupIntValue(config, "width", &opts->width);
349 _lookupIntValue(config, "height", &opts->height);
350
351 _lookupCharValue(config, "savegamePath", &opts->savegamePath);
352 _lookupCharValue(config, "savestatePath", &opts->savestatePath);
353 _lookupCharValue(config, "screenshotPath", &opts->screenshotPath);
354 _lookupCharValue(config, "patchPath", &opts->patchPath);
355}
356
357void mCoreConfigLoadDefaults(struct mCoreConfig* config, const struct mCoreOptions* opts) {
358 ConfigurationSetValue(&config->defaultsTable, 0, "bios", opts->bios);
359 ConfigurationSetValue(&config->defaultsTable, 0, "shader", opts->shader);
360 ConfigurationSetIntValue(&config->defaultsTable, 0, "skipBios", opts->skipBios);
361 ConfigurationSetIntValue(&config->defaultsTable, 0, "useBios", opts->useBios);
362 ConfigurationSetIntValue(&config->defaultsTable, 0, "logLevel", opts->logLevel);
363 ConfigurationSetIntValue(&config->defaultsTable, 0, "frameskip", opts->frameskip);
364 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindEnable", opts->rewindEnable);
365 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferCapacity", opts->rewindBufferCapacity);
366 ConfigurationSetIntValue(&config->defaultsTable, 0, "rewindBufferInterval", opts->rewindBufferInterval);
367 ConfigurationSetFloatValue(&config->defaultsTable, 0, "fpsTarget", opts->fpsTarget);
368 ConfigurationSetUIntValue(&config->defaultsTable, 0, "audioBuffers", opts->audioBuffers);
369 ConfigurationSetUIntValue(&config->defaultsTable, 0, "sampleRate", opts->sampleRate);
370 ConfigurationSetIntValue(&config->defaultsTable, 0, "audioSync", opts->audioSync);
371 ConfigurationSetIntValue(&config->defaultsTable, 0, "videoSync", opts->videoSync);
372 ConfigurationSetIntValue(&config->defaultsTable, 0, "fullscreen", opts->fullscreen);
373 ConfigurationSetIntValue(&config->defaultsTable, 0, "width", opts->width);
374 ConfigurationSetIntValue(&config->defaultsTable, 0, "height", opts->height);
375 ConfigurationSetIntValue(&config->defaultsTable, 0, "volume", opts->volume);
376 ConfigurationSetIntValue(&config->defaultsTable, 0, "mute", opts->mute);
377 ConfigurationSetIntValue(&config->defaultsTable, 0, "lockAspectRatio", opts->lockAspectRatio);
378 ConfigurationSetIntValue(&config->defaultsTable, 0, "resampleVideo", opts->resampleVideo);
379 ConfigurationSetIntValue(&config->defaultsTable, 0, "suspendScreensaver", opts->suspendScreensaver);
380}
381
382// These two are basically placeholders in case the internal layout changes, e.g. for loading separate files
383struct Configuration* mCoreConfigGetInput(struct mCoreConfig* config) {
384 return &config->configTable;
385}
386
387struct Configuration* mCoreConfigGetOverrides(struct mCoreConfig* config) {
388 return &config->configTable;
389}
390
391const struct Configuration* mCoreConfigGetOverridesConst(const struct mCoreConfig* config) {
392 return &config->configTable;
393}
394
395void mCoreConfigFreeOpts(struct mCoreOptions* opts) {
396 free(opts->bios);
397 free(opts->shader);
398 free(opts->savegamePath);
399 free(opts->savestatePath);
400 free(opts->screenshotPath);
401 free(opts->patchPath);
402 opts->bios = 0;
403 opts->shader = 0;
404 opts->savegamePath = 0;
405 opts->savestatePath = 0;
406 opts->screenshotPath = 0;
407 opts->patchPath = 0;
408}