2022-06-03 23:08:01 +01:00
|
|
|
#include "EnsureMetadataTask.h"
|
|
|
|
|
|
|
|
#include <MurmurHash2.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "Json.h"
|
2022-07-24 19:13:42 +01:00
|
|
|
|
2022-06-03 23:08:01 +01:00
|
|
|
#include "minecraft/mod/Mod.h"
|
|
|
|
#include "minecraft/mod/tasks/LocalModUpdateTask.h"
|
2022-07-24 19:13:42 +01:00
|
|
|
|
2022-06-03 23:08:01 +01:00
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
|
|
|
#include "modplatform/flame/FlameModIndex.h"
|
2023-06-18 21:27:26 +01:00
|
|
|
#include "modplatform/helpers/HashUtils.h"
|
2022-06-03 23:08:01 +01:00
|
|
|
#include "modplatform/modrinth/ModrinthAPI.h"
|
|
|
|
#include "modplatform/modrinth/ModrinthPackIndex.h"
|
2022-07-24 19:13:42 +01:00
|
|
|
|
2022-06-03 23:08:01 +01:00
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
|
|
|
static ModrinthAPI modrinth_api;
|
|
|
|
static FlameAPI flame_api;
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
EnsureMetadataTask::EnsureMetadataTask(Mod* mod, QDir dir, ModPlatform::ResourceProvider prov)
|
2022-07-24 19:13:42 +01:00
|
|
|
: Task(nullptr), m_index_dir(dir), m_provider(prov), m_hashing_task(nullptr), m_current_task(nullptr)
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2022-07-24 19:13:42 +01:00
|
|
|
auto hash_task = createNewHash(mod);
|
2022-08-02 19:38:51 +01:00
|
|
|
if (!hash_task)
|
|
|
|
return;
|
2023-06-18 21:27:26 +01:00
|
|
|
connect(hash_task.get(), &Hashing::Hasher::getResults, [this, mod](QString hash) { m_mods.insert(hash, mod); });
|
|
|
|
connect(hash_task.get(), &Task::failed, [this, mod] { emitFail(mod, "", RemoveFromList::No); });
|
2022-07-24 19:13:42 +01:00
|
|
|
hash_task->start();
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
EnsureMetadataTask::EnsureMetadataTask(QList<Mod*>& mods, QDir dir, ModPlatform::ResourceProvider prov)
|
2022-07-24 19:13:42 +01:00
|
|
|
: Task(nullptr), m_index_dir(dir), m_provider(prov), m_current_task(nullptr)
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
m_hashing_task.reset(new ConcurrentTask(this, "MakeHashesTask", 10));
|
2022-06-26 18:17:15 +01:00
|
|
|
for (auto* mod : mods) {
|
2022-07-24 19:13:42 +01:00
|
|
|
auto hash_task = createNewHash(mod);
|
|
|
|
if (!hash_task)
|
2022-06-16 15:45:29 +01:00
|
|
|
continue;
|
2023-06-18 21:27:26 +01:00
|
|
|
connect(hash_task.get(), &Hashing::Hasher::getResults, [this, mod](QString hash) { m_mods.insert(hash, mod); });
|
|
|
|
connect(hash_task.get(), &Task::failed, [this, mod] { emitFail(mod, "", RemoveFromList::No); });
|
2022-07-24 19:13:42 +01:00
|
|
|
m_hashing_task->addTask(hash_task);
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-07-24 19:13:42 +01:00
|
|
|
Hashing::Hasher::Ptr EnsureMetadataTask::createNewHash(Mod* mod)
|
2022-06-16 15:45:29 +01:00
|
|
|
{
|
2022-08-28 18:47:52 +01:00
|
|
|
if (!mod || !mod->valid() || mod->type() == ResourceType::FOLDER)
|
2022-07-24 19:13:42 +01:00
|
|
|
return nullptr;
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-07-24 19:13:42 +01:00
|
|
|
return Hashing::createHasher(mod->fileinfo().absoluteFilePath(), m_provider);
|
|
|
|
}
|
2022-06-16 15:45:29 +01:00
|
|
|
|
2022-07-24 19:13:42 +01:00
|
|
|
QString EnsureMetadataTask::getExistingHash(Mod* mod)
|
|
|
|
{
|
|
|
|
// Check for already computed hashes
|
|
|
|
// (linear on the number of mods vs. linear on the size of the mod's JAR)
|
|
|
|
auto it = m_mods.keyValueBegin();
|
|
|
|
while (it != m_mods.keyValueEnd()) {
|
|
|
|
if ((*it).second == mod)
|
2022-07-24 03:13:53 +01:00
|
|
|
break;
|
2022-07-24 19:13:42 +01:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already have the hash computed
|
|
|
|
if (it != m_mods.keyValueEnd()) {
|
|
|
|
return (*it).first;
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 19:13:42 +01:00
|
|
|
// No existing hash
|
|
|
|
return {};
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:17:15 +01:00
|
|
|
for (auto* mod : m_mods) {
|
2022-07-17 15:43:12 +01:00
|
|
|
if (!mod->valid()) {
|
|
|
|
qDebug() << "Mod" << mod->name() << "is invalid!";
|
|
|
|
emitFail(mod);
|
2022-06-16 15:45:29 +01:00
|
|
|
continue;
|
2022-07-17 15:43:12 +01:00
|
|
|
}
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
// They already have the right metadata :o
|
2022-06-26 18:17:15 +01:00
|
|
|
if (mod->status() != ModStatus::NoMetadata && mod->metadata() && mod->metadata()->provider == m_provider) {
|
|
|
|
qDebug() << "Mod" << mod->name() << "already has metadata!";
|
2022-06-16 15:45:29 +01:00
|
|
|
emitReady(mod);
|
2022-07-17 15:43:12 +01:00
|
|
|
continue;
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Folders don't have metadata
|
2022-08-09 05:53:50 +01:00
|
|
|
if (mod->type() == ResourceType::FOLDER) {
|
2022-06-16 15:45:29 +01:00
|
|
|
emitReady(mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr version_task;
|
2022-06-03 23:08:01 +01:00
|
|
|
|
|
|
|
switch (m_provider) {
|
2022-11-25 12:23:46 +00:00
|
|
|
case (ModPlatform::ResourceProvider::MODRINTH):
|
2022-06-16 15:45:29 +01:00
|
|
|
version_task = modrinthVersionsTask();
|
2022-06-03 23:08:01 +01:00
|
|
|
break;
|
2022-11-25 12:23:46 +00:00
|
|
|
case (ModPlatform::ResourceProvider::FLAME):
|
2022-06-16 15:45:29 +01:00
|
|
|
version_task = flameVersionsTask();
|
2022-06-03 23:08:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
auto invalidade_leftover = [this] {
|
2022-08-05 20:23:46 +01:00
|
|
|
for (auto mod = m_mods.constBegin(); mod != m_mods.constEnd(); mod++)
|
|
|
|
emitFail(mod.value(), mod.key(), RemoveFromList::No);
|
|
|
|
m_mods.clear();
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(version_task.get(), &Task::finished, this, [this, invalidade_leftover] {
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr project_task;
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
switch (m_provider) {
|
2022-11-25 12:23:46 +00:00
|
|
|
case (ModPlatform::ResourceProvider::MODRINTH):
|
2022-06-16 15:45:29 +01:00
|
|
|
project_task = modrinthProjectsTask();
|
|
|
|
break;
|
2022-11-25 12:23:46 +00:00
|
|
|
case (ModPlatform::ResourceProvider::FLAME):
|
2022-06-16 15:45:29 +01:00
|
|
|
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-03 23:08:01 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
m_current_task = project_task;
|
2022-06-16 15:45:29 +01:00
|
|
|
project_task->start();
|
2022-06-03 23:08:01 +01:00
|
|
|
});
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
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 18:17:15 +01:00
|
|
|
.arg(ProviderCaps.readableName(m_provider), m_mods.begin().value()->name()));
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
m_current_task = version_task;
|
2022-06-16 15:45:29 +01:00
|
|
|
version_task->start();
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:38:51 +01:00
|
|
|
void EnsureMetadataTask::emitReady(Mod* m, QString key, RemoveFromList remove)
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2022-08-02 19:38:51 +01:00
|
|
|
if (!m) {
|
|
|
|
qCritical() << "Tried to mark a null mod as ready.";
|
|
|
|
if (!key.isEmpty())
|
|
|
|
m_mods.remove(key);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
qDebug() << QString("Generated metadata for %1").arg(m->name());
|
2022-06-16 15:45:29 +01:00
|
|
|
emit metadataReady(m);
|
|
|
|
|
2022-08-02 19:38:51 +01:00
|
|
|
if (remove == RemoveFromList::Yes) {
|
|
|
|
if (key.isEmpty())
|
|
|
|
key = getExistingHash(m);
|
|
|
|
m_mods.remove(key);
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:38:51 +01:00
|
|
|
void EnsureMetadataTask::emitFail(Mod* m, QString key, RemoveFromList remove)
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2022-08-02 19:38:51 +01:00
|
|
|
if (!m) {
|
|
|
|
qCritical() << "Tried to mark a null mod as failed.";
|
|
|
|
if (!key.isEmpty())
|
|
|
|
m_mods.remove(key);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
qDebug() << QString("Failed to generate metadata for %1").arg(m->name());
|
2022-06-16 15:45:29 +01:00
|
|
|
emit metadataFailed(m);
|
|
|
|
|
2022-08-02 19:38:51 +01:00
|
|
|
if (remove == RemoveFromList::Yes) {
|
|
|
|
if (key.isEmpty())
|
|
|
|
key = getExistingHash(m);
|
|
|
|
m_mods.remove(key);
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
// Modrinth
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr EnsureMetadataTask::modrinthVersionsTask()
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2022-11-25 12:23:46 +00:00
|
|
|
auto hash_type = ProviderCaps.hashType(ModPlatform::ResourceProvider::MODRINTH).first();
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-06-16 15:45:29 +01:00
|
|
|
auto ver_task = modrinth_api.currentVersions(m_mods.keys(), hash_type, response);
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
// Prevents unfortunate timings when aborting the task
|
|
|
|
if (!ver_task)
|
2023-06-15 20:59:41 +01:00
|
|
|
return Task::Ptr{ nullptr };
|
2022-06-16 15:45:29 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
connect(ver_task.get(), &Task::succeeded, this, [this, response] {
|
2022-06-16 15:45:29 +01:00
|
|
|
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 18:17:15 +01:00
|
|
|
setStatus(tr("Parsing API response from Modrinth for '%1'...").arg(mod->name()));
|
|
|
|
qDebug() << "Getting version for" << mod->name() << "from Modrinth";
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
m_temp_versions.insert(hash, Modrinth::loadIndexedPackVersion(entry));
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
2022-06-16 15:45:29 +01:00
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return ver_task;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr EnsureMetadataTask::modrinthProjectsTask()
|
2022-06-16 15:45:29 +01:00
|
|
|
{
|
|
|
|
QHash<QString, QString> addonIds;
|
|
|
|
for (auto const& data : m_temp_versions)
|
|
|
|
addonIds.insert(data.addonId.toString(), data.hash);
|
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr proj_task;
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
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)
|
2023-06-15 20:59:41 +01:00
|
|
|
return Task::Ptr{ nullptr };
|
2022-06-16 15:45:29 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
connect(proj_task.get(), &Task::succeeded, this, [this, response, addonIds] {
|
2022-06-16 15:45:29 +01:00
|
|
|
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-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
QJsonArray entries;
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
try {
|
|
|
|
if (addonIds.size() == 1)
|
|
|
|
entries = { doc.object() };
|
|
|
|
else
|
|
|
|
entries = Json::requireArray(doc);
|
2023-01-13 19:49:21 +00:00
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
for (auto entry : entries) {
|
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
|
|
|
|
try {
|
2022-06-16 15:45:29 +01:00
|
|
|
auto entry_obj = Json::requireObject(entry);
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
Modrinth::loadIndexedPack(pack, entry_obj);
|
2023-01-13 19:49:21 +00:00
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
// Skip this entry, since it has problems
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
auto hash = addonIds.find(pack.addonId.toString()).value();
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
auto mod_iter = m_mods.find(hash);
|
|
|
|
if (mod_iter == m_mods.end()) {
|
|
|
|
qWarning() << "Invalid project id from the API response.";
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-26 18:17:15 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
auto* mod = mod_iter.value();
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
try {
|
|
|
|
setStatus(tr("Parsing API response from Modrinth for '%1'...").arg(mod->name()));
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2023-01-13 19:49:21 +00:00
|
|
|
modrinthCallback(pack, m_temp_versions.find(hash).value(), mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
|
|
|
|
|
|
|
emitFail(mod);
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return proj_task;
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
// Flame
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr EnsureMetadataTask::flameVersionsTask()
|
2022-06-03 23:08:01 +01:00
|
|
|
{
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-07-10 19:15:25 +01:00
|
|
|
QList<uint> fingerprints;
|
2022-06-16 15:45:29 +01:00
|
|
|
for (auto& murmur : m_mods.keys()) {
|
|
|
|
fingerprints.push_back(murmur.toUInt());
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
|
|
|
auto ver_task = flame_api.matchFingerprints(fingerprints, response);
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
connect(ver_task.get(), &Task::succeeded, this, [this, response] {
|
2022-06-03 23:08:01 +01:00
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
2022-06-16 15:45:29 +01:00
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth::CurrentVersions at " << parse_error.offset
|
2022-06-03 23:08:01 +01:00
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
failed(parse_error.errorString());
|
2022-06-03 23:08:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
auto doc_obj = Json::requireObject(doc);
|
2022-06-16 15:45:29 +01:00
|
|
|
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-03 23:08:01 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
for (auto match : data_arr) {
|
|
|
|
auto match_obj = Json::ensureObject(match, {});
|
|
|
|
auto file_obj = Json::ensureObject(match_obj, "file", {});
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
if (match_obj.isEmpty() || file_obj.isEmpty()) {
|
|
|
|
qWarning() << "Fingerprint match is empty!";
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
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-03 23:08:01 +01:00
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg((*mod)->name()));
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
m_temp_versions.insert(fingerprint, FlameMod::loadIndexedPackVersion(file_obj));
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
|
|
|
}
|
|
|
|
});
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return ver_task;
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr EnsureMetadataTask::flameProjectsTask()
|
2022-06-16 15:45:29 +01:00
|
|
|
{
|
|
|
|
QHash<QString, QString> addonIds;
|
|
|
|
for (auto const& hash : m_mods.keys()) {
|
|
|
|
if (m_temp_versions.contains(hash)) {
|
2023-06-15 20:59:41 +01:00
|
|
|
auto data = m_temp_versions.find(hash).value();
|
2022-06-26 18:17:15 +01:00
|
|
|
|
|
|
|
auto id_str = data.addonId.toString();
|
|
|
|
if (!id_str.isEmpty())
|
|
|
|
addonIds.insert(data.addonId.toString(), hash);
|
2022-06-16 15:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2023-01-03 16:58:27 +00:00
|
|
|
Task::Ptr proj_task;
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
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)
|
2023-06-15 20:59:41 +01:00
|
|
|
return Task::Ptr{ nullptr };
|
2022-06-16 15:45:29 +01:00
|
|
|
|
2023-01-03 16:58:27 +00:00
|
|
|
connect(proj_task.get(), &Task::succeeded, this, [this, response, addonIds] {
|
2022-06-16 15:45:29 +01:00
|
|
|
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 18:17:15 +01:00
|
|
|
setStatus(tr("Parsing API response from CurseForge for '%1'...").arg(mod->name()));
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
FlameMod::loadIndexedPack(pack, entry_obj);
|
2022-06-03 23:08:01 +01:00
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
flameCallback(pack, m_temp_versions.find(hash).value(), mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << entries;
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
} catch (Json::JsonException& e) {
|
2022-06-16 15:45:29 +01:00
|
|
|
qDebug() << e.cause();
|
|
|
|
qDebug() << doc;
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-16 15:45:29 +01:00
|
|
|
return proj_task;
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
void EnsureMetadataTask::modrinthCallback(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, Mod* mod)
|
2022-06-16 15:45:29 +01:00
|
|
|
{
|
|
|
|
// Prevent file name mismatch
|
2022-06-26 18:17:15 +01:00
|
|
|
ver.fileName = mod->fileinfo().fileName();
|
2022-06-20 12:55:35 +01:00
|
|
|
if (ver.fileName.endsWith(".disabled"))
|
|
|
|
ver.fileName.chop(9);
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
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 18:17:15 +01:00
|
|
|
mod->setMetadata(metadata);
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
emitReady(mod);
|
|
|
|
}
|
|
|
|
|
2022-06-26 18:17:15 +01:00
|
|
|
void EnsureMetadataTask::flameCallback(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, Mod* mod)
|
2022-06-16 15:45:29 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Prevent file name mismatch
|
2022-06-26 18:17:15 +01:00
|
|
|
ver.fileName = mod->fileinfo().fileName();
|
2022-06-20 12:55:35 +01:00
|
|
|
if (ver.fileName.endsWith(".disabled"))
|
|
|
|
ver.fileName.chop(9);
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
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 18:17:15 +01:00
|
|
|
mod->setMetadata(metadata);
|
2022-06-16 15:45:29 +01:00
|
|
|
|
|
|
|
emitReady(mod);
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
qDebug() << e.cause();
|
|
|
|
|
|
|
|
emitFail(mod);
|
|
|
|
}
|
2022-06-03 23:08:01 +01:00
|
|
|
}
|