diff --git a/src/backend/game.cpp b/src/backend/game.cpp index 18ed5ef9..e29e08ac 100644 --- a/src/backend/game.cpp +++ b/src/backend/game.cpp @@ -62,6 +62,26 @@ namespace loot { return games; } + size_t SelectGame(const YAML::Node& settings, const std::vector& games, const std::string& cmdLineGame) { + string preferredGame(cmdLineGame); + if (preferredGame.empty()) { + // Get preferred game from settings. + if (settings["Game"] && settings["Game"].as() != "auto") + preferredGame = settings["Game"].as(); + else if (settings["Last Game"] && settings["Last Game"].as() != "auto") + preferredGame = settings["Last Game"].as(); + } + + // 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 tempMap; + uintmax_t meanFileSize = 0; + unordered_map tempMap; std::vector 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) { diff --git a/src/backend/game.h b/src/backend/game.h index c15f13a6..540c53b1 100644 --- a/src/backend/game.h +++ b/src/backend/game.h @@ -157,6 +157,8 @@ namespace loot { }; std::vector GetGames(const YAML::Node& settings); + + size_t SelectGame(const YAML::Node& settings, const std::vector& games, const std::string& cmdLineGame); } #endif diff --git a/src/gui/main.cpp b/src/gui/main.cpp index bc595d80..15ea9edf 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -34,18 +34,12 @@ #include "../backend/helpers.h" #include "../backend/generators.h" #include "../backend/streams.h" -//#include "../backend/graph.h" -#include #include -#include -#include #include -#include #include #include -#include #include #include #include @@ -54,7 +48,6 @@ #include #include #include -#include #include #include @@ -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() != "auto") - target = _settings["Game"].as(); - else if (_settings["Last Game"] && _settings["Last Game"].as() != "auto") - target = _settings["Last Game"].as(); + 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();