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
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
11 changed files with 82 additions and 31 deletions

View File

@ -322,6 +322,7 @@ set(MINECRAFT_SOURCES
minecraft/WorldList.h
minecraft/WorldList.cpp
minecraft/mod/MetadataHandler.h
minecraft/mod/Mod.h
minecraft/mod/Mod.cpp
minecraft/mod/ModDetails.h

View File

@ -151,23 +151,23 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
continue;
if (mod.type() == Mod::MOD_ZIPFILE)
{
if (!mergeZipFiles(&zipOut, mod.filename(), addedFiles))
if (!mergeZipFiles(&zipOut, mod.fileinfo(), addedFiles))
{
zipOut.close();
QFile::remove(targetJarPath);
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
qCritical() << "Failed to add" << mod.fileinfo().fileName() << "to the jar.";
return false;
}
}
else if (mod.type() == Mod::MOD_SINGLEFILE)
{
// FIXME: buggy - does not work with addedFiles
auto filename = mod.filename();
auto filename = mod.fileinfo();
if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
{
zipOut.close();
QFile::remove(targetJarPath);
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
qCritical() << "Failed to add" << mod.fileinfo().fileName() << "to the jar.";
return false;
}
addedFiles.insert(filename.fileName());
@ -176,7 +176,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
{
// untested, but seems to be unused / not possible to reach
// FIXME: buggy - does not work with addedFiles
auto filename = mod.filename();
auto filename = mod.fileinfo();
QString what_to_zip = filename.absoluteFilePath();
QDir dir(what_to_zip);
dir.cdUp();
@ -193,7 +193,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
{
zipOut.close();
QFile::remove(targetJarPath);
qCritical() << "Failed to add" << mod.filename().fileName() << "to the jar.";
qCritical() << "Failed to add" << mod.fileinfo().fileName() << "to the jar.";
return false;
}
qDebug() << "Adding folder " << filename.fileName() << " from "
@ -204,7 +204,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
// Make sure we do not continue launching when something is missing or undefined...
zipOut.close();
QFile::remove(targetJarPath);
qCritical() << "Failed to add unknown mod type" << mod.filename().fileName() << "to the jar.";
qCritical() << "Failed to add unknown mod type" << mod.fileinfo().fileName() << "to the jar.";
return false;
}
}

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;
}

View File

@ -9,13 +9,15 @@
#include "modplatform/ModIndex.h"
#include "minecraft/mod/Mod.h"
namespace Packwiz {
// Helpers
static inline QString indexFileName(QString const& mod_name)
{
return QString("%1.toml").arg(mod_name);
}
auto Packwiz::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;
@ -33,7 +35,7 @@ auto Packwiz::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pac
return mod;
}
auto Packwiz::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod
auto V1::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod
{
auto mod_name = internal_mod.name();
@ -44,7 +46,7 @@ auto Packwiz::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod
// Manually construct packwiz mod
mod.name = internal_mod.name();
mod.filename = internal_mod.filename().fileName();
mod.filename = internal_mod.fileinfo().fileName();
// TODO: Have a mechanism for telling the UI subsystem that we want to gather user information
// (i.e. which mod provider we want to use). Maybe an object parameter with a signal for that?
@ -52,7 +54,7 @@ auto Packwiz::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod
return mod;
}
void Packwiz::updateModIndex(QDir& index_dir, Mod& mod)
void V1::updateModIndex(QDir& index_dir, Mod& mod)
{
if(!mod.isValid()){
qCritical() << QString("Tried to update metadata of an invalid mod!");
@ -94,7 +96,7 @@ void Packwiz::updateModIndex(QDir& index_dir, Mod& mod)
}
}
void Packwiz::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)));
@ -108,7 +110,7 @@ void Packwiz::deleteModIndex(QDir& index_dir, QString& mod_name)
}
}
auto Packwiz::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
auto V1::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
{
Mod mod;
@ -201,3 +203,5 @@ auto Packwiz::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
return mod;
}
} // namespace Packwiz

View File

@ -11,7 +11,9 @@ class QDir;
// Mod from launcher/minecraft/mod/Mod.h
class Mod;
class Packwiz {
namespace Packwiz {
class V1 {
public:
struct Mod {
QString name {};
@ -58,3 +60,5 @@ class Packwiz {
* */
static auto getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod;
};
} // namespace Packwiz