2022-05-11 04:27:47 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-04-14 03:46:36 +05:30
|
|
|
#include "Packwiz.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QObject>
|
|
|
|
|
2022-04-16 07:07:10 +05:30
|
|
|
#include "toml.h"
|
2022-05-18 14:16:07 +05:30
|
|
|
#include "FileSystem.h"
|
2022-04-16 07:07:10 +05:30
|
|
|
|
|
|
|
#include "minecraft/mod/Mod.h"
|
2022-04-20 04:49:51 +05:30
|
|
|
#include "modplatform/ModIndex.h"
|
2022-04-16 07:07:10 +05:30
|
|
|
|
2022-04-16 21:57:29 +05:30
|
|
|
namespace Packwiz {
|
|
|
|
|
2022-05-18 14:16:07 +05:30
|
|
|
auto getRealIndexName(QDir& index_dir, QString normalized_fname, bool should_find_match) -> QString
|
|
|
|
{
|
|
|
|
QFile index_file(index_dir.absoluteFilePath(normalized_fname));
|
|
|
|
|
|
|
|
QString real_fname = normalized_fname;
|
|
|
|
if (!index_file.exists()) {
|
|
|
|
// Tries to get similar entries
|
|
|
|
for (auto& file_name : index_dir.entryList(QDir::Filter::Files)) {
|
|
|
|
if (!QString::compare(normalized_fname, file_name, Qt::CaseInsensitive)) {
|
|
|
|
real_fname = file_name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(should_find_match && !QString::compare(normalized_fname, real_fname, Qt::CaseSensitive)){
|
|
|
|
qCritical() << "Could not find a match for a valid metadata file!";
|
|
|
|
qCritical() << "File: " << normalized_fname;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return real_fname;
|
|
|
|
}
|
|
|
|
|
2022-04-15 06:32:41 +05:30
|
|
|
// Helpers
|
2022-06-19 22:56:15 +05:30
|
|
|
static inline auto indexFileName(QString const& mod_slug) -> QString
|
2022-04-15 06:32:41 +05:30
|
|
|
{
|
2022-06-19 22:56:15 +05:30
|
|
|
if(mod_slug.endsWith(".pw.toml"))
|
|
|
|
return mod_slug;
|
|
|
|
return QString("%1.pw.toml").arg(mod_slug);
|
2022-04-15 06:32:41 +05:30
|
|
|
}
|
|
|
|
|
2022-04-20 05:40:12 +05:30
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
2022-05-18 14:16:07 +05:30
|
|
|
// Helper functions 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 str property '%1' in mod metadata.").arg(entry_name);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QString tmp = var.u.s;
|
|
|
|
free(var.u.s);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-16 21:57:29 +05:30
|
|
|
auto V1::createModFormat(QDir& index_dir, ModPlatform::IndexedPack& mod_pack, ModPlatform::IndexedVersion& mod_version) -> Mod
|
2022-04-14 03:46:36 +05:30
|
|
|
{
|
|
|
|
Mod mod;
|
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
mod.slug = mod_pack.slug;
|
2022-04-14 03:46:36 +05:30
|
|
|
mod.name = mod_pack.name;
|
|
|
|
mod.filename = mod_version.fileName;
|
|
|
|
|
2022-05-08 04:09:00 +05:30
|
|
|
if(mod_pack.provider == ModPlatform::Provider::FLAME){
|
|
|
|
mod.mode = "metadata:curseforge";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mod.mode = "url";
|
|
|
|
mod.url = mod_version.downloadUrl;
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:12:01 +05:30
|
|
|
mod.hash_format = mod_version.hash_type;
|
2022-04-22 00:15:20 +05:30
|
|
|
mod.hash = mod_version.hash;
|
2022-04-14 03:46:36 +05:30
|
|
|
|
|
|
|
mod.provider = mod_pack.provider;
|
2022-05-13 20:12:08 +05:30
|
|
|
mod.file_id = mod_version.fileId;
|
|
|
|
mod.project_id = mod_pack.addonId;
|
2022-04-14 03:46:36 +05:30
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
auto V1::createModFormat(QDir& index_dir, ::Mod& internal_mod, QString slug) -> Mod
|
2022-04-16 07:07:10 +05:30
|
|
|
{
|
|
|
|
// Try getting metadata if it exists
|
2022-06-19 22:56:15 +05:30
|
|
|
Mod mod { getIndexForMod(index_dir, slug) };
|
2022-04-16 07:07:10 +05:30
|
|
|
if(mod.isValid())
|
|
|
|
return mod;
|
|
|
|
|
2022-04-21 03:15:39 +05:30
|
|
|
qWarning() << QString("Tried to create mod metadata with a Mod without metadata!");
|
2022-04-16 07:07:10 +05:30
|
|
|
|
2022-04-21 03:15:39 +05:30
|
|
|
return {};
|
2022-04-16 07:07:10 +05:30
|
|
|
}
|
|
|
|
|
2022-04-16 21:57:29 +05:30
|
|
|
void V1::updateModIndex(QDir& index_dir, Mod& mod)
|
2022-04-14 03:46:36 +05:30
|
|
|
{
|
2022-04-16 07:07:10 +05:30
|
|
|
if(!mod.isValid()){
|
|
|
|
qCritical() << QString("Tried to update metadata of an invalid mod!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:46:36 +05:30
|
|
|
// Ensure the corresponding mod's info exists, and create it if not
|
2022-05-18 14:16:07 +05:30
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
auto normalized_fname = indexFileName(mod.slug);
|
2022-05-18 14:16:07 +05:30
|
|
|
auto real_fname = getRealIndexName(index_dir, normalized_fname);
|
|
|
|
|
|
|
|
QFile index_file(index_dir.absoluteFilePath(real_fname));
|
2022-04-14 03:46:36 +05:30
|
|
|
|
2022-06-12 01:49:34 +05:30
|
|
|
if (real_fname != normalized_fname)
|
|
|
|
index_file.rename(normalized_fname);
|
|
|
|
|
2022-04-14 03:46:36 +05:30
|
|
|
// There's already data on there!
|
2022-04-16 07:07:10 +05:30
|
|
|
// 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!
|
2022-04-14 03:46:36 +05:30
|
|
|
if (index_file.exists()) { index_file.remove(); }
|
|
|
|
|
|
|
|
if (!index_file.open(QIODevice::ReadWrite)) {
|
2022-04-15 06:32:41 +05:30
|
|
|
qCritical() << QString("Could not open file %1!").arg(indexFileName(mod.name));
|
2022-04-14 03:46:36 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put TOML data into the file
|
|
|
|
QTextStream in_stream(&index_file);
|
|
|
|
auto addToStream = [&in_stream](QString&& key, QString value) { in_stream << QString("%1 = \"%2\"\n").arg(key, value); };
|
|
|
|
|
|
|
|
{
|
|
|
|
addToStream("name", mod.name);
|
|
|
|
addToStream("filename", mod.filename);
|
|
|
|
addToStream("side", mod.side);
|
|
|
|
|
|
|
|
in_stream << QString("\n[download]\n");
|
2022-05-08 04:09:00 +05:30
|
|
|
addToStream("mode", mod.mode);
|
2022-04-14 03:46:36 +05:30
|
|
|
addToStream("url", mod.url.toString());
|
|
|
|
addToStream("hash-format", mod.hash_format);
|
|
|
|
addToStream("hash", mod.hash);
|
|
|
|
|
|
|
|
in_stream << QString("\n[update]\n");
|
2022-04-20 05:40:12 +05:30
|
|
|
in_stream << QString("[update.%1]\n").arg(ProviderCaps.name(mod.provider));
|
2022-04-17 20:10:41 +05:30
|
|
|
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;
|
|
|
|
}
|
2022-04-14 03:46:36 +05:30
|
|
|
}
|
2022-05-18 14:16:07 +05:30
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
index_file.flush();
|
2022-05-18 14:16:07 +05:30
|
|
|
index_file.close();
|
2022-04-14 03:46:36 +05:30
|
|
|
}
|
2022-04-14 05:55:08 +05:30
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
void V1::deleteModIndex(QDir& index_dir, QString& mod_slug)
|
2022-04-15 06:32:41 +05:30
|
|
|
{
|
2022-06-19 22:56:15 +05:30
|
|
|
auto normalized_fname = indexFileName(mod_slug);
|
2022-05-18 14:16:07 +05:30
|
|
|
auto real_fname = getRealIndexName(index_dir, normalized_fname);
|
|
|
|
if (real_fname.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QFile index_file(index_dir.absoluteFilePath(real_fname));
|
2022-04-15 06:32:41 +05:30
|
|
|
|
2022-06-12 01:49:34 +05:30
|
|
|
if (!index_file.exists()) {
|
2022-06-19 22:56:15 +05:30
|
|
|
qWarning() << QString("Tried to delete non-existent mod metadata for %1!").arg(mod_slug);
|
2022-04-15 06:32:41 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-12 01:49:34 +05:30
|
|
|
if (!index_file.remove()) {
|
2022-06-19 22:56:15 +05:30
|
|
|
qWarning() << QString("Failed to remove metadata for mod %1!").arg(mod_slug);
|
2022-04-15 06:32:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-12 01:49:34 +05:30
|
|
|
void V1::deleteModIndex(QDir& index_dir, QVariant& mod_id)
|
|
|
|
{
|
|
|
|
for (auto& file_name : index_dir.entryList(QDir::Filter::Files)) {
|
|
|
|
auto mod = getIndexForMod(index_dir, file_name);
|
|
|
|
|
|
|
|
if (mod.mod_id() == mod_id) {
|
|
|
|
deleteModIndex(index_dir, mod.name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
auto V1::getIndexForMod(QDir& index_dir, QString slug) -> Mod
|
2022-04-14 05:55:08 +05:30
|
|
|
{
|
|
|
|
Mod mod;
|
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
auto normalized_fname = indexFileName(slug);
|
2022-05-18 14:16:07 +05:30
|
|
|
auto real_fname = getRealIndexName(index_dir, normalized_fname, true);
|
|
|
|
if (real_fname.isEmpty())
|
2022-04-16 07:07:10 +05:30
|
|
|
return {};
|
2022-05-18 14:16:07 +05:30
|
|
|
|
|
|
|
QFile index_file(index_dir.absoluteFilePath(real_fname));
|
|
|
|
|
2022-04-15 06:32:41 +05:30
|
|
|
if (!index_file.open(QIODevice::ReadOnly)) {
|
2022-06-19 22:56:15 +05:30
|
|
|
qWarning() << QString("Failed to open mod metadata for %1").arg(slug);
|
2022-04-16 07:07:10 +05:30
|
|
|
return {};
|
2022-04-15 06:32:41 +05:30
|
|
|
}
|
2022-04-14 05:55:08 +05:30
|
|
|
|
2022-04-20 04:49:51 +05:30
|
|
|
toml_table_t* table = nullptr;
|
2022-04-14 05:55:08 +05:30
|
|
|
|
2022-04-20 04:49:51 +05:30
|
|
|
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
|
2022-04-14 05:55:08 +05:30
|
|
|
char errbuf[200];
|
2022-04-22 00:17:46 +05:30
|
|
|
auto file_bytearray = index_file.readAll();
|
|
|
|
table = toml_parse(file_bytearray.data(), errbuf, sizeof(errbuf));
|
2022-04-14 05:55:08 +05:30
|
|
|
|
|
|
|
index_file.close();
|
|
|
|
|
|
|
|
if (!table) {
|
2022-06-19 22:56:15 +05:30
|
|
|
qWarning() << QString("Could not open file %1!").arg(normalized_fname);
|
2022-04-22 00:17:46 +05:30
|
|
|
qWarning() << "Reason: " << QString(errbuf);
|
2022-04-16 07:07:10 +05:30
|
|
|
return {};
|
2022-04-14 05:55:08 +05:30
|
|
|
}
|
2022-05-18 14:16:07 +05:30
|
|
|
|
2022-06-19 22:56:15 +05:30
|
|
|
mod.slug = slug;
|
|
|
|
|
2022-05-18 14:16:07 +05:30
|
|
|
{ // Basic info
|
2022-04-14 05:55:08 +05:30
|
|
|
mod.name = stringEntry(table, "name");
|
|
|
|
mod.filename = stringEntry(table, "filename");
|
|
|
|
mod.side = stringEntry(table, "side");
|
|
|
|
}
|
|
|
|
|
2022-05-18 14:16:07 +05:30
|
|
|
{ // [download] info
|
2022-04-14 05:55:08 +05:30
|
|
|
toml_table_t* download_table = toml_table_in(table, "download");
|
|
|
|
if (!download_table) {
|
|
|
|
qCritical() << QString("No [download] section found on mod metadata!");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-08 04:09:00 +05:30
|
|
|
mod.mode = stringEntry(download_table, "mode");
|
2022-04-14 05:55:08 +05:30
|
|
|
mod.url = stringEntry(download_table, "url");
|
|
|
|
mod.hash_format = stringEntry(download_table, "hash-format");
|
|
|
|
mod.hash = stringEntry(download_table, "hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // [update] info
|
|
|
|
using Provider = ModPlatform::Provider;
|
|
|
|
|
|
|
|
toml_table_t* update_table = toml_table_in(table, "update");
|
|
|
|
if (!update_table) {
|
|
|
|
qCritical() << QString("No [update] section found on mod metadata!");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-04-20 04:49:51 +05:30
|
|
|
toml_table_t* mod_provider_table = nullptr;
|
2022-04-20 05:40:12 +05:30
|
|
|
if ((mod_provider_table = toml_table_in(update_table, ProviderCaps.name(Provider::FLAME)))) {
|
2022-04-14 05:55:08 +05:30
|
|
|
mod.provider = Provider::FLAME;
|
2022-04-17 20:10:41 +05:30
|
|
|
mod.file_id = intEntry(mod_provider_table, "file-id");
|
|
|
|
mod.project_id = intEntry(mod_provider_table, "project-id");
|
2022-04-20 05:40:12 +05:30
|
|
|
} else if ((mod_provider_table = toml_table_in(update_table, ProviderCaps.name(Provider::MODRINTH)))) {
|
2022-04-14 05:55:08 +05:30
|
|
|
mod.provider = Provider::MODRINTH;
|
2022-04-17 20:10:41 +05:30
|
|
|
mod.mod_id() = stringEntry(mod_provider_table, "mod-id");
|
|
|
|
mod.version() = stringEntry(mod_provider_table, "version");
|
2022-04-14 05:55:08 +05:30
|
|
|
} else {
|
2022-04-15 06:32:41 +05:30
|
|
|
qCritical() << QString("No mod provider on mod metadata!");
|
2022-04-14 05:55:08 +05:30
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
toml_free(table);
|
|
|
|
|
|
|
|
return mod;
|
|
|
|
}
|
2022-04-16 21:57:29 +05:30
|
|
|
|
2022-06-12 01:49:34 +05:30
|
|
|
auto V1::getIndexForMod(QDir& index_dir, QVariant& mod_id) -> Mod
|
|
|
|
{
|
|
|
|
for (auto& file_name : index_dir.entryList(QDir::Filter::Files)) {
|
|
|
|
auto mod = getIndexForMod(index_dir, file_name);
|
|
|
|
|
|
|
|
if (mod.mod_id() == mod_id)
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Packwiz
|