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.
This commit is contained in:
Sude 2019-04-22 18:14:53 +03:00
parent 8540955af8
commit 86bb528c39
3 changed files with 80 additions and 2 deletions

View File

@ -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<zipFileEntry>& 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;

View File

@ -263,8 +263,8 @@ int main(int argc, char *argv[])
;
options_cli_experimental.add_options()
("galaxy-install", bpo::value<std::string>(&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<std::string>(&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<std::string>(&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<std::string>(&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<std::string>(&sGalaxyPlatform)->default_value("w"), galaxy_platform_text.c_str())
("galaxy-language", bpo::value<std::string>(&sGalaxyLanguage)->default_value("en"), galaxy_language_text.c_str())
("galaxy-arch", bpo::value<std::string>(&sGalaxyArch)->default_value("x64"), galaxy_arch_text.c_str())

View File

@ -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<std::string::const_iterator> 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;