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-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 19:01:46 +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);
|
2021-12-31 09:57:59 +05:30
|
|
|
m_dljob = 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);
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(m_dljob.get(), &NetJob::finished, this, &Flame::FileResolvingTask::netJobFinished);
|
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);
|
2018-07-15 18:21:05 +05:30
|
|
|
int index = 0;
|
2022-05-29 01:23:12 +05:30
|
|
|
// job to check modrinth for blocked projects
|
2022-10-22 19:01:46 +05:30
|
|
|
m_checkJob = new NetJob("Modrinth check", m_network);
|
2022-05-29 01:23:12 +05:30
|
|
|
blockedProjects = QMap<File *,QByteArray *>();
|
|
|
|
auto doc = Json::requireDocument(*result);
|
|
|
|
auto array = Json::requireArray(doc.object()["data"]);
|
|
|
|
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);
|
|
|
|
auto output = new QByteArray();
|
|
|
|
auto dl = Net::Download::makeByteArray(QUrl(url), output);
|
|
|
|
QObject::connect(dl.get(), &Net::Download::succeeded, [&out]() {
|
|
|
|
out.resolved = true;
|
|
|
|
});
|
|
|
|
|
2022-10-22 19:01:46 +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
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2022-10-22 19:01:46 +05:30
|
|
|
connect(m_checkJob.get(), &NetJob::finished, this, &Flame::FileResolvingTask::modrinthCheckFinished);
|
2022-05-29 01:23:12 +05:30
|
|
|
|
2022-10-22 19:01:46 +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) {
|
|
|
|
delete bytes;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*bytes);
|
|
|
|
auto obj = doc.object();
|
|
|
|
auto array = Json::requireArray(obj,"files");
|
|
|
|
for (auto file: array) {
|
|
|
|
auto fileObj = Json::requireObject(file);
|
|
|
|
auto primary = Json::requireBoolean(fileObj,"primary");
|
|
|
|
if (primary) {
|
|
|
|
out->url = Json::requireUrl(fileObj,"url");
|
|
|
|
qDebug() << "Found alternative on modrinth " << out->fileName;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete bytes;
|
|
|
|
}
|
|
|
|
//copy to an output list and filter out projects found on modrinth
|
|
|
|
auto block = new QList<File *>();
|
|
|
|
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 !
|
|
|
|
auto slugJob = new NetJob("Slug Job", m_network);
|
|
|
|
auto slugs = QVector<QByteArray>(block->size());
|
|
|
|
auto index = 0;
|
|
|
|
for (auto fileInfo: *block) {
|
|
|
|
auto projectId = fileInfo->projectId;
|
|
|
|
slugs[index] = QByteArray();
|
|
|
|
auto url = QString("https://api.curseforge.com/v1/mods/%1").arg(projectId);
|
|
|
|
auto dl = Net::Download::makeByteArray(url, &slugs[index]);
|
|
|
|
slugJob->addNetAction(dl);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
connect(slugJob, &NetJob::succeeded, this, [slugs, this, slugJob, block]() {
|
|
|
|
slugJob->deleteLater();
|
|
|
|
auto index = 0;
|
|
|
|
for (const auto &slugResult: slugs) {
|
|
|
|
auto json = QJsonDocument::fromJson(slugResult);
|
|
|
|
auto base = Json::requireString(Json::requireObject(Json::requireObject(Json::requireObject(json),"data"),"links"),
|
|
|
|
"websiteUrl");
|
|
|
|
auto mod = block->at(index);
|
|
|
|
auto link = QString("%1/download/%2").arg(base, QString::number(mod->fileId));
|
|
|
|
mod->websiteUrl = link;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
emitSucceeded();
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|