refactor: abstract metadata handling and clarify names

This commit is contained in:
flow
2022-04-16 13:27:29 -03:00
committed by flow
parent 092d2f8917
commit fab4a7a602
11 changed files with 82 additions and 31 deletions

View File

@ -659,23 +659,23 @@ QStringList MinecraftInstance::verboseDescription(AuthSessionPtr session, Minecr
out << QString("%1:").arg(label);
auto modList = model.allMods();
std::sort(modList.begin(), modList.end(), [](Mod &a, Mod &b) {
auto aName = a.filename().completeBaseName();
auto bName = b.filename().completeBaseName();
auto aName = a.fileinfo().completeBaseName();
auto bName = b.fileinfo().completeBaseName();
return aName.localeAwareCompare(bName) < 0;
});
for(auto & mod: modList)
{
if(mod.type() == Mod::MOD_FOLDER)
{
out << u8" [📁] " + mod.filename().completeBaseName() + " (folder)";
out << u8" [📁] " + mod.fileinfo().completeBaseName() + " (folder)";
continue;
}
if(mod.enabled()) {
out << u8" [✔️] " + mod.filename().completeBaseName();
out << u8" [✔️] " + mod.fileinfo().completeBaseName();
}
else {
out << u8" [❌] " + mod.filename().completeBaseName() + " (disabled)";
out << u8" [❌] " + mod.fileinfo().completeBaseName() + " (disabled)";
}
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <memory>
#include "modplatform/packwiz/Packwiz.h"
// launcher/minecraft/mod/Mod.h
class Mod;
/* Abstraction file for easily changing the way metadata is stored / handled
* Needs to be a class because of -Wunused-function and no C++17 [[maybe_unused]]
* */
class Metadata {
public:
using ModStruct = Packwiz::V1::Mod;
static auto create(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> ModStruct
{
return Packwiz::V1::createModFormat(index_dir, mod_pack, mod_version);
}
static auto create(QDir& index_dir, Mod& internal_mod) -> ModStruct
{
return Packwiz::V1::createModFormat(index_dir, internal_mod);
}
static void update(QDir& index_dir, ModStruct& mod)
{
Packwiz::V1::updateModIndex(index_dir, mod);
}
static void remove(QDir& index_dir, QString& mod_name)
{
Packwiz::V1::deleteModIndex(index_dir, mod_name);
}
static auto get(QDir& index_dir, QString& mod_name) -> ModStruct
{
return Packwiz::V1::getIndexForMod(index_dir, mod_name);
}
};

View File

@ -20,6 +20,7 @@
#include <FileSystem.h>
#include <QDebug>
#include "MetadataHandler.h"
namespace {
@ -33,7 +34,7 @@ Mod::Mod(const QFileInfo& file)
m_changedDateTime = file.lastModified();
}
Mod::Mod(const QDir& mods_dir, const Packwiz::Mod& metadata)
Mod::Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata)
: m_file(mods_dir.absoluteFilePath(metadata.filename))
// It is weird, but name is not reliable for comparing with the JAR files name
// FIXME: Maybe use hash when implemented?
@ -121,8 +122,7 @@ bool Mod::enable(bool value)
bool Mod::destroy(QDir& index_dir)
{
// Delete metadata
Packwiz::deleteModIndex(index_dir, m_name);
Metadata::remove(index_dir, m_name);
m_type = MOD_UNKNOWN;
return FS::deletePath(m_file.filePath());

View File

@ -21,7 +21,7 @@
#include <memory>
#include "ModDetails.h"
#include "modplatform/packwiz/Packwiz.h"
#include "minecraft/mod/MetadataHandler.h"
class Mod
{
@ -37,9 +37,9 @@ public:
Mod() = default;
Mod(const QFileInfo &file);
explicit Mod(const QDir& mods_dir, const Packwiz::Mod& metadata);
explicit Mod(const QDir& mods_dir, const Metadata::ModStruct& metadata);
QFileInfo filename() const { return m_file; }
QFileInfo fileinfo() const { return m_file; }
QDateTime dateTimeChanged() const { return m_changedDateTime; }
QString internal_id() const { return m_internal_id; }
ModType type() const { return m_type; }
@ -82,6 +82,7 @@ protected:
QDateTime m_changedDateTime;
QString m_internal_id;
/* Name as reported via the file name */
QString m_name;
ModType m_type = MOD_UNKNOWN;
bool m_from_metadata = false;

View File

@ -180,7 +180,7 @@ void ModFolderModel::resolveMod(Mod& m)
return;
}
auto task = new LocalModParseTask(nextResolutionTicket, m.type(), m.filename());
auto task = new LocalModParseTask(nextResolutionTicket, m.type(), m.fileinfo());
auto result = task->result();
result->id = m.internal_id();
activeTickets.insert(nextResolutionTicket, result);

View File

@ -3,7 +3,7 @@
#include <toml.h>
#include "FileSystem.h"
#include "modplatform/packwiz/Packwiz.h"
#include "minecraft/mod/MetadataHandler.h"
LocalModUpdateTask::LocalModUpdateTask(QDir index_dir, ModPlatform::IndexedPack& mod, ModPlatform::IndexedVersion& mod_version)
: m_index_dir(index_dir), m_mod(mod), m_mod_version(mod_version)
@ -18,8 +18,8 @@ void LocalModUpdateTask::executeTask()
{
setStatus(tr("Updating index for mod:\n%1").arg(m_mod.name));
auto pw_mod = Packwiz::createModFormat(m_index_dir, m_mod, m_mod_version);
Packwiz::updateModIndex(m_index_dir, pw_mod);
auto pw_mod = Metadata::create(m_index_dir, m_mod, m_mod_version);
Metadata::update(m_index_dir, pw_mod);
emitSucceeded();
}

View File

@ -1,7 +1,7 @@
#include "ModFolderLoadTask.h"
#include <QDebug>
#include "modplatform/packwiz/Packwiz.h"
#include "minecraft/mod/MetadataHandler.h"
ModFolderLoadTask::ModFolderLoadTask(QDir& mods_dir, QDir& index_dir)
: m_mods_dir(mods_dir), m_index_dir(index_dir), m_result(new Result())
@ -17,7 +17,7 @@ void ModFolderLoadTask::run()
continue;
entry.chop(5); // Remove .toml at the end
Mod mod(m_mods_dir, Packwiz::getIndexForMod(m_index_dir, entry));
Mod mod(m_mods_dir, Metadata::get(m_index_dir, entry));
m_result->mods[mod.internal_id()] = mod;
}