Skip to content

Commit

Permalink
Merge pull request #1563 from Trial97/modrinth_pack
Browse files Browse the repository at this point in the history
Pack import fixes and improvements
  • Loading branch information
TayouVR committed Oct 13, 2023
2 parents ca4b58d + 17f696b commit 7015b8f
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 41 deletions.
4 changes: 4 additions & 0 deletions launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ SET(LAUNCHER_SOURCES
ui/pages/modplatform/ImportPage.cpp
ui/pages/modplatform/ImportPage.h

ui/pages/modplatform/OptionalModDialog.cpp
ui/pages/modplatform/OptionalModDialog.h

ui/pages/modplatform/modrinth/ModrinthResourceModels.cpp
ui/pages/modplatform/modrinth/ModrinthResourceModels.h
ui/pages/modplatform/modrinth/ModrinthResourcePages.cpp
Expand Down Expand Up @@ -1068,6 +1071,7 @@ qt_wrap_ui(LAUNCHER_UI
ui/pages/modplatform/legacy_ftb/Page.ui
ui/pages/modplatform/import_ftb/ImportFTBPage.ui
ui/pages/modplatform/ImportPage.ui
ui/pages/modplatform/OptionalModDialog.ui
ui/pages/modplatform/modrinth/ModrinthPage.ui
ui/pages/modplatform/technic/TechnicPage.ui
ui/widgets/InstanceCardWidget.ui
Expand Down
2 changes: 1 addition & 1 deletion launcher/modplatform/atlauncher/ATLPackIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ void ATLauncher::loadIndexedPack(ATLauncher::IndexedPack& m, QJsonObject& obj)
m.system = Json::ensureBoolean(obj, QString("system"), false);
m.description = Json::ensureString(obj, "description", "");

m.safeName = Json::requireString(obj, "name").replace(QRegularExpression("[^A-Za-z0-9]"), "");
m.safeName = Json::requireString(obj, "name").replace(QRegularExpression("[^A-Za-z0-9]"), "").toLower() + ".png";
}
29 changes: 25 additions & 4 deletions launcher/modplatform/flame/FlameInstanceCreationTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "minecraft/World.h"
#include "minecraft/mod/tasks/LocalResourceParse.h"
#include "net/ApiDownload.h"
#include "ui/pages/modplatform/OptionalModDialog.h"

static const FlameAPI api;

Expand Down Expand Up @@ -509,13 +510,33 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
{
m_files_job.reset(new NetJob(tr("Mod Download Flame"), APPLICATION->network()));
for (const auto& result : m_mod_id_resolver->getResults().files) {
QString filename = result.fileName;
auto results = m_mod_id_resolver->getResults().files;

QStringList optionalFiles;
for (auto& result : results) {
if (!result.required) {
filename += ".disabled";
optionalFiles << FS::PathCombine(result.targetFolder, result.fileName);
}
}

QStringList selectedOptionalMods;
if (!optionalFiles.empty()) {
OptionalModDialog optionalModDialog(m_parent, optionalFiles);
if (optionalModDialog.exec() == QDialog::Rejected) {
emitAborted();
loop.quit();
return;
}

selectedOptionalMods = optionalModDialog.getResult();
}
for (const auto& result : results) {
auto relpath = FS::PathCombine(result.targetFolder, result.fileName);
if (!result.required && !selectedOptionalMods.contains(relpath)) {
relpath += ".disabled";
}

auto relpath = FS::PathCombine("minecraft", result.targetFolder, filename);
relpath = FS::PathCombine("minecraft", relpath);
auto path = FS::PathCombine(m_stagingPath, relpath);

switch (result.type) {
Expand Down
4 changes: 3 additions & 1 deletion launcher/modplatform/flame/FlamePackIndex.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "FlamePackIndex.h"
#include <QFileInfo>
#include <QUrl>

#include "Json.h"

Expand All @@ -9,8 +11,8 @@ void Flame::loadIndexedPack(Flame::IndexedPack& pack, QJsonObject& obj)
pack.description = Json::ensureString(obj, "summary", "");

auto logo = Json::requireObject(obj, "logo");
pack.logoName = Json::requireString(logo, "title");
pack.logoUrl = Json::requireString(logo, "thumbnailUrl");
pack.logoName = Json::requireString(obj, "slug") + "." + QFileInfo(QUrl(pack.logoUrl).fileName()).suffix();

auto authors = Json::requireArray(obj, "authors");
for (auto authorIter : authors) {
Expand Down
2 changes: 1 addition & 1 deletion launcher/modplatform/flame/PackManifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct File {

int projectId = 0;
int fileId = 0;
// NOTE: the opposite to 'optional'. This is at the time of writing unused.
// NOTE: the opposite to 'optional'
bool required = true;
QString hash;
// NOTE: only set on blocked files ! Empty otherwise.
Expand Down
42 changes: 27 additions & 15 deletions launcher/modplatform/modrinth/ModrinthInstanceCreationTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

#include "modplatform/helpers/OverrideUtils.h"

#include "modplatform/modrinth/ModrinthPackManifest.h"
#include "net/ChecksumValidator.h"

#include "net/ApiDownload.h"
#include "net/NetJob.h"
#include "settings/INISettingsObject.h"

#include "ui/dialogs/CustomMessageBox.h"
#include "ui/pages/modplatform/OptionalModDialog.h"

#include <QAbstractButton>
#include <vector>

bool ModrinthCreationTask::abort()
{
Expand Down Expand Up @@ -319,10 +322,10 @@ bool ModrinthCreationTask::parseManifest(const QString& index_path,
}

auto jsonFiles = Json::requireIsArrayOf<QJsonObject>(obj, "files", "modrinth.index.json");
bool had_optional = false;
std::vector<Modrinth::File> optionalFiles;
for (const auto& modInfo : jsonFiles) {
Modrinth::File file;
file.path = Json::requireString(modInfo, "path");
file.path = Json::requireString(modInfo, "path").replace("\\", "/");

auto env = Json::ensureObject(modInfo, "env");
// 'env' field is optional
Expand All @@ -331,18 +334,7 @@ bool ModrinthCreationTask::parseManifest(const QString& index_path,
if (support == "unsupported") {
continue;
} else if (support == "optional") {
// TODO: Make a review dialog for choosing which ones the user wants!
if (!had_optional && show_optional_dialog) {
had_optional = true;
auto info = CustomMessageBox::selectable(
m_parent, tr("Optional mod detected!"),
tr("One or more mods from this modpack are optional. They will be downloaded, but disabled by default!"),
QMessageBox::Information);
info->exec();
}

if (file.path.endsWith(".jar"))
file.path += ".disabled";
file.required = false;
}
}

Expand Down Expand Up @@ -385,9 +377,29 @@ bool ModrinthCreationTask::parseManifest(const QString& index_path,
}
}

files.push_back(file);
(file.required ? files : optionalFiles).push_back(file);
}

if (!optionalFiles.empty()) {
QStringList oFiles;
for (auto file : optionalFiles)
oFiles.push_back(file.path);
OptionalModDialog optionalModDialog(m_parent, oFiles);
if (optionalModDialog.exec() == QDialog::Rejected) {
emitAborted();
return false;
}

auto selectedMods = optionalModDialog.getResult();
for (auto file : optionalFiles) {
if (selectedMods.contains(file.path)) {
file.required = true;
} else {
file.path += ".disabled";
}
files.push_back(file);
}
}
if (set_internal_data) {
auto dependencies = Json::requireObject(obj, "dependencies", "modrinth.index.json");
for (auto it = dependencies.begin(), end = dependencies.end(); it != end; ++it) {
Expand Down
3 changes: 2 additions & 1 deletion launcher/modplatform/modrinth/ModrinthPackManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

#include "ModrinthPackManifest.h"
#include <QFileInfo>
#include "Json.h"

#include "modplatform/modrinth/ModrinthAPI.h"
Expand All @@ -56,8 +57,8 @@ void loadIndexedPack(Modpack& pack, QJsonObject& obj)
pack.description = Json::ensureString(obj, "description");
auto temp_author_name = Json::ensureString(obj, "author");
pack.author = std::make_tuple(temp_author_name, api.getAuthorURL(temp_author_name));
pack.iconName = QString("modrinth_%1").arg(Json::ensureString(obj, "slug"));
pack.iconUrl = Json::ensureString(obj, "icon_url");
pack.iconName = QString("modrinth_%1.%2").arg(Json::ensureString(obj, "slug"), QFileInfo(pack.iconUrl.fileName()).suffix());
}

void loadIndexedInfo(Modpack& pack, QJsonObject& obj)
Expand Down
1 change: 1 addition & 0 deletions launcher/modplatform/modrinth/ModrinthPackManifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct File {
QCryptographicHash::Algorithm hashAlgorithm;
QByteArray hash;
QQueue<QUrl> downloads;
bool required = true;
};

struct DonationData {
Expand Down
3 changes: 1 addition & 2 deletions launcher/ui/dialogs/NewInstanceDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ void NewInstanceDialog::setSuggestedIcon(const QString& key)

InstanceTask* NewInstanceDialog::extractTask()
{
InstanceTask* extracted = creationTask.get();
creationTask.release();
InstanceTask* extracted = creationTask.release();

InstanceName inst_name(ui->instNameTextBox->placeholderText().trimmed(), importVersion);
inst_name.setName(ui->instNameTextBox->text().trimmed());
Expand Down
63 changes: 63 additions & 0 deletions launcher/ui/pages/modplatform/OptionalModDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@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/>.
*/

#include "OptionalModDialog.h"
#include "ui_OptionalModDialog.h"

OptionalModDialog::OptionalModDialog(QWidget* parent, const QStringList& mods) : QDialog(parent), ui(new Ui::OptionalModDialog)
{
ui->setupUi(this);
for (const QString& mod : mods) {
auto item = new QListWidgetItem(mod, ui->list);
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(Qt::Unchecked);
item->setData(Qt::UserRole, mod);
}

connect(ui->selectAllButton, &QPushButton::clicked, ui->list, [this] {
for (int i = 0; i < ui->list->count(); i++)
ui->list->item(i)->setCheckState(Qt::Checked);
});
connect(ui->clearAllButton, &QPushButton::clicked, ui->list, [this] {
for (int i = 0; i < ui->list->count(); i++)
ui->list->item(i)->setCheckState(Qt::Unchecked);
});
connect(ui->list, &QListWidget::itemActivated, [](QListWidgetItem* item) {
if (item->checkState() == Qt::Checked)
item->setCheckState(Qt::Unchecked);
else
item->setCheckState(Qt::Checked);
});
}

OptionalModDialog::~OptionalModDialog()
{
delete ui;
}

QStringList OptionalModDialog::getResult()
{
QStringList result;
result.reserve(ui->list->count());
for (int i = 0; i < ui->list->count(); i++) {
auto item = ui->list->item(i);
if (item->checkState() == Qt::Checked)
result.append(item->data(Qt::UserRole).toString());
}
return result;
}
39 changes: 39 additions & 0 deletions launcher/ui/pages/modplatform/OptionalModDialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@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/>.
*/

#pragma once

#include <QAbstractListModel>
#include <QDialog>

namespace Ui {
class OptionalModDialog;
}

class OptionalModDialog : public QDialog {
Q_OBJECT

public:
OptionalModDialog(QWidget* parent, const QStringList& mods);
~OptionalModDialog() override;

QStringList getResult();

private:
Ui::OptionalModDialog* ui;
};

0 comments on commit 7015b8f

Please sign in to comment.