Skip to content

Commit e313c55

Browse files
committed
rg_settings: Added a safe_mode (no loading/saving of config files)
`rg_settings_reset` is still allowed to change files on disk, because wiping existing config is "safe" in that context.
1 parent 017c702 commit e313c55

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

components/retro-go/rg_settings.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <cJSON.h>
77

88
static cJSON *config_root = NULL;
9+
static bool safe_mode = false;
910

1011

1112
static cJSON *json_root(const char *name, bool set_dirty)
@@ -30,23 +31,25 @@ static cJSON *json_root(const char *name, bool set_dirty)
3031
branch = cJSON_AddObjectToObject(config_root, name);
3132
cJSON_AddStringToObject(branch, "namespace", name);
3233
cJSON_AddNumberToObject(branch, "changed", 0);
33-
34-
char *data; size_t data_len;
35-
char pathbuf[RG_PATH_MAX];
36-
snprintf(pathbuf, RG_PATH_MAX, "%s/%s.json", RG_BASE_PATH_CONFIG, name);
37-
if (rg_storage_read_file(pathbuf, (void **)&data, &data_len, 0))
34+
if (!safe_mode)
3835
{
39-
cJSON *values = cJSON_Parse(data);
40-
if (!values) // Parse failure, clean the markup and try again
41-
values = cJSON_Parse(rg_json_fixup(data));
42-
if (values)
36+
char *data; size_t data_len;
37+
char pathbuf[RG_PATH_MAX];
38+
snprintf(pathbuf, RG_PATH_MAX, "%s/%s.json", RG_BASE_PATH_CONFIG, name);
39+
if (rg_storage_read_file(pathbuf, (void **)&data, &data_len, 0))
4340
{
44-
RG_LOGI("Config file loaded: '%s'", pathbuf);
45-
cJSON_AddItemToObject(branch, "values", values);
41+
cJSON *values = cJSON_Parse(data);
42+
if (!values) // Parse failure, clean the markup and try again
43+
values = cJSON_Parse(rg_json_fixup(data));
44+
if (values)
45+
{
46+
RG_LOGI("Config file loaded: '%s'", pathbuf);
47+
cJSON_AddItemToObject(branch, "values", values);
48+
}
49+
else
50+
RG_LOGE("Config file parsing failed: '%s'", pathbuf);
51+
free(data);
4652
}
47-
else
48-
RG_LOGE("Config file parsing failed: '%s'", pathbuf);
49-
free(data);
5053
}
5154
}
5255

@@ -76,16 +79,17 @@ static void update_value(const char *section, const char *key, cJSON *new_value)
7679
cJSON_Delete(new_value);
7780
}
7881

79-
void rg_settings_init(void)
82+
void rg_settings_init(bool _safe_mode)
8083
{
8184
config_root = cJSON_CreateObject();
85+
safe_mode = _safe_mode;
8286
json_root(NS_GLOBAL, 0);
8387
json_root(NS_BOOT, 0);
8488
}
8589

8690
void rg_settings_commit(void)
8791
{
88-
if (!config_root)
92+
if (!config_root || safe_mode)
8993
return;
9094

9195
for (cJSON *branch = config_root->child; branch; branch = branch->next)

components/retro-go/rg_settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#define NS_WIFI ((char *)3)
1111
#define NS_BOOT ((char *)4)
1212

13-
void rg_settings_init(void);
13+
// Safe mode means no loading/saving config files
14+
void rg_settings_init(bool safe_mode);
1415
void rg_settings_commit(void);
1516
void rg_settings_reset(void);
1617
double rg_settings_get_number(const char *section, const char *key, double default_value);

components/retro-go/rg_system.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ static void enter_recovery_mode(void)
289289
{
290290
RG_LOGW("Entering recovery mode...\n");
291291

292+
// FIXME: At this point we don't have valid settings, we should find way to get the user's language...
292293
const rg_gui_option_t options[] = {
293294
{0, _("Reset all settings"), NULL, RG_DIALOG_FLAG_NORMAL, NULL},
294295
{1, _("Reboot to factory "), NULL, RG_DIALOG_FLAG_NORMAL, NULL},
@@ -414,11 +415,12 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
414415
rg_task_delay(100);
415416
}
416417

418+
rg_settings_init(enterRecoveryMode || showCrashDialog);
419+
rg_display_init();
420+
rg_gui_init();
421+
417422
if (enterRecoveryMode)
418423
{
419-
rg_display_init();
420-
//rg_settings_init(); FIXME: Find a way to get the user's language without loading all settings
421-
rg_gui_init();
422424
enter_recovery_mode();
423425
}
424426
else if (showCrashDialog)
@@ -433,8 +435,6 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
433435
if (rg_system_save_trace(RG_STORAGE_ROOT "/crash.log", 1))
434436
strcat(message, "\nLog saved to SD Card.");
435437
}
436-
rg_display_init();
437-
rg_gui_init();
438438
rg_display_clear(C_BLUE);
439439
rg_gui_alert("System Panic!", message);
440440
rg_system_exit();
@@ -445,7 +445,6 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
445445
update_memory_statistics();
446446
app.lowMemoryMode = statistics.totalMemoryExt == 0;
447447

448-
rg_settings_init();
449448
app.configNs = rg_settings_get_string(NS_BOOT, SETTING_BOOT_NAME, app.name);
450449
app.bootArgs = rg_settings_get_string(NS_BOOT, SETTING_BOOT_ARGS, "");
451450
app.bootFlags = rg_settings_get_number(NS_BOOT, SETTING_BOOT_FLAGS, 0);
@@ -454,8 +453,6 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, const rg
454453
// app.isLauncher = strcmp(app.name, RG_APP_LAUNCHER) == 0; // Might be overriden after init
455454
app.indicatorsMask = rg_settings_get_number(NS_GLOBAL, SETTING_INDICATOR_MASK, app.indicatorsMask);
456455

457-
rg_display_init();
458-
rg_gui_init();
459456
rg_gui_draw_hourglass();
460457
rg_audio_init(sampleRate);
461458

0 commit comments

Comments
 (0)