From bd570aa5d1d371675ecedcf8df1437bf155853e7 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Sat, 16 Nov 2024 00:54:36 +0200 Subject: [PATCH] fix:resource update with loaders Signed-off-by: Trial97 --- launcher/modplatform/flame/FlameAPI.cpp | 12 +++- launcher/modplatform/flame/FlameAPI.h | 3 +- .../modplatform/flame/FlameCheckUpdate.cpp | 2 +- .../modrinth/ModrinthCheckUpdate.cpp | 61 ++++++++++++++----- .../modrinth/ModrinthCheckUpdate.h | 8 +-- 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/launcher/modplatform/flame/FlameAPI.cpp b/launcher/modplatform/flame/FlameAPI.cpp index d1facfd23..b0d9af804 100644 --- a/launcher/modplatform/flame/FlameAPI.cpp +++ b/launcher/modplatform/flame/FlameAPI.cpp @@ -218,9 +218,19 @@ QList FlameAPI::loadModCategories(std::shared_ptr FlameAPI::getLatestVersion(QList versions, QList instanceLoaders, - ModPlatform::ModLoaderTypes modLoaders) + ModPlatform::ModLoaderTypes modLoaders, + bool checkLoaders) { static const auto noLoader = ModPlatform::ModLoaderType(0); + if (!checkLoaders) { + std::optional ver; + for (auto file_tmp : versions) { + if (!ver.has_value() || file_tmp.date > ver->date) { + ver = file_tmp; + } + } + return ver; + } QHash bestMatch; auto checkVersion = [&bestMatch](const ModPlatform::IndexedVersion& version, const ModPlatform::ModLoaderType& loader) { if (bestMatch.contains(loader)) { diff --git a/launcher/modplatform/flame/FlameAPI.h b/launcher/modplatform/flame/FlameAPI.h index f72bdb624..5b8f794e6 100644 --- a/launcher/modplatform/flame/FlameAPI.h +++ b/launcher/modplatform/flame/FlameAPI.h @@ -18,7 +18,8 @@ class FlameAPI : public NetworkResourceAPI { std::optional getLatestVersion(QList versions, QList instanceLoaders, - ModPlatform::ModLoaderTypes fallback); + ModPlatform::ModLoaderTypes fallback, + bool checkLoaders); Task::Ptr getProjects(QStringList addonIds, std::shared_ptr response) const override; Task::Ptr matchFingerprints(const QList& fingerprints, std::shared_ptr response); diff --git a/launcher/modplatform/flame/FlameCheckUpdate.cpp b/launcher/modplatform/flame/FlameCheckUpdate.cpp index 8f54ee0c6..17d13deda 100644 --- a/launcher/modplatform/flame/FlameCheckUpdate.cpp +++ b/launcher/modplatform/flame/FlameCheckUpdate.cpp @@ -87,7 +87,7 @@ void FlameCheckUpdate::getLatestVersionCallback(Resource* resource, std::shared_ qCritical() << e.what(); qDebug() << doc; } - auto latest_ver = api.getLatestVersion(pack->versions, m_loadersList, resource->metadata()->loaders); + auto latest_ver = api.getLatestVersion(pack->versions, m_loadersList, resource->metadata()->loaders, !m_loadersList.isEmpty()); setStatus(tr("Parsing the API response from CurseForge for '%1'...").arg(resource->name())); diff --git a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp index 041ffddb7..6683a0ed5 100644 --- a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp +++ b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp @@ -15,6 +15,26 @@ static ModrinthAPI api; +ModrinthCheckUpdate::ModrinthCheckUpdate(QList& resources, + std::list& mcVersions, + QList loadersList, + std::shared_ptr resourceModel) + : CheckUpdateTask(resources, mcVersions, std::move(loadersList), std::move(resourceModel)) + , m_hashType(ModPlatform::ProviderCapabilities::hashType(ModPlatform::ResourceProvider::MODRINTH).first()) +{ + if (!m_loadersList.isEmpty()) { // this is for mods so append all the other posible loaders to the initial list + m_initialSize = m_loadersList.length(); + ModPlatform::ModLoaderTypes modLoaders; + for (auto m : resources) { + modLoaders |= m->metadata()->loaders; + } + for (auto l : m_loadersList) { + modLoaders &= ~l; + } + m_loadersList.append(ModPlatform::modLoaderTypesToList(modLoaders)); + } +} + bool ModrinthCheckUpdate::abort() { if (m_job) @@ -34,6 +54,7 @@ void ModrinthCheckUpdate::executeTask() auto hashing_task = makeShared("MakeModrinthHashesTask", APPLICATION->settings()->get("NumberOfConcurrentTasks").toInt()); + bool startHasing = false; for (auto* resource : m_resources) { auto hash = resource->metadata()->hash; @@ -45,23 +66,37 @@ void ModrinthCheckUpdate::executeTask() connect(hash_task.get(), &Hashing::Hasher::resultsReady, [this, resource](QString hash) { m_mappings.insert(hash, resource); }); connect(hash_task.get(), &Task::failed, [this] { failed("Failed to generate hash"); }); hashing_task->addTask(hash_task); + startHasing = true; } else { m_mappings.insert(hash, resource); } } - connect(hashing_task.get(), &Task::finished, this, &ModrinthCheckUpdate::checkNextLoader); - m_job = hashing_task; - hashing_task->start(); + if (startHasing) { + connect(hashing_task.get(), &Task::finished, this, &ModrinthCheckUpdate::checkNextLoader); + m_job = hashing_task; + hashing_task->start(); + } else { + checkNextLoader(); + } } -void ModrinthCheckUpdate::getUpdateModsForLoader(std::optional loader) +void ModrinthCheckUpdate::getUpdateModsForLoader(std::optional loader, bool forceModLoaderCheck) { setStatus(tr("Waiting for the API response from Modrinth...")); setProgress(m_progress + 1, m_progressTotal); auto response = std::make_shared(); - QStringList hashes = m_mappings.keys(); + QStringList hashes; + if (forceModLoaderCheck && loader.has_value()) { + for (auto hash : m_mappings.keys()) { + if (m_mappings[hash]->metadata()->loaders & loader.value()) { + hashes.append(hash); + } + } + } else { + hashes = m_mappings.keys(); + } auto job = api.latestVersions(hashes, m_hashType, m_gameVersions, loader, response); connect(job.get(), &Task::succeeded, this, [this, response, loader] { checkVersionsResponse(response, loader); }); @@ -69,6 +104,7 @@ void ModrinthCheckUpdate::getUpdateModsForLoader(std::optionalstart(); } @@ -165,16 +201,11 @@ void ModrinthCheckUpdate::checkNextLoader() emitSucceeded(); return; } - - if (m_loadersList.isEmpty() && m_loaderIdx == 0) { - getUpdateModsForLoader({}); - m_loaderIdx++; + if (m_loaderIdx < m_loadersList.size()) { // this are mods so check with loades + getUpdateModsForLoader(m_loadersList.at(m_loaderIdx), m_loaderIdx > m_initialSize); return; - } - - if (m_loaderIdx < m_loadersList.size()) { - getUpdateModsForLoader(m_loadersList.at(m_loaderIdx)); - m_loaderIdx++; + } else if (m_loadersList.isEmpty() && m_loaderIdx == 0) { // this are other resources no need to check more than once with empty loader + getUpdateModsForLoader(); return; } @@ -192,4 +223,4 @@ void ModrinthCheckUpdate::checkNextLoader() } emitSucceeded(); -} +} \ No newline at end of file diff --git a/launcher/modplatform/modrinth/ModrinthCheckUpdate.h b/launcher/modplatform/modrinth/ModrinthCheckUpdate.h index bde61bb23..eb8057694 100644 --- a/launcher/modplatform/modrinth/ModrinthCheckUpdate.h +++ b/launcher/modplatform/modrinth/ModrinthCheckUpdate.h @@ -9,17 +9,14 @@ class ModrinthCheckUpdate : public CheckUpdateTask { ModrinthCheckUpdate(QList& resources, std::list& mcVersions, QList loadersList, - std::shared_ptr resourceModel) - : CheckUpdateTask(resources, mcVersions, std::move(loadersList), std::move(resourceModel)) - , m_hashType(ModPlatform::ProviderCapabilities::hashType(ModPlatform::ResourceProvider::MODRINTH).first()) - {} + std::shared_ptr resourceModel); public slots: bool abort() override; protected slots: void executeTask() override; - void getUpdateModsForLoader(std::optional loader); + void getUpdateModsForLoader(std::optional loader = {}, bool forceModLoaderCheck = false); void checkVersionsResponse(std::shared_ptr response, std::optional loader); void checkNextLoader(); @@ -28,4 +25,5 @@ class ModrinthCheckUpdate : public CheckUpdateTask { QHash m_mappings; QString m_hashType; int m_loaderIdx = 0; + int m_initialSize = 0; };