From f91daae84e36d5b58f48a379280ce42e2d2531d1 Mon Sep 17 00:00:00 2001 From: Nikolai Wuttke Date: Sat, 13 Jan 2024 15:33:02 +0100 Subject: [PATCH 1/2] Refactor: Make options deserialization more flexible --- src/frontend/user_profile.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/frontend/user_profile.cpp b/src/frontend/user_profile.cpp index 1d5a3965..a3344288 100644 --- a/src/frontend/user_profile.cpp +++ b/src/frontend/user_profile.cpp @@ -672,10 +672,10 @@ data::GameOptions deserialize(const nlohmann::json& json) } -template -void deserializeJsonObjectIfPresent( +template +void deserializeJsonFile( const std::filesystem::path& path, - T& result) + Func&& deserializeFunc) { namespace fs = std::filesystem; @@ -692,7 +692,7 @@ void deserializeJsonObjectIfPresent( nlohmann::json serializedObject; jsonFile >> serializedObject; - result = deserialize(serializedObject); + deserializeFunc(serializedObject); } catch (const std::exception& ex) { @@ -738,12 +738,16 @@ UserProfile loadProfile( { auto optionsFile = fileOnDisk; optionsFile.replace_filename(OPTIONS_FILENAME); - deserializeJsonObjectIfPresent( - optionsFile, profile.mOptions); + deserializeJsonFile( + optionsFile, [&](const nlohmann::json& serializedObject) { + profile.mOptions = deserialize(serializedObject); + }); optionsFile.replace_filename(MOD_LIBRARY_FILENAME); - deserializeJsonObjectIfPresent( - optionsFile, profile.mModLibrary); + deserializeJsonFile( + optionsFile, [&](const nlohmann::json& serializedObject) { + profile.mModLibrary = deserialize(serializedObject); + }); } return profile; From 4267e3befdd331c6c0b8318973ff4b70f1627dca Mon Sep 17 00:00:00 2001 From: Nikolai Wuttke Date: Sat, 13 Jan 2024 15:39:56 +0100 Subject: [PATCH 2/2] Store gamepath in Options.json as well --- src/frontend/user_profile.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/frontend/user_profile.cpp b/src/frontend/user_profile.cpp index a3344288..ce6df762 100644 --- a/src/frontend/user_profile.cpp +++ b/src/frontend/user_profile.cpp @@ -741,6 +741,13 @@ UserProfile loadProfile( deserializeJsonFile( optionsFile, [&](const nlohmann::json& serializedObject) { profile.mOptions = deserialize(serializedObject); + + if (serializedObject.contains("gamePath")) + { + const auto gamePathStr = + serializedObject.at("gamePath").get(); + profile.mGamePath = fs::u8path(gamePathStr); + } }); optionsFile.replace_filename(MOD_LIBRARY_FILENAME); @@ -865,7 +872,15 @@ void UserProfile::saveToDisk() "Failed to open %s for writing", path.u8string().c_str()); - optionsFile << std::setw(4) << options; + + auto optionsPlusGamePath = options; + + if (mGamePath) + { + optionsPlusGamePath["gamePath"] = mGamePath->u8string(); + } + + optionsFile << std::setw(4) << optionsPlusGamePath; } {