From 2206e988a66582041c0a279d33750917d538a790 Mon Sep 17 00:00:00 2001 From: Sude Date: Tue, 1 Sep 2015 14:45:34 +0300 Subject: [PATCH] Initial support for using platform/language strings to set options --- include/util.h | 2 ++ main.cpp | 41 +++++++++++++++++++++++++++-------------- src/downloader.cpp | 7 ++----- src/util.cpp | 27 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/include/util.h b/include/util.h index afc8506..865791d 100644 --- a/include/util.h +++ b/include/util.h @@ -17,6 +17,7 @@ #include #include #include +#include #include struct gameSpecificConfig @@ -45,6 +46,7 @@ namespace Util std::string getConfigHome(); std::string getCacheHome(); std::vector tokenize(const std::string& str, const std::string& separator = ","); + unsigned int getOptionValue(const std::string& str, const std::vector& options); } #endif // UTIL_H diff --git a/main.cpp b/main.cpp index d7dbfd7..9b39246 100644 --- a/main.cpp +++ b/main.cpp @@ -27,12 +27,12 @@ template void set_vm_value(std::map &priority, unsigned int &type) +void handle_priority(const std::string &what, const std::string &priority_string, std::vector &priority, unsigned int &type, const std::vector& options) { std::vector tokens = Util::tokenize(priority_string, ","); for (std::vector::iterator it = tokens.begin(); it != tokens.end(); it++) { - priority.push_back(std::stoi(*it)); + priority.push_back(Util::getOptionValue(*it, options)); } unsigned int wanted = 0; @@ -57,6 +57,17 @@ void handle_priority(const std::string &what, const std::string &priority_string } } +unsigned int parseOptionString(const std::string &option_string, const std::vector& options) +{ + unsigned int value = 0; + std::vector tokens = Util::tokenize(option_string, ","); + for (std::vector::iterator it = tokens.begin(); it != tokens.end(); it++) + { + value |= Util::getOptionValue(*it, options); + } + return value; +} + int main(int argc, char *argv[]) { Config config; @@ -72,23 +83,21 @@ int main(int argc, char *argv[]) // Create help text for --platform option std::string platform_text = "Select which installers are downloaded\n"; - unsigned int platform_sum = 0; + unsigned int platform_all = Util::getOptionValue("all", GlobalConstants::PLATFORMS); for (unsigned int i = 0; i < GlobalConstants::PLATFORMS.size(); ++i) { platform_text += std::to_string(GlobalConstants::PLATFORMS[i].id) + " = " + GlobalConstants::PLATFORMS[i].str + "\n"; - platform_sum += GlobalConstants::LANGUAGES[i].id; } - platform_text += std::to_string(platform_sum) + " = All"; + platform_text += std::to_string(platform_all) + " = All"; // Create help text for --language option std::string language_text = "Select which language installers are downloaded\n"; - unsigned int language_sum = 0; + unsigned int language_all = Util::getOptionValue("all", GlobalConstants::LANGUAGES); for (unsigned int i = 0; i < GlobalConstants::LANGUAGES.size(); ++i) { language_text += std::to_string(GlobalConstants::LANGUAGES[i].id) + " = " + GlobalConstants::LANGUAGES[i].str + "\n"; - language_sum += GlobalConstants::LANGUAGES[i].id; } - language_text += "Add the values to download multiple languages\nAll = " + std::to_string(language_sum) + "\n" + language_text += "Add the values to download multiple languages\nAll = " + std::to_string(language_all) + "\n" + "French + Polish = " + std::to_string(GlobalConstants::LANGUAGE_FR) + "+" + std::to_string(GlobalConstants::LANGUAGE_PL) + " = " + std::to_string(GlobalConstants::LANGUAGE_FR | GlobalConstants::LANGUAGE_PL); // Create help text for --check-orphans @@ -124,6 +133,8 @@ int main(int argc, char *argv[]) bool bNoCover = false; bool bNoPlatformDetection = false; bool bLogin = false; + std::string sInstallerPlatform; + std::string sInstallerLanguage; config.bReport = false; // Commandline options (no config file) options_cli_no_cfg.add_options() @@ -157,8 +168,8 @@ int main(int argc, char *argv[]) ("limit-rate", bpo::value(&config.iDownloadRate)->default_value(0), "Limit download rate to value in kB\n0 = unlimited") ("xml-directory", bpo::value(&config.sXMLDirectory), "Set directory for GOG XML files") ("chunk-size", bpo::value(&config.iChunkSize)->default_value(10), "Chunk size (in MB) when creating XML") - ("platform", bpo::value(&config.iInstallerPlatform)->default_value(GlobalConstants::PLATFORM_WINDOWS|GlobalConstants::PLATFORM_LINUX), platform_text.c_str()) - ("language", bpo::value(&config.iInstallerLanguage)->default_value(GlobalConstants::LANGUAGE_EN), language_text.c_str()) + ("platform", bpo::value(&sInstallerPlatform)->default_value(std::to_string(GlobalConstants::PLATFORM_WINDOWS|GlobalConstants::PLATFORM_LINUX)), platform_text.c_str()) + ("language", bpo::value(&sInstallerLanguage)->default_value(std::to_string(GlobalConstants::LANGUAGE_EN)), language_text.c_str()) ("no-installers", bpo::value(&bNoInstallers)->zero_tokens()->default_value(false), "Don't download/list/repair installers") ("no-extras", bpo::value(&bNoExtras)->zero_tokens()->default_value(false), "Don't download/list/repair extras") ("no-patches", bpo::value(&bNoPatches)->zero_tokens()->default_value(false), "Don't download/list/repair patches") @@ -313,6 +324,8 @@ int main(int argc, char *argv[]) config.bRemoteXML = !bNoRemoteXML; config.bSubDirectories = !bNoSubDirectories; config.bPlatformDetection = !bNoPlatformDetection; + config.iInstallerLanguage = parseOptionString(sInstallerLanguage, GlobalConstants::LANGUAGES); + config.iInstallerPlatform = parseOptionString(sInstallerPlatform, GlobalConstants::PLATFORMS); for (auto i = unrecognized_options_cli.begin(); i != unrecognized_options_cli.end(); ++i) if (i->compare(0, GlobalConstants::PROTOCOL_PREFIX.length(), GlobalConstants::PROTOCOL_PREFIX) == 0) @@ -336,9 +349,9 @@ int main(int argc, char *argv[]) // Handle priority business if (!config.sLanguagePriority.empty()) - handle_priority("languages", config.sLanguagePriority, config.vLanguagePriority, config.iInstallerLanguage); + handle_priority("languages", config.sLanguagePriority, config.vLanguagePriority, config.iInstallerLanguage, GlobalConstants::LANGUAGES); if (!config.sPlatformPriority.empty()) - handle_priority("platforms", config.sPlatformPriority, config.vPlatformPriority, config.iInstallerPlatform); + handle_priority("platforms", config.sPlatformPriority, config.vPlatformPriority, config.iInstallerPlatform, GlobalConstants::PLATFORMS); } catch (std::exception& e) @@ -352,13 +365,13 @@ int main(int argc, char *argv[]) return 1; } - if (config.iInstallerPlatform < GlobalConstants::PLATFORMS[0].id || config.iInstallerPlatform > platform_sum) + if (config.iInstallerPlatform < GlobalConstants::PLATFORMS[0].id || config.iInstallerPlatform > platform_all) { std::cout << "Invalid value for --platform" << std::endl; return 1; } - if (config.iInstallerLanguage < GlobalConstants::LANGUAGES[0].id || config.iInstallerLanguage > language_sum) + if (config.iInstallerLanguage < GlobalConstants::LANGUAGES[0].id || config.iInstallerLanguage > language_all) { std::cout << "Invalid value for --language" << std::endl; return 1; diff --git a/src/downloader.cpp b/src/downloader.cpp index fdef8ec..fdafda1 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -3103,17 +3103,14 @@ std::vector Downloader::getGameDetailsFromJsonNode(Json::Value root void Downloader::updateCache() { // Make sure that all details get cached - unsigned int all_platforms = (2 << (GlobalConstants::PLATFORMS.size() - 1)) - 1; - unsigned int all_languages = (2 << (GlobalConstants::LANGUAGES.size() - 1)) - 1; - config.bExtras = true; config.bInstallers = true; config.bPatches = true; config.bLanguagePacks = true; config.bDLC = true; config.sGameRegex = ".*"; - config.iInstallerLanguage = all_languages; - config.iInstallerPlatform = all_platforms; + config.iInstallerLanguage = Util::getOptionValue("all", GlobalConstants::LANGUAGES); + config.iInstallerPlatform = Util::getOptionValue("all", GlobalConstants::PLATFORMS); config.vLanguagePriority.clear(); config.vPlatformPriority.clear(); diff --git a/src/util.cpp b/src/util.cpp index c841824..33ce98b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -424,3 +424,30 @@ std::vector Util::tokenize(const std::string& str, const std::strin return tokens; } + +unsigned int Util::getOptionValue(const std::string& str, const std::vector& options) +{ + unsigned int value = 0; + boost::regex expression("^[+-]?\\d+$", boost::regex::perl); + boost::match_results what; + if (str == "all") + { + value = (2 << (options.size() - 1)) - 1; + } + else if (boost::regex_search(str, what, expression)) + { + value = std::stoi(str); + } + else + { + for (unsigned int i = 0; i < options.size(); ++i) + { + if (str == options[i].code) + { + value = options[i].id; + break; + } + } + } + return value; +}