settings layer refactor

This commit is contained in:
jacob 2025-07-30 20:54:10 -05:00
parent 061e88a75f
commit 7253e8f210
3 changed files with 73 additions and 41 deletions

View File

@ -204,7 +204,7 @@ void P_AppStartup(String args_str)
P_CloseFIle(settings_file); P_CloseFIle(settings_file);
P_LogInfoF("Deserializing settings file data: %F", FmtString(file_data)); P_LogInfoF("Deserializing settings file data: %F", FmtString(file_data));
String error = ZI; 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) { if (error.len > 0) {
P_LogInfoF("Failed to load settings file with error - %F", FmtString(error)); P_LogInfoF("Failed to load settings file with error - %F", FmtString(error));
String msg = StringFormat(temp.arena, 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); String window_settings_path = CatAppWritePath(temp.arena, settings_file_name);
P_WindowSettings settings = P_GetWindowSettings(window); 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("Serialized window settings: %F", FmtString(str));
P_LogInfoF("Writing settings file to path \"%F\"", FmtString(window_settings_path)); P_LogInfoF("Writing settings file to path \"%F\"", FmtString(window_settings_path));

View File

@ -1,4 +1,7 @@
String settings_serialize(Arena *arena, const P_WindowSettings *settings) ////////////////////////////////
//~ Serialize
String SETTINGS_StringFromWindowSettings(Arena *arena, const P_WindowSettings *settings)
{ {
__prof; __prof;
@ -25,19 +28,22 @@ String settings_serialize(Arena *arena, const P_WindowSettings *settings)
); );
String formatted = StringFormat(arena, String formatted = StringFormat(arena,
fmt, fmt,
FmtString(minimized), FmtString(minimized),
FmtString(maximized), FmtString(maximized),
FmtString(fullscreen), FmtString(fullscreen),
FmtSint(x), FmtSint(x),
FmtSint(y), FmtSint(y),
FmtSint(width), FmtSint(width),
FmtSint(height)); FmtSint(height));
return formatted; 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; __prof;
TempArena scratch = BeginScratch(arena); 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); P_WindowSettings *settings = PushStruct(arena, P_WindowSettings);
JSON_Result parse_result = JSON_BlobFromString(scratch.arena, src); 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; json_error = *parse_result.errors.first;
goto abort; goto abort;
} }
JSON_Blob *root = parse_result.root; JSON_Blob *root = parse_result.root;
if (!root) { if (!root)
{
error = Lit("Root object not found."); error = Lit("Root object not found.");
goto abort; goto abort;
} }
JSON_Blob *window = root->child_first; 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"); error = Lit("\"window\" object not found");
goto abort; goto abort;
} }
@ -71,50 +80,70 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
b32 found_y = 0; b32 found_y = 0;
b32 found_width = 0; b32 found_width = 0;
b32 found_height = 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; String key = child->key;
if (EqString(key, Lit("maximized"))) { if (EqString(key, Lit("maximized")))
if (child->type != JSON_Type_Bool) { {
if (child->type != JSON_Type_Bool)
{
error = Lit("Expected boolean for \"maximized\""); error = Lit("Expected boolean for \"maximized\"");
goto abort; goto abort;
} }
if (child->value.boolean) { if (child->value.boolean)
{
settings->flags |= P_WindowSettingsFlag_Maximized; settings->flags |= P_WindowSettingsFlag_Maximized;
} }
found_maximized = 1; 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\""); error = Lit("Expected boolean for \"fulscreen\"");
goto abort; goto abort;
} }
if (child->value.boolean) { if (child->value.boolean)
{
settings->flags |= P_WindowSettingsFlag_Fullscreen; settings->flags |= P_WindowSettingsFlag_Fullscreen;
} }
found_fullscreen = 1; 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\""); error = Lit("Expected number for \"x\"");
goto abort; goto abort;
} }
settings->floating_x = RoundF32ToI32(child->value.number); settings->floating_x = RoundF32ToI32(child->value.number);
found_x = 1; 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\""); error = Lit("Expected number for \"y\"");
goto abort; goto abort;
} }
settings->floating_y = RoundF32ToI32(child->value.number); settings->floating_y = RoundF32ToI32(child->value.number);
found_y = 1; 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\""); error = Lit("Expected number for \"width\"");
goto abort; goto abort;
} }
settings->floating_width = RoundF32ToI32(child->value.number); settings->floating_width = RoundF32ToI32(child->value.number);
found_width = 1; 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\""); error = Lit("Expected number for \"height\"");
goto abort; 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_width) { error = Lit("Missing \"width\""); goto abort; }
if (!found_height) { error = Lit("Missing \"height\""); 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 (error_out && (error.len > 0 || json_error.msg.len > 0))
if (json_error.msg.len > 0) { {
if (json_error.msg.len > 0)
{
*error_out = StringFormat(arena, *error_out = StringFormat(arena,
Lit("%F\n(%F:%F)"), Lit("%F\n(%F:%F)"),
FmtString(json_error.msg), FmtString(json_error.msg),
FmtUint(json_error.start), FmtUint(json_error.start),
FmtUint(json_error.end)); FmtUint(json_error.end));
} else { }
else
{
*error_out = CopyString(arena, error); *error_out = CopyString(arena, error);
} }
} }

View File

@ -1,3 +1,2 @@
String settings_serialize(Arena *arena, const P_WindowSettings *settings); String SETTINGS_StringFromWindowSettings(Arena *arena, const P_WindowSettings *settings);
P_WindowSettings *SETTINGS_WindowSettingsFromString(Arena *arena, String src, String *error_out);
P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_out);