diff --git a/src/app/app_core.c b/src/app/app_core.c index 3f1f3f9e..9262bed8 100644 --- a/src/app/app_core.c +++ b/src/app/app_core.c @@ -204,7 +204,7 @@ void P_AppStartup(String args_str) P_CloseFIle(settings_file); P_LogInfoF("Deserializing settings file data: %F", FmtString(file_data)); String error = ZI; - P_WindowSettings *deser = settings_deserialize(temp.arena, file_data, &error); + P_WindowSettings *deser = SETTINGS_WindowSettingsFromString(temp.arena, file_data, &error); if (error.len > 0) { P_LogInfoF("Failed to load settings file with error - %F", FmtString(error)); String msg = StringFormat(temp.arena, @@ -263,7 +263,7 @@ void P_AppStartup(String args_str) String window_settings_path = CatAppWritePath(temp.arena, settings_file_name); P_WindowSettings settings = P_GetWindowSettings(window); - String str = settings_serialize(temp.arena, &settings); + String str = SETTINGS_StringFromWindowSettings(temp.arena, &settings); P_LogInfoF("Serialized window settings: %F", FmtString(str)); P_LogInfoF("Writing settings file to path \"%F\"", FmtString(window_settings_path)); diff --git a/src/settings/settings_core.c b/src/settings/settings_core.c index a1c8bfc5..c247a933 100644 --- a/src/settings/settings_core.c +++ b/src/settings/settings_core.c @@ -1,4 +1,7 @@ -String settings_serialize(Arena *arena, const P_WindowSettings *settings) +//////////////////////////////// +//~ Serialize + +String SETTINGS_StringFromWindowSettings(Arena *arena, const P_WindowSettings *settings) { __prof; @@ -25,19 +28,22 @@ String settings_serialize(Arena *arena, const P_WindowSettings *settings) ); String formatted = StringFormat(arena, - fmt, - FmtString(minimized), - FmtString(maximized), - FmtString(fullscreen), - FmtSint(x), - FmtSint(y), - FmtSint(width), - FmtSint(height)); + fmt, + FmtString(minimized), + FmtString(maximized), + FmtString(fullscreen), + FmtSint(x), + FmtSint(y), + FmtSint(width), + FmtSint(height)); return formatted; } -P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_out) +//////////////////////////////// +//~ Deserialize + +P_WindowSettings *SETTINGS_WindowSettingsFromString(Arena *arena, String src, String *error_out) { __prof; TempArena scratch = BeginScratch(arena); @@ -48,19 +54,22 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o P_WindowSettings *settings = PushStruct(arena, P_WindowSettings); JSON_Result parse_result = JSON_BlobFromString(scratch.arena, src); - if (parse_result.errors.count > 0) { + if (parse_result.errors.count > 0) + { json_error = *parse_result.errors.first; goto abort; } JSON_Blob *root = parse_result.root; - if (!root) { + if (!root) + { error = Lit("Root object not found."); goto abort; } JSON_Blob *window = root->child_first; - if (!window || window->type != JSON_Type_Object || !EqString(window->key, Lit("window"))) { + if (!window || window->type != JSON_Type_Object || !EqString(window->key, Lit("window"))) + { error = Lit("\"window\" object not found"); goto abort; } @@ -71,50 +80,70 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o b32 found_y = 0; b32 found_width = 0; b32 found_height = 0; - for (JSON_Blob *child = window->child_first; child; child = child->next) { + for (JSON_Blob *child = window->child_first; child; child = child->next) + { String key = child->key; - if (EqString(key, Lit("maximized"))) { - if (child->type != JSON_Type_Bool) { + if (EqString(key, Lit("maximized"))) + { + if (child->type != JSON_Type_Bool) + { error = Lit("Expected boolean for \"maximized\""); goto abort; } - if (child->value.boolean) { + if (child->value.boolean) + { settings->flags |= P_WindowSettingsFlag_Maximized; } found_maximized = 1; - } else if (EqString(key, Lit("fullscreen"))) { - if (child->type != JSON_Type_Bool) { + } + else if (EqString(key, Lit("fullscreen"))) + { + if (child->type != JSON_Type_Bool) + { error = Lit("Expected boolean for \"fulscreen\""); goto abort; } - if (child->value.boolean) { + if (child->value.boolean) + { settings->flags |= P_WindowSettingsFlag_Fullscreen; } found_fullscreen = 1; - } else if (EqString(key, Lit("x"))) { - if (child->type != JSON_Type_Number) { + } + else if (EqString(key, Lit("x"))) + { + if (child->type != JSON_Type_Number) + { error = Lit("Expected number for \"x\""); goto abort; } settings->floating_x = RoundF32ToI32(child->value.number); found_x = 1; - } else if (EqString(key, Lit("y"))) { - if (child->type != JSON_Type_Number) { + } + else if (EqString(key, Lit("y"))) + { + if (child->type != JSON_Type_Number) + { error = Lit("Expected number for \"y\""); goto abort; } settings->floating_y = RoundF32ToI32(child->value.number); found_y = 1; - } else if (EqString(key, Lit("width"))) { - if (child->type != JSON_Type_Number) { + } + else if (EqString(key, Lit("width"))) + { + if (child->type != JSON_Type_Number) + { error = Lit("Expected number for \"width\""); goto abort; } settings->floating_width = RoundF32ToI32(child->value.number); found_width = 1; - } else if (EqString(key, Lit("height"))) { - if (child->type != JSON_Type_Number) { + } + else if (EqString(key, Lit("height"))) + { + if (child->type != JSON_Type_Number) + { error = Lit("Expected number for \"height\""); goto abort; } @@ -130,16 +159,20 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o if (!found_width) { error = Lit("Missing \"width\""); goto abort; } if (!found_height) { error = Lit("Missing \"height\""); goto abort; } - abort: +abort: - if (error_out && (error.len > 0 || json_error.msg.len > 0)) { - if (json_error.msg.len > 0) { + if (error_out && (error.len > 0 || json_error.msg.len > 0)) + { + if (json_error.msg.len > 0) + { *error_out = StringFormat(arena, - Lit("%F\n(%F:%F)"), - FmtString(json_error.msg), - FmtUint(json_error.start), - FmtUint(json_error.end)); - } else { + Lit("%F\n(%F:%F)"), + FmtString(json_error.msg), + FmtUint(json_error.start), + FmtUint(json_error.end)); + } + else + { *error_out = CopyString(arena, error); } } diff --git a/src/settings/settings_core.h b/src/settings/settings_core.h index 749b5318..cea134ec 100644 --- a/src/settings/settings_core.h +++ b/src/settings/settings_core.h @@ -1,3 +1,2 @@ -String settings_serialize(Arena *arena, const P_WindowSettings *settings); - -P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_out); +String SETTINGS_StringFromWindowSettings(Arena *arena, const P_WindowSettings *settings); +P_WindowSettings *SETTINGS_WindowSettingsFromString(Arena *arena, String src, String *error_out);