2022-06-04 05:56:26 +05:30
|
|
|
#include "ModrinthCheckUpdate.h"
|
|
|
|
#include "ModrinthAPI.h"
|
|
|
|
#include "ModrinthPackIndex.h"
|
|
|
|
|
|
|
|
#include "Json.h"
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
#include "ResourceDownloadTask.h"
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2022-07-24 23:41:41 +05:30
|
|
|
#include "modplatform/helpers/HashUtils.h"
|
|
|
|
|
|
|
|
#include "tasks/ConcurrentTask.h"
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
#include "minecraft/mod/ModFolderModel.h"
|
|
|
|
#include "minecraft/mod/ResourceFolderModel.h"
|
|
|
|
|
2022-06-04 05:56:26 +05:30
|
|
|
static ModrinthAPI api;
|
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
|
|
|
bool ModrinthCheckUpdate::abort()
|
|
|
|
{
|
|
|
|
if (m_net_job)
|
|
|
|
return m_net_job->abort();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for update:
|
|
|
|
* - Get latest version available
|
|
|
|
* - Compare hash of the latest version with the current hash
|
|
|
|
* - If equal, no updates, else, there's updates, so add to the list
|
|
|
|
* */
|
|
|
|
void ModrinthCheckUpdate::executeTask()
|
|
|
|
{
|
|
|
|
setStatus(tr("Preparing mods for Modrinth..."));
|
|
|
|
setProgress(0, 3);
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
QHash<QString, Mod*> mappings;
|
2022-06-04 05:56:26 +05:30
|
|
|
|
|
|
|
// Create all hashes
|
|
|
|
QStringList hashes;
|
2022-11-25 17:53:46 +05:30
|
|
|
auto best_hash_type = ProviderCaps.hashType(ModPlatform::ResourceProvider::MODRINTH).first();
|
2022-07-24 23:41:41 +05:30
|
|
|
|
|
|
|
ConcurrentTask hashing_task(this, "MakeModrinthHashesTask", 10);
|
2022-06-26 22:47:15 +05:30
|
|
|
for (auto* mod : m_mods) {
|
|
|
|
if (!mod->enabled()) {
|
2022-06-20 17:25:35 +05:30
|
|
|
emit checkFailed(mod, tr("Disabled mods won't be updated, to prevent mod duplication issues!"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-26 22:47:15 +05:30
|
|
|
auto hash = mod->metadata()->hash;
|
2022-06-04 05:56:26 +05:30
|
|
|
|
|
|
|
// Sadly the API can only handle one hash type per call, se we
|
|
|
|
// need to generate a new hash if the current one is innadequate
|
|
|
|
// (though it will rarely happen, if at all)
|
2022-06-26 22:47:15 +05:30
|
|
|
if (mod->metadata()->hash_format != best_hash_type) {
|
2022-07-24 23:41:41 +05:30
|
|
|
auto hash_task = Hashing::createModrinthHasher(mod->fileinfo().absoluteFilePath());
|
|
|
|
connect(hash_task.get(), &Task::succeeded, [&] {
|
2023-06-03 02:34:06 +05:30
|
|
|
QString hash(hash_task->getResult());
|
2022-07-24 23:41:41 +05:30
|
|
|
hashes.append(hash);
|
|
|
|
mappings.insert(hash, mod);
|
|
|
|
});
|
|
|
|
connect(hash_task.get(), &Task::failed, [this, hash_task] { failed("Failed to generate hash"); });
|
|
|
|
hashing_task.addTask(hash_task);
|
|
|
|
} else {
|
|
|
|
hashes.append(hash);
|
|
|
|
mappings.insert(hash, mod);
|
2022-06-04 05:56:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:41:41 +05:30
|
|
|
QEventLoop loop;
|
2023-06-03 02:34:06 +05:30
|
|
|
connect(&hashing_task, &Task::finished, [&loop] { loop.quit(); });
|
2022-07-24 23:41:41 +05:30
|
|
|
hashing_task.start();
|
|
|
|
loop.exec();
|
|
|
|
|
2022-06-04 05:56:26 +05:30
|
|
|
auto* response = new QByteArray();
|
|
|
|
auto job = api.latestVersions(hashes, best_hash_type, m_game_versions, m_loaders, response);
|
|
|
|
|
|
|
|
QEventLoop lock;
|
|
|
|
|
|
|
|
connect(job.get(), &Task::succeeded, this, [this, response, &mappings, best_hash_type, job] {
|
|
|
|
QJsonParseError parse_error{};
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from ModrinthCheckUpdate at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
|
|
|
|
failed(parse_error.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatus(tr("Parsing the API response from Modrinth..."));
|
|
|
|
setProgress(2, 3);
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (auto hash : mappings.keys()) {
|
|
|
|
auto project_obj = doc[hash].toObject();
|
2022-06-05 00:52:46 +05:30
|
|
|
|
|
|
|
// If the returned project is empty, but we have Modrinth metadata,
|
|
|
|
// it means this specific version is not available
|
2022-06-04 05:56:26 +05:30
|
|
|
if (project_obj.isEmpty()) {
|
2022-06-26 22:47:15 +05:30
|
|
|
qDebug() << "Mod " << mappings.find(hash).value()->name() << " got an empty response.";
|
2022-06-04 05:56:26 +05:30
|
|
|
qDebug() << "Hash: " << hash;
|
|
|
|
|
2022-06-16 20:15:29 +05:30
|
|
|
emit checkFailed(
|
|
|
|
mappings.find(hash).value(),
|
|
|
|
tr("No valid version found for this mod. It's probably unavailable for the current game version / mod loader."));
|
2022-06-04 05:56:26 +05:30
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sometimes a version may have multiple files, one with "forge" and one with "fabric",
|
|
|
|
// so we may want to filter it
|
|
|
|
QString loader_filter;
|
2022-11-25 17:53:46 +05:30
|
|
|
if (m_loaders.has_value()) {
|
2023-06-03 02:34:06 +05:30
|
|
|
static auto flags = { ResourceAPI::ModLoaderType::Forge, ResourceAPI::ModLoaderType::Fabric,
|
|
|
|
ResourceAPI::ModLoaderType::Quilt };
|
2022-11-25 17:53:46 +05:30
|
|
|
for (auto flag : flags) {
|
|
|
|
if (m_loaders.value().testFlag(flag)) {
|
|
|
|
loader_filter = api.getModLoaderString(flag);
|
|
|
|
break;
|
|
|
|
}
|
2022-06-04 05:56:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, we rely on a couple heuristics to determine whether an update is actually available or not:
|
2023-06-03 02:34:06 +05:30
|
|
|
// - The file needs to be preferred: It is either the primary file, or the one found via (explicit) usage of the
|
|
|
|
// loader_filter
|
2022-06-04 05:56:26 +05:30
|
|
|
// - The version reported by the JAR is different from the version reported by the indexed version (it's usually the case)
|
|
|
|
// Such is the pain of having arbitrary files for a given version .-.
|
|
|
|
|
|
|
|
auto project_ver = Modrinth::loadIndexedPackVersion(project_obj, best_hash_type, loader_filter);
|
|
|
|
if (project_ver.downloadUrl.isEmpty()) {
|
|
|
|
qCritical() << "Modrinth mod without download url!";
|
|
|
|
qCritical() << project_ver.fileName;
|
|
|
|
|
|
|
|
emit checkFailed(mappings.find(hash).value(), tr("Mod has an empty download URL"));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mod_iter = mappings.find(hash);
|
|
|
|
if (mod_iter == mappings.end()) {
|
|
|
|
qCritical() << "Failed to remap mod from Modrinth!";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto mod = *mod_iter;
|
|
|
|
|
|
|
|
auto key = project_ver.hash;
|
2022-06-26 22:47:15 +05:30
|
|
|
if ((key != hash && project_ver.is_preferred) || (mod->status() == ModStatus::NotInstalled)) {
|
|
|
|
if (mod->version() == project_ver.version_number)
|
2022-06-04 05:56:26 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
// Fake pack with the necessary info to pass to the download task :)
|
2023-06-03 02:34:06 +05:30
|
|
|
auto pack = std::make_shared<ModPlatform::IndexedPack>();
|
|
|
|
pack->name = mod->name();
|
|
|
|
pack->slug = mod->metadata()->slug;
|
|
|
|
pack->addonId = mod->metadata()->project_id;
|
|
|
|
pack->websiteUrl = mod->homeurl();
|
2022-06-26 22:47:15 +05:30
|
|
|
for (auto& author : mod->authors())
|
2023-06-03 02:34:06 +05:30
|
|
|
pack->authors.append({ author });
|
|
|
|
pack->description = mod->description();
|
|
|
|
pack->provider = ModPlatform::ResourceProvider::MODRINTH;
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2023-01-25 01:22:09 +05:30
|
|
|
auto download_task = makeShared<ResourceDownloadTask>(pack, project_ver, m_mods_folder);
|
2022-06-04 05:56:26 +05:30
|
|
|
|
2023-06-03 02:34:06 +05:30
|
|
|
m_updatable.emplace_back(pack->name, hash, mod->version(), project_ver.version_number, project_ver.changelog,
|
2022-11-25 17:53:46 +05:30
|
|
|
ModPlatform::ResourceProvider::MODRINTH, download_task);
|
2022-06-04 05:56:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Json::JsonException& e) {
|
|
|
|
failed(e.cause() + " : " + e.what());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(job.get(), &Task::finished, &lock, &QEventLoop::quit);
|
|
|
|
|
|
|
|
setStatus(tr("Waiting for the API response from Modrinth..."));
|
|
|
|
setProgress(1, 3);
|
|
|
|
|
2023-01-03 22:28:27 +05:30
|
|
|
m_net_job = qSharedPointerObjectCast<NetJob, Task>(job);
|
2022-06-04 05:56:26 +05:30
|
|
|
job->start();
|
|
|
|
|
|
|
|
lock.exec();
|
|
|
|
|
|
|
|
emitSucceeded();
|
|
|
|
}
|