feat: allow trying to use multiple hash types

This commit is contained in:
flow 2022-05-06 12:42:01 -03:00 committed by flow
parent 5c5699bba5
commit 59d628208b
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
5 changed files with 68 additions and 19 deletions

View File

@ -1,5 +1,7 @@
#include "modplatform/ModIndex.h" #include "modplatform/ModIndex.h"
#include <QCryptographicHash>
namespace ModPlatform { namespace ModPlatform {
auto ProviderCapabilities::name(Provider p) -> const char* auto ProviderCapabilities::name(Provider p) -> const char*
@ -12,13 +14,45 @@ auto ProviderCapabilities::name(Provider p) -> const char*
} }
return {}; return {};
} }
auto ProviderCapabilities::hashType(Provider p) -> QString auto ProviderCapabilities::readableName(Provider p) -> QString
{ {
switch (p) { switch (p) {
case Provider::MODRINTH: case Provider::MODRINTH:
return "sha512"; return "Modrinth";
case Provider::FLAME: case Provider::FLAME:
return "murmur2"; return "CurseForge";
}
return {};
}
auto ProviderCapabilities::hashType(Provider p) -> QStringList
{
switch (p) {
case Provider::MODRINTH:
return { "sha512", "sha1" };
case Provider::FLAME:
return { "murmur2" };
}
return {};
}
auto ProviderCapabilities::hash(Provider p, QByteArray& data, QString type) -> QByteArray
{
switch (p) {
case Provider::MODRINTH: {
// NOTE: Data is the result of reading the entire JAR file!
// If 'type' was specified, we use that
if (!type.isEmpty() && hashType(p).contains(type)) {
if (type == "sha512")
return QCryptographicHash::hash(data, QCryptographicHash::Sha512);
else if (type == "sha1")
return QCryptographicHash::hash(data, QCryptographicHash::Sha1);
}
return QCryptographicHash::hash(data, QCryptographicHash::Sha512);
}
case Provider::FLAME:
// TODO
break;
} }
return {}; return {};
} }

View File

@ -16,7 +16,9 @@ enum class Provider {
class ProviderCapabilities { class ProviderCapabilities {
public: public:
auto name(Provider) -> const char*; auto name(Provider) -> const char*;
auto hashType(Provider) -> QString; auto readableName(Provider) -> QString;
auto hashType(Provider) -> QStringList;
auto hash(Provider, QByteArray&, QString type = "") -> QByteArray;
}; };
struct ModpackAuthor { struct ModpackAuthor {
@ -33,6 +35,7 @@ struct IndexedVersion {
QString date; QString date;
QString fileName; QString fileName;
QVector<QString> loaders = {}; QVector<QString> loaders = {};
QString hash_type;
QString hash; QString hash;
}; };

View File

@ -64,8 +64,14 @@ void FlameMod::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
auto hash_list = Json::ensureArray(obj, "hashes"); auto hash_list = Json::ensureArray(obj, "hashes");
if(!hash_list.isEmpty()){ if(!hash_list.isEmpty()){
if(hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::FLAME))) auto hash_types = ProviderCaps.hashType(ModPlatform::Provider::FLAME);
for(auto& hash_type : hash_types) {
if(hash_list.contains(hash_type)) {
file.hash = Json::requireString(hash_list, "value"); file.hash = Json::requireString(hash_list, "value");
file.hash_type = hash_type;
break;
}
}
} }
unsortedVersions.append(file); unsortedVersions.append(file);

View File

@ -116,8 +116,14 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedV
file.downloadUrl = Json::requireString(parent, "url"); file.downloadUrl = Json::requireString(parent, "url");
file.fileName = Json::requireString(parent, "filename"); file.fileName = Json::requireString(parent, "filename");
auto hash_list = Json::requireObject(parent, "hashes"); auto hash_list = Json::requireObject(parent, "hashes");
if (hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::MODRINTH))) auto hash_types = ProviderCaps.hashType(ModPlatform::Provider::MODRINTH);
file.hash = Json::requireString(hash_list, ProviderCaps.hashType(ModPlatform::Provider::MODRINTH)); for (auto& hash_type : hash_types) {
if (hash_list.contains(hash_type)) {
file.hash = Json::requireString(hash_list, hash_type);
file.hash_type = hash_type;
break;
}
}
return file; return file;
} }

View File

@ -29,7 +29,7 @@ auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, Mo
mod.filename = mod_version.fileName; mod.filename = mod_version.fileName;
mod.url = mod_version.downloadUrl; mod.url = mod_version.downloadUrl;
mod.hash_format = ProviderCaps.hashType(mod_pack.provider); mod.hash_format = mod_version.hash_type;
mod.hash = mod_version.hash; mod.hash = mod_version.hash;
mod.provider = mod_pack.provider; mod.provider = mod_pack.provider;