Add option to save changelogs

This commit is contained in:
Sude 2016-03-06 15:45:22 +02:00
parent 852cb563ba
commit 987e053ad4
6 changed files with 120 additions and 0 deletions

View File

@ -47,6 +47,7 @@ class Config
bool bPlatformDetection; bool bPlatformDetection;
bool bShowWishlist; bool bShowWishlist;
bool bAutomaticXMLCreation; bool bAutomaticXMLCreation;
bool bSaveChangelogs;
std::string sGameRegex; std::string sGameRegex;
std::string sDirectory; std::string sDirectory;
std::string sCacheDirectory; std::string sCacheDirectory;

View File

@ -98,6 +98,8 @@ class Downloader
Json::Value getGameDetailsJSON(const std::string& gameid); Json::Value getGameDetailsJSON(const std::string& gameid);
std::string getSerialsFromJSON(const Json::Value& json); std::string getSerialsFromJSON(const Json::Value& json);
void saveSerials(const std::string& serials, const std::string& filepath); void saveSerials(const std::string& serials, const std::string& filepath);
std::string getChangelogFromJSON(const Json::Value& json);
void saveChangelog(const std::string& changelog, const std::string& filepath);
static int progressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow); static int progressCallback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
static size_t writeMemoryCallback(char *ptr, size_t size, size_t nmemb, void *userp); static size_t writeMemoryCallback(char *ptr, size_t size, size_t nmemb, void *userp);

View File

@ -23,15 +23,18 @@ class gameDetails
std::string title; std::string title;
std::string icon; std::string icon;
std::string serials; std::string serials;
std::string changelog;
void filterWithPriorities(const gameSpecificConfig& config); void filterWithPriorities(const gameSpecificConfig& config);
void makeFilepaths(const gameSpecificDirectoryConfig& config); void makeFilepaths(const gameSpecificDirectoryConfig& config);
std::string getSerialsFilepath(); std::string getSerialsFilepath();
std::string getChangelogFilepath();
Json::Value getDetailsAsJson(); Json::Value getDetailsAsJson();
virtual ~gameDetails(); virtual ~gameDetails();
protected: protected:
void filterListWithPriorities(std::vector<gameFile>& list, const gameSpecificConfig& config); void filterListWithPriorities(std::vector<gameFile>& list, const gameSpecificConfig& config);
private: private:
std::string serialsFilepath; std::string serialsFilepath;
std::string changelogFilepath;
}; };
#endif // GAMEDETAILS_H #endif // GAMEDETAILS_H

View File

@ -176,6 +176,7 @@ int main(int argc, char *argv[])
("include", bpo::value<std::string>(&sIncludeOptions)->default_value("all"), ("Select what to download/list/repair\n" + include_options_text).c_str()) ("include", bpo::value<std::string>(&sIncludeOptions)->default_value("all"), ("Select what to download/list/repair\n" + include_options_text).c_str())
("exclude", bpo::value<std::string>(&sExcludeOptions)->default_value("covers"), ("Select what not to download/list/repair\n" + include_options_text).c_str()) ("exclude", bpo::value<std::string>(&sExcludeOptions)->default_value("covers"), ("Select what not to download/list/repair\n" + include_options_text).c_str())
("automatic-xml-creation", bpo::value<bool>(&config.bAutomaticXMLCreation)->zero_tokens()->default_value(false), "Automatically create XML data after download has completed") ("automatic-xml-creation", bpo::value<bool>(&config.bAutomaticXMLCreation)->zero_tokens()->default_value(false), "Automatically create XML data after download has completed")
("save-changelogs", bpo::value<bool>(&config.bSaveChangelogs)->zero_tokens()->default_value(false), "Save changelogs when downloading")
; ;
// Options read from config file // Options read from config file
options_cfg_only.add_options() options_cfg_only.add_options()

View File

@ -330,6 +330,12 @@ int Downloader::getGameDetails()
gameDetailsJSON = this->getGameDetailsJSON(gameItems[i].id); gameDetailsJSON = this->getGameDetailsJSON(gameItems[i].id);
game.serials = this->getSerialsFromJSON(gameDetailsJSON); game.serials = this->getSerialsFromJSON(gameDetailsJSON);
} }
if (config.bSaveChangelogs)
{
if (gameDetailsJSON.empty())
gameDetailsJSON = this->getGameDetailsJSON(gameItems[i].id);
game.changelog = this->getChangelogFromJSON(gameDetailsJSON);
}
// Ignore DLC count and try to get DLCs from JSON // Ignore DLC count and try to get DLCs from JSON
if (game.dlcs.empty() && !bHasDLC && conf.bDLC && conf.bIgnoreDLCCount) if (game.dlcs.empty() && !bHasDLC && conf.bDLC && conf.bIgnoreDLCCount)
@ -395,6 +401,31 @@ int Downloader::getGameDetails()
} }
} }
if (config.bSaveChangelogs)
{
if (gameDetailsJSON.empty())
gameDetailsJSON = this->getGameDetailsJSON(gameItems[i].id);
// Make sure we save changelog for the right DLC
for (unsigned int k = 0; k < gameDetailsJSON["dlcs"].size(); ++k)
{
std::vector<std::string> urls;
if (gameDetailsJSON["dlcs"][k].isMember("changelog") && gameDetailsJSON["dlcs"][k].isMember("downloads"))
{
// Assuming that only DLC with installers can have changelog
Util::getDownloaderUrlsFromJSON(gameDetailsJSON["dlcs"][k]["downloads"], urls);
}
if (!urls.empty())
{
if (urls[0].find("/" + gameItems[i].dlcnames[j] + "/") != std::string::npos)
{
dlc.changelog = this->getChangelogFromJSON(gameDetailsJSON["dlcs"][k]);
}
}
}
}
game.dlcs.push_back(dlc); game.dlcs.push_back(dlc);
} }
} }
@ -881,6 +912,12 @@ void Downloader::download()
this->saveSerials(games[i].serials, filepath); this->saveSerials(games[i].serials, filepath);
} }
if (config.bSaveChangelogs && !games[i].changelog.empty())
{
std::string filepath = games[i].getChangelogFilepath();
this->saveChangelog(games[i].changelog, filepath);
}
// Download covers // Download covers
if (config.bCover && !config.bUpdateCheck) if (config.bCover && !config.bUpdateCheck)
{ {
@ -1084,6 +1121,11 @@ void Downloader::download()
std::string filepath = games[i].dlcs[j].getSerialsFilepath(); std::string filepath = games[i].dlcs[j].getSerialsFilepath();
this->saveSerials(games[i].dlcs[j].serials, filepath); this->saveSerials(games[i].dlcs[j].serials, filepath);
} }
if (config.bSaveChangelogs && !games[i].dlcs[j].changelog.empty())
{
std::string filepath = games[i].dlcs[j].getChangelogFilepath();
this->saveChangelog(games[i].dlcs[j].changelog, filepath);
}
if (config.bInstallers) if (config.bInstallers)
{ {
@ -2457,6 +2499,27 @@ std::string Downloader::getSerialsFromJSON(const Json::Value& json)
return serials.str(); return serials.str();
} }
std::string Downloader::getChangelogFromJSON(const Json::Value& json)
{
std::string changelog;
std::string title = "Changelog";
if (!json.isMember("changelog"))
return std::string();
changelog = json["changelog"].asString();
if (changelog.empty())
return std::string();
if (json.isMember("title"))
title = title + ": " + json["title"].asString();
changelog = "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n<title>" + title + "</title>\n</head>\n<body>" + changelog + "</body>\n</html>";
return changelog;
}
// Linear search. Good thing computers are fast and lists are small. // Linear search. Good thing computers are fast and lists are small.
static int isPresent(std::vector<gameFile>& list, const boost::filesystem::path& path, Blacklist& blacklist) static int isPresent(std::vector<gameFile>& list, const boost::filesystem::path& path, Blacklist& blacklist)
{ {
@ -3043,6 +3106,7 @@ std::vector<gameDetails> Downloader::getGameDetailsFromJsonNode(Json::Value root
game.title = gameDetailsNode["title"].asString(); game.title = gameDetailsNode["title"].asString();
game.icon = gameDetailsNode["icon"].asString(); game.icon = gameDetailsNode["icon"].asString();
game.serials = gameDetailsNode["serials"].asString(); game.serials = gameDetailsNode["serials"].asString();
game.changelog = gameDetailsNode["changelog"].asString();
// Make a vector of valid node names to make things easier // Make a vector of valid node names to make things easier
std::vector<std::string> nodes; std::vector<std::string> nodes;
@ -3183,6 +3247,47 @@ void Downloader::saveSerials(const std::string& serials, const std::string& file
return; return;
} }
// Save changelog to file
void Downloader::saveChangelog(const std::string& changelog, const std::string& filepath)
{
// Get directory from filepath
boost::filesystem::path pathname = filepath;
std::string directory = pathname.parent_path().string();
// Check that directory exists and create subdirectories
boost::filesystem::path path = directory;
if (boost::filesystem::exists(path))
{
if (!boost::filesystem::is_directory(path))
{
std::cout << path << " is not directory" << std::endl;
return;
}
}
else
{
if (!boost::filesystem::create_directories(path))
{
std::cout << "Failed to create directory: " << path << std::endl;
return;
}
}
std::ofstream ofs(filepath);
if (ofs)
{
std::cout << "Saving changelog: " << filepath << std::endl;
ofs << changelog;
ofs.close();
}
else
{
std::cout << "Failed to create file: " << filepath << std::endl;
}
return;
}
void Downloader::downloadFileWithId(const std::string& fileid_string, const std::string& output_filepath) void Downloader::downloadFileWithId(const std::string& fileid_string, const std::string& output_filepath)
{ {
size_t pos = fileid_string.find("/"); size_t pos = fileid_string.find("/");

View File

@ -71,6 +71,7 @@ void gameDetails::makeFilepaths(const gameSpecificDirectoryConfig& config)
std::string directory = config.sDirectory + "/" + config.sGameSubdir + "/"; std::string directory = config.sDirectory + "/" + config.sGameSubdir + "/";
std::string subdir; std::string subdir;
this->serialsFilepath = Util::makeFilepath(directory, "serials.txt", this->gamename, subdir, 0); this->serialsFilepath = Util::makeFilepath(directory, "serials.txt", this->gamename, subdir, 0);
this->changelogFilepath = Util::makeFilepath(directory, "changelog_" + gamename + ".html", this->gamename, subdir, 0);
for (unsigned int i = 0; i < this->installers.size(); ++i) for (unsigned int i = 0; i < this->installers.size(); ++i)
{ {
@ -104,6 +105,7 @@ void gameDetails::makeFilepaths(const gameSpecificDirectoryConfig& config)
{ {
subdir = config.bSubDirectories ? config.sDLCSubdir + "/" + config.sInstallersSubdir : ""; subdir = config.bSubDirectories ? config.sDLCSubdir + "/" + config.sInstallersSubdir : "";
this->dlcs[i].serialsFilepath = Util::makeFilepath(directory, "serials.txt", this->gamename, subdir, 0); this->dlcs[i].serialsFilepath = Util::makeFilepath(directory, "serials.txt", this->gamename, subdir, 0);
this->dlcs[i].changelogFilepath = Util::makeFilepath(directory, "changelog_" + this->dlcs[i].gamename + ".html", this->gamename, subdir, 0);
for (unsigned int j = 0; j < this->dlcs[i].installers.size(); ++j) for (unsigned int j = 0; j < this->dlcs[i].installers.size(); ++j)
{ {
subdir = config.bSubDirectories ? config.sDLCSubdir + "/" + config.sInstallersSubdir : ""; subdir = config.bSubDirectories ? config.sDLCSubdir + "/" + config.sInstallersSubdir : "";
@ -135,6 +137,7 @@ Json::Value gameDetails::getDetailsAsJson()
json["title"] = this->title; json["title"] = this->title;
json["icon"] = this->icon; json["icon"] = this->icon;
json["serials"] = this->serials; json["serials"] = this->serials;
json["changelog"] = this->changelog;
for (unsigned int i = 0; i < this->extras.size(); ++i) for (unsigned int i = 0; i < this->extras.size(); ++i)
json["extras"].append(this->extras[i].getAsJson()); json["extras"].append(this->extras[i].getAsJson());
@ -160,3 +163,8 @@ std::string gameDetails::getSerialsFilepath()
{ {
return this->serialsFilepath; return this->serialsFilepath;
} }
std::string gameDetails::getChangelogFilepath()
{
return this->changelogFilepath;
}