fix: enable using more than one download url in mrpacks

Kinda, it's ugly and hackish, since we don't have the facilities to
do this properly (yet!)
This commit is contained in:
flow 2022-06-09 19:54:50 -03:00
parent 46e403b20b
commit 1b878030aa
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 43 additions and 16 deletions

View File

@ -645,18 +645,32 @@ void InstanceImportTask::processModrinth()
}
file.hash = QByteArray::fromHex(hash.toLatin1());
file.hashAlgorithm = hashAlgorithm;
// Do not use requireUrl, which uses StrictMode, instead use QUrl's default TolerantMode
// (as Modrinth seems to incorrectly handle spaces)
auto download_arr = Json::ensureArray(modInfo, "downloads");
for(auto download : download_arr) {
qWarning() << download.toString();
bool is_last = download.toString() == download_arr.last().toString();
file.download = Json::requireString(Json::ensureArray(modInfo, "downloads").first(), "Download URL for " + file.path);
auto download_url = QUrl(download.toString());
if (!file.download.isValid()) {
qDebug() << QString("Download URL (%1) for %2 is not a correctly formatted URL").arg(file.download.toString(), file.path);
throw JSONValidationError(tr("Download URL for %1 is not a correctly formatted URL").arg(file.path));
}
else if (!Modrinth::validateDownloadUrl(file.download)) {
qDebug() << QString("Download URL (%1) for %2 is from a non-whitelisted by Modrinth domain").arg(file.download.toString(), file.path);
non_whitelisted_files.push_back(file);
if (!download_url.isValid()) {
qDebug() << QString("Download URL (%1) for %2 is not a correctly formatted URL")
.arg(download_url.toString(), file.path);
if(is_last && file.downloads.isEmpty())
throw JSONValidationError(tr("Download URL for %1 is not a correctly formatted URL").arg(file.path));
}
else {
if (!Modrinth::validateDownloadUrl(download_url)) {
qDebug() << QString("Download URL (%1) for %2 is from a non-whitelisted by Modrinth domain").arg(download_url.toString(), file.path);
if(is_last && file.downloads.isEmpty())
non_whitelisted_files.push_back(file);
}
file.downloads.push_back(download_url);
}
}
files.push_back(file);
@ -665,7 +679,10 @@ void InstanceImportTask::processModrinth()
if (!non_whitelisted_files.empty()) {
QString text;
for (const auto& file : non_whitelisted_files) {
text += tr("Filepath: %1<br>URL: <a href='%2'>%2</a><br>").arg(file.path, file.download.toString());
text += tr("Filepath: %1<br>").arg(file.path);
for(auto d : file.downloads)
text += tr("URL:") + QString("<a href='%1'>%2</a>").arg(d.toString());
text += "<br>";
}
auto message_dialog = new ScrollMessageBox(m_parent, tr("Non-whitelisted mods found"),
@ -740,13 +757,24 @@ void InstanceImportTask::processModrinth()
instance.saveNow();
m_filesNetJob = new NetJob(tr("Mod download"), APPLICATION->network());
for (auto &file : files)
for (auto file : files)
{
auto path = FS::PathCombine(m_stagingPath, ".minecraft", file.path);
qDebug() << "Will download" << file.download << "to" << path;
auto dl = Net::Download::makeFile(file.download, path);
qDebug() << "Will try to download" << file.downloads.front() << "to" << path;
auto dl = Net::Download::makeFile(file.downloads.front(), path);
dl->addValidator(new Net::ChecksumValidator(file.hashAlgorithm, file.hash));
m_filesNetJob->addNetAction(dl);
if (file.downloads.size() > 1) {
// FIXME: This really needs to be put into a ConcurrentTask of
// MultipleOptionsTask's , once those exist :)
connect(dl.get(), &NetAction::failed, [this, &file, path, dl]{
auto dl = Net::Download::makeFile(file.downloads.dequeue(), path);
dl->addValidator(new Net::ChecksumValidator(file.hashAlgorithm, file.hash));
m_filesNetJob->addNetAction(dl);
dl->succeeded();
});
}
}
connect(m_filesNetJob.get(), &NetJob::succeeded, this, [&]()
{

View File

@ -40,6 +40,7 @@
#include <QByteArray>
#include <QCryptographicHash>
#include <QQueue>
#include <QString>
#include <QUrl>
#include <QVector>
@ -48,14 +49,12 @@ class MinecraftInstance;
namespace Modrinth {
struct File
{
struct File {
QString path;
QCryptographicHash::Algorithm hashAlgorithm;
QByteArray hash;
// TODO: should this support multiple download URLs, like the JSON does?
QUrl download;
QQueue<QUrl> downloads;
};
struct ModpackExtra {