Mercurial > repos > blastem
comparison config.c @ 1852:a4cae960fd08
Allow config file to be saved with executable for "portable" setups
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 24 Apr 2019 19:08:49 -0700 |
parents | eda8df5bc74c |
children | 93960907807a |
comparison
equal
deleted
inserted
replaced
1851:419b458f93cd | 1852:a4cae960fd08 |
---|---|
223 } | 223 } |
224 #endif | 224 #endif |
225 return ret; | 225 return ret; |
226 } | 226 } |
227 | 227 |
228 tern_node *load_overrideable_config(char *name, char *bundled_name) | 228 tern_node *load_overrideable_config(char *name, char *bundled_name, uint8_t *used_config_dir) |
229 { | 229 { |
230 char const *confdir = get_config_dir(); | 230 char const *confdir = get_config_dir(); |
231 char *confpath = NULL; | 231 char *confpath = NULL; |
232 tern_node *ret; | 232 tern_node *ret; |
233 if (confdir) { | 233 if (confdir) { |
234 confpath = path_append(confdir, name); | 234 confpath = path_append(confdir, name); |
235 ret = parse_config_file(confpath); | 235 ret = parse_config_file(confpath); |
236 if (ret) { | 236 } |
237 free(confpath); | 237 free(confpath); |
238 return ret; | 238 if (used_config_dir) { |
239 } | 239 *used_config_dir = ret != NULL; |
240 } | 240 } |
241 | 241 |
242 ret = parse_bundled_config(bundled_name); | 242 if (!ret) { |
243 if (ret) { | 243 ret = parse_bundled_config(name); |
244 free(confpath); | 244 if (!ret) { |
245 return ret; | 245 ret = parse_bundled_config(bundled_name); |
246 } | 246 } |
247 return NULL; | 247 } |
248 } | 248 |
249 | 249 return ret; |
250 } | |
251 | |
252 static uint8_t app_config_in_config_dir; | |
250 tern_node *load_config() | 253 tern_node *load_config() |
251 { | 254 { |
252 char const *confdir = get_config_dir(); | 255 tern_node *ret = load_overrideable_config("blastem.cfg", "default.cfg", &app_config_in_config_dir); |
253 char *confpath = NULL; | 256 |
254 tern_node *ret = load_overrideable_config("blastem.cfg", "default.cfg"); | 257 if (!ret) { |
255 if (confdir) { | 258 if (get_config_dir()) { |
256 confpath = path_append(confdir, "blastem.cfg"); | 259 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); | 260 } else { |
258 if (ret) { | 261 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); | 262 } |
260 return ret; | 263 } |
261 } | 264 return ret; |
262 } | 265 } |
263 | 266 |
264 ret = parse_bundled_config("default.cfg"); | 267 void persist_config_at(tern_node *app_config, tern_node *to_save, char *fname) |
265 if (ret) { | 268 { |
266 free(confpath); | 269 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; | 270 char *confpath; |
268 } | 271 if (!strcmp(use_exe_dir, "on")) { |
269 | 272 confpath = path_append(get_exe_dir(), fname); |
270 if (get_config_dir()) { | 273 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()); | 274 //user switched to "portable" configs this session and there is an |
275 //existing config file in the user-specific config directory | |
276 //delete it so we don't end up loading it next time | |
277 char *oldpath = path_append(get_config_dir(), fname); | |
278 delete_file(oldpath); | |
279 free(oldpath); | |
280 } | |
272 } else { | 281 } 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"); | 282 char const *confdir = get_config_dir(); |
274 } | 283 if (!confdir) { |
275 //this will never get reached, but the compiler doesn't know that. Let's make it happy | 284 fatal_error("Failed to locate config file directory\n"); |
276 return NULL; | 285 } |
277 } | 286 ensure_dir_exists(confdir); |
278 | 287 confpath = path_append(confdir, fname); |
279 void persist_config_at(tern_node *config, char *fname) | 288 } |
280 { | 289 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); | 290 fatal_error("Failed to write config to %s\n", confpath); |
289 } | 291 } |
290 free(confpath); | 292 free(confpath); |
291 } | 293 } |
292 | 294 |
293 void persist_config(tern_node *config) | 295 void persist_config(tern_node *config) |
294 { | 296 { |
295 persist_config_at(config, "blastem.cfg"); | 297 persist_config_at(config, config, "blastem.cfg"); |
296 } | 298 } |
297 | 299 |
298 char **get_extension_list(tern_node *config, uint32_t *num_exts_out) | 300 char **get_extension_list(tern_node *config, uint32_t *num_exts_out) |
299 { | 301 { |
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); | 302 char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval); |