2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
#include "PackFetchTask.h"
|
|
|
|
#include "PrivatePackManager.h"
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
#include <QDomDocument>
|
2021-11-21 22:21:12 +00:00
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "Application.h"
|
2019-01-04 00:48:36 +00:00
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
namespace LegacyFTB {
|
|
|
|
|
|
|
|
void PackFetchTask::fetch()
|
2018-04-01 19:24:28 +01:00
|
|
|
{
|
2018-08-01 23:52:31 +01:00
|
|
|
publicPacks.clear();
|
|
|
|
thirdPartyPacks.clear();
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
jobPtr.reset(new NetJob("LegacyFTB::ModpackFetch", m_network));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2020-07-18 15:18:02 +01:00
|
|
|
QUrl publicPacksUrl = QUrl(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "static/modpacks.xml");
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Downloading public version info from" << publicPacksUrl.toString();
|
2021-11-21 22:21:12 +00:00
|
|
|
jobPtr->addNetAction(Net::Download::makeByteArray(publicPacksUrl, &publicModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2020-07-18 15:18:02 +01:00
|
|
|
QUrl thirdPartyUrl = QUrl(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "static/thirdparty.xml");
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Downloading thirdparty version info from" << thirdPartyUrl.toString();
|
2021-11-21 22:21:12 +00:00
|
|
|
jobPtr->addNetAction(Net::Download::makeByteArray(thirdPartyUrl, &thirdPartyModpacksXmlFileData));
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
QObject::connect(jobPtr.get(), &NetJob::succeeded, this, &PackFetchTask::fileDownloadFinished);
|
|
|
|
QObject::connect(jobPtr.get(), &NetJob::failed, this, &PackFetchTask::fileDownloadFailed);
|
2022-07-28 19:58:04 +01:00
|
|
|
QObject::connect(jobPtr.get(), &NetJob::aborted, this, &PackFetchTask::fileDownloadAborted);
|
2018-02-28 18:42:30 +00:00
|
|
|
|
2021-12-31 04:27:59 +00:00
|
|
|
jobPtr->start();
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
void PackFetchTask::fetchPrivate(const QStringList & toFetch)
|
2018-08-01 23:52:31 +01:00
|
|
|
{
|
2020-07-18 15:18:02 +01:00
|
|
|
QString privatePackBaseUrl = BuildConfig.LEGACY_FTB_CDN_BASE_URL + "static/%1.xml";
|
2018-08-01 23:52:31 +01:00
|
|
|
|
|
|
|
for (auto &packCode: toFetch)
|
|
|
|
{
|
|
|
|
QByteArray *data = new QByteArray();
|
2021-12-31 04:27:59 +00:00
|
|
|
NetJob *job = new NetJob("Fetching private pack", m_network);
|
2018-08-01 23:52:31 +01:00
|
|
|
job->addNetAction(Net::Download::makeByteArray(privatePackBaseUrl.arg(packCode), data));
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, job, data, packCode]
|
|
|
|
{
|
2019-11-03 22:48:12 +00:00
|
|
|
ModpackList packs;
|
|
|
|
parseAndAddPacks(*data, PackType::Private, packs);
|
|
|
|
foreach(Modpack currentPack, packs)
|
2018-08-01 23:52:31 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2022-07-28 19:58:04 +01:00
|
|
|
QObject::connect(job, &NetJob::aborted, this, [this, job, data]{
|
|
|
|
emit aborted();
|
|
|
|
job->deleteLater();
|
|
|
|
|
|
|
|
data->clear();
|
|
|
|
delete data;
|
|
|
|
});
|
|
|
|
|
2021-12-31 04:27:59 +00:00
|
|
|
job->start();
|
2018-08-01 23:52:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
void PackFetchTask::fileDownloadFinished()
|
2018-04-01 19:24:28 +01:00
|
|
|
{
|
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
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
if(!parseAndAddPacks(publicModpacksXmlFileData, PackType::Public, publicPacks))
|
2018-08-01 23:52:31 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
failedLists.append(tr("Public Packs"));
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
if(!parseAndAddPacks(thirdPartyModpacksXmlFileData, PackType::ThirdParty, thirdPartyPacks))
|
2018-08-01 23:52:31 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
bool PackFetchTask::parseAndAddPacks(QByteArray &data, PackType packType, ModpackList &list)
|
2018-04-01 19:24:28 +01:00
|
|
|
{
|
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))
|
|
|
|
{
|
2022-05-02 20:34:55 +01:00
|
|
|
auto fullErrMsg = QString("Failed to fetch modpack data: %1 %2:%3!").arg(errorMsg).arg(errorLine).arg(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();
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
Modpack modpack;
|
2018-07-15 13:51:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
void PackFetchTask::fileDownloadFailed(QString reason)
|
2018-08-01 23:52:31 +01:00
|
|
|
{
|
2019-11-03 22:48:12 +00:00
|
|
|
qWarning() << "Fetching FTBPacks failed:" << reason;
|
2018-07-15 13:51:05 +01:00
|
|
|
emit failed(reason);
|
2018-02-28 18:42:30 +00:00
|
|
|
}
|
2019-11-03 22:48:12 +00:00
|
|
|
|
2022-07-28 19:58:04 +01:00
|
|
|
void PackFetchTask::fileDownloadAborted()
|
|
|
|
{
|
|
|
|
emit aborted();
|
|
|
|
}
|
|
|
|
|
2019-11-03 22:48:12 +00:00
|
|
|
}
|