summaryrefslogtreecommitdiffstats
path: root/api/logic/modplatform/ftb/FtbPackFetchTask.cpp
blob: fe3f3fac0fceb89a3276d2ad1d71399887f917d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "FtbPackFetchTask.h"
#include <QDomDocument>
#include "FtbPrivatePackManager.h"

#include "net/URLConstants.h"

void FtbPackFetchTask::fetch()
{
    publicPacks.clear();
    thirdPartyPacks.clear();

    NetJob *netJob = new NetJob("FtbModpackFetch");

    QUrl publicPacksUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/modpacks.xml");
    qDebug() << "Downloading public version info from" << publicPacksUrl.toString();
    netJob->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData));

    QUrl thirdPartyUrl = QUrl(URLConstants::FTB_CDN_BASE_URL + "static/thirdparty.xml");
    qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString();
    netJob->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData));

    QObject::connect(netJob, &NetJob::succeeded, this, &FtbPackFetchTask::fileDownloadFinished);
    QObject::connect(netJob, &NetJob::failed, this, &FtbPackFetchTask::fileDownloadFailed);

    jobPtr.reset(netJob);
    netJob->start();
}

void FtbPackFetchTask::fetchPrivate(const QStringList & toFetch)
{
    QString privatePackBaseUrl = URLConstants::FTB_CDN_BASE_URL + "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))
    {
        failedLists.append(tr("Public Packs"));
    }

    if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, FtbPackType::ThirdParty, thirdPartyPacks))
    {
        failedLists.append(tr("Third Party Packs"));
    }

    if(failedLists.size() > 0)
    {
        emit failed(tr("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)
{
    QDomDocument doc;

    QString errorMsg = "Unknown error.";
    int errorLine = -1;
    int errorCol = -1;

    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++)
    {
        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
        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!";
            }
        }

        modpack.author = element.attribute("author");

        modpack.dir = element.attribute("dir");
        modpack.file = element.attribute("url");

        modpack.type = packType;

        list.append(modpack);
    }

    return true;
}

void FtbPackFetchTask::fileDownloadFailed(QString reason)
{
    qWarning() << "Fetching FtbPacks failed:" << reason;
    emit failed(reason);
}