Swapped game vector for list.

Fixes #357.
This commit is contained in:
Oliver Hamlet
2014-12-26 17:17:35 +00:00
parent d31cd5dede
commit 94afd1a5d3
5 changed files with 57 additions and 54 deletions
+3 -29
View File
@@ -43,11 +43,11 @@ namespace fs = boost::filesystem;
namespace lc = boost::locale;
namespace loot {
std::vector<Game> GetGames(YAML::Node& settings) {
vector<Game> games;
std::list<Game> GetGames(YAML::Node& settings) {
list<Game> games;
if (settings["games"])
games = settings["games"].as< vector<Game> >();
games = settings["games"].as< list<Game> >();
if (find(games.begin(), games.end(), Game(Game::tes4)) == games.end())
games.push_back(Game(Game::tes4));
@@ -67,32 +67,6 @@ namespace loot {
return games;
}
size_t SelectGame(const YAML::Node& settings, const std::vector<Game>& games, const std::string& cmdLineGame) {
string preferredGame(cmdLineGame);
if (preferredGame.empty()) {
// Get preferred game from settings.
if (settings["game"] && settings["game"].as<string>() != "auto")
preferredGame = settings["game"].as<string>();
else if (settings["lastGame"] && settings["lastGame"].as<string>() != "auto")
preferredGame = settings["lastGame"].as<string>();
}
// Get index of preferred game if there is one.
for (size_t i = 0; i < games.size(); ++i) {
if (preferredGame.empty() && games[i].IsInstalled())
return i;
else if (!preferredGame.empty() && preferredGame == games[i].FolderName() && games[i].IsInstalled())
return i;
}
// Preferred game not found, just pick the first installed one.
for (size_t i = 0; i < games.size(); ++i) {
if (games[i].IsInstalled())
return i;
}
throw error(error::no_game_detected, "None of the supported games were detected.");
}
// MetadataList member functions
//------------------------------
+1 -3
View File
@@ -185,9 +185,7 @@ namespace loot {
void InitLibloHandle();
};
std::vector<Game> GetGames(YAML::Node& settings);
size_t SelectGame(const YAML::Node& settings, const std::vector<Game>& games, const std::string& cmdLineGame);
std::list<Game> GetGames(YAML::Node& settings);
}
#endif
+46 -18
View File
@@ -136,7 +136,7 @@ namespace loot {
// LootState member functions
//---------------------------
LootState::LootState() : numUnappliedChanges(0), _currentGame(0) {}
LootState::LootState() : numUnappliedChanges(0), _currentGame(_games.end()) {}
void LootState::Init(const std::string& cmdLineGame) {
// Do some preliminary locale / UTF-8 support setup here, in case the settings file reading requires it.
@@ -239,9 +239,9 @@ namespace loot {
try {
BOOST_LOG_TRIVIAL(debug) << "Selecting game.";
_currentGame = SelectGame(_settings, _games, cmdLineGame);
SelectGame(cmdLineGame);
BOOST_LOG_TRIVIAL(debug) << "Initialising game-specific settings.";
_games[_currentGame].Init(true);
_currentGame->Init(true);
// Update game path in settings object.
_settings["games"] = _games;
}
@@ -255,20 +255,21 @@ namespace loot {
_initErrors.push_back((format(translate("Error: Game-specific settings could not be initialised. %1%")) % e.what()).str());
}
}
BOOST_LOG_TRIVIAL(debug) << "Game selected is " << _games[_currentGame].Name();
BOOST_LOG_TRIVIAL(debug) << "Game selected is " << _currentGame->Name();
}
const std::vector<std::string>& LootState::InitErrors() const {
return _initErrors;
}
void LootState::UpdateGames(std::vector<Game>& games) {
void LootState::UpdateGames(std::list<Game>& games) {
// Acquire the lock for the scope of this method.
base::AutoLock lock_scope(_lock);
unordered_set<string> newGameFolders;
// Update existing games, add new games.
BOOST_LOG_TRIVIAL(trace) << "Updating existing games and adding new games.";
for (auto &game : games) {
auto pos = find(_games.begin(), _games.end(), game);
@@ -276,6 +277,7 @@ namespace loot {
pos->SetDetails(game.Name(), game.Master(), game.RepoURL(), game.RepoBranch(), game.GamePath().string(), game.RegistryKey());
}
else {
BOOST_LOG_TRIVIAL(trace) << "Adding new game entry for: " << game.FolderName();
_games.push_back(game);
}
@@ -284,13 +286,10 @@ namespace loot {
// 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()) {
if (distance(_games.begin(), it) < _currentGame) {
// Deleting a game before the current game, so the current game's
// index decreases by one.
--_currentGame;
}
BOOST_LOG_TRIVIAL(trace) << "Removing game: " << it->FolderName();
it = _games.erase(it);
}
else
@@ -298,7 +297,7 @@ namespace loot {
}
// Re-initialise the current game in case the game path setting was changed.
_games[_currentGame].Init(true);
_currentGame->Init(true);
// Update game path in settings object.
_settings["games"] = _games;
}
@@ -308,20 +307,19 @@ namespace loot {
base::AutoLock lock_scope(_lock);
BOOST_LOG_TRIVIAL(debug) << "Changing current game to that with folder: " << newGameFolder;
auto it = find(_games.begin(), _games.end(), newGameFolder);
_currentGame = find(_games.begin(), _games.end(), newGameFolder);
_currentGame->Init(true);
_currentGame = std::distance(_games.begin(), it);
_games[_currentGame].Init(true);
// Update game path in settings object.
_settings["games"] = _games;
BOOST_LOG_TRIVIAL(debug) << "New game is " << _games[_currentGame].Name();
BOOST_LOG_TRIVIAL(debug) << "New game is " << _currentGame->Name();
}
Game& LootState::CurrentGame() {
// Acquire the lock for the scope of this method.
base::AutoLock lock_scope(_lock);
return _games[_currentGame];
return *_currentGame;
}
std::vector<std::string> LootState::InstalledGames() const {
@@ -348,7 +346,7 @@ namespace loot {
// Acquire the lock for the scope of this method.
base::AutoLock lock_scope(_lock);
_settings["lastGame"] = _games[_currentGame].FolderName();
_settings["lastGame"] = _currentGame->FolderName();
_settings["lastVersion"] = to_string(g_version_major) + "." + to_string(g_version_minor) + "." + to_string(g_version_patch);
//Save settings.
@@ -367,6 +365,36 @@ namespace loot {
}
}
void LootState::SelectGame(std::string preferredGame) {
if (preferredGame.empty()) {
// Get preferred game from settings.
if (_settings["game"] && _settings["game"].as<string>() != "auto")
preferredGame = _settings["game"].as<string>();
else if (_settings["lastGame"] && _settings["lastGame"].as<string>() != "auto")
preferredGame = _settings["lastGame"].as<string>();
}
// Get iterator to preferred game if there is one.
_currentGame = _games.end();
for (auto it = _games.begin(); it != _games.end(); ++it) {
if ((preferredGame.empty() && it->IsInstalled())
|| (!preferredGame.empty() && preferredGame == it->FolderName() && it->IsInstalled())) {
_currentGame = it;
return;
}
}
// Preferred game not found, just pick the first installed one.
for (auto it = _games.begin(); it != _games.end(); ++it) {
if (it->IsInstalled()) {
_currentGame = it;
return;
}
}
throw error(error::no_game_detected, "None of the supported games were detected.");
}
bool LootState::AreSettingsValid() {
// Acquire the lock for the scope of this method.
base::AutoLock lock_scope(_lock);
@@ -455,7 +483,7 @@ namespace loot {
root["enableDebugLogging"] = false;
root["updateMasterlist"] = true;
std::vector<Game> games;
std::list<Game> games;
games.push_back(Game(Game::tes4));
games.push_back(Game(Game::tes5));
games.push_back(Game(Game::fo3));
+6 -3
View File
@@ -74,7 +74,7 @@ namespace loot {
Game& CurrentGame();
void ChangeGame(const std::string& newGameFolder);
void UpdateGames(std::vector<Game>& games);
void UpdateGames(std::list<Game>& games);
// Get the folder names of the installed games.
std::vector<std::string> InstalledGames() const;
@@ -86,10 +86,13 @@ namespace loot {
int numUnappliedChanges;
private:
YAML::Node _settings;
std::vector<Game> _games;
size_t _currentGame;
std::list<Game> _games;
std::list<Game>::iterator _currentGame;
std::vector<std::string> _initErrors;
// Select initial game.
void SelectGame(std::string cmdLineGame);
// Check if the settings file has the right root keys (doesn't check their values).
bool AreSettingsValid();
YAML::Node GetDefaultSettings() const;
+1 -1
View File
@@ -273,7 +273,7 @@ namespace loot {
// It will be restored when LOOT is next loaded.
try {
BOOST_LOG_TRIVIAL(trace) << "Updating games object.";
vector<Game> games(request["args"][0]["games"].as< vector<Game> >());
list<Game> games(request["args"][0]["games"].as< list<Game> >());
g_app_state.UpdateGames(games);
// Also enable/disable debug logging as required.