2018-02-28 18:42:30 +00:00
|
|
|
#include "FtbPackFetchTask.h"
|
|
|
|
#include <QDomDocument>
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
FtbPackFetchTask::FtbPackFetchTask()
|
|
|
|
{
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
FtbPackFetchTask::~FtbPackFetchTask()
|
|
|
|
{
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
void FtbPackFetchTask::fetch()
|
|
|
|
{
|
2018-02-28 18:42:30 +00:00
|
|
|
NetJob *netJob = new NetJob("FtbModpackFetch");
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
QUrl publicPacksUrl = QUrl("https://ftb.cursecdn.com/FTB2/static/modpacks.xml");
|
|
|
|
qDebug() << "Downloading public version info from" << publicPacksUrl.toString();
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
QUrl thirdPartyUrl = QUrl("https://ftb.cursecdn.com/FTB2/static/thirdparty.xml");
|
|
|
|
qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString();
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &FtbPackFetchTask::fileDownloadFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &FtbPackFetchTask::fileDownloadFailed);
|
|
|
|
|
|
|
|
jobPtr.reset(netJob);
|
|
|
|
netJob->start();
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
void FtbPackFetchTask::fileDownloadFinished()
|
|
|
|
{
|
2018-02-28 18:42:30 +00:00
|
|
|
jobPtr.reset();
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
QStringList failedLists;
|
|
|
|
|
|
|
|
if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks)) {
|
|
|
|
failedLists.append(tr("Public Packs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks)) {
|
|
|
|
failedLists.append(tr("Third Party Packs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(failedLists.size() > 0) {
|
|
|
|
emit failed(QString("Failed to download some pack lists:%1").arg(failedLists.join("\n- ")));
|
|
|
|
} else {
|
|
|
|
emit finished(publicPacks, thirdPartyPacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType, FtbModpackList &list)
|
|
|
|
{
|
2018-02-28 18:42:30 +00:00
|
|
|
QDomDocument doc;
|
|
|
|
|
|
|
|
QString errorMsg = "Unknown error.";
|
|
|
|
int errorLine = -1;
|
|
|
|
int errorCol = -1;
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)){
|
2018-02-28 18:42:30 +00:00
|
|
|
auto fullErrMsg = QString("Failed to fetch modpack data: %s %d:%d!").arg(errorMsg, errorLine, errorCol);
|
|
|
|
qWarning() << fullErrMsg;
|
2018-04-01 19:24:28 +01:00
|
|
|
data.clear();
|
|
|
|
return false;
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDomNodeList nodes = doc.elementsByTagName("modpack");
|
|
|
|
for(int i = 0; i < nodes.length(); i++) {
|
|
|
|
QDomElement element = nodes.at(i).toElement();
|
|
|
|
|
|
|
|
FtbModpack modpack;
|
|
|
|
modpack.name = element.attribute("name");
|
|
|
|
modpack.currentVersion = element.attribute("version");
|
|
|
|
modpack.mcVersion = element.attribute("mcVersion");
|
|
|
|
modpack.description = element.attribute("description");
|
|
|
|
modpack.mods = element.attribute("mods");
|
2018-04-01 19:24:28 +01:00
|
|
|
modpack.logo = element.attribute("logo");
|
2018-02-28 18:42:30 +00:00
|
|
|
modpack.oldVersions = element.attribute("oldVersions").split(";");
|
2018-03-19 01:36:12 +00:00
|
|
|
modpack.broken = false;
|
|
|
|
modpack.bugged = false;
|
2018-03-03 20:22:00 +00:00
|
|
|
|
|
|
|
//remove empty if the xml is bugged
|
|
|
|
for(QString curr : modpack.oldVersions) {
|
|
|
|
if(curr.isNull() || curr.isEmpty()) {
|
|
|
|
modpack.oldVersions.removeAll(curr);
|
|
|
|
modpack.bugged = true;
|
|
|
|
qWarning() << "Removed some empty versions from" << modpack.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(modpack.oldVersions.size() < 1) {
|
|
|
|
if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty()) {
|
|
|
|
modpack.oldVersions.append(modpack.currentVersion);
|
|
|
|
qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")";
|
|
|
|
} else {
|
|
|
|
modpack.broken = true;
|
|
|
|
qWarning() << "Broken pack:" << modpack.name << " => No valid version!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:42:30 +00:00
|
|
|
modpack.author = element.attribute("author");
|
|
|
|
|
|
|
|
modpack.dir = element.attribute("dir");
|
|
|
|
modpack.file = element.attribute("url");
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
modpack.type = packType;
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
list.append(modpack);
|
|
|
|
}
|
2018-03-03 20:22:00 +00:00
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
return true;
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FtbPackFetchTask::fileDownloadFailed(QString reason){
|
|
|
|
qWarning() << "Fetching FtbPacks failed: " << reason;
|
|
|
|
emit failed(reason);
|
|
|
|
}
|