From c35dbd972ef5506cf49ff071f743da91d0b36d68 Mon Sep 17 00:00:00 2001 From: asie Date: Sat, 15 Jun 2019 13:54:20 +0200 Subject: [PATCH 1/2] Add Fabric mod JSON parsing support --- api/logic/minecraft/Mod.cpp | 52 +++++++++++++++++++++++++++++++++++++ api/logic/minecraft/Mod.h | 1 + 2 files changed, 53 insertions(+) diff --git a/api/logic/minecraft/Mod.cpp b/api/logic/minecraft/Mod.cpp index 069a27a4..e41c319f 100644 --- a/api/logic/minecraft/Mod.cpp +++ b/api/logic/minecraft/Mod.cpp @@ -96,6 +96,19 @@ void Mod::repath(const QFileInfo &file) zip.close(); return; } + else if (zip.setCurrentFile("fabric.mod.json")) + { + if (!file.open(QIODevice::ReadOnly)) + { + zip.close(); + return; + } + + ReadFabricModInfo(file.readAll()); + file.close(); + zip.close(); + return; + } else if (zip.setCurrentFile("forgeversion.properties")) { if (!file.open(QIODevice::ReadOnly)) @@ -224,6 +237,45 @@ void Mod::ReadMCModInfo(QByteArray contents) } } +// https://fabricmc.net/wiki/documentation:fabric_mod_json +void Mod::ReadFabricModInfo(QByteArray contents) +{ + QJsonParseError jsonError; + QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError); + auto object = jsonDoc.object(); + auto schemaVersion = object.contains("schemaVersion") ? object.value("schemaVersion").toInt(0) : 0; + + m_mod_id = object.value("id").toString(); + m_version = object.value("version").toString(); + + m_name = object.contains("name") ? object.value("name").toString() : m_mod_id; + m_description = object.value("description").toString(); + + if (schemaVersion >= 1) + { + QJsonArray authors = object.value("authors").toArray(); + + if (authors.size() == 0) + m_authors = ""; + else if (authors.size() >= 1) + { + m_authors = authors.at(0).toObject().value("name").toString(); + for (int i = 1; i < authors.size(); i++) + { + m_authors += ", " + authors.at(i).toObject().value("name").toString(); + } + } + + if (object.contains("contact")) + { + QJsonObject contact = object.value("contact").toObject(); + + if (contact.contains("homepage")) + m_homeurl = contact.value("homepage").toString(); + } + } +} + void Mod::ReadForgeInfo(QByteArray contents) { // Read the data diff --git a/api/logic/minecraft/Mod.h b/api/logic/minecraft/Mod.h index 6c329363..63bf5ab4 100644 --- a/api/logic/minecraft/Mod.h +++ b/api/logic/minecraft/Mod.h @@ -113,6 +113,7 @@ public: private: void ReadMCModInfo(QByteArray contents); + void ReadFabricModInfo(QByteArray contents); void ReadForgeInfo(QByteArray contents); void ReadLiteModInfo(QByteArray contents); From d251097545a3dc4b26b00a3d050856e4cbe9b7ef Mon Sep 17 00:00:00 2001 From: asie Date: Sat, 15 Jun 2019 16:35:10 +0200 Subject: [PATCH 2/2] fix author name parsing --- api/logic/minecraft/Mod.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/api/logic/minecraft/Mod.cpp b/api/logic/minecraft/Mod.cpp index e41c319f..6217c9d2 100644 --- a/api/logic/minecraft/Mod.cpp +++ b/api/logic/minecraft/Mod.cpp @@ -254,15 +254,18 @@ void Mod::ReadFabricModInfo(QByteArray contents) if (schemaVersion >= 1) { QJsonArray authors = object.value("authors").toArray(); + m_authors = ""; - if (authors.size() == 0) - m_authors = ""; - else if (authors.size() >= 1) + for (int i = 0; i < authors.size(); i++) { - m_authors = authors.at(0).toObject().value("name").toString(); - for (int i = 1; i < authors.size(); i++) - { - m_authors += ", " + authors.at(i).toObject().value("name").toString(); + QString author_name = authors.at(i).isObject() + ? authors.at(i).toObject().value("name").toString() + : authors.at(i).toString(); + + if (i > 0) + m_authors += ", " + author_name; + else { + m_authors += author_name; } }