Skip to content

Commit

Permalink
Merge pull request #918 from lethal-guitar/gamepath-in-options
Browse files Browse the repository at this point in the history
Store gamepath in `Options.json`
  • Loading branch information
lethal-guitar authored Jan 26, 2024
2 parents 4d97e1e + 4267e3b commit 6f76229
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/frontend/user_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ data::GameOptions deserialize<data::GameOptions>(const nlohmann::json& json)
}


template <typename T>
void deserializeJsonObjectIfPresent(
template <typename Func>
void deserializeJsonFile(
const std::filesystem::path& path,
T& result)
Func&& deserializeFunc)
{
namespace fs = std::filesystem;

Expand All @@ -692,7 +692,7 @@ void deserializeJsonObjectIfPresent(
nlohmann::json serializedObject;
jsonFile >> serializedObject;

result = deserialize<T>(serializedObject);
deserializeFunc(serializedObject);
}
catch (const std::exception& ex)
{
Expand Down Expand Up @@ -738,12 +738,23 @@ UserProfile loadProfile(
{
auto optionsFile = fileOnDisk;
optionsFile.replace_filename(OPTIONS_FILENAME);
deserializeJsonObjectIfPresent<data::GameOptions>(
optionsFile, profile.mOptions);
deserializeJsonFile(
optionsFile, [&](const nlohmann::json& serializedObject) {
profile.mOptions = deserialize<data::GameOptions>(serializedObject);

if (serializedObject.contains("gamePath"))
{
const auto gamePathStr =
serializedObject.at("gamePath").get<std::string>();
profile.mGamePath = fs::u8path(gamePathStr);
}
});

optionsFile.replace_filename(MOD_LIBRARY_FILENAME);
deserializeJsonObjectIfPresent<data::ModLibrary>(
optionsFile, profile.mModLibrary);
deserializeJsonFile(
optionsFile, [&](const nlohmann::json& serializedObject) {
profile.mModLibrary = deserialize<data::ModLibrary>(serializedObject);
});
}

return profile;
Expand Down Expand Up @@ -861,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;
}

{
Expand Down

0 comments on commit 6f76229

Please sign in to comment.