comparison config.c @ 1931:374a5ae694e8 mame_interp

Merge from default
author Michael Pavone <pavone@retrodev.com>
date Sat, 18 Apr 2020 11:42:53 -0700
parents 5433252329fb
children 193b804c9845
comparison
equal deleted inserted replaced
1843:13abdc98379e 1931:374a5ae694e8
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. 4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */ 5 */
6 #include "tern.h" 6 #include "tern.h"
7 #include "util.h" 7 #include "util.h"
8 #include "paths.h" 8 #include "paths.h"
9 #include "config.h"
9 #include <stdio.h> 10 #include <stdio.h>
10 #include <stdlib.h> 11 #include <stdlib.h>
11 #include <string.h> 12 #include <string.h>
12 13
13 #ifdef __MINGW64_VERSION_MAJOR 14 #ifdef __MINGW64_VERSION_MAJOR
223 } 224 }
224 #endif 225 #endif
225 return ret; 226 return ret;
226 } 227 }
227 228
228 tern_node *load_overrideable_config(char *name, char *bundled_name) 229 tern_node *load_overrideable_config(char *name, char *bundled_name, uint8_t *used_config_dir)
229 { 230 {
230 char const *confdir = get_config_dir(); 231 char const *confdir = get_config_dir();
231 char *confpath = NULL; 232 char *confpath = NULL;
232 tern_node *ret; 233 tern_node *ret;
233 if (confdir) { 234 if (confdir) {
234 confpath = path_append(confdir, name); 235 confpath = path_append(confdir, name);
235 ret = parse_config_file(confpath); 236 ret = parse_config_file(confpath);
236 if (ret) { 237 }
237 free(confpath); 238 free(confpath);
238 return ret; 239 if (used_config_dir) {
239 } 240 *used_config_dir = ret != NULL;
240 } 241 }
241 242
242 ret = parse_bundled_config(bundled_name); 243 if (!ret) {
243 if (ret) { 244 ret = parse_bundled_config(name);
244 free(confpath); 245 if (!ret) {
245 return ret; 246 ret = parse_bundled_config(bundled_name);
246 } 247 }
247 return NULL; 248 }
248 } 249
249 250 return ret;
251 }
252
253 static uint8_t app_config_in_config_dir;
250 tern_node *load_config() 254 tern_node *load_config()
251 { 255 {
252 char const *confdir = get_config_dir(); 256 tern_node *ret = load_overrideable_config("blastem.cfg", "default.cfg", &app_config_in_config_dir);
253 char *confpath = NULL; 257
254 tern_node *ret = load_overrideable_config("blastem.cfg", "default.cfg"); 258 if (!ret) {
255 if (confdir) { 259 if (get_config_dir()) {
256 confpath = path_append(confdir, "blastem.cfg"); 260 fatal_error("Failed to find a config file at %s or in the blastem executable directory\n", get_config_dir());
257 ret = parse_config_file(confpath); 261 } else {
258 if (ret) { 262 fatal_error("Failed to find a config file in the BlastEm executable directory and the config directory path could not be determined\n");
259 free(confpath); 263 }
260 return ret; 264 }
261 } 265 return ret;
262 } 266 }
263 267
264 ret = parse_bundled_config("default.cfg"); 268 void persist_config_at(tern_node *app_config, tern_node *to_save, char *fname)
265 if (ret) { 269 {
266 free(confpath); 270 char*use_exe_dir = tern_find_path_default(app_config, "ui\0config_in_exe_dir\0", (tern_val){.ptrval = "off"}, TVAL_PTR).ptrval;
267 return ret; 271 char *confpath;
268 } 272 if (!strcmp(use_exe_dir, "on")) {
269 273 confpath = path_append(get_exe_dir(), fname);
270 if (get_config_dir()) { 274 if (app_config == to_save && app_config_in_config_dir) {
271 fatal_error("Failed to find a config file at %s or in the blastem executable directory\n", get_config_dir()); 275 //user switched to "portable" configs this session and there is an
276 //existing config file in the user-specific config directory
277 //delete it so we don't end up loading it next time
278 char *oldpath = path_append(get_config_dir(), fname);
279 delete_file(oldpath);
280 free(oldpath);
281 }
272 } else { 282 } else {
273 fatal_error("Failed to find a config file in the BlastEm executable directory and the config directory path could not be determined\n"); 283 char const *confdir = get_config_dir();
274 } 284 if (!confdir) {
275 //this will never get reached, but the compiler doesn't know that. Let's make it happy 285 fatal_error("Failed to locate config file directory\n");
276 return NULL; 286 }
277 } 287 ensure_dir_exists(confdir);
278 288 confpath = path_append(confdir, fname);
279 void persist_config_at(tern_node *config, char *fname) 289 }
280 { 290 if (!serialize_config_file(to_save, confpath)) {
281 char const *confdir = get_config_dir();
282 if (!confdir) {
283 fatal_error("Failed to locate config file directory\n");
284 }
285 ensure_dir_exists(confdir);
286 char *confpath = path_append(confdir, fname);
287 if (!serialize_config_file(config, confpath)) {
288 fatal_error("Failed to write config to %s\n", confpath); 291 fatal_error("Failed to write config to %s\n", confpath);
289 } 292 }
290 free(confpath); 293 free(confpath);
291 } 294 }
292 295
293 void persist_config(tern_node *config) 296 void persist_config(tern_node *config)
294 { 297 {
295 persist_config_at(config, "blastem.cfg"); 298 persist_config_at(config, config, "blastem.cfg");
296 } 299 }
297 300
298 char **get_extension_list(tern_node *config, uint32_t *num_exts_out) 301 char **get_extension_list(tern_node *config, uint32_t *num_exts_out)
299 { 302 {
300 char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval); 303 char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval);
318 uint32_t get_lowpass_cutoff(tern_node *config) 321 uint32_t get_lowpass_cutoff(tern_node *config)
319 { 322 {
320 char * lowpass_cutoff_str = tern_find_path(config, "audio\0lowpass_cutoff\0", TVAL_PTR).ptrval; 323 char * lowpass_cutoff_str = tern_find_path(config, "audio\0lowpass_cutoff\0", TVAL_PTR).ptrval;
321 return lowpass_cutoff_str ? atoi(lowpass_cutoff_str) : DEFAULT_LOWPASS_CUTOFF; 324 return lowpass_cutoff_str ? atoi(lowpass_cutoff_str) : DEFAULT_LOWPASS_CUTOFF;
322 } 325 }
326
327 tern_node *get_systems_config(void)
328 {
329 static tern_node *systems;
330 if (!systems) {
331 systems = parse_bundled_config("systems.cfg");
332 }
333 return systems;
334 }
335
336 tern_node *get_model(tern_node *config, system_type stype)
337 {
338 char *model = tern_find_path_default(config, "system\0model\0", (tern_val){.ptrval = "md1va3"}, TVAL_PTR).ptrval;
339 return tern_find_node(get_systems_config(), model);
340 }