2022-07-24 23:41:41 +05:30
|
|
|
#include "HashUtils.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include "FileSystem.h"
|
2022-11-07 22:34:48 +05:30
|
|
|
#include "StringUtils.h"
|
2022-07-24 23:41:41 +05:30
|
|
|
|
|
|
|
#include <MurmurHash2.h>
|
|
|
|
|
|
|
|
namespace Hashing {
|
|
|
|
|
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
Hasher::Ptr createHasher(QString file_path, ModPlatform::ResourceProvider provider)
|
2022-07-24 23:41:41 +05:30
|
|
|
{
|
|
|
|
switch (provider) {
|
2022-11-25 17:53:46 +05:30
|
|
|
case ModPlatform::ResourceProvider::MODRINTH:
|
2022-07-24 23:41:41 +05:30
|
|
|
return createModrinthHasher(file_path);
|
2022-11-25 17:53:46 +05:30
|
|
|
case ModPlatform::ResourceProvider::FLAME:
|
2022-07-24 23:41:41 +05:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
Hasher::Ptr createBlockedModHasher(QString file_path, ModPlatform::ResourceProvider provider)
|
2022-10-25 13:49:19 +05:30
|
|
|
{
|
|
|
|
return new BlockedModHasher(file_path, provider);
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
Hasher::Ptr createBlockedModHasher(QString file_path, ModPlatform::ResourceProvider provider, QString type)
|
2022-10-25 13:49:19 +05:30
|
|
|
{
|
|
|
|
auto hasher = new BlockedModHasher(file_path, provider);
|
|
|
|
hasher->useHashType(type);
|
|
|
|
return hasher;
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:41:41 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
auto hash_type = ProviderCaps.hashType(ModPlatform::ResourceProvider::MODRINTH).first();
|
|
|
|
m_hash = ProviderCaps.hash(ModPlatform::ResourceProvider::MODRINTH, &file, hash_type);
|
2022-07-24 23:41:41 +05:30
|
|
|
|
|
|
|
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); };
|
|
|
|
|
2022-11-07 22:34:48 +05:30
|
|
|
std::ifstream file_stream(StringUtils::toStdString(m_path), std::ifstream::binary);
|
2022-07-24 23:41:41 +05:30
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-25 17:53:46 +05:30
|
|
|
BlockedModHasher::BlockedModHasher(QString file_path, ModPlatform::ResourceProvider provider)
|
2022-10-25 13:49:19 +05:30
|
|
|
: Hasher(file_path), provider(provider) {
|
|
|
|
setObjectName(QString("BlockedModHasher: %1").arg(file_path));
|
|
|
|
hash_type = ProviderCaps.hashType(provider).first();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockedModHasher::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_hash = ProviderCaps.hash(provider, &file, hash_type);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
if (m_hash.isEmpty()) {
|
|
|
|
emitFailed("Empty hash!");
|
|
|
|
} else {
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList BlockedModHasher::getHashTypes() {
|
|
|
|
return ProviderCaps.hashType(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockedModHasher::useHashType(QString type) {
|
|
|
|
auto types = ProviderCaps.hashType(provider);
|
|
|
|
if (types.contains(type)) {
|
|
|
|
hash_type = type;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
qDebug() << "Bad hash type " << type << " for provider";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-24 23:41:41 +05:30
|
|
|
} // namespace Hashing
|