From 6b4010d9c53aa2e0a7b5e5ec7a376606b157bbba Mon Sep 17 00:00:00 2001 From: Sude Date: Tue, 19 Nov 2013 12:47:10 +0200 Subject: [PATCH] Changed all remaining "negative" variables to "positive" in config.h Code was becoming really confusing with all the double negatives in conditions, for example: if(!bNoInstallers) Added some comments to code --- include/config.h | 12 +++--- main.cpp | 24 ++++++++--- src/api.cpp | 21 +++++----- src/downloader.cpp | 101 +++++++++++++++++++++++++-------------------- 4 files changed, 91 insertions(+), 67 deletions(-) diff --git a/include/config.h b/include/config.h index 688884a..1841dad 100644 --- a/include/config.h +++ b/include/config.h @@ -16,18 +16,18 @@ class Config Config() {}; virtual ~Config() {}; bool bVerbose; - bool bNoRemoteXML; - bool bNoCover; + bool bRemoteXML; + bool bCover; bool bUpdateCheck; bool bDownload; bool bList; bool bListDetails; bool bLogin; bool bRepair; - bool bNoInstallers; - bool bNoExtras; - bool bNoPatches; - bool bNoLanguagePacks; + bool bInstallers; + bool bExtras; + bool bPatches; + bool bLanguagePacks; bool bUnicode; // use Unicode in console output bool bColor; // use colors bool bVerifyPeer; diff --git a/main.cpp b/main.cpp index 98526ac..e520134 100644 --- a/main.cpp +++ b/main.cpp @@ -79,6 +79,12 @@ int main(int argc, char *argv[]) bool bNoColor = false; bool bNoUnicode = false; bool bNoDuplicateHandler = false; + bool bNoCover = false; + bool bNoInstallers = false; + bool bNoExtras = false; + bool bNoPatches = false; + bool bNoLanguagePacks = false; + bool bNoRemoteXML = false; desc.add_options() ("help,h", "Print help message") ("login", bpo::value(&config.bLogin)->zero_tokens()->default_value(false), "Login") @@ -95,12 +101,12 @@ int main(int argc, char *argv[]) ("update-check", bpo::value(&config.bUpdateCheck)->zero_tokens()->default_value(false), "Check for update notifications") ("platform", bpo::value(&config.iInstallerType)->default_value(GlobalConstants::PLATFORM_WINDOWS), platform_text.c_str()) ("language", bpo::value(&config.iInstallerLanguage)->default_value(GlobalConstants::LANGUAGE_EN), language_text.c_str()) - ("no-installers", bpo::value(&config.bNoInstallers)->zero_tokens()->default_value(false), "Don't download/list/repair installers") - ("no-extras", bpo::value(&config.bNoExtras)->zero_tokens()->default_value(false), "Don't download/list/repair extras") - ("no-patches", bpo::value(&config.bNoPatches)->zero_tokens()->default_value(false), "Don't download/list/repair patches") - ("no-language-packs", bpo::value(&config.bNoLanguagePacks)->zero_tokens()->default_value(false), "Don't download/list/repair language packs") - ("no-cover", bpo::value(&config.bNoCover)->zero_tokens()->default_value(false), "Don't download cover images") - ("no-remote-xml", bpo::value(&config.bNoRemoteXML)->zero_tokens()->default_value(false), "Don't use remote XML for repair") + ("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") + ("no-language-packs", bpo::value(&bNoLanguagePacks)->zero_tokens()->default_value(false), "Don't download/list/repair language packs") + ("no-cover", bpo::value(&bNoCover)->zero_tokens()->default_value(false), "Don't download cover images") + ("no-remote-xml", bpo::value(&bNoRemoteXML)->zero_tokens()->default_value(false), "Don't use remote XML for repair") ("no-unicode", bpo::value(&bNoUnicode)->zero_tokens()->default_value(false), "Don't use Unicode in the progress bar") ("no-color", bpo::value(&bNoColor)->zero_tokens()->default_value(false), "Don't use coloring in the progress bar") ("no-duplicate-handling", bpo::value(&bNoDuplicateHandler)->zero_tokens()->default_value(false), "Don't use duplicate handler for installers\nDuplicate installers from different languages are handled separately") @@ -153,6 +159,12 @@ int main(int argc, char *argv[]) config.bColor = !bNoColor; config.bUnicode = !bNoUnicode; config.bDuplicateHandler = !bNoDuplicateHandler; + config.bCover = !bNoCover; + config.bInstallers = !bNoInstallers; + config.bExtras = !bNoExtras; + config.bPatches = !bNoPatches; + config.bLanguagePacks = !bNoLanguagePacks; + config.bRemoteXML = !bNoRemoteXML; } catch (std::exception& e) { diff --git a/src/api.cpp b/src/api.cpp index 8d8074c..7a09cc0 100644 --- a/src/api.cpp +++ b/src/api.cpp @@ -286,16 +286,17 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int game.icon = root["game"]["icon"].asString(); // Installer details + // Create a list of installers from JSON std::vector> installers; for (unsigned int i = 0; i < GlobalConstants::PLATFORMS.size(); ++i) - { + { // Check against the specified platforms if (type & GlobalConstants::PLATFORMS[i].platformId) { std::string installer = "installer_" + GlobalConstants::PLATFORMS[i].platformCode + "_"; for (unsigned int j = 0; j < GlobalConstants::LANGUAGES.size(); ++j) - { + { // Check against the specified languages if (lang & GlobalConstants::LANGUAGES[j].languageId) - { + { // Make sure that the installer exists in the JSON if (root["game"].isMember(installer+GlobalConstants::LANGUAGES[j].languageCode)) installers.push_back(std::make_pair(root["game"][installer+GlobalConstants::LANGUAGES[j].languageCode],GlobalConstants::LANGUAGES[j].languageId)); } @@ -319,7 +320,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int { if (game.installers[j].path == installer["link"].asString()) { - game.installers[j].language |= language; + game.installers[j].language |= language; // Add language code to installer bDuplicate = true; break; } @@ -359,7 +360,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int // Patch details for (unsigned int i = 0; i < GlobalConstants::LANGUAGES.size(); ++i) - { + { // Check against the specified languages if (lang & GlobalConstants::LANGUAGES[i].languageId) { // Try to find a patch @@ -372,7 +373,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int while (patch_number_file < maxTries) { std::string patchname = GlobalConstants::LANGUAGES[i].languageCode + std::to_string(patch_number) + "patch" + std::to_string(patch_number_file); - if (root["game"].isMember(patchname)) + if (root["game"].isMember(patchname)) // Check that patch node exists patchnames.push_back(patchname); patch_number_file++; } @@ -384,7 +385,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int for (unsigned int i = 0; i < patchnames.size(); ++i) { Json::Value patchnode = root["game"][patchnames[i]]; - if (patchnode.isArray()) + if (patchnode.isArray()) // Patch has multiple files { for ( unsigned int index = 0; index < patchnode.size(); ++index ) { @@ -401,7 +402,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int ); } } - else + else // Patch is a single file { game.patches.push_back( gameFile( false, /* patches don't have "updated" flag */ @@ -420,7 +421,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int // Language pack details for (unsigned int i = 0; i < GlobalConstants::LANGUAGES.size(); ++i) - { + { // Check against the specified languages if (lang & GlobalConstants::LANGUAGES[i].languageId) { // Try to find a language pack @@ -433,7 +434,7 @@ gameDetails API::getGameDetails(const std::string& game_name, const unsigned int while (lang_pack_number_file < maxTries) { std::string langpackname = GlobalConstants::LANGUAGES[i].languageCode + std::to_string(lang_pack_number) + "langpack" + std::to_string(lang_pack_number_file); - if (root["game"].isMember(langpackname)) + if (root["game"].isMember(langpackname)) // Check that language pack node exists langpacknames.push_back(langpackname); lang_pack_number_file++; } diff --git a/src/downloader.cpp b/src/downloader.cpp index 2b185d3..d92fe4e 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -47,6 +47,7 @@ int Downloader::init() { this->resume_position = 0; + // Initialize curl and set curl options curl_global_init(CURL_GLOBAL_ALL); curlhandle = curl_easy_init(); curl_easy_setopt(curlhandle, CURLOPT_FOLLOWLOCATION, 1); @@ -64,6 +65,7 @@ int Downloader::init() curl_easy_setopt(curlhandle, CURLOPT_PROGRESSFUNCTION, Downloader::progressCallback); curl_easy_setopt(curlhandle, CURLOPT_MAX_RECV_SPEED_LARGE, config.iDownloadRate); + // Create new API handle and set curl options for the API gogAPI = new API(config.sToken, config.sSecret); gogAPI->curlSetOpt(CURLOPT_VERBOSE, config.bVerbose); gogAPI->curlSetOpt(CURLOPT_SSL_VERIFYPEER, config.bVerifyPeer); @@ -71,19 +73,19 @@ int Downloader::init() progressbar = new ProgressBar(config.bUnicode, config.bColor); - bool bInitOK = gogAPI->init(); + bool bInitOK = gogAPI->init(); // Initialize the API if (config.bLogin || !bInitOK) return this->login(); - if (!config.bNoCover && config.bDownload && !config.bUpdateCheck) + if (config.bCover && config.bDownload && !config.bUpdateCheck) coverXML = this->getResponse("https://sites.google.com/site/gogdownloader/covers.xml"); if (config.bCheckOrphans) // Always check everything when checking for orphaned files { - config.bNoInstallers = false; - config.bNoExtras = false; - config.bNoPatches = false; - config.bNoLanguagePacks = false; + config.bInstallers = true; + config.bExtras = true; + config.bPatches = true; + config.bLanguagePacks = true; } if (!config.bUpdateCheck) // updateCheck() calls getGameList() if needed @@ -196,13 +198,13 @@ void Downloader::getGameList() gameNamesIds = this->getFreeGames(); } else - { + { // Filter the names std::vector> gameNamesIdsFiltered; boost::regex expression(config.sGameRegex); boost::match_results what; for (unsigned int i = 0; i < gameNamesIds.size(); ++i) { - if (boost::regex_search(gameNamesIds[i].first, what, expression)) + if (boost::regex_search(gameNamesIds[i].first, what, expression)) // Check if name matches the specified regex gameNamesIdsFiltered.push_back(gameNamesIds[i]); } gameNamesIds = gameNamesIdsFiltered; @@ -227,7 +229,7 @@ int Downloader::getGameDetails() game = gogAPI->getGameDetails(gameNamesIds[i].first, config.iInstallerType, config.iInstallerLanguage, config.bDuplicateHandler); if (!gogAPI->getError()) { - if (game.extras.empty() && !config.bNoExtras) + if (game.extras.empty() && config.bExtras) // Try to get extras from account page if API didn't return any extras { game.extras = this->getExtras(gameNamesIds[i].first, gameNamesIds[i].second); } @@ -271,7 +273,7 @@ void Downloader::listGames() << "title: " << games[i].title << std::endl << "icon: " << "http://static.gog.com" << games[i].icon << std::endl; // List installers - if (!config.bNoInstallers) + if (config.bInstallers) { std::cout << "installers: " << std::endl; for (unsigned int j = 0; j < games[i].installers.size(); ++j) @@ -279,7 +281,7 @@ void Downloader::listGames() if (!config.bUpdateCheck || games[i].installers[j].updated) // Always list updated files { std::string languages; - for (unsigned int k = 0; k < GlobalConstants::LANGUAGES.size(); k++) + for (unsigned int k = 0; k < GlobalConstants::LANGUAGES.size(); k++) // Check which languages the installer supports { if (games[i].installers[j].language & GlobalConstants::LANGUAGES[k].languageId) languages += (languages.empty() ? "" : ", ")+GlobalConstants::LANGUAGES[k].languageString; @@ -296,7 +298,7 @@ void Downloader::listGames() } } // List extras - if (!config.bNoExtras && !config.bUpdateCheck && !games[i].extras.empty()) + if (config.bExtras && !config.bUpdateCheck && !games[i].extras.empty()) { std::cout << "extras: " << std::endl; for (unsigned int j = 0; j < games[i].extras.size(); ++j) @@ -309,7 +311,7 @@ void Downloader::listGames() } } // List patches - if (!config.bNoPatches && !config.bUpdateCheck && !games[i].patches.empty()) + if (config.bPatches && !config.bUpdateCheck && !games[i].patches.empty()) { std::cout << "patches: " << std::endl; for (unsigned int j = 0; j < games[i].patches.size(); ++j) @@ -322,7 +324,7 @@ void Downloader::listGames() } } // List language packs - if (!config.bNoLanguagePacks && !config.bUpdateCheck && !games[i].languagepacks.empty()) + if (config.bLanguagePacks && !config.bUpdateCheck && !games[i].languagepacks.empty()) { std::cout << "language packs: " << std::endl; for (unsigned int j = 0; j < games[i].languagepacks.size(); ++j) @@ -337,7 +339,7 @@ void Downloader::listGames() } } else - { + { // List game names for (unsigned int i = 0; i < gameNamesIds.size(); ++i) std::cout << gameNamesIds[i].first << std::endl; } @@ -349,7 +351,7 @@ void Downloader::repair() for (unsigned int i = 0; i < games.size(); ++i) { // Installers (use remote or local file) - if (!config.bNoInstallers) + if (config.bInstallers) { for (unsigned int j = 0; j < games[i].installers.size(); ++j) { @@ -357,7 +359,7 @@ void Downloader::repair() // Get XML data std::string XML = ""; - if (!config.bNoRemoteXML) + if (config.bRemoteXML) { XML = gogAPI->getXML(games[i].gamename, games[i].installers[j].id); if (gogAPI->getError()) @@ -369,7 +371,8 @@ void Downloader::repair() } // Repair - if (!XML.empty() || config.bNoRemoteXML) + bool bUseLocalXML = !config.bRemoteXML; + if (!XML.empty() || bUseLocalXML) { std::string url = gogAPI->getInstallerLink(games[i].gamename, games[i].installers[j].id); if (gogAPI->getError()) @@ -386,7 +389,7 @@ void Downloader::repair() } // Extras (GOG doesn't provide XML data for extras, use local file) - if (!config.bNoExtras) + if (config.bExtras) { for (unsigned int j = 0; j < games[i].extras.size(); ++j) { @@ -406,7 +409,7 @@ void Downloader::repair() } // Patches (use remote or local file) - if (!config.bNoPatches) + if (config.bPatches) { for (unsigned int j = 0; j < games[i].patches.size(); ++j) { @@ -426,7 +429,7 @@ void Downloader::repair() } // Language packs (GOG doesn't provide XML data for language packs, use local file) - if (!config.bNoLanguagePacks) + if (config.bLanguagePacks) { for (unsigned int j = 0; j < games[i].languagepacks.size(); ++j) { @@ -453,7 +456,7 @@ void Downloader::download() for (unsigned int i = 0; i < games.size(); ++i) { // Download covers - if (!config.bNoCover && !config.bUpdateCheck) + if (config.bCover && !config.bUpdateCheck) { // Take path from installer path because for some games the base directory for installer/extra path is not "gamename" std::string filepath = Util::makeFilepath(config.sDirectory, games[i].installers[0].path, games[i].gamename); @@ -467,7 +470,7 @@ void Downloader::download() this->downloadCovers(games[i].gamename, directory, coverXML); } // Download installers - if (!config.bNoInstallers) + if (config.bInstallers) { for (unsigned int j = 0; j < games[i].installers.size(); ++j) { @@ -490,7 +493,7 @@ void Downloader::download() if (!url.empty()) { std::string XML; - if (!config.bNoRemoteXML) + if (config.bRemoteXML) XML = gogAPI->getXML(games[i].gamename, games[i].installers[j].id); if (!games[i].installers[j].name.empty()) std::cout << "Dowloading: " << games[i].installers[j].name << std::endl; @@ -501,7 +504,7 @@ void Downloader::download() } } // Download extras - if (!config.bNoExtras && !config.bUpdateCheck) + if (config.bExtras && !config.bUpdateCheck) { // Save some time and don't process extras when running update check. Extras don't have updated flag, all of them would be skipped anyway. for (unsigned int j = 0; j < games[i].extras.size(); ++j) { @@ -534,7 +537,7 @@ void Downloader::download() } } // Download patches - if (!config.bNoPatches) + if (config.bPatches) { for (unsigned int j = 0; j < games[i].patches.size(); ++j) { @@ -567,7 +570,7 @@ void Downloader::download() } } // Download language packs - if (!config.bNoLanguagePacks) + if (config.bLanguagePacks) { for (unsigned int j = 0; j < games[i].languagepacks.size(); ++j) { @@ -681,12 +684,12 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil } } else - { + { // File exists but is not the same version fclose(outfile); std::cout << "Remote file is different, renaming local file" << std::endl; - boost::filesystem::path new_name = filepath + ".old"; + boost::filesystem::path new_name = filepath + ".old"; // Rename old file by appending ".old" to filename if (boost::filesystem::exists(new_name)) - { + { // One even older file exists, delete the older file before renaming std::cout << "Old renamed file found, deleting old file" << std::endl; if (!boost::filesystem::remove(new_name)) { @@ -696,7 +699,7 @@ CURLcode Downloader::downloadFile(const std::string& url, const std::string& fil } } boost::system::error_code ec; - boost::filesystem::rename(pathname, new_name, ec); + boost::filesystem::rename(pathname, new_name, ec); // Rename the file if (ec) { std::cout << "Failed to rename " << filepath << " to " << new_name.string() << std::endl; @@ -787,20 +790,22 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath, std::string filename = pathname.filename().string(); TiXmlDocument xml; - if (!xml_data.empty()) { + if (!xml_data.empty()) // Parse remote XML data + { std::cout << "XML: Using remote file" << std::endl; xml.Parse(xml_data.c_str()); } else - { + { // Parse local XML data std::string xml_file = config.sXMLDirectory + "/" + filename + ".xml"; std::cout << "XML: Using local file" << std::endl; xml.LoadFile(xml_file); } + // Check if file node exists in XML data TiXmlNode *fileNode = xml.FirstChild("file"); if (!fileNode) - { + { // File node doesn't exist std::cout << "XML: Parsing failed / not valid XML" << std::endl; if (config.bDownload) bParsingFailed = true; @@ -808,7 +813,7 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath, return res; } else - { + { // File node exists --> valid XML std::cout << "XML: Valid XML" << std::endl; TiXmlElement *fileElem = fileNode->ToElement(); filename = fileElem->Attribute("name"); @@ -898,12 +903,13 @@ int Downloader::repairFile(const std::string& url, const std::string& filepath, return res; } + // Check all chunks for (int i=0; iprogressbar->draw(bar_length, fraction); + + // Download rate unit conversion std::string rate_unit; if (rate > 1048576) // 1 MB { @@ -1432,6 +1440,7 @@ void Downloader::checkOrphans() { if (boost::filesystem::is_directory(path)) { + // Recursively iterate over files in directory boost::filesystem::recursive_directory_iterator end_iter; boost::filesystem::recursive_directory_iterator dir_iter(path); while (dir_iter != end_iter) @@ -1439,7 +1448,7 @@ void Downloader::checkOrphans() if (boost::filesystem::is_regular_file(dir_iter->status())) { std::string filename = dir_iter->path().filename().string(); - boost::regex expression(".*\\.(zip|exe|bin|dmg|old)$"); + boost::regex expression(".*\\.(zip|exe|bin|dmg|old)$"); // Limit to files with these extensions (".old" is for renamed older version files) boost::match_results what; if (boost::regex_search(filename, what, expression)) filepath_vector.push_back(dir_iter->path()); @@ -1460,7 +1469,9 @@ void Downloader::checkOrphans() { for (unsigned int j = 0; j < filepath_vector.size(); ++j) { - bool bFoundFile = false; + bool bFoundFile = false; // Assume that the file is orphaned + + // Check installers for (unsigned int k = 0; k < games[i].installers.size(); ++k) { if (games[i].installers[k].path.find(filepath_vector[j].filename().string()) != std::string::npos) @@ -1470,7 +1481,7 @@ void Downloader::checkOrphans() } } if (!bFoundFile) - { + { // Check extras for (unsigned int k = 0; k < games[i].extras.size(); ++k) { if (games[i].extras[k].path.find(filepath_vector[j].filename().string()) != std::string::npos) @@ -1481,7 +1492,7 @@ void Downloader::checkOrphans() } } if (!bFoundFile) - { + { // Check patches for (unsigned int k = 0; k < games[i].patches.size(); ++k) { if (games[i].patches[k].path.find(filepath_vector[j].filename().string()) != std::string::npos) @@ -1492,7 +1503,7 @@ void Downloader::checkOrphans() } } if (!bFoundFile) - { + { // Check language packs for (unsigned int k = 0; k < games[i].languagepacks.size(); ++k) { if (games[i].languagepacks[k].path.find(filepath_vector[j].filename().string()) != std::string::npos) @@ -1529,7 +1540,7 @@ void Downloader::checkStatus() { for (unsigned int i = 0; i < games.size(); ++i) { - if (!config.bNoInstallers) + if (config.bInstallers) { for (unsigned int j = 0; j < games[i].installers.size(); ++j) { @@ -1559,7 +1570,7 @@ void Downloader::checkStatus() } } - if (!config.bNoExtras) + if (config.bExtras) { for (unsigned int j = 0; j < games[i].extras.size(); ++j) { @@ -1580,7 +1591,7 @@ void Downloader::checkStatus() } } - if (!config.bNoPatches) + if (config.bPatches) { for (unsigned int j = 0; j < games[i].patches.size(); ++j) { @@ -1601,7 +1612,7 @@ void Downloader::checkStatus() } } - if (!config.bNoLanguagePacks) + if (config.bLanguagePacks) { for (unsigned int j = 0; j < games[i].languagepacks.size(); ++j) {