2017-04-20 08:52:04 +05:30
|
|
|
#include "FileResolvingTask.h"
|
2022-05-29 01:23:12 +05:30
|
|
|
|
2017-04-20 08:52:04 +05:30
|
|
|
#include "Json.h"
|
2022-05-29 01:23:12 +05:30
|
|
|
#include "net/Upload.h"
|
2017-04-20 08:52:04 +05:30
|
|
|
|
2022-10-28 22:01:21 +05:30
|
|
|
#include "modplatform/modrinth/ModrinthPackIndex.h"
|
|
|
|
|
2022-05-29 01:23:12 +05:30
|
|
|
Flame::FileResolvingTask::FileResolvingTask(const shared_qobject_ptr<QNetworkAccessManager>& network, Flame::Manifest& toProcess)
|
2021-11-22 03:51:12 +05:30
|
|
|
: m_network(network), m_toProcess(toProcess)
|
2022-05-08 12:52:50 +05:30
|
|
|
{}
|
2017-04-20 08:52:04 +05:30
|
|
|
|
2022-07-26 01:07:10 +05:30
|
|
|
bool Flame::FileResolvingTask::abort()
|
|
|
|
{
|
2022-10-13 23:40:35 +05:30
|
|
|
bool aborted = true;
|
2022-07-26 01:07:10 +05:30
|
|
|
if (m_dljob)
|
2022-10-13 23:40:35 +05:30
|
|
|
aborted &= m_dljob->abort();
|
2022-10-22 17:41:51 +05:30
|
|
|
if (m_checkJob)
|
|
|
|
aborted &= m_checkJob->abort();
|
2022-10-13 23:40:35 +05:30
|
|
|
return aborted ? Task::abort() : false;
|
2022-07-26 01:07:10 +05:30
|
|
|
}
|
|
|
|
|
2017-04-22 22:21:04 +05:30
|
|
|
void Flame::FileResolvingTask::executeTask()
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setStatus(tr("Resolving mod IDs..."));
|
2022-06-23 16:28:27 +05:30
|
|
|
setProgress(0, 3);
|
2023-01-25 01:22:09 +05:30
|
|
|
m_dljob.reset(new NetJob("Mod id resolver", m_network));
|
2022-05-29 01:23:12 +05:30
|
|
|
result.reset(new QByteArray());
|
|
|
|
//build json data to send
|
|
|
|
QJsonObject object;
|
|
|
|
|
|
|
|
object["fileIds"] = QJsonArray::fromVariantList(std::accumulate(m_toProcess.files.begin(), m_toProcess.files.end(), QVariantList(), [](QVariantList& l, const File& s) {
|
|
|
|
l.push_back(s.fileId);
|
|
|
|
return l;
|
|
|
|
}));
|
|
|
|
QByteArray data = Json::toText(object);
|
|
|
|
auto dl = Net::Upload::makeByteArray(QUrl("https://api.curseforge.com/v1/mods/files"), result.get(), data);
|
|
|
|
m_dljob->addNetAction(dl);
|
2023-05-21 14:16:28 +05:30
|
|
|
|
|
|
|
auto step_progress = std::make_shared<TaskStepProgress>();
|
|
|
|
connect(m_dljob.get(), &NetJob::finished, this, [this, step_progress]() {
|
|
|
|
step_progress->state = TaskStepState::Succeeded;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
netJobFinished();
|
|
|
|
});
|
|
|
|
connect(m_dljob.get(), &NetJob::failed, this, [this, step_progress](QString reason) {
|
|
|
|
step_progress->state = TaskStepState::Failed;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
emitFailed(reason);
|
|
|
|
});
|
|
|
|
connect(m_dljob.get(), &NetJob::stepProgress, this, &FileResolvingTask::propogateStepProgress);
|
|
|
|
connect(m_dljob.get(), &NetJob::progress, this, [this, step_progress](qint64 current, qint64 total) {
|
|
|
|
qDebug() << "Resolve slug progress" << current << total;
|
|
|
|
step_progress->update(current, total);
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
|
|
|
connect(m_dljob.get(), &NetJob::status, this, [this, step_progress](QString status) {
|
|
|
|
step_progress->status = status;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
m_dljob->start();
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|
|
|
|
|
2017-04-22 22:21:04 +05:30
|
|
|
void Flame::FileResolvingTask::netJobFinished()
|
2017-04-20 08:52:04 +05:30
|
|
|
{
|
2022-06-23 16:28:27 +05:30
|
|
|
setProgress(1, 3);
|
2022-05-29 01:23:12 +05:30
|
|
|
// job to check modrinth for blocked projects
|
2023-01-25 01:22:09 +05:30
|
|
|
m_checkJob.reset(new NetJob("Modrinth check", m_network));
|
2023-05-21 14:16:28 +05:30
|
|
|
blockedProjects = QMap<File*, std::shared_ptr<QByteArray>>();
|
2022-12-02 00:03:35 +05:30
|
|
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
QJsonArray array;
|
|
|
|
|
|
|
|
try {
|
|
|
|
doc = Json::requireDocument(*result);
|
|
|
|
array = Json::requireArray(doc.object()["data"]);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qCritical() << "Non-JSON data returned from the CF API";
|
|
|
|
qCritical() << e.cause();
|
|
|
|
|
|
|
|
emitFailed(tr("Invalid data returned from the API."));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-29 01:23:12 +05:30
|
|
|
for (QJsonValueRef file : array) {
|
|
|
|
auto fileid = Json::requireInteger(Json::requireObject(file)["id"]);
|
|
|
|
auto& out = m_toProcess.files[fileid];
|
2022-05-08 12:52:50 +05:30
|
|
|
try {
|
2022-05-29 01:23:12 +05:30
|
|
|
out.parseFromObject(Json::requireObject(file));
|
2022-05-08 12:52:50 +05:30
|
|
|
} catch (const JSONValidationError& e) {
|
2022-05-29 01:23:12 +05:30
|
|
|
qDebug() << "Blocked mod on curseforge" << out.fileName;
|
|
|
|
auto hash = out.hash;
|
|
|
|
if(!hash.isEmpty()) {
|
|
|
|
auto url = QString("https://api.modrinth.com/v2/version_file/%1?algorithm=sha1").arg(hash);
|
2023-05-21 14:16:28 +05:30
|
|
|
auto output = std::make_shared<QByteArray>();
|
|
|
|
auto dl = Net::Download::makeByteArray(QUrl(url), output.get());
|
2022-05-29 01:23:12 +05:30
|
|
|
QObject::connect(dl.get(), &Net::Download::succeeded, [&out]() {
|
|
|
|
out.resolved = true;
|
|
|
|
});
|
|
|
|
|
2022-10-22 17:41:51 +05:30
|
|
|
m_checkJob->addNetAction(dl);
|
2022-05-29 01:23:12 +05:30
|
|
|
blockedProjects.insert(&out, output);
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2023-05-21 14:16:28 +05:30
|
|
|
auto step_progress = std::make_shared<TaskStepProgress>();
|
|
|
|
connect(m_checkJob.get(), &NetJob::finished, this, [this, step_progress]() {
|
|
|
|
step_progress->state = TaskStepState::Succeeded;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
modrinthCheckFinished();
|
|
|
|
});
|
|
|
|
connect(m_checkJob.get(), &NetJob::failed, this, [this, step_progress](QString reason) {
|
|
|
|
step_progress->state = TaskStepState::Failed;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
emitFailed(reason);
|
|
|
|
});
|
|
|
|
connect(m_checkJob.get(), &NetJob::stepProgress, this, &FileResolvingTask::propogateStepProgress);
|
|
|
|
connect(m_checkJob.get(), &NetJob::progress, this, [this, step_progress](qint64 current, qint64 total) {
|
|
|
|
qDebug() << "Resolve slug progress" << current << total;
|
|
|
|
step_progress->update(current, total);
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
|
|
|
connect(m_checkJob.get(), &NetJob::status, this, [this, step_progress](QString status) {
|
|
|
|
step_progress->status = status;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
2022-05-29 01:23:12 +05:30
|
|
|
|
2022-10-22 17:41:51 +05:30
|
|
|
m_checkJob->start();
|
2022-05-29 01:23:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Flame::FileResolvingTask::modrinthCheckFinished() {
|
2022-06-23 16:28:27 +05:30
|
|
|
setProgress(2, 3);
|
2022-05-29 01:23:12 +05:30
|
|
|
qDebug() << "Finished with blocked mods : " << blockedProjects.size();
|
|
|
|
|
|
|
|
for (auto it = blockedProjects.keyBegin(); it != blockedProjects.keyEnd(); it++) {
|
|
|
|
auto &out = *it;
|
|
|
|
auto bytes = blockedProjects[out];
|
|
|
|
if (!out->resolved) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-28 22:01:21 +05:30
|
|
|
|
2022-05-29 01:23:12 +05:30
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*bytes);
|
|
|
|
auto obj = doc.object();
|
2022-10-28 22:01:21 +05:30
|
|
|
auto file = Modrinth::loadIndexedPackVersion(obj);
|
|
|
|
|
|
|
|
// If there's more than one mod loader for this version, we can't know for sure
|
|
|
|
// which file is relative to each loader, so it's best to not use any one and
|
|
|
|
// let the user download it manually.
|
|
|
|
if (file.loaders.size() <= 1) {
|
|
|
|
out->url = file.downloadUrl;
|
|
|
|
qDebug() << "Found alternative on modrinth " << out->fileName;
|
|
|
|
} else {
|
|
|
|
out->resolved = false;
|
2022-05-29 01:23:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
//copy to an output list and filter out projects found on modrinth
|
2023-05-21 14:16:28 +05:30
|
|
|
auto block = std::make_shared<QList<File*>>();
|
2022-05-29 01:23:12 +05:30
|
|
|
auto it = blockedProjects.keys();
|
|
|
|
std::copy_if(it.begin(), it.end(), std::back_inserter(*block), [](File *f) {
|
|
|
|
return !f->resolved;
|
|
|
|
});
|
|
|
|
//Display not found mods early
|
|
|
|
if (!block->empty()) {
|
|
|
|
//blocked mods found, we need the slug for displaying.... we need another job :D !
|
2023-05-21 14:16:28 +05:30
|
|
|
m_slugJob.reset(new NetJob("Slug Job", m_network));
|
|
|
|
int index = 0;
|
|
|
|
for (auto mod : *block) {
|
|
|
|
auto projectId = mod->projectId;
|
|
|
|
auto output = std::make_shared<QByteArray>();
|
2022-05-29 01:23:12 +05:30
|
|
|
auto url = QString("https://api.curseforge.com/v1/mods/%1").arg(projectId);
|
2023-05-21 14:16:28 +05:30
|
|
|
auto dl = Net::Download::makeByteArray(url, output.get());
|
|
|
|
qDebug() << "Fetching url slug for file:" << mod->fileName;
|
|
|
|
QObject::connect(dl.get(), &Net::Download::succeeded, [block, index, output]() {
|
|
|
|
auto mod = block->at(index); // use the shared_ptr so it is captured and only freed when we are done
|
|
|
|
auto json = QJsonDocument::fromJson(*output);
|
2022-05-29 01:23:12 +05:30
|
|
|
auto base = Json::requireString(Json::requireObject(Json::requireObject(Json::requireObject(json),"data"),"links"),
|
|
|
|
"websiteUrl");
|
|
|
|
auto link = QString("%1/download/%2").arg(base, QString::number(mod->fileId));
|
|
|
|
mod->websiteUrl = link;
|
2023-05-21 14:16:28 +05:30
|
|
|
});
|
|
|
|
m_slugJob->addNetAction(dl);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
auto step_progress = std::make_shared<TaskStepProgress>();
|
|
|
|
connect(m_slugJob.get(), &NetJob::succeeded, this, [this, step_progress]() {
|
|
|
|
step_progress->state = TaskStepState::Succeeded;
|
|
|
|
stepProgress(*step_progress);
|
2022-05-29 01:23:12 +05:30
|
|
|
emitSucceeded();
|
|
|
|
});
|
2023-05-21 14:16:28 +05:30
|
|
|
connect(m_slugJob.get(), &NetJob::failed, this, [this, step_progress](QString reason) {
|
|
|
|
step_progress->state = TaskStepState::Failed;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
emitFailed(reason);
|
|
|
|
});
|
|
|
|
connect(m_slugJob.get(), &NetJob::stepProgress, this, &FileResolvingTask::propogateStepProgress);
|
|
|
|
connect(m_slugJob.get(), &NetJob::progress, this, [this, step_progress](qint64 current, qint64 total) {
|
|
|
|
qDebug() << "Resolve slug progress" << current << total;
|
|
|
|
step_progress->update(current, total);
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
|
|
|
connect(m_slugJob.get(), &NetJob::status, this, [this, step_progress](QString status) {
|
|
|
|
step_progress->status = status;
|
|
|
|
stepProgress(*step_progress);
|
|
|
|
});
|
|
|
|
|
|
|
|
m_slugJob->start();
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2022-05-29 01:23:12 +05:30
|
|
|
emitSucceeded();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2017-04-20 08:52:04 +05:30
|
|
|
}
|