diff options
author | Sefa Eyeoglu <contact@scrumplex.net> | 2022-08-28 11:03:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-28 11:03:12 +0200 |
commit | afcd669d2f6934c2b6076939d7665f791d495994 (patch) | |
tree | b5868b4e26fc52a6b48443847fbd7ef0aafee908 /launcher/modplatform/helpers/HashUtils.cpp | |
parent | fbf542d2051576ee25556c3b28112eea094da309 (diff) | |
parent | 7b27f200b1f131f0ea3b23433974cbe68eb979bb (diff) | |
download | PrismLauncher-afcd669d2f6934c2b6076939d7665f791d495994.tar.gz PrismLauncher-afcd669d2f6934c2b6076939d7665f791d495994.tar.bz2 PrismLauncher-afcd669d2f6934c2b6076939d7665f791d495994.zip |
Merge pull request #965 from flowln/fat_files_in_memory
Refactor a bit EnsureMetadataTask and calculate hashes in a incremental manner
Diffstat (limited to 'launcher/modplatform/helpers/HashUtils.cpp')
-rw-r--r-- | launcher/modplatform/helpers/HashUtils.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/launcher/modplatform/helpers/HashUtils.cpp b/launcher/modplatform/helpers/HashUtils.cpp new file mode 100644 index 00000000..a7bbaba5 --- /dev/null +++ b/launcher/modplatform/helpers/HashUtils.cpp @@ -0,0 +1,81 @@ +#include "HashUtils.h" + +#include <QDebug> +#include <QFile> + +#include "FileSystem.h" + +#include <MurmurHash2.h> + +namespace Hashing { + +static ModPlatform::ProviderCapabilities ProviderCaps; + +Hasher::Ptr createHasher(QString file_path, ModPlatform::Provider provider) +{ + switch (provider) { + case ModPlatform::Provider::MODRINTH: + return createModrinthHasher(file_path); + case ModPlatform::Provider::FLAME: + return createFlameHasher(file_path); + default: + qCritical() << "[Hashing]" + << "Unrecognized mod platform!"; + return nullptr; + } +} + +Hasher::Ptr createModrinthHasher(QString file_path) +{ + return new ModrinthHasher(file_path); +} + +Hasher::Ptr createFlameHasher(QString file_path) +{ + return new FlameHasher(file_path); +} + +void ModrinthHasher::executeTask() +{ + QFile file(m_path); + + try { + file.open(QFile::ReadOnly); + } catch (FS::FileSystemException& e) { + qCritical() << QString("Failed to open JAR file in %1").arg(m_path); + qCritical() << QString("Reason: ") << e.cause(); + + emitFailed("Failed to open file for hashing."); + return; + } + + auto hash_type = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH).first(); + m_hash = ProviderCaps.hash(ModPlatform::Provider::MODRINTH, &file, hash_type); + + file.close(); + + if (m_hash.isEmpty()) { + emitFailed("Empty hash!"); + } else { + emitSucceeded(); + } +} + +void FlameHasher::executeTask() +{ + // CF-specific + auto should_filter_out = [](char c) { return (c == 9 || c == 10 || c == 13 || c == 32); }; + + std::ifstream file_stream(m_path.toStdString(), std::ifstream::binary); + // TODO: This is very heavy work, but apparently QtConcurrent can't use move semantics, so we can't boop this to another thread. + // How do we make this non-blocking then? + m_hash = QString::number(MurmurHash2(std::move(file_stream), 4 * MiB, should_filter_out)); + + if (m_hash.isEmpty()) { + emitFailed("Empty hash!"); + } else { + emitSucceeded(); + } +} + +} // namespace Hashing |