diff --git a/src/gui/handler.cpp b/src/gui/handler.cpp index b185dd6b..4d80422a 100644 --- a/src/gui/handler.cpp +++ b/src/gui/handler.cpp @@ -275,16 +275,11 @@ namespace loot { else if (requestName == "closeSettings") { BOOST_LOG_TRIVIAL(trace) << "Settings dialog closed and changes accepted, updating settings object."; - // Update the settings. - _lootState.load(request["args"][0]); - // If the user has deleted a default game, we don't want to restore it now. - // It will be restored when LOOT is next loaded. try { - BOOST_LOG_TRIVIAL(trace) << "Updating games object."; - _lootState.UpdateGamesFromSettings(); - - // Also enable/disable debug logging as required. - boost::log::core::get()->set_logging_enabled(_lootState.isDebugLoggingEnabled()); + // Update the settings. + // If the user has deleted a default game, we don't want to restore it now. + // It will be restored when LOOT is next loaded. + _lootState.load(request["args"][0]); // Now send back the new list of installed games to the UI. BOOST_LOG_TRIVIAL(trace) << "Getting new list of installed games."; diff --git a/src/gui/loot_state.cpp b/src/gui/loot_state.cpp index 8f8ff875..693f7af3 100644 --- a/src/gui/loot_state.cpp +++ b/src/gui/loot_state.cpp @@ -52,6 +52,54 @@ namespace fs = boost::filesystem; namespace loot { LootState::LootState() : unappliedChangeCounter(0), _currentGame(_games.end()) {} + void LootState::load(YAML::Node& settings) { + std::lock_guard guard(mutex); + + LootSettings::load(settings); + + // Enable/disable debug logging in case it has changed. + boost::log::core::get()->set_logging_enabled(isDebugLoggingEnabled()); + + // Update existing games, add new games. + unordered_set newGameFolders; + BOOST_LOG_TRIVIAL(trace) << "Updating existing games and adding new games."; + for (const auto &game : getGameSettings()) { + auto pos = find(_games.begin(), _games.end(), game); + + if (pos != _games.end()) { + pos->SetName(game.Name()) + .SetMaster(game.Master()) + .SetRepoURL(game.RepoURL()) + .SetRepoBranch(game.RepoBranch()) + .SetGamePath(game.GamePath()) + .SetRegistryKey(game.RegistryKey()); + } + else { + BOOST_LOG_TRIVIAL(trace) << "Adding new game entry for: " << game.FolderName(); + _games.push_back(game); + } + + newGameFolders.insert(game.FolderName()); + } + + // Remove deleted games. As the current game is stored using its index, + // removing an earlier game may invalidate it. + BOOST_LOG_TRIVIAL(trace) << "Removing deleted games."; + for (auto it = _games.begin(); it != _games.end();) { + if (newGameFolders.find(it->FolderName()) == newGameFolders.end()) { + BOOST_LOG_TRIVIAL(trace) << "Removing game: " << it->FolderName(); + it = _games.erase(it); + } + else + ++it; + } + + // Re-initialise the current game in case the game path setting was changed. + _currentGame->Init(true); + // Update game path in settings object. + storeGameSettings(ToGameSettings(_games)); + } + void LootState::Init(const std::string& cmdLineGame) { // Do some preliminary locale / UTF-8 support setup here, in case the settings file reading requires it. //Boost.Locale initialisation: Specify location of language dictionaries. @@ -75,7 +123,7 @@ namespace loot { } if (fs::exists(g_path_settings)) { try { - load(g_path_settings); + LootSettings::load(g_path_settings); } catch (exception& e) { _initErrors.push_back((format(translate("Error: Settings parsing failed. %1%")) % e.what()).str()); @@ -159,50 +207,6 @@ namespace loot { LootSettings::save(file); } - void LootState::UpdateGamesFromSettings() { - std::lock_guard guard(mutex); - - unordered_set newGameFolders; - - // Update existing games, add new games. - BOOST_LOG_TRIVIAL(trace) << "Updating existing games and adding new games."; - for (const auto &game : getGameSettings()) { - auto pos = find(_games.begin(), _games.end(), game); - - if (pos != _games.end()) { - pos->SetName(game.Name()) - .SetMaster(game.Master()) - .SetRepoURL(game.RepoURL()) - .SetRepoBranch(game.RepoBranch()) - .SetGamePath(game.GamePath()) - .SetRegistryKey(game.RegistryKey()); - } - else { - BOOST_LOG_TRIVIAL(trace) << "Adding new game entry for: " << game.FolderName(); - _games.push_back(game); - } - - newGameFolders.insert(game.FolderName()); - } - - // Remove deleted games. As the current game is stored using its index, - // removing an earlier game may invalidate it. - BOOST_LOG_TRIVIAL(trace) << "Removing deleted games."; - for (auto it = _games.begin(); it != _games.end();) { - if (newGameFolders.find(it->FolderName()) == newGameFolders.end()) { - BOOST_LOG_TRIVIAL(trace) << "Removing game: " << it->FolderName(); - it = _games.erase(it); - } - else - ++it; - } - - // Re-initialise the current game in case the game path setting was changed. - _currentGame->Init(true); - // Update game path in settings object. - storeGameSettings(ToGameSettings(_games)); - } - void LootState::ChangeGame(const std::string& newGameFolder) { std::lock_guard guard(mutex); diff --git a/src/gui/loot_state.h b/src/gui/loot_state.h index b43242ca..b7d7934b 100644 --- a/src/gui/loot_state.h +++ b/src/gui/loot_state.h @@ -33,6 +33,7 @@ namespace loot { public: LootState(); + void load(YAML::Node& settings); void Init(const std::string& cmdLineGame); const std::vector& InitErrors() const; @@ -40,7 +41,6 @@ namespace loot { Game& CurrentGame(); void ChangeGame(const std::string& newGameFolder); - void UpdateGamesFromSettings(); // Get the folder names of the installed games. std::vector InstalledGames();