feat: add support for converting builtin -> packwiz mod formats

Also adds more documentation.
This commit is contained in:
flow 2022-04-15 22:37:10 -03:00 committed by flow
parent e9fb566c07
commit 092d2f8917
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 58 additions and 19 deletions

View File

@ -1,12 +1,14 @@
#include "Packwiz.h" #include "Packwiz.h"
#include "modplatform/ModIndex.h"
#include "toml.h"
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
#include <QObject> #include <QObject>
#include "toml.h"
#include "modplatform/ModIndex.h"
#include "minecraft/mod/Mod.h"
// Helpers // Helpers
static inline QString indexFileName(QString const& mod_name) static inline QString indexFileName(QString const& mod_name)
{ {
@ -31,12 +33,39 @@ auto Packwiz::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pac
return mod; return mod;
} }
auto Packwiz::createModFormat(QDir& index_dir, ::Mod& internal_mod) -> Mod
{
auto mod_name = internal_mod.name();
// Try getting metadata if it exists
Mod mod { getIndexForMod(index_dir, mod_name) };
if(mod.isValid())
return mod;
// Manually construct packwiz mod
mod.name = internal_mod.name();
mod.filename = internal_mod.filename().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?
return mod;
}
void Packwiz::updateModIndex(QDir& index_dir, Mod& mod) void Packwiz::updateModIndex(QDir& index_dir, Mod& mod)
{ {
if(!mod.isValid()){
qCritical() << QString("Tried to update metadata of an invalid mod!");
return;
}
// 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))); QFile index_file(index_dir.absoluteFilePath(indexFileName(mod.name)));
// 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
// override a file. In this case, check versions and ask the user what
// they want to do!
if (index_file.exists()) { index_file.remove(); } if (index_file.exists()) { index_file.remove(); }
if (!index_file.open(QIODevice::ReadWrite)) { if (!index_file.open(QIODevice::ReadWrite)) {
@ -87,11 +116,11 @@ auto Packwiz::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
if (!index_file.exists()) { if (!index_file.exists()) {
qWarning() << QString("Tried to get a non-existent mod metadata for %1").arg(mod_name); qWarning() << QString("Tried to get a non-existent mod metadata for %1").arg(mod_name);
return mod; return {};
} }
if (!index_file.open(QIODevice::ReadOnly)) { if (!index_file.open(QIODevice::ReadOnly)) {
qWarning() << QString("Failed to open mod metadata for %1").arg(mod_name); qWarning() << QString("Failed to open mod metadata for %1").arg(mod_name);
return mod; return {};
} }
toml_table_t* table; toml_table_t* table;
@ -103,7 +132,7 @@ auto Packwiz::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
if (!table) { if (!table) {
qCritical() << QString("Could not open file %1!").arg(indexFileName(mod.name)); qCritical() << QString("Could not open file %1!").arg(indexFileName(mod.name));
return mod; return {};
} }
// Helper function for extracting data from the TOML file // Helper function for extracting data from the TOML file

View File

@ -6,33 +6,43 @@
#include <QUrl> #include <QUrl>
#include <QVariant> #include <QVariant>
namespace ModPlatform {
} // namespace ModPlatform
class QDir; class QDir;
// Mod from launcher/minecraft/mod/Mod.h
class Mod;
class Packwiz { class Packwiz {
public: public:
struct Mod { struct Mod {
QString name; QString name {};
QString filename; QString filename {};
// FIXME: make side an enum // FIXME: make side an enum
QString side = "both"; QString side {"both"};
// [download] // [download]
QUrl url; QUrl url {};
// FIXME: make hash-format an enum // FIXME: make hash-format an enum
QString hash_format; QString hash_format {};
QString hash; QString hash {};
// [update] // [update]
ModPlatform::Provider provider; ModPlatform::Provider provider {};
QVariant file_id; QVariant file_id {};
QVariant project_id; QVariant project_id {};
public:
// This is a heuristic, but should work for now.
auto isValid() const -> bool { return !name.isEmpty(); }
}; };
/* Generates the object representing the information in a mod.toml file via its common representation in the launcher */ /* Generates the object representing the information in a mod.toml file via
* its common representation in the launcher, when downloading mods.
* */
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.toml file via
* its common representation in the launcher.
* */
static auto createModFormat(QDir& index_dir, ::Mod& internal_mod) -> 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