fix: use a more robust method of finding metadata indexes

Often times, mods can have their name in different forms, changing one
letter to caps or the other way (e.g. JourneyMaps -> Journeymaps).
This makes it possible to find those as well, which is not perfect by
any means, but should suffice for the majority of cases.
This commit is contained in:
flow 2022-05-18 05:46:07 -03:00
parent 42f8ec5b14
commit 5a1de15332
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 80 additions and 36 deletions

View File

@ -23,12 +23,37 @@
#include <QObject> #include <QObject>
#include "toml.h" #include "toml.h"
#include "FileSystem.h"
#include "minecraft/mod/Mod.h" #include "minecraft/mod/Mod.h"
#include "modplatform/ModIndex.h" #include "modplatform/ModIndex.h"
namespace Packwiz { namespace Packwiz {
auto getRealIndexName(QDir& index_dir, QString normalized_fname, bool should_find_match) -> QString
{
QFile index_file(index_dir.absoluteFilePath(normalized_fname));
QString real_fname = normalized_fname;
if (!index_file.exists()) {
// Tries to get similar entries
for (auto& file_name : index_dir.entryList(QDir::Filter::Files)) {
if (!QString::compare(normalized_fname, file_name, Qt::CaseInsensitive)) {
real_fname = file_name;
break;
}
}
if(should_find_match && !QString::compare(normalized_fname, real_fname, Qt::CaseSensitive)){
qCritical() << "Could not find a match for a valid metadata file!";
qCritical() << "File: " << normalized_fname;
return {};
}
}
return real_fname;
}
// Helpers // Helpers
static inline auto indexFileName(QString const& mod_name) -> QString static inline auto indexFileName(QString const& mod_name) -> QString
{ {
@ -39,6 +64,33 @@ static inline auto indexFileName(QString const& mod_name) -> QString
static ModPlatform::ProviderCapabilities ProviderCaps; static ModPlatform::ProviderCapabilities ProviderCaps;
// Helper functions for extracting data from the TOML file
auto stringEntry(toml_table_t* parent, const char* entry_name) -> QString
{
toml_datum_t var = toml_string_in(parent, entry_name);
if (!var.ok) {
qCritical() << QString("Failed to read str property '%1' in mod metadata.").arg(entry_name);
return {};
}
QString tmp = var.u.s;
free(var.u.s);
return tmp;
}
auto intEntry(toml_table_t* parent, const char* entry_name) -> int
{
toml_datum_t var = toml_int_in(parent, entry_name);
if (!var.ok) {
qCritical() << QString("Failed to read int property '%1' in mod metadata.").arg(entry_name);
return {};
}
return var.u.i;
}
auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> Mod auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> Mod
{ {
Mod mod; Mod mod;
@ -86,7 +138,11 @@ void V1::updateModIndex(QDir& index_dir, Mod& mod)
} }
// Ensure the corresponding mod's info exists, and create it if not // Ensure the corresponding mod's info exists, and create it if not
QFile index_file(index_dir.absoluteFilePath(indexFileName(mod.name)));
auto normalized_fname = indexFileName(mod.name);
auto real_fname = getRealIndexName(index_dir, normalized_fname);
QFile index_file(index_dir.absoluteFilePath(real_fname));
// There's already data on there! // There's already data on there!
// TODO: We should do more stuff here, as the user is likely trying to // TODO: We should do more stuff here, as the user is likely trying to
@ -127,11 +183,18 @@ void V1::updateModIndex(QDir& index_dir, Mod& mod)
break; break;
} }
} }
index_file.close();
} }
void V1::deleteModIndex(QDir& index_dir, QString& mod_name) void V1::deleteModIndex(QDir& index_dir, QString& mod_name)
{ {
QFile index_file(index_dir.absoluteFilePath(indexFileName(mod_name))); auto normalized_fname = indexFileName(mod_name);
auto real_fname = getRealIndexName(index_dir, normalized_fname);
if (real_fname.isEmpty())
return;
QFile index_file(index_dir.absoluteFilePath(real_fname));
if(!index_file.exists()){ if(!index_file.exists()){
qWarning() << QString("Tried to delete non-existent mod metadata for %1!").arg(mod_name); qWarning() << QString("Tried to delete non-existent mod metadata for %1!").arg(mod_name);
@ -143,42 +206,17 @@ void V1::deleteModIndex(QDir& index_dir, QString& mod_name)
} }
} }
// Helper functions for extracting data from the TOML file
static auto stringEntry(toml_table_t* parent, const char* entry_name) -> QString
{
toml_datum_t var = toml_string_in(parent, entry_name);
if (!var.ok) {
qCritical() << QString("Failed to read str property '%1' in mod metadata.").arg(entry_name);
return {};
}
QString tmp = var.u.s;
free(var.u.s);
return tmp;
}
static auto intEntry(toml_table_t* parent, const char* entry_name) -> int
{
toml_datum_t var = toml_int_in(parent, entry_name);
if (!var.ok) {
qCritical() << QString("Failed to read int property '%1' in mod metadata.").arg(entry_name);
return {};
}
return var.u.i;
}
auto V1::getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod auto V1::getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod
{ {
Mod mod; Mod mod;
QFile index_file(index_dir.absoluteFilePath(indexFileName(index_file_name))); auto normalized_fname = indexFileName(index_file_name);
auto real_fname = getRealIndexName(index_dir, normalized_fname, true);
if (!index_file.exists()) { if (real_fname.isEmpty())
qWarning() << QString("Tried to get a non-existent mod metadata for %1").arg(index_file_name);
return {}; return {};
}
QFile index_file(index_dir.absoluteFilePath(real_fname));
if (!index_file.open(QIODevice::ReadOnly)) { if (!index_file.open(QIODevice::ReadOnly)) {
qWarning() << QString("Failed to open mod metadata for %1").arg(index_file_name); qWarning() << QString("Failed to open mod metadata for %1").arg(index_file_name);
return {}; return {};

View File

@ -24,6 +24,7 @@
#include <QUrl> #include <QUrl>
#include <QVariant> #include <QVariant>
struct toml_table_t;
class QDir; class QDir;
// Mod from launcher/minecraft/mod/Mod.h // Mod from launcher/minecraft/mod/Mod.h
@ -31,6 +32,11 @@ class Mod;
namespace Packwiz { namespace Packwiz {
auto getRealIndexName(QDir& index_dir, QString normalized_index_name, bool should_match = false) -> QString;
auto stringEntry(toml_table_t* parent, const char* entry_name) -> QString;
auto intEntry(toml_table_t* parent, const char* entry_name) -> int;
class V1 { class V1 {
public: public:
struct Mod { struct Mod {