Refactored game selection code.

This commit is contained in:
WrinklyNinja
2014-07-14 19:43:37 +01:00
parent ba1bb8b877
commit bdfb13c82b
3 changed files with 37 additions and 41 deletions
+24 -4
View File
@@ -62,6 +62,26 @@ 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["Last Game"] && settings["Last Game"].as<string>() != "auto")
preferredGame = settings["Last Game"].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;
}
throw error(error::no_game_detected, "None of the supported games were detected.");
}
// MetadataList member functions
//------------------------------
@@ -588,20 +608,20 @@ namespace loot {
void Game::LoadPlugins(bool headersOnly) {
boost::thread_group group;
size_t meanFileSize = 0;
unordered_map<std::string, size_t> tempMap;
uintmax_t meanFileSize = 0;
unordered_map<std::string, uintmax_t> tempMap;
std::vector<Plugin*> groupPlugins;
//First calculate the mean plugin size. Store it temporarily in a map to reduce filesystem lookups and file size recalculation.
for (fs::directory_iterator it(this->DataPath()); it != fs::directory_iterator(); ++it) {
if (fs::is_regular_file(it->status()) && IsPlugin(it->path().string())) {
size_t fileSize = fs::file_size(it->path());
uintmax_t fileSize = fs::file_size(it->path());
meanFileSize += fileSize;
tempMap.emplace(it->path().filename().string(), fileSize);
}
}
meanFileSize /= tempMap.size();
meanFileSize /= tempMap.size(); //Rounding error, but not important.
//Now load plugins.
for (const auto &pluginPair : tempMap) {
+2
View File
@@ -157,6 +157,8 @@ namespace loot {
};
std::vector<Game> GetGames(const YAML::Node& settings);
size_t SelectGame(const YAML::Node& settings, const std::vector<Game>& games, const std::string& cmdLineGame);
}
#endif
+11 -37
View File
@@ -34,18 +34,12 @@
#include "../backend/helpers.h"
#include "../backend/generators.h"
#include "../backend/streams.h"
//#include "../backend/graph.h"
#include <ostream>
#include <algorithm>
#include <iterator>
#include <ctime>
#include <clocale>
#include <unordered_map>
#include <boost/filesystem.hpp>
#include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/locale.hpp>
#include <boost/log/core.hpp>
@@ -54,7 +48,6 @@
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/thread.hpp>
#include <wx/snglinst.h>
#include <wx/aboutdlg.h>
@@ -237,7 +230,6 @@ bool LOOT::OnInit() {
BOOST_LOG_TRIVIAL(debug) << "Selecting game.";
string target;
int gameIndex = -1;
wxCmdLineEntryDesc cmdLineDesc[2];
cmdLineDesc[0].kind = wxCMD_LINE_OPTION;
@@ -263,36 +255,18 @@ bool LOOT::OnInit() {
break;
}
if (target.empty()) {
if (_settings["Game"] && _settings["Game"].as<string>() != "auto")
target = _settings["Game"].as<string>();
else if (_settings["Last Game"] && _settings["Last Game"].as<string>() != "auto")
target = _settings["Last Game"].as<string>();
size_t gameIndex(0);
try {
gameIndex = SelectGame(_settings, _games, target);
}
if (!target.empty()) {
for (size_t i=0, max=_games.size(); i < max; ++i) {
if (target == _games[i].FolderName() && _games[i].IsInstalled())
gameIndex = i;
}
}
if (gameIndex < 0) {
//Set gameIndex to the first installed game.
for (size_t i=0, max=_games.size(); i < max; ++i) {
if (_games[i].IsInstalled()) {
gameIndex = i;
break;
}
}
if (gameIndex < 0) {
BOOST_LOG_TRIVIAL(error) << "None of the supported games were detected.";
wxMessageBox(
translate("Error: None of the supported games were detected."),
translate("LOOT: Error"),
wxOK | wxICON_ERROR,
nullptr);
return false;
}
catch (exception &e) {
BOOST_LOG_TRIVIAL(error) << "None of the supported games were detected.";
wxMessageBox(
translate("Error: None of the supported games were detected."),
translate("LOOT: Error"),
wxOK | wxICON_ERROR,
nullptr);
return false;
}
BOOST_LOG_TRIVIAL(debug) << "Game selected is " << _games[gameIndex].Name();