2022-06-04 03:38:01 +05:30
|
|
|
#include "EnsureMetadataTask.h"
|
|
|
|
|
|
|
|
#include <MurmurHash2.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "Json.h"
|
|
|
|
#include "minecraft/mod/Mod.h"
|
|
|
|
#include "minecraft/mod/tasks/LocalModUpdateTask.h"
|
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
|
|
|
#include "modplatform/flame/FlameModIndex.h"
|
|
|
|
#include "modplatform/modrinth/ModrinthAPI.h"
|
|
|
|
#include "modplatform/modrinth/ModrinthPackIndex.h"
|
|
|
|
#include "net/NetJob.h"
|
|
|
|
#include "tasks/MultipleOptionsTask.h"
|
|
|
|
|
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
|
|
|
static ModrinthAPI modrinth_api;
|
|
|
|
static FlameAPI flame_api;
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
EnsureMetadataTask::EnsureMetadataTask(Mod* mod, QDir dir, ModPlatform::Provider prov) : Task(nullptr), m_index_dir(dir), m_provider(prov)
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
2022-06-16 20:15:29 +05:30
|
|
|
auto hash = getHash(mod);
|
|
|
|
if (hash.isEmpty())
|
|
|
|
emitFail(mod);
|
|
|
|
else
|
|
|
|
m_mods.insert(hash, mod);
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-07-10 23:45:25 +05:30
|
|
|
EnsureMetadataTask::EnsureMetadataTask(QList<Mod*>& mods, QDir dir, ModPlatform::Provider prov)
|
2022-06-16 20:15:29 +05:30
|
|
|
: Task(nullptr), m_index_dir(dir), m_provider(prov)
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
2022-06-26 22:47:15 +05:30
|
|
|
for (auto* mod : mods) {
|
|
|
|
if (!mod->valid()) {
|
2022-06-16 20:15:29 +05:30
|
|
|
emitFail(mod);
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto hash = getHash(mod);
|
|
|
|
if (hash.isEmpty()) {
|
|
|
|
emitFail(mod);
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
m_mods.insert(hash, mod);
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
QString EnsureMetadataTask::getHash(Mod* mod)
|
2022-06-16 20:15:29 +05:30
|
|
|
{
|
|
|
|
/* Here we create a mapping hash -> mod, because we need that relationship to parse the API routes */
|
2022-06-04 03:38:01 +05:30
|
|
|
QByteArray jar_data;
|
|
|
|
try {
|
2022-06-26 22:47:15 +05:30
|
|
|
jar_data = FS::read(mod->fileinfo().absoluteFilePath());
|
2022-06-04 03:38:01 +05:30
|
|
|
} catch (FS::FileSystemException& e) {
|
2022-06-26 22:47:15 +05:30
|
|
|
qCritical() << QString("Failed to open / read JAR file of %1").arg(mod->name());
|
2022-06-04 03:38:01 +05:30
|
|
|
qCritical() << QString("Reason: ") << e.cause();
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return {};
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
switch (m_provider) {
|
|
|
|
case ModPlatform::Provider::MODRINTH: {
|
|
|
|
auto hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first();
|
|
|
|
|
|
|
|
return QString(ProviderCaps.hash(ModPlatform::Provider::MODRINTH, jar_data, hash_type).toHex());
|
|
|
|
}
|
|
|
|
case ModPlatform::Provider::FLAME: {
|
|
|
|
QByteArray jar_data_treated;
|
|
|
|
for (char c : jar_data) {
|
|
|
|
// CF-specific
|
|
|
|
if (!(c == 9 || c == 10 || c == 13 || c == 32))
|
|
|
|
jar_data_treated.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString::number(MurmurHash2(jar_data_treated, jar_data_treated.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EnsureMetadataTask::abort()
|
|
|
|
{
|
|
|
|
// Prevent sending signals to a dead object
|
|
|
|
disconnect(this, 0, 0, 0);
|
|
|
|
|
|
|
|
if (m_current_task)
|
|
|
|
return m_current_task->abort();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureMetadataTask::executeTask()
|
|
|
|
{
|
|
|
|
setStatus(tr("Checking if mods have metadata..."));
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
for (auto* mod : m_mods) {
|
2022-07-17 20:13:12 +05:30
|
|
|
if (!mod->valid()) {
|
|
|
|
qDebug() << "Mod" << mod->name() << "is invalid!";
|
|
|
|
emitFail(mod);
|
2022-06-16 20:15:29 +05:30
|
|
|
continue;
|
2022-07-17 20:13:12 +05:30
|
|
|
}
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
// They already have the right metadata :o
|
2022-06-26 22:47:15 +05:30
|
|
|
if (mod->status() != ModStatus::NoMetadata && mod->metadata() && mod->metadata()->provider == m_provider) {
|
|
|
|
qDebug() << "Mod" << mod->name() << "already has metadata!";
|
2022-06-16 20:15:29 +05:30
|
|
|
emitReady(mod);
|
2022-07-17 20:13:12 +05:30
|
|
|
continue;
|
2022-06-16 20:15:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Folders don't have metadata
|
2022-08-09 10:23:50 +05:30
|
|
|
if (mod->type() == ResourceType::FOLDER) {
|
2022-06-16 20:15:29 +05:30
|
|
|
emitReady(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NetJob::Ptr version_task;
|
2022-06-04 03:38:01 +05:30
|
|
|
|
|
|
|
switch (m_provider) {
|
|
|
|
case (ModPlatform::Provider::MODRINTH):
|
2022-06-16 20:15:29 +05:30
|
|
|
version_task = modrinthVersionsTask();
|
2022-06-04 03:38:01 +05:30
|
|
|
break;
|
|
|
|
case (ModPlatform::Provider::FLAME):
|
2022-06-16 20:15:29 +05:30
|
|
|
version_task = flameVersionsTask();
|
2022-06-04 03:38:01 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto invalidade_leftover = [this] {
|
2022-06-26 22:47:15 +05:30
|
|
|
QMutableHashIterator<QString, Mod*> mods_iter(m_mods);
|
2022-06-16 20:15:29 +05:30
|
|
|
while (mods_iter.hasNext()) {
|
|
|
|
auto mod = mods_iter.next();
|
|
|
|
emitFail(mod.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(version_task.get(), &Task::finished, this, [this, invalidade_leftover] {
|
|
|
|
NetJob::Ptr project_task;
|
|
|
|
|
|
|
|
switch (m_provider) {
|
|
|
|
case (ModPlatform::Provider::MODRINTH):
|
|
|
|
project_task = modrinthProjectsTask();
|
|
|
|
break;
|
|
|
|
case (ModPlatform::Provider::FLAME):
|
|
|
|
project_task = flameProjectsTask();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!project_task) {
|
|
|
|
invalidade_leftover();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(project_task.get(), &Task::finished, this, [=] {
|
|
|
|
invalidade_leftover();
|
|
|
|
project_task->deleteLater();
|
|
|
|
m_current_task = nullptr;
|
|
|
|
});
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
m_current_task = project_task.get();
|
|
|
|
project_task->start();
|
2022-06-04 03:38:01 +05:30
|
|
|
});
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
connect(version_task.get(), &Task::finished, [=] {
|
|
|
|
version_task->deleteLater();
|
|
|
|
m_current_task = nullptr;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (m_mods.size() > 1)
|
|
|
|
setStatus(tr("Requesting metadata information from %1...").arg(ProviderCaps.readableName(m_provider)));
|
|
|
|
else if (!m_mods.empty())
|
|
|
|
setStatus(tr("Requesting metadata information from %1 for '%2'...")
|
2022-06-26 22:47:15 +05:30
|
|
|
.arg(ProviderCaps.readableName(m_provider), m_mods.begin().value()->name()));
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
m_current_task = version_task.get();
|
|
|
|
version_task->start();
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
void EnsureMetadataTask::emitReady(Mod* m)
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
2022-06-26 22:47:15 +05:30
|
|
|
qDebug() << QString("Generated metadata for %1").arg(m->name());
|
2022-06-16 20:15:29 +05:30
|
|
|
emit metadataReady(m);
|
|
|
|
|
|
|
|
m_mods.remove(getHash(m));
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
void EnsureMetadataTask::emitFail(Mod* m)
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
2022-06-26 22:47:15 +05:30
|
|
|
qDebug() << QString("Failed to generate metadata for %1").arg(m->name());
|
2022-06-16 20:15:29 +05:30
|
|
|
emit metadataFailed(m);
|
|
|
|
|
|
|
|
m_mods.remove(getHash(m));
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
// Modrinth
|
|
|
|
|
|
|
|
NetJob::Ptr EnsureMetadataTask::modrinthVersionsTask()
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
2022-06-16 20:15:29 +05:30
|
|
|
auto hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first();
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto* response = new QByteArray();
|
|
|
|
auto ver_task = modrinth_api.currentVersions(m_mods.keys(), hash_type, response);
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
// Prevents unfortunate timings when aborting the task
|
|
|
|
if (!ver_task)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
connect(ver_task.get(), &NetJob::succeeded, this, [this, response] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth::CurrentVersions at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
|
|
|
|
failed(parse_error.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto entries = Json::requireObject(doc);
|
|
|
|
for (auto& hash : m_mods.keys()) {
|
|
|
|
auto mod = m_mods.find(hash).value();
|
|
|
|
try {
|
|
|
|
auto entry = Json::requireObject(entries, hash);
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
setStatus(tr("Parsing API response from Modrinth for '%1'...").arg(mod->name()));
|
|
|
|
qDebug() << "Getting version for" << mod->name() << "from Modrinth";
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
m_temp_versions.insert(hash, Modrinth::loadIndexedPackVersion(entry));
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
2022-06-16 20:15:29 +05:30
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return ver_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetJob::Ptr EnsureMetadataTask::modrinthProjectsTask()
|
|
|
|
{
|
|
|
|
QHash<QString, QString> addonIds;
|
|
|
|
for (auto const& data : m_temp_versions)
|
|
|
|
addonIds.insert(data.addonId.toString(), data.hash);
|
|
|
|
|
|
|
|
auto response = new QByteArray();
|
|
|
|
NetJob::Ptr proj_task;
|
|
|
|
|
|
|
|
if (addonIds.isEmpty()) {
|
|
|
|
qWarning() << "No addonId found!";
|
|
|
|
} else if (addonIds.size() == 1) {
|
|
|
|
proj_task = modrinth_api.getProject(*addonIds.keyBegin(), response);
|
|
|
|
} else {
|
|
|
|
proj_task = modrinth_api.getProjects(addonIds.keys(), response);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevents unfortunate timings when aborting the task
|
|
|
|
if (!proj_task)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
connect(proj_task.get(), &NetJob::succeeded, this, [this, response, addonIds] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
auto doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth projects task at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
try {
|
|
|
|
QJsonArray entries;
|
|
|
|
if (addonIds.size() == 1)
|
|
|
|
entries = { doc.object() };
|
|
|
|
else
|
|
|
|
entries = Json::requireArray(doc);
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
for (auto entry : entries) {
|
|
|
|
auto entry_obj = Json::requireObject(entry);
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
Modrinth::loadIndexedPack(pack, entry_obj);
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
auto hash = addonIds.find(pack.addonId.toString()).value();
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
auto mod_iter = m_mods.find(hash);
|
|
|
|
if (mod_iter == m_mods.end()) {
|
|
|
|
qWarning() << "Invalid project id from the API response.";
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
auto* mod = mod_iter.value();
|
|
|
|
|
|
|
|
try {
|
|
|
|
setStatus(tr("Parsing API response from Modrinth for '%1'...").arg(mod->name()));
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
modrinthCallback(pack, m_temp_versions.find(hash).value(), mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
emitFail(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return proj_task;
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
// Flame
|
|
|
|
NetJob::Ptr EnsureMetadataTask::flameVersionsTask()
|
2022-06-04 03:38:01 +05:30
|
|
|
{
|
|
|
|
auto* response = new QByteArray();
|
|
|
|
|
2022-07-10 23:45:25 +05:30
|
|
|
QList<uint> fingerprints;
|
2022-06-16 20:15:29 +05:30
|
|
|
for (auto& murmur : m_mods.keys()) {
|
|
|
|
fingerprints.push_back(murmur.toUInt());
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
|
|
|
auto ver_task = flame_api.matchFingerprints(fingerprints, response);
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
connect(ver_task.get(), &Task::succeeded, this, [this, response] {
|
2022-06-04 03:38:01 +05:30
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
2022-06-16 20:15:29 +05:30
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth::CurrentVersions at " << parse_error.offset
|
2022-06-04 03:38:01 +05:30
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
failed(parse_error.errorString());
|
2022-06-04 03:38:01 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto doc_obj = Json::requireObject(doc);
|
2022-06-16 20:15:29 +05:30
|
|
|
auto data_obj = Json::requireObject(doc_obj, "data");
|
|
|
|
auto data_arr = Json::requireArray(data_obj, "exactMatches");
|
|
|
|
|
|
|
|
if (data_arr.isEmpty()) {
|
|
|
|
qWarning() << "No matches found for fingerprint search!";
|
2022-06-04 03:38:01 +05:30
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
for (auto match : data_arr) {
|
|
|
|
auto match_obj = Json::ensureObject(match, {});
|
|
|
|
auto file_obj = Json::ensureObject(match_obj, "file", {});
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
if (match_obj.isEmpty() || file_obj.isEmpty()) {
|
|
|
|
qWarning() << "Fingerprint match is empty!";
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
auto fingerprint = QString::number(Json::ensureVariant(file_obj, "fileFingerprint").toUInt());
|
|
|
|
auto mod = m_mods.find(fingerprint);
|
|
|
|
if (mod == m_mods.end()) {
|
|
|
|
qWarning() << "Invalid fingerprint from the API response.";
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg((*mod)->name()));
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
m_temp_versions.insert(fingerprint, FlameMod::loadIndexedPackVersion(file_obj));
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return ver_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetJob::Ptr EnsureMetadataTask::flameProjectsTask()
|
|
|
|
{
|
|
|
|
QHash<QString, QString> addonIds;
|
|
|
|
for (auto const& hash : m_mods.keys()) {
|
|
|
|
if (m_temp_versions.contains(hash)) {
|
|
|
|
auto const& data = m_temp_versions.find(hash).value();
|
2022-06-26 22:47:15 +05:30
|
|
|
|
|
|
|
auto id_str = data.addonId.toString();
|
|
|
|
if (!id_str.isEmpty())
|
|
|
|
addonIds.insert(data.addonId.toString(), hash);
|
2022-06-16 20:15:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto response = new QByteArray();
|
|
|
|
NetJob::Ptr proj_task;
|
|
|
|
|
|
|
|
if (addonIds.isEmpty()) {
|
|
|
|
qWarning() << "No addonId found!";
|
|
|
|
} else if (addonIds.size() == 1) {
|
|
|
|
proj_task = flame_api.getProject(*addonIds.keyBegin(), response);
|
|
|
|
} else {
|
|
|
|
proj_task = flame_api.getProjects(addonIds.keys(), response);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevents unfortunate timings when aborting the task
|
|
|
|
if (!proj_task)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
connect(proj_task.get(), &NetJob::succeeded, this, [this, response, addonIds] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
auto doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth projects task at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
QJsonArray entries;
|
|
|
|
if (addonIds.size() == 1)
|
|
|
|
entries = { Json::requireObject(Json::requireObject(doc), "data") };
|
|
|
|
else
|
|
|
|
entries = Json::requireArray(Json::requireObject(doc), "data");
|
|
|
|
|
|
|
|
for (auto entry : entries) {
|
|
|
|
auto entry_obj = Json::requireObject(entry);
|
|
|
|
|
|
|
|
auto id = QString::number(Json::requireInteger(entry_obj, "id"));
|
|
|
|
auto hash = addonIds.find(id).value();
|
|
|
|
auto mod = m_mods.find(hash).value();
|
|
|
|
|
|
|
|
try {
|
2022-06-26 22:47:15 +05:30
|
|
|
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
FlameMod::loadIndexedPack(pack, entry_obj);
|
2022-06-04 03:38:01 +05:30
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
flameCallback(pack, m_temp_versions.find(hash).value(), mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
} catch (Json::JsonException& e) {
|
2022-06-16 20:15:29 +05:30
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
return proj_task;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
void EnsureMetadataTask::modrinthCallback(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, Mod* mod)
|
2022-06-16 20:15:29 +05:30
|
|
|
{
|
|
|
|
// Prevent file name mismatch
|
2022-06-26 22:47:15 +05:30
|
|
|
ver.fileName = mod->fileinfo().fileName();
|
2022-06-20 17:25:35 +05:30
|
|
|
if (ver.fileName.endsWith(".disabled"))
|
|
|
|
ver.fileName.chop(9);
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
QDir tmp_index_dir(m_index_dir);
|
|
|
|
|
|
|
|
{
|
|
|
|
LocalModUpdateTask update_metadata(m_index_dir, pack, ver);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
QObject::connect(&update_metadata, &Task::finished, &loop, &QEventLoop::quit);
|
|
|
|
|
|
|
|
update_metadata.start();
|
|
|
|
|
|
|
|
if (!update_metadata.isFinished())
|
|
|
|
loop.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto metadata = Metadata::get(tmp_index_dir, pack.slug);
|
|
|
|
if (!metadata.isValid()) {
|
|
|
|
qCritical() << "Failed to generate metadata at last step!";
|
|
|
|
emitFail(mod);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
mod->setMetadata(metadata);
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
emitReady(mod);
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
void EnsureMetadataTask::flameCallback(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, Mod* mod)
|
2022-06-16 20:15:29 +05:30
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Prevent file name mismatch
|
2022-06-26 22:47:15 +05:30
|
|
|
ver.fileName = mod->fileinfo().fileName();
|
2022-06-20 17:25:35 +05:30
|
|
|
if (ver.fileName.endsWith(".disabled"))
|
|
|
|
ver.fileName.chop(9);
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
QDir tmp_index_dir(m_index_dir);
|
|
|
|
|
|
|
|
{
|
|
|
|
LocalModUpdateTask update_metadata(m_index_dir, pack, ver);
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
QObject::connect(&update_metadata, &Task::finished, &loop, &QEventLoop::quit);
|
|
|
|
|
|
|
|
update_metadata.start();
|
|
|
|
|
|
|
|
if (!update_metadata.isFinished())
|
|
|
|
loop.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto metadata = Metadata::get(tmp_index_dir, pack.slug);
|
|
|
|
if (!metadata.isValid()) {
|
|
|
|
qCritical() << "Failed to generate metadata at last step!";
|
|
|
|
emitFail(mod);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
mod->setMetadata(metadata);
|
2022-06-16 20:15:29 +05:30
|
|
|
|
|
|
|
emitReady(mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
2022-06-04 03:38:01 +05:30
|
|
|
}
|