From 15ec1abb6a3acb77b36f14d3ddccc97a8df8c8e1 Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 23 Jul 2022 23:13:53 -0300 Subject: [PATCH] feat: use QIODevice for calcuating the JAR hash on Modrinth Signed-off-by: flow --- launcher/modplatform/EnsureMetadataTask.cpp | 30 ++++++++++++------- .../modrinth/ModrinthCheckUpdate.cpp | 8 +++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/launcher/modplatform/EnsureMetadataTask.cpp b/launcher/modplatform/EnsureMetadataTask.cpp index 60c54c4e..a5c9cbca 100644 --- a/launcher/modplatform/EnsureMetadataTask.cpp +++ b/launcher/modplatform/EnsureMetadataTask.cpp @@ -50,21 +50,29 @@ EnsureMetadataTask::EnsureMetadataTask(QList& mods, QDir dir, ModPlatform: QString EnsureMetadataTask::getHash(Mod* mod) { /* Here we create a mapping hash -> mod, because we need that relationship to parse the API routes */ - QByteArray jar_data; - try { - jar_data = FS::read(mod->fileinfo().absoluteFilePath()); - } catch (FS::FileSystemException& e) { - qCritical() << QString("Failed to open / read JAR file of %1").arg(mod->name()); - qCritical() << QString("Reason: ") << e.cause(); - + if (mod->type() == Mod::MOD_FOLDER) return {}; - } + QString result; switch (m_provider) { case ModPlatform::Provider::MODRINTH: { - auto hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first(); + QFile file(mod->fileinfo().absoluteFilePath()); - return QString(ProviderCaps.hash(ModPlatform::Provider::MODRINTH, jar_data, hash_type).toHex()); + try { + file.open(QFile::ReadOnly); + } catch (FS::FileSystemException& e) { + qCritical() << QString("Failed to open JAR file of %1").arg(mod->name()); + qCritical() << QString("Reason: ") << e.cause(); + + return {}; + } + + auto hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first(); + result = ProviderCaps.hash(ModPlatform::Provider::MODRINTH, &file, hash_type); + + file.close(); + + break; } case ModPlatform::Provider::FLAME: { QByteArray jar_data_treated; @@ -78,7 +86,7 @@ QString EnsureMetadataTask::getHash(Mod* mod) } } - return {}; + return result; } bool EnsureMetadataTask::abort() diff --git a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp index 79d8edf7..f4898591 100644 --- a/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp +++ b/launcher/modplatform/modrinth/ModrinthCheckUpdate.cpp @@ -46,17 +46,19 @@ void ModrinthCheckUpdate::executeTask() if (mod->metadata()->hash_format != best_hash_type) { QByteArray jar_data; + QFile file(mod->fileinfo().absoluteFilePath()); try { - jar_data = FS::read(mod->fileinfo().absoluteFilePath()); + file.open(QFile::ReadOnly); } catch (FS::FileSystemException& e) { - qCritical() << QString("Failed to open / read JAR file of %1").arg(mod->name()); + qCritical() << QString("Failed to open JAR file of %1").arg(mod->name()); qCritical() << QString("Reason: ") << e.cause(); failed(e.what()); return; } - hash = QString(ProviderCaps.hash(ModPlatform::Provider::MODRINTH, jar_data, best_hash_type).toHex()); + hash = ProviderCaps.hash(ModPlatform::Provider::MODRINTH, &file, best_hash_type); + file.close(); } hashes.append(hash);