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,26 +1,60 @@
#include "modplatform/ModIndex.h"
namespace ModPlatform{
#include <QCryptographicHash>
namespace ModPlatform {
auto ProviderCapabilities::name(Provider p) -> const char*
{
switch(p){
case Provider::MODRINTH:
return "modrinth";
case Provider::FLAME:
return "curseforge";
switch (p) {
case Provider::MODRINTH:
return "modrinth";
case Provider::FLAME:
return "curseforge";
}
return {};
}
auto ProviderCapabilities::hashType(Provider p) -> QString
auto ProviderCapabilities::readableName(Provider p) -> QString
{
switch(p){
case Provider::MODRINTH:
return "sha512";
case Provider::FLAME:
return "murmur2";
switch (p) {
case Provider::MODRINTH:
return "Modrinth";
case Provider::FLAME:
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 {};
}
} // namespace ModPlatform
} // namespace ModPlatform

View File

@ -16,7 +16,9 @@ enum class Provider {
class ProviderCapabilities {
public:
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 {
@ -33,6 +35,7 @@ struct IndexedVersion {
QString date;
QString fileName;
QVector<QString> loaders = {};
QString hash_type;
QString hash;
};

View File

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

View File

@ -116,8 +116,14 @@ auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedV
file.downloadUrl = Json::requireString(parent, "url");
file.fileName = Json::requireString(parent, "filename");
auto hash_list = Json::requireObject(parent, "hashes");
if (hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::MODRINTH)))
file.hash = Json::requireString(hash_list, ProviderCaps.hashType(ModPlatform::Provider::MODRINTH));
auto hash_types = 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;
}

View File

@ -29,7 +29,7 @@ auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, Mo
mod.filename = mod_version.fileName;
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.provider = mod_pack.provider;