Merge branch 'loot-state-improvements' into dev

This commit is contained in:
Oliver Hamlet
2016-01-20 18:29:04 +00:00
7 changed files with 139 additions and 61 deletions
+3 -1
View File
@@ -165,6 +165,7 @@ set (LOOT_VALIDATOR_HEADERS ${LOOT_HEADERS})
set (LOOT_TESTS_SRC ${LOOT_SRC}
"${CMAKE_SOURCE_DIR}/src/api/loot_db.cpp"
"${CMAKE_SOURCE_DIR}/src/gui/loot_settings.cpp"
"${CMAKE_SOURCE_DIR}/src/gui/loot_state.cpp"
"${CMAKE_SOURCE_DIR}/src/tests/main.cpp")
set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h"
@@ -193,7 +194,8 @@ set (LOOT_TESTS_HEADERS "${CMAKE_SOURCE_DIR}/src/tests/fixtures.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/test_metadata_list.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/test_masterlist.h"
"${CMAKE_SOURCE_DIR}/src/tests/backend/test_plugin_sorter.h"
"${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_settings.h")
"${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_settings.h"
"${CMAKE_SOURCE_DIR}/src/tests/gui/test_loot_state.h")
source_group("Header Files" FILES ${LOOT_HEADERS} ${LOOT_GUI_HEADERS} ${LOOT_API_HEADERS} ${LOOT_TESTS_HEADERS})
+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.";
+53 -48
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);
@@ -239,7 +243,8 @@ namespace loot {
}
void LootState::decrementUnappliedChangeCounter() {
--unappliedChangeCounter;
if (unappliedChangeCounter > 0)
--unappliedChangeCounter;
}
void LootState::SelectGame(std::string preferredGame) {
@@ -252,12 +257,12 @@ namespace loot {
}
// Get iterator to preferred game.
_currentGame = find_if(begin(_games), end(_games), [&](auto& game) {
_currentGame = find_if(begin(_games), end(_games), [&](Game& game) {
return (preferredGame.empty() || preferredGame == game.FolderName()) && game.IsInstalled();
});
// If the preferred game cannot be found, get the first installed game.
if (_currentGame == end(_games)) {
_currentGame = find_if(begin(_games), end(_games), [](auto& game) {
_currentGame = find_if(begin(_games), end(_games), [](Game& game) {
return game.IsInstalled();
});
}
+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();
-2
View File
@@ -28,8 +28,6 @@ along with LOOT. If not, see
#include "gui/loot_settings.h"
#include "backend/globals.h"
#include "tests/fixtures.h"
namespace loot {
namespace test {
class LootSettings : public ::testing::Test {
+77
View File
@@ -0,0 +1,77 @@
/* LOOT
A load order optimisation tool for Oblivion, Skyrim, Fallout 3 and
Fallout: New Vegas.
Copyright (C) 2014-2015 WrinklyNinja
This file is part of LOOT.
LOOT is free software: you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
LOOT is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LOOT. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef LOOT_TEST_GUI_LOOT_STATE
#define LOOT_TEST_GUI_LOOT_STATE
#include "gui/loot_state.h"
#include "tests/fixtures.h"
namespace loot {
namespace test {
class LootState : public ::testing::Test {
protected:
loot::LootState lootState;
};
TEST_F(LootState, hasUnappliedChangesShouldBeFalseByDefault) {
EXPECT_FALSE(lootState.hasUnappliedChanges());
}
TEST_F(LootState, shouldNotHaveUnappliedChangesIfCounterIsDeccremented) {
lootState.decrementUnappliedChangeCounter();
EXPECT_FALSE(lootState.hasUnappliedChanges());
}
TEST_F(LootState, shouldHaveUnappliedChangesIfCounterIsIncremented) {
lootState.incrementUnappliedChangeCounter();
EXPECT_TRUE(lootState.hasUnappliedChanges());
}
TEST_F(LootState, incrementingTheChangeCounterMoreThanItIsDecrementedShouldLeaveUnappliedChanges) {
lootState.incrementUnappliedChangeCounter();
lootState.incrementUnappliedChangeCounter();
lootState.decrementUnappliedChangeCounter();
EXPECT_TRUE(lootState.hasUnappliedChanges());
}
TEST_F(LootState, incrementingTheChangeCounterLessThanItIsDecrementedShouldLeaveNoUnappliedChanges) {
lootState.incrementUnappliedChangeCounter();
lootState.decrementUnappliedChangeCounter();
lootState.decrementUnappliedChangeCounter();
EXPECT_FALSE(lootState.hasUnappliedChanges());
}
TEST_F(LootState, incrementingTheChangeCounterThenDecrementingItEquallyShouldLeaveNoUnappliedChanges) {
lootState.incrementUnappliedChangeCounter();
lootState.incrementUnappliedChangeCounter();
lootState.decrementUnappliedChangeCounter();
lootState.decrementUnappliedChangeCounter();
EXPECT_FALSE(lootState.hasUnappliedChanges());
}
}
}
#endif
+1
View File
@@ -52,6 +52,7 @@
#include "backend/test_masterlist.h"
#include "backend/test_plugin_sorter.h"
#include "gui/test_loot_settings.h"
#include "gui/test_loot_state.h"
#include <boost/log/core.hpp>