Replace LootState::UpdateGamesFromSettings()

With an load() that calls LootSettings::load() and enables/disables
debug logging and includes the functionality of
UpdateGamesFromSettings().

There isn't a test for this because it can only be tested for games
detected as installed, and none can be without significant changes to
the testing files/folders that would break most other tests.
This commit is contained in:
Oliver Hamlet
2016-01-18 20:35:41 +00:00
parent 5e9566d8c0
commit 49db4fb7e1
3 changed files with 54 additions and 55 deletions
+4 -9
View File
@@ -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.";
+49 -45
View File
@@ -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<std::mutex> 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<string> 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<std::mutex> guard(mutex);
unordered_set<string> 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<std::mutex> guard(mutex);
+1 -1
View File
@@ -33,6 +33,7 @@ namespace loot {
public:
LootState();
void load(YAML::Node& settings);
void Init(const std::string& cmdLineGame);
const std::vector<std::string>& 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<std::string> InstalledGames();