Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store gamepath in Options.json #918

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading