aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform
diff options
context:
space:
mode:
authorflow <thiagodonato300@gmail.com>2022-04-17 11:40:41 -0300
committerflow <flowlnlnln@gmail.com>2022-05-23 14:42:28 -0300
commitd7f6b3699074b268fd554bd1eb9da68f1e533355 (patch)
treedf9a4e34bc4c851040e80c933cf8858c26503ca5 /launcher/modplatform
parent4439666e67573a6a36af981fdc68410fdf9e4f9f (diff)
downloadPrismLauncher-d7f6b3699074b268fd554bd1eb9da68f1e533355.tar.gz
PrismLauncher-d7f6b3699074b268fd554bd1eb9da68f1e533355.tar.bz2
PrismLauncher-d7f6b3699074b268fd554bd1eb9da68f1e533355.zip
test+fix: add basic tests and fix issues with it
Diffstat (limited to 'launcher/modplatform')
-rw-r--r--launcher/modplatform/ModIndex.h2
-rw-r--r--launcher/modplatform/packwiz/Packwiz.cpp76
-rw-r--r--launcher/modplatform/packwiz/Packwiz.h11
-rw-r--r--launcher/modplatform/packwiz/Packwiz_test.cpp68
-rw-r--r--launcher/modplatform/packwiz/testdata/borderless-mining.toml13
-rw-r--r--launcher/modplatform/packwiz/testdata/screenshot-to-clipboard-fabric.toml13
6 files changed, 150 insertions, 33 deletions
diff --git a/launcher/modplatform/ModIndex.h b/launcher/modplatform/ModIndex.h
index c5329772..ee623b78 100644
--- a/launcher/modplatform/ModIndex.h
+++ b/launcher/modplatform/ModIndex.h
@@ -19,7 +19,7 @@ class ProviderCapabilities {
{
switch(p){
case Provider::MODRINTH:
- return "sha256";
+ return "sha512";
case Provider::FLAME:
return "murmur2";
}
diff --git a/launcher/modplatform/packwiz/Packwiz.cpp b/launcher/modplatform/packwiz/Packwiz.cpp
index 27339c2d..8fd74a3e 100644
--- a/launcher/modplatform/packwiz/Packwiz.cpp
+++ b/launcher/modplatform/packwiz/Packwiz.cpp
@@ -14,6 +14,8 @@ namespace Packwiz {
// Helpers
static inline QString indexFileName(QString const& mod_name)
{
+ if(mod_name.endsWith(".toml"))
+ return mod_name;
return QString("%1.toml").arg(mod_name);
}
@@ -91,8 +93,16 @@ void V1::updateModIndex(QDir& index_dir, Mod& mod)
in_stream << QString("\n[update]\n");
in_stream << QString("[update.%1]\n").arg(ModPlatform::ProviderCapabilities::providerName(mod.provider));
- addToStream("file-id", mod.file_id.toString());
- addToStream("project-id", mod.project_id.toString());
+ switch(mod.provider){
+ case(ModPlatform::Provider::FLAME):
+ in_stream << QString("file-id = %1\n").arg(mod.file_id.toString());
+ in_stream << QString("project-id = %1\n").arg(mod.project_id.toString());
+ break;
+ case(ModPlatform::Provider::MODRINTH):
+ addToStream("mod-id", mod.mod_id().toString());
+ addToStream("version", mod.version().toString());
+ break;
+ }
}
}
@@ -110,18 +120,44 @@ void V1::deleteModIndex(QDir& index_dir, QString& mod_name)
}
}
-auto V1::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
+// 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
{
Mod mod;
- QFile index_file(index_dir.absoluteFilePath(indexFileName(mod_name)));
+ QFile index_file(index_dir.absoluteFilePath(indexFileName(index_file_name)));
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(index_file_name);
return {};
}
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(index_file_name);
return {};
}
@@ -136,29 +172,9 @@ auto V1::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
qCritical() << QString("Could not open file %1!").arg(indexFileName(mod.name));
return {};
}
-
- // Helper function 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 property '%1' in mod metadata.").arg(entry_name);
- return {};
- }
-
- QString tmp = var.u.s;
- free(var.u.s);
-
- return tmp;
- };
-
+
{ // Basic info
mod.name = stringEntry(table, "name");
- // Basic sanity check
- if (mod.name != mod_name) {
- qCritical() << QString("Name mismatch in mod metadata:\nExpected:%1\nGot:%2").arg(mod_name, mod.name);
- return {};
- }
-
mod.filename = stringEntry(table, "filename");
mod.side = stringEntry(table, "side");
}
@@ -188,15 +204,17 @@ auto V1::getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod
toml_table_t* mod_provider_table;
if ((mod_provider_table = toml_table_in(update_table, ProviderCaps::providerName(Provider::FLAME)))) {
mod.provider = Provider::FLAME;
+ mod.file_id = intEntry(mod_provider_table, "file-id");
+ mod.project_id = intEntry(mod_provider_table, "project-id");
} else if ((mod_provider_table = toml_table_in(update_table, ProviderCaps::providerName(Provider::MODRINTH)))) {
mod.provider = Provider::MODRINTH;
+ mod.mod_id() = stringEntry(mod_provider_table, "mod-id");
+ mod.version() = stringEntry(mod_provider_table, "version");
} else {
qCritical() << QString("No mod provider on mod metadata!");
return {};
}
- mod.file_id = stringEntry(mod_provider_table, "file-id");
- mod.project_id = stringEntry(mod_provider_table, "project-id");
}
toml_free(table);
diff --git a/launcher/modplatform/packwiz/Packwiz.h b/launcher/modplatform/packwiz/Packwiz.h
index 777a365f..69125dbc 100644
--- a/launcher/modplatform/packwiz/Packwiz.h
+++ b/launcher/modplatform/packwiz/Packwiz.h
@@ -33,8 +33,13 @@ class V1 {
QVariant project_id {};
public:
- // This is a heuristic, but should work for now.
- auto isValid() const -> bool { return !name.isEmpty(); }
+ // This is a totally heuristic, but should work for now.
+ auto isValid() const -> bool { return !name.isEmpty() && !project_id.isNull(); }
+
+ // Different providers can use different names for the same thing
+ // Modrinth-specific
+ auto mod_id() -> QVariant& { return project_id; }
+ auto version() -> QVariant& { return file_id; }
};
/* Generates the object representing the information in a mod.toml file via
@@ -58,7 +63,7 @@ class V1 {
/* Gets the metadata for a mod with a particular name.
* If the mod doesn't have a metadata, it simply returns an empty Mod object.
* */
- static auto getIndexForMod(QDir& index_dir, QString& mod_name) -> Mod;
+ static auto getIndexForMod(QDir& index_dir, QString& index_file_name) -> Mod;
};
} // namespace Packwiz
diff --git a/launcher/modplatform/packwiz/Packwiz_test.cpp b/launcher/modplatform/packwiz/Packwiz_test.cpp
new file mode 100644
index 00000000..2e61c167
--- /dev/null
+++ b/launcher/modplatform/packwiz/Packwiz_test.cpp
@@ -0,0 +1,68 @@
+#include <QTemporaryDir>
+#include <QTest>
+
+#include "TestUtil.h"
+#include "Packwiz.h"
+
+class PackwizTest : public QObject {
+ Q_OBJECT
+
+ private slots:
+ // Files taken from https://github.com/packwiz/packwiz-example-pack
+ void loadFromFile_Modrinth()
+ {
+ QString source = QFINDTESTDATA("testdata");
+
+ QDir index_dir(source);
+ QString name_mod("borderless-mining.toml");
+ QVERIFY(index_dir.entryList().contains(name_mod));
+
+ auto metadata = Packwiz::V1::getIndexForMod(index_dir, name_mod);
+
+ QVERIFY(metadata.isValid());
+
+ QCOMPARE(metadata.name, "Borderless Mining");
+ QCOMPARE(metadata.filename, "borderless-mining-1.1.1+1.18.jar");
+ QCOMPARE(metadata.side, "client");
+
+ QCOMPARE(metadata.url, QUrl("https://cdn.modrinth.com/data/kYq5qkSL/versions/1.1.1+1.18/borderless-mining-1.1.1+1.18.jar"));
+ QCOMPARE(metadata.hash_format, "sha512");
+ QCOMPARE(metadata.hash, "c8fe6e15ddea32668822dddb26e1851e5f03834be4bcb2eff9c0da7fdc086a9b6cead78e31a44d3bc66335cba11144ee0337c6d5346f1ba63623064499b3188d");
+
+ QCOMPARE(metadata.provider, ModPlatform::Provider::MODRINTH);
+ QCOMPARE(metadata.version(), "ug2qKTPR");
+ QCOMPARE(metadata.mod_id(), "kYq5qkSL");
+ }
+
+ void loadFromFile_Curseforge()
+ {
+ QString source = QFINDTESTDATA("testdata");
+
+ QDir index_dir(source);
+ QString name_mod("screenshot-to-clipboard-fabric.toml");
+ QVERIFY(index_dir.entryList().contains(name_mod));
+
+ // Try without the .toml at the end
+ name_mod.chop(5);
+
+ auto metadata = Packwiz::V1::getIndexForMod(index_dir, name_mod);
+
+ QVERIFY(metadata.isValid());
+
+ QCOMPARE(metadata.name, "Screenshot to Clipboard (Fabric)");
+ QCOMPARE(metadata.filename, "screenshot-to-clipboard-1.0.7-fabric.jar");
+ QCOMPARE(metadata.side, "both");
+
+ QCOMPARE(metadata.url, QUrl("https://edge.forgecdn.net/files/3509/43/screenshot-to-clipboard-1.0.7-fabric.jar"));
+ QCOMPARE(metadata.hash_format, "murmur2");
+ QCOMPARE(metadata.hash, "1781245820");
+
+ QCOMPARE(metadata.provider, ModPlatform::Provider::FLAME);
+ QCOMPARE(metadata.file_id, 3509043);
+ QCOMPARE(metadata.project_id, 327154);
+ }
+};
+
+QTEST_GUILESS_MAIN(PackwizTest)
+
+#include "Packwiz_test.moc"
diff --git a/launcher/modplatform/packwiz/testdata/borderless-mining.toml b/launcher/modplatform/packwiz/testdata/borderless-mining.toml
new file mode 100644
index 00000000..16545fd4
--- /dev/null
+++ b/launcher/modplatform/packwiz/testdata/borderless-mining.toml
@@ -0,0 +1,13 @@
+name = "Borderless Mining"
+filename = "borderless-mining-1.1.1+1.18.jar"
+side = "client"
+
+[download]
+url = "https://cdn.modrinth.com/data/kYq5qkSL/versions/1.1.1+1.18/borderless-mining-1.1.1+1.18.jar"
+hash-format = "sha512"
+hash = "c8fe6e15ddea32668822dddb26e1851e5f03834be4bcb2eff9c0da7fdc086a9b6cead78e31a44d3bc66335cba11144ee0337c6d5346f1ba63623064499b3188d"
+
+[update]
+[update.modrinth]
+mod-id = "kYq5qkSL"
+version = "ug2qKTPR"
diff --git a/launcher/modplatform/packwiz/testdata/screenshot-to-clipboard-fabric.toml b/launcher/modplatform/packwiz/testdata/screenshot-to-clipboard-fabric.toml
new file mode 100644
index 00000000..87d70ada
--- /dev/null
+++ b/launcher/modplatform/packwiz/testdata/screenshot-to-clipboard-fabric.toml
@@ -0,0 +1,13 @@
+name = "Screenshot to Clipboard (Fabric)"
+filename = "screenshot-to-clipboard-1.0.7-fabric.jar"
+side = "both"
+
+[download]
+url = "https://edge.forgecdn.net/files/3509/43/screenshot-to-clipboard-1.0.7-fabric.jar"
+hash-format = "murmur2"
+hash = "1781245820"
+
+[update]
+[update.curseforge]
+file-id = 3509043
+project-id = 327154