2018-02-28 18:42:30 +00:00
|
|
|
#include "FtbPackFetchTask.h"
|
|
|
|
#include <QDomDocument>
|
2018-08-01 23:52:31 +01:00
|
|
|
#include "FtbPrivatePackManager.h"
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2019-01-04 00:48:36 +00:00
|
|
|
#include "net/URLConstants.h"
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
void FtbPackFetchTask::fetch()
|
|
|
|
{
|
2018-08-01 23:52:31 +01:00
|
|
|
publicPacks.clear();
|
|
|
|
thirdPartyPacks.clear();
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
NetJob *netJob = new NetJob("FtbModpackFetch");
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2019-01-04 00:48:36 +00:00
|
|
|
QUrl publicPacksUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/modpacks.xml");
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Downloading public version info from" << publicPacksUrl.toString();
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2019-01-04 00:48:36 +00:00
|
|
|
QUrl thirdPartyUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/thirdparty.xml");
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString();
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &FtbPackFetchTask::fileDownloadFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &FtbPackFetchTask::fileDownloadFailed);
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
jobPtr.reset(netJob);
|
|
|
|
netJob->start();
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
void FtbPackFetchTask::fetchPrivate(const QStringList & toFetch)
|
|
|
|
{
|
2019-01-04 00:48:36 +00:00
|
|
|
QString privatePackBaseUrl = URLConstants::FTB_CDN_BASE_URL + "static/%1.xml";
|
2018-08-01 23:52:31 +01:00
|
|
|
|
|
|
|
for (auto &packCode: toFetch)
|
|
|
|
{
|
|
|
|
QByteArray *data = new QByteArray();
|
|
|
|
NetJob *job = new NetJob("Fetching private pack");
|
|
|
|
job->addNetAction(Net::Download::makeByteArray(privatePackBaseUrl.arg(packCode), data));
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, job, data, packCode]
|
|
|
|
{
|
|
|
|
FtbModpackList packs;
|
|
|
|
parseAndAddPacks(*data, FtbPackType::Private, packs);
|
|
|
|
foreach(FtbModpack currentPack, packs)
|
|
|
|
{
|
|
|
|
currentPack.packCode = packCode;
|
|
|
|
emit privateFileDownloadFinished(currentPack);
|
|
|
|
}
|
|
|
|
|
|
|
|
job->deleteLater();
|
|
|
|
|
|
|
|
data->clear();
|
|
|
|
delete data;
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, job, packCode, data](QString reason)
|
|
|
|
{
|
|
|
|
emit privateFileDownloadFailed(reason, packCode);
|
|
|
|
job->deleteLater();
|
|
|
|
|
|
|
|
data->clear();
|
|
|
|
delete data;
|
|
|
|
});
|
|
|
|
|
|
|
|
job->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
void FtbPackFetchTask::fileDownloadFinished()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
jobPtr.reset();
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
QStringList failedLists;
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks))
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
failedLists.append(tr("Public Packs"));
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks))
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
failedLists.append(tr("Third Party Packs"));
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
if(failedLists.size() > 0)
|
|
|
|
{
|
|
|
|
emit failed(tr("Failed to download some pack lists: %1").arg(failedLists.join("\n- ")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
emit finished(publicPacks, thirdPartyPacks);
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType, FtbModpackList &list)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
QDomDocument doc;
|
|
|
|
|
|
|
|
QString errorMsg = "Unknown error.";
|
|
|
|
int errorLine = -1;
|
|
|
|
int errorCol = -1;
|
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol))
|
|
|
|
{
|
|
|
|
auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:3d!").arg(errorMsg, errorLine, errorCol);
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << fullErrMsg;
|
|
|
|
data.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDomNodeList nodes = doc.elementsByTagName("modpack");
|
2018-08-01 23:52:31 +01:00
|
|
|
for(int i = 0; i < nodes.length(); i++)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
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");
|
|
|
|
modpack.logo = element.attribute("logo");
|
|
|
|
modpack.oldVersions = element.attribute("oldVersions").split(";");
|
|
|
|
modpack.broken = false;
|
|
|
|
modpack.bugged = false;
|
|
|
|
|
|
|
|
//remove empty if the xml is bugged
|
2018-08-01 23:52:31 +01:00
|
|
|
for(QString curr : modpack.oldVersions)
|
|
|
|
{
|
|
|
|
if(curr.isNull() || curr.isEmpty())
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
modpack.oldVersions.removeAll(curr);
|
|
|
|
modpack.bugged = true;
|
|
|
|
qWarning() << "Removed some empty versions from" << modpack.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
if(modpack.oldVersions.size() < 1)
|
|
|
|
{
|
|
|
|
if(!modpack.currentVersion.isNull() && !modpack.currentVersion.isEmpty())
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
modpack.oldVersions.append(modpack.currentVersion);
|
|
|
|
qWarning() << "Added current version to oldVersions because oldVersions was empty! (" + modpack.name + ")";
|
2018-08-01 23:52:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
modpack.broken = true;
|
|
|
|
qWarning() << "Broken pack:" << modpack.name << " => No valid version!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modpack.author = element.attribute("author");
|
|
|
|
|
|
|
|
modpack.dir = element.attribute("dir");
|
|
|
|
modpack.file = element.attribute("url");
|
|
|
|
|
|
|
|
modpack.type = packType;
|
|
|
|
|
|
|
|
list.append(modpack);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 23:52:31 +01:00
|
|
|
void FtbPackFetchTask::fileDownloadFailed(QString reason)
|
|
|
|
{
|
|
|
|
qWarning() << "Fetching FtbPacks failed:" << reason;
|
2018-07-15 13:51:05 +01:00
|
|
|
emit failed(reason);
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|