From 86bb528c39daeee7592ca3c2b8974eb193e13a22 Mon Sep 17 00:00:00 2001 From: Sude Date: Mon, 22 Apr 2019 18:14:53 +0300 Subject: [PATCH] Add regex support to --galaxy-install and --galaxy-show-builds Check if option value is regex and then ask user to select one product from a list of products matching the regex. --- include/downloader.h | 3 ++ main.cpp | 4 +-- src/downloader.cpp | 75 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/include/downloader.h b/include/downloader.h index acdda27..f754530 100644 --- a/include/downloader.h +++ b/include/downloader.h @@ -107,7 +107,9 @@ class Downloader void saveGalaxyJSON(); void galaxyInstallGame(const std::string& product_id, int build_index = -1, const unsigned int& iGalaxyArch = GlobalConstants::ARCH_X64); + void galaxyInstallGameById(const std::string& product_id, int build_index = -1, const unsigned int& iGalaxyArch = GlobalConstants::ARCH_X64); void galaxyShowBuilds(const std::string& product_id, int build_index = -1); + void galaxyShowBuildsById(const std::string& product_id, int build_index = -1); protected: private: CURLcode downloadFile(const std::string& url, const std::string& filepath, const std::string& xml_data = std::string(), const std::string& gamename = std::string()); @@ -143,6 +145,7 @@ class Downloader static void processGalaxyDownloadQueue_MojoSetupHack(Config conf, const unsigned int& tid); int mojoSetupGetFileVector(const gameFile& gf, std::vector& vFiles); std::string getGalaxyInstallDirectory(galaxyAPI *galaxyHandle, const Json::Value& manifest); + bool galaxySelectProductIdHelper(const std::string& product_id, std::string& selected_product); Website *gogWebsite; API *gogAPI; diff --git a/main.cpp b/main.cpp index ccef6d8..82c8c00 100644 --- a/main.cpp +++ b/main.cpp @@ -263,8 +263,8 @@ int main(int argc, char *argv[]) ; options_cli_experimental.add_options() - ("galaxy-install", bpo::value(&galaxy_product_id_install)->default_value(""), "Install game using product id [product_id/build_index]\nBuild index is used to select a build and defaults to 0 if not specified.\n\nExample: 12345/2 selects build 2 for product 12345") - ("galaxy-show-builds", bpo::value(&galaxy_product_id_show_builds)->default_value(""), "Show game builds using product id [product_id/build_index]\nBuild index is used to select a build and defaults to 0 if not specified.\n\nExample: 12345/2 selects build 2 for product 12345") + ("galaxy-install", bpo::value(&galaxy_product_id_install)->default_value(""), "Install game using product id [product_id/build_index] or gamename regex [gamename/build_id]\nBuild index is used to select a build and defaults to 0 if not specified.\n\nExample: 12345/2 selects build 2 for product 12345") + ("galaxy-show-builds", bpo::value(&galaxy_product_id_show_builds)->default_value(""), "Show game builds using product id [product_id/build_index] or gamename regex [gamename/build_id]\nBuild index is used to select a build and defaults to 0 if not specified.\n\nExample: 12345/2 selects build 2 for product 12345") ("galaxy-platform", bpo::value(&sGalaxyPlatform)->default_value("w"), galaxy_platform_text.c_str()) ("galaxy-language", bpo::value(&sGalaxyLanguage)->default_value("en"), galaxy_language_text.c_str()) ("galaxy-arch", bpo::value(&sGalaxyArch)->default_value("x64"), galaxy_arch_text.c_str()) diff --git a/src/downloader.cpp b/src/downloader.cpp index c20381e..ba4bc4b 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -3197,7 +3197,72 @@ void Downloader::saveGalaxyJSON() } } +bool Downloader::galaxySelectProductIdHelper(const std::string& product_id, std::string& selected_product) +{ + selected_product = product_id; + + // Check to see if product_id is id or gamename + boost::regex expression("^[0-9]+$"); + boost::match_results what; + if (!boost::regex_search(product_id, what, expression)) + { + Globals::globalConfig.sGameRegex = product_id; + this->getGameList(); + if (this->gameItems.empty()) + { + std::cerr << "Didn't match any products" << std::endl; + return false; + } + + if (this->gameItems.size() == 1) + { + selected_product = this->gameItems[0].id; + } + else + { + std::cout << "Select product:" << std::endl; + for (unsigned int i = 0; i < this->gameItems.size(); ++i) + std::cout << i << ": " << this->gameItems[i].name << std::endl; + + if (!isatty(STDIN_FILENO)) { + std::cerr << "Unable to read selection" << std::endl; + return false; + } + + int iSelect = -1; + int iSelectMax = this->gameItems.size(); + while (iSelect < 0 || iSelect >= iSelectMax) + { + std::cerr << "> "; + std::string selection; + + std::getline(std::cin, selection); + try + { + iSelect = std::stoi(selection); + } + catch(std::invalid_argument& e) + { + std::cerr << e.what() << std::endl; + } + } + selected_product = this->gameItems[iSelect].id; + } + } + return true; +} + void Downloader::galaxyInstallGame(const std::string& product_id, int build_index, const unsigned int& iGalaxyArch) +{ + std::string id; + if(this->galaxySelectProductIdHelper(product_id, id)) + { + if (!id.empty()) + this->galaxyInstallGameById(id, build_index, iGalaxyArch); + } +} + +void Downloader::galaxyInstallGameById(const std::string& product_id, int build_index, const unsigned int& iGalaxyArch) { if (build_index < 0) build_index = 0; @@ -3752,6 +3817,16 @@ void Downloader::processGalaxyDownloadQueue(const std::string& install_path, Con } void Downloader::galaxyShowBuilds(const std::string& product_id, int build_index) +{ + std::string id; + if(this->galaxySelectProductIdHelper(product_id, id)) + { + if (!id.empty()) + this->galaxyShowBuildsById(id, build_index); + } +} + +void Downloader::galaxyShowBuildsById(const std::string& product_id, int build_index) { std::string sPlatform; unsigned int iPlatform = Globals::globalConfig.dlConf.iGalaxyPlatform;