change: mod metadata improvements

- Use slug instead of name
- Keep temporary status before having local details

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-06-19 14:26:15 -03:00
parent a53ee2e35c
commit fd6755c93f
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
10 changed files with 96 additions and 67 deletions

View File

@ -47,10 +47,9 @@ void ModDownloadTask::downloadSucceeded()
{ {
m_filesNetJob.reset(); m_filesNetJob.reset();
auto name = std::get<0>(to_delete); auto name = std::get<0>(to_delete);
if (!name.isEmpty()) { auto filename = std::get<1>(to_delete);
// If they have the same name, we keep the metadata. if (!name.isEmpty() && filename != m_mod_version.fileName) {
// This is a workaround for mods that change names between versions ;c mods->uninstallMod(filename, true);
mods->uninstallMod(std::get<1>(to_delete), name == m_mod.name);
} }
} }

View File

@ -37,9 +37,9 @@ class Metadata {
return Packwiz::V1::createModFormat(index_dir, mod_pack, mod_version); return Packwiz::V1::createModFormat(index_dir, mod_pack, mod_version);
} }
static auto create(QDir& index_dir, Mod& internal_mod) -> ModStruct static auto create(QDir& index_dir, Mod& internal_mod, QString mod_slug) -> ModStruct
{ {
return Packwiz::V1::createModFormat(index_dir, internal_mod); return Packwiz::V1::createModFormat(index_dir, internal_mod, mod_slug);
} }
static void update(QDir& index_dir, ModStruct& mod) static void update(QDir& index_dir, ModStruct& mod)
@ -47,9 +47,9 @@ class Metadata {
Packwiz::V1::updateModIndex(index_dir, mod); Packwiz::V1::updateModIndex(index_dir, mod);
} }
static void remove(QDir& index_dir, QString& mod_name) static void remove(QDir& index_dir, QString mod_slug)
{ {
Packwiz::V1::deleteModIndex(index_dir, mod_name); Packwiz::V1::deleteModIndex(index_dir, mod_slug);
} }
static void remove(QDir& index_dir, QVariant& mod_id) static void remove(QDir& index_dir, QVariant& mod_id)
@ -57,9 +57,9 @@ class Metadata {
Packwiz::V1::deleteModIndex(index_dir, mod_id); Packwiz::V1::deleteModIndex(index_dir, mod_id);
} }
static auto get(QDir& index_dir, QString& mod_name) -> ModStruct static auto get(QDir& index_dir, QString mod_slug) -> ModStruct
{ {
return Packwiz::V1::getIndexForMod(index_dir, mod_name); return Packwiz::V1::getIndexForMod(index_dir, mod_slug);
} }
static auto get(QDir& index_dir, QVariant& mod_id) -> ModStruct static auto get(QDir& index_dir, QVariant& mod_id) -> ModStruct

View File

@ -147,25 +147,36 @@ void Mod::setStatus(ModStatus status)
if (m_localDetails) { if (m_localDetails) {
m_localDetails->status = status; m_localDetails->status = status;
} else { } else {
m_temp_status = status; if (!m_temp_status.get())
m_temp_status.reset(new ModStatus());
*m_temp_status = status;
} }
} }
void Mod::setMetadata(Metadata::ModStruct* metadata) void Mod::setMetadata(const Metadata::ModStruct& metadata)
{ {
if (status() == ModStatus::NoMetadata) if (status() == ModStatus::NoMetadata)
setStatus(ModStatus::Installed); setStatus(ModStatus::Installed);
if (m_localDetails) { if (m_localDetails) {
m_localDetails->metadata.reset(metadata); m_localDetails->metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
} else { } else {
m_temp_metadata.reset(metadata); m_temp_metadata = std::make_shared<Metadata::ModStruct>(std::move(metadata));
} }
} }
auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool auto Mod::destroy(QDir& index_dir, bool preserve_metadata) -> bool
{ {
if (!preserve_metadata && status() != ModStatus::NoMetadata) if (!preserve_metadata) {
Metadata::remove(index_dir, metadata()->mod_id()); qDebug() << QString("Destroying metadata for '%1' on purpose").arg(name());
if (metadata()) {
Metadata::remove(index_dir, metadata()->slug);
} else {
auto n = name();
Metadata::remove(index_dir, n);
}
}
m_type = MOD_UNKNOWN; m_type = MOD_UNKNOWN;
return FS::deletePath(m_file.filePath()); return FS::deletePath(m_file.filePath());
@ -182,7 +193,7 @@ auto Mod::name() const -> QString
if (!d_name.isEmpty()) if (!d_name.isEmpty())
return d_name; return d_name;
if (status() != ModStatus::NoMetadata) if (metadata())
return metadata()->name; return metadata()->name;
return m_name; return m_name;
@ -211,7 +222,7 @@ auto Mod::authors() const -> QStringList
auto Mod::status() const -> ModStatus auto Mod::status() const -> ModStatus
{ {
if (!m_localDetails) if (!m_localDetails)
return m_temp_status; return m_temp_status ? *m_temp_status : ModStatus::NoMetadata;
return details().status; return details().status;
} }
@ -235,11 +246,10 @@ void Mod::finishResolvingWithDetails(std::shared_ptr<ModDetails> details)
m_resolved = true; m_resolved = true;
m_localDetails = details; m_localDetails = details;
if (m_localDetails && m_temp_metadata && m_temp_metadata->isValid()) { setStatus(m_temp_status ? *m_temp_status : ModStatus::NoMetadata);
m_localDetails->metadata = m_temp_metadata;
if (status() == ModStatus::NoMetadata)
setStatus(ModStatus::Installed);
}
setStatus(m_temp_status); if (m_localDetails && m_temp_metadata && m_temp_metadata->isValid()) {
setMetadata(*m_temp_metadata);
m_temp_metadata.reset();
}
} }

View File

@ -77,7 +77,7 @@ public:
auto metadata() const -> const std::shared_ptr<Metadata::ModStruct>; auto metadata() const -> const std::shared_ptr<Metadata::ModStruct>;
void setStatus(ModStatus status); void setStatus(ModStatus status);
void setMetadata(Metadata::ModStruct* metadata); void setMetadata(const Metadata::ModStruct& metadata);
auto enable(bool value) -> bool; auto enable(bool value) -> bool;
@ -111,7 +111,7 @@ protected:
std::shared_ptr<Metadata::ModStruct> m_temp_metadata; std::shared_ptr<Metadata::ModStruct> m_temp_metadata;
/* Set the mod status while it doesn't have local details just yet */ /* Set the mod status while it doesn't have local details just yet */
ModStatus m_temp_status = ModStatus::NotInstalled; std::shared_ptr<ModStatus> m_temp_status;
std::shared_ptr<ModDetails> m_localDetails; std::shared_ptr<ModDetails> m_localDetails;

View File

@ -65,15 +65,21 @@ void ModFolderModel::startWatching()
update(); update();
// Watch the mods folder
is_watching = m_watcher->addPath(m_dir.absolutePath()); is_watching = m_watcher->addPath(m_dir.absolutePath());
if (is_watching) if (is_watching) {
{
qDebug() << "Started watching " << m_dir.absolutePath(); qDebug() << "Started watching " << m_dir.absolutePath();
} } else {
else
{
qDebug() << "Failed to start watching " << m_dir.absolutePath(); qDebug() << "Failed to start watching " << m_dir.absolutePath();
} }
// Watch the mods index folder
is_watching = m_watcher->addPath(indexDir().absolutePath());
if (is_watching) {
qDebug() << "Started watching " << indexDir().absolutePath();
} else {
qDebug() << "Failed to start watching " << indexDir().absolutePath();
}
} }
void ModFolderModel::stopWatching() void ModFolderModel::stopWatching()
@ -82,14 +88,18 @@ void ModFolderModel::stopWatching()
return; return;
is_watching = !m_watcher->removePath(m_dir.absolutePath()); is_watching = !m_watcher->removePath(m_dir.absolutePath());
if (!is_watching) if (!is_watching) {
{
qDebug() << "Stopped watching " << m_dir.absolutePath(); qDebug() << "Stopped watching " << m_dir.absolutePath();
} } else {
else
{
qDebug() << "Failed to stop watching " << m_dir.absolutePath(); qDebug() << "Failed to stop watching " << m_dir.absolutePath();
} }
is_watching = !m_watcher->removePath(indexDir().absolutePath());
if (!is_watching) {
qDebug() << "Stopped watching " << indexDir().absolutePath();
} else {
qDebug() << "Failed to stop watching " << indexDir().absolutePath();
}
} }
bool ModFolderModel::update() bool ModFolderModel::update()

View File

@ -47,12 +47,18 @@ void LocalModUpdateTask::executeTask()
auto old_metadata = Metadata::get(m_index_dir, m_mod.addonId); auto old_metadata = Metadata::get(m_index_dir, m_mod.addonId);
if (old_metadata.isValid()) { if (old_metadata.isValid()) {
emit hasOldMod(old_metadata.name, old_metadata.filename); emit hasOldMod(old_metadata.name, old_metadata.filename);
if (m_mod.slug.isEmpty())
m_mod.slug = old_metadata.slug;
} }
auto pw_mod = Metadata::create(m_index_dir, m_mod, m_mod_version); auto pw_mod = Metadata::create(m_index_dir, m_mod, m_mod_version);
if (pw_mod.isValid()) {
Metadata::update(m_index_dir, pw_mod); Metadata::update(m_index_dir, pw_mod);
emitSucceeded(); emitSucceeded();
} else {
qCritical() << "Tried to update an invalid mod!";
emitFailed(tr("Invalid metadata"));
}
} }
auto LocalModUpdateTask::abort() -> bool auto LocalModUpdateTask::abort() -> bool

View File

@ -71,7 +71,7 @@ void ModFolderLoadTask::run()
auto metadata = m_result->mods[chopped_id].metadata(); auto metadata = m_result->mods[chopped_id].metadata();
if (metadata) { if (metadata) {
mod.setMetadata(new Metadata::ModStruct(*metadata)); mod.setMetadata(*metadata);
m_result->mods[mod.internal_id()].setStatus(ModStatus::Installed); m_result->mods[mod.internal_id()].setStatus(ModStatus::Installed);
m_result->mods.remove(chopped_id); m_result->mods.remove(chopped_id);

View File

@ -55,11 +55,11 @@ auto getRealIndexName(QDir& index_dir, QString normalized_fname, bool should_fin
} }
// Helpers // Helpers
static inline auto indexFileName(QString const& mod_name) -> QString static inline auto indexFileName(QString const& mod_slug) -> QString
{ {
if(mod_name.endsWith(".pw.toml")) if(mod_slug.endsWith(".pw.toml"))
return mod_name; return mod_slug;
return QString("%1.pw.toml").arg(mod_name); return QString("%1.pw.toml").arg(mod_slug);
} }
static ModPlatform::ProviderCapabilities ProviderCaps; static ModPlatform::ProviderCapabilities ProviderCaps;
@ -95,6 +95,7 @@ auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, Mo
{ {
Mod mod; Mod mod;
mod.slug = mod_pack.slug;
mod.name = mod_pack.name; mod.name = mod_pack.name;
mod.filename = mod_version.fileName; mod.filename = mod_version.fileName;
@ -116,12 +117,10 @@ auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, Mo
return mod; return mod;
} }
auto V1::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod auto V1::createModFormat(QDir& index_dir, ::Mod& internal_mod, QString slug) -> Mod
{ {
auto mod_name = internal_mod.name();
// Try getting metadata if it exists // Try getting metadata if it exists
Mod mod { getIndexForMod(index_dir, mod_name) }; Mod mod { getIndexForMod(index_dir, slug) };
if(mod.isValid()) if(mod.isValid())
return mod; return mod;
@ -139,7 +138,7 @@ 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
auto normalized_fname = indexFileName(mod.name); auto normalized_fname = indexFileName(mod.slug);
auto real_fname = getRealIndexName(index_dir, normalized_fname); auto real_fname = getRealIndexName(index_dir, normalized_fname);
QFile index_file(index_dir.absoluteFilePath(real_fname)); QFile index_file(index_dir.absoluteFilePath(real_fname));
@ -187,12 +186,13 @@ void V1::updateModIndex(QDir& index_dir, Mod& mod)
} }
} }
index_file.flush();
index_file.close(); index_file.close();
} }
void V1::deleteModIndex(QDir& index_dir, QString& mod_name) void V1::deleteModIndex(QDir& index_dir, QString& mod_slug)
{ {
auto normalized_fname = indexFileName(mod_name); auto normalized_fname = indexFileName(mod_slug);
auto real_fname = getRealIndexName(index_dir, normalized_fname); auto real_fname = getRealIndexName(index_dir, normalized_fname);
if (real_fname.isEmpty()) if (real_fname.isEmpty())
return; return;
@ -200,12 +200,12 @@ void V1::deleteModIndex(QDir& index_dir, QString& mod_name)
QFile index_file(index_dir.absoluteFilePath(real_fname)); 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_slug);
return; return;
} }
if (!index_file.remove()) { if (!index_file.remove()) {
qWarning() << QString("Failed to remove metadata for mod %1!").arg(mod_name); qWarning() << QString("Failed to remove metadata for mod %1!").arg(mod_slug);
} }
} }
@ -221,11 +221,11 @@ void V1::deleteModIndex(QDir& index_dir, QVariant& mod_id)
} }
} }
auto V1::getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod auto V1::getIndexForMod(QDir& index_dir, QString slug) -> Mod
{ {
Mod mod; Mod mod;
auto normalized_fname = indexFileName(index_file_name); auto normalized_fname = indexFileName(slug);
auto real_fname = getRealIndexName(index_dir, normalized_fname, true); auto real_fname = getRealIndexName(index_dir, normalized_fname, true);
if (real_fname.isEmpty()) if (real_fname.isEmpty())
return {}; return {};
@ -233,7 +233,7 @@ auto V1::getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod
QFile index_file(index_dir.absoluteFilePath(real_fname)); 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(slug);
return {}; return {};
} }
@ -247,11 +247,13 @@ auto V1::getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod
index_file.close(); index_file.close();
if (!table) { if (!table) {
qWarning() << QString("Could not open file %1!").arg(indexFileName(index_file_name)); qWarning() << QString("Could not open file %1!").arg(normalized_fname);
qWarning() << "Reason: " << QString(errbuf); qWarning() << "Reason: " << QString(errbuf);
return {}; return {};
} }
mod.slug = slug;
{ // Basic info { // Basic info
mod.name = stringEntry(table, "name"); mod.name = stringEntry(table, "name");
mod.filename = stringEntry(table, "filename"); mod.filename = stringEntry(table, "filename");

View File

@ -40,6 +40,7 @@ auto intEntry(toml_table_t* parent, const char* entry_name) -> int;
class V1 { class V1 {
public: public:
struct Mod { struct Mod {
QString slug {};
QString name {}; QString name {};
QString filename {}; QString filename {};
// FIXME: make side an enum // FIXME: make side an enum
@ -58,7 +59,7 @@ class V1 {
public: public:
// This is a totally heuristic, but should work for now. // This is a totally heuristic, but should work for now.
auto isValid() const -> bool { return !name.isEmpty() && !project_id.isNull(); } auto isValid() const -> bool { return !slug.isEmpty() && !project_id.isNull(); }
// Different providers can use different names for the same thing // Different providers can use different names for the same thing
// Modrinth-specific // Modrinth-specific
@ -71,9 +72,9 @@ class V1 {
* */ * */
static auto createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> Mod; static auto createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> Mod;
/* Generates the object representing the information in a mod.pw.toml file via /* Generates the object representing the information in a mod.pw.toml file via
* its common representation in the launcher. * its common representation in the launcher, plus a necessary slug.
* */ * */
static auto createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod; static auto createModFormat(QDir& index_dir, ::Mod& internal_mod, QString slug) -> Mod;
/* Updates the mod index for the provided mod. /* Updates the mod index for the provided mod.
* This creates a new index if one does not exist already * This creates a new index if one does not exist already
@ -81,16 +82,16 @@ class V1 {
* */ * */
static void updateModIndex(QDir& index_dir, Mod& mod); static void updateModIndex(QDir& index_dir, Mod& mod);
/* Deletes the metadata for the mod with the given name. If the metadata doesn't exist, it does nothing. */ /* Deletes the metadata for the mod with the given slug. If the metadata doesn't exist, it does nothing. */
static void deleteModIndex(QDir& index_dir, QString& mod_name); static void deleteModIndex(QDir& index_dir, QString& mod_slug);
/* Deletes the metadata for the mod with the given id. If the metadata doesn't exist, it does nothing. */ /* Deletes the metadata for the mod with the given id. If the metadata doesn't exist, it does nothing. */
static void deleteModIndex(QDir& index_dir, QVariant& mod_id); static void deleteModIndex(QDir& index_dir, QVariant& mod_id);
/* Gets the metadata for a mod with a particular name. /* Gets the metadata for a mod with a particular file name.
* If the mod doesn't have a metadata, it simply returns an empty Mod object. * If the mod doesn't have a metadata, it simply returns an empty Mod object.
* */ * */
static auto getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod; static auto getIndexForMod(QDir& index_dir, QString slug) -> Mod;
/* Gets the metadata for a mod with a particular id. /* Gets the metadata for a mod with a particular id.
* If the mod doesn't have a metadata, it simply returns an empty Mod object. * If the mod doesn't have a metadata, it simply returns an empty Mod object.

View File

@ -32,10 +32,11 @@ class PackwizTest : public QObject {
QString source = QFINDTESTDATA("testdata"); QString source = QFINDTESTDATA("testdata");
QDir index_dir(source); QDir index_dir(source);
QString name_mod("borderless-mining.pw.toml"); QString slug_mod("borderless-mining");
QVERIFY(index_dir.entryList().contains(name_mod)); QString file_name = slug_mod + ".pw.toml";
QVERIFY(index_dir.entryList().contains(file_name));
auto metadata = Packwiz::V1::getIndexForMod(index_dir, name_mod); auto metadata = Packwiz::V1::getIndexForMod(index_dir, slug_mod);
QVERIFY(metadata.isValid()); QVERIFY(metadata.isValid());