NOISSUE FTB pack code implementation, cleaned up
This commit is contained in:

committed by
Petr Mrázek

parent
6cee50eac6
commit
6aada8adf7
@ -425,6 +425,9 @@ set(FTB_SOURCES
|
||||
modplatform/ftb/FtbPackInstallTask.h
|
||||
modplatform/ftb/FtbPackInstallTask.cpp
|
||||
|
||||
modplatform/ftb/FtbPrivatePackManager.h
|
||||
modplatform/ftb/FtbPrivatePackManager.cpp
|
||||
|
||||
modplatform/ftb/PackHelpers.h
|
||||
)
|
||||
|
||||
|
@ -1,16 +1,12 @@
|
||||
#include "FtbPackFetchTask.h"
|
||||
#include <QDomDocument>
|
||||
|
||||
FtbPackFetchTask::FtbPackFetchTask()
|
||||
{
|
||||
}
|
||||
|
||||
FtbPackFetchTask::~FtbPackFetchTask()
|
||||
{
|
||||
}
|
||||
#include "FtbPrivatePackManager.h"
|
||||
|
||||
void FtbPackFetchTask::fetch()
|
||||
{
|
||||
publicPacks.clear();
|
||||
thirdPartyPacks.clear();
|
||||
|
||||
NetJob *netJob = new NetJob("FtbModpackFetch");
|
||||
|
||||
QUrl publicPacksUrl = QUrl("https://ftb.cursecdn.com/FTB2/static/modpacks.xml");
|
||||
@ -28,23 +24,67 @@ void FtbPackFetchTask::fetch()
|
||||
netJob->start();
|
||||
}
|
||||
|
||||
void FtbPackFetchTask::fetchPrivate(const QStringList & toFetch)
|
||||
{
|
||||
QString privatePackBaseUrl = QString("https://ftb.cursecdn.com/FTB2/static/%1.xml");
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
void FtbPackFetchTask::fileDownloadFinished()
|
||||
{
|
||||
jobPtr.reset();
|
||||
|
||||
QStringList failedLists;
|
||||
|
||||
if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks)) {
|
||||
if(!parseAndAddPacks(publicModpacksXmlFileData, FtbPackType::Public, publicPacks))
|
||||
{
|
||||
failedLists.append(tr("Public Packs"));
|
||||
}
|
||||
|
||||
if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks)) {
|
||||
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 {
|
||||
if(failedLists.size() > 0)
|
||||
{
|
||||
emit failed(tr("Failed to download some pack lists: %1").arg(failedLists.join("\n- ")));
|
||||
}
|
||||
else
|
||||
{
|
||||
emit finished(publicPacks, thirdPartyPacks);
|
||||
}
|
||||
}
|
||||
@ -57,15 +97,17 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
|
||||
int errorLine = -1;
|
||||
int errorCol = -1;
|
||||
|
||||
if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)){
|
||||
auto fullErrMsg = QString("Failed to fetch modpack data: %s %d:%d!").arg(errorMsg, errorLine, errorCol);
|
||||
if(!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol))
|
||||
{
|
||||
auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:3d!").arg(errorMsg, errorLine, errorCol);
|
||||
qWarning() << fullErrMsg;
|
||||
data.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
QDomNodeList nodes = doc.elementsByTagName("modpack");
|
||||
for(int i = 0; i < nodes.length(); i++) {
|
||||
for(int i = 0; i < nodes.length(); i++)
|
||||
{
|
||||
QDomElement element = nodes.at(i).toElement();
|
||||
|
||||
FtbModpack modpack;
|
||||
@ -80,19 +122,25 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
|
||||
modpack.bugged = false;
|
||||
|
||||
//remove empty if the xml is bugged
|
||||
for(QString curr : modpack.oldVersions) {
|
||||
if(curr.isNull() || curr.isEmpty()) {
|
||||
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()) {
|
||||
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 {
|
||||
}
|
||||
else
|
||||
{
|
||||
modpack.broken = true;
|
||||
qWarning() << "Broken pack:" << modpack.name << " => No valid version!";
|
||||
}
|
||||
@ -111,7 +159,8 @@ bool FtbPackFetchTask::parseAndAddPacks(QByteArray &data, FtbPackType packType,
|
||||
return true;
|
||||
}
|
||||
|
||||
void FtbPackFetchTask::fileDownloadFailed(QString reason){
|
||||
qWarning() << "Fetching FtbPacks failed: " << reason;
|
||||
void FtbPackFetchTask::fileDownloadFailed(QString reason)
|
||||
{
|
||||
qWarning() << "Fetching FtbPacks failed:" << reason;
|
||||
emit failed(reason);
|
||||
}
|
||||
|
@ -11,10 +11,11 @@ class MULTIMC_LOGIC_EXPORT FtbPackFetchTask : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FtbPackFetchTask();
|
||||
virtual ~FtbPackFetchTask();
|
||||
FtbPackFetchTask() = default;
|
||||
virtual ~FtbPackFetchTask() = default;
|
||||
|
||||
void fetch();
|
||||
void fetchPrivate(const QStringList &toFetch);
|
||||
|
||||
private:
|
||||
NetJobPtr jobPtr;
|
||||
@ -34,4 +35,6 @@ signals:
|
||||
void finished(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks);
|
||||
void failed(QString reason);
|
||||
|
||||
void privateFileDownloadFinished(FtbModpack modpack);
|
||||
void privateFileDownloadFailed(QString reason, QString packCode);
|
||||
};
|
||||
|
@ -29,7 +29,15 @@ void FtbPackInstallTask::downloadPack()
|
||||
NetJob *job = new NetJob("Download FTB Pack");
|
||||
|
||||
entry->setStale(true);
|
||||
QString url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1").arg(packoffset);
|
||||
QString url;
|
||||
if(m_pack.type == FtbPackType::Private)
|
||||
{
|
||||
url = QString("http://ftb.cursecdn.com/FTB2/privatepacks/%1").arg(packoffset);
|
||||
}
|
||||
else
|
||||
{
|
||||
url = QString("http://ftb.cursecdn.com/FTB2/modpacks/%1").arg(packoffset);
|
||||
}
|
||||
job->addNetAction(Net::Download::makeCached(url, entry));
|
||||
archivePath = entry->getFullPath();
|
||||
|
||||
|
@ -8,8 +8,8 @@
|
||||
#include "meta/VersionList.h"
|
||||
#include "modplatform/ftb/PackHelpers.h"
|
||||
|
||||
class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask {
|
||||
|
||||
class MULTIMC_LOGIC_EXPORT FtbPackInstallTask : public InstanceTask
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
36
api/logic/modplatform/ftb/FtbPrivatePackManager.cpp
Normal file
36
api/logic/modplatform/ftb/FtbPrivatePackManager.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "FtbPrivatePackManager.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "FileSystem.h"
|
||||
|
||||
void FtbPrivatePackManager::load()
|
||||
{
|
||||
try
|
||||
{
|
||||
currentPacks = QString::fromUtf8(FS::read(m_filename)).split('\n', QString::SkipEmptyParts).toSet();
|
||||
dirty = false;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
currentPacks = {};
|
||||
qWarning() << "Failed to read third party FTB pack codes from" << m_filename;
|
||||
}
|
||||
}
|
||||
|
||||
void FtbPrivatePackManager::save() const
|
||||
{
|
||||
if(!dirty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
FS::write(m_filename, currentPacks.toList().join('\n').toUtf8());
|
||||
dirty = false;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
qWarning() << "Failed to write third party FTB pack codes to" << m_filename;
|
||||
}
|
||||
}
|
40
api/logic/modplatform/ftb/FtbPrivatePackManager.h
Normal file
40
api/logic/modplatform/ftb/FtbPrivatePackManager.h
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QFile>
|
||||
#include "multimc_logic_export.h"
|
||||
|
||||
class MULTIMC_LOGIC_EXPORT FtbPrivatePackManager
|
||||
{
|
||||
public:
|
||||
~FtbPrivatePackManager()
|
||||
{
|
||||
save();
|
||||
}
|
||||
void load();
|
||||
void save() const;
|
||||
bool empty() const
|
||||
{
|
||||
return currentPacks.empty();
|
||||
}
|
||||
const QSet<QString> &getCurrentPackCodes() const
|
||||
{
|
||||
return currentPacks;
|
||||
}
|
||||
void add(const QString &code)
|
||||
{
|
||||
currentPacks.insert(code);
|
||||
dirty = true;
|
||||
}
|
||||
void remove(const QString &code)
|
||||
{
|
||||
currentPacks.remove(code);
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
private:
|
||||
QSet<QString> currentPacks;
|
||||
QString m_filename = "private_packs.txt";
|
||||
mutable bool dirty = false;
|
||||
};
|
@ -6,13 +6,15 @@
|
||||
#include <QMetaType>
|
||||
|
||||
//Header for structs etc...
|
||||
enum FtbPackType {
|
||||
enum class FtbPackType
|
||||
{
|
||||
Public,
|
||||
ThirdParty,
|
||||
Private
|
||||
};
|
||||
|
||||
struct FtbModpack {
|
||||
struct FtbModpack
|
||||
{
|
||||
QString name;
|
||||
QString description;
|
||||
QString author;
|
||||
@ -30,6 +32,7 @@ struct FtbModpack {
|
||||
bool broken = false;
|
||||
|
||||
FtbPackType type;
|
||||
QString packCode;
|
||||
};
|
||||
|
||||
//We need it for the proxy model
|
||||
|
Reference in New Issue
Block a user