2023-03-01 19:38:27 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2023-06-24 12:54:39 +01:00
|
|
|
#include "ExportPackDialog.h"
|
2023-05-15 18:48:30 +01:00
|
|
|
#include "minecraft/mod/ModFolderModel.h"
|
2023-06-22 18:03:44 +01:00
|
|
|
#include "modplatform/ModIndex.h"
|
|
|
|
#include "modplatform/flame/FlamePackExportTask.h"
|
2023-03-18 12:21:13 +00:00
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
2023-03-02 19:48:41 +00:00
|
|
|
#include "ui/dialogs/ProgressDialog.h"
|
2023-06-24 13:02:00 +01:00
|
|
|
#include "ui_ExportPackDialog.h"
|
2023-03-01 19:38:27 +00:00
|
|
|
|
2023-03-02 16:40:49 +00:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileSystemModel>
|
2023-03-02 19:48:41 +00:00
|
|
|
#include <QJsonDocument>
|
2023-03-02 16:40:49 +00:00
|
|
|
#include <QMessageBox>
|
2023-06-05 17:52:48 +01:00
|
|
|
#include <QPushButton>
|
2023-06-03 13:39:42 +01:00
|
|
|
#include "FastFileIconProvider.h"
|
2023-03-02 19:48:41 +00:00
|
|
|
#include "FileSystem.h"
|
2023-03-02 16:40:49 +00:00
|
|
|
#include "MMCZip.h"
|
2023-03-02 19:48:41 +00:00
|
|
|
#include "modplatform/modrinth/ModrinthPackExportTask.h"
|
2023-03-02 16:40:49 +00:00
|
|
|
|
2023-06-24 12:54:39 +01:00
|
|
|
ExportPackDialog::ExportPackDialog(InstancePtr instance, QWidget* parent, ModPlatform::ResourceProvider provider)
|
|
|
|
: QDialog(parent), instance(instance), ui(new Ui::ExportPackDialog), m_provider(provider)
|
2023-03-01 19:38:27 +00:00
|
|
|
{
|
2023-08-12 21:18:22 +01:00
|
|
|
Q_ASSERT(m_provider == ModPlatform::ResourceProvider::MODRINTH || m_provider == ModPlatform::ResourceProvider::FLAME);
|
|
|
|
|
2023-03-01 19:38:27 +00:00
|
|
|
ui->setupUi(this);
|
2023-08-12 21:18:22 +01:00
|
|
|
ui->name->setPlaceholderText(instance->name());
|
|
|
|
ui->name->setText(instance->settings()->get("ExportName").toString());
|
|
|
|
ui->version->setText(instance->settings()->get("ExportVersion").toString());
|
|
|
|
ui->optionalFiles->setChecked(instance->settings()->get("ExportOptionalFiles").toBool());
|
|
|
|
|
2023-06-22 19:06:01 +01:00
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH) {
|
2023-08-12 21:18:22 +01:00
|
|
|
setWindowTitle(tr("Export Modrinth Pack"));
|
|
|
|
ui->summary->setText(instance->settings()->get("ExportSummary").toString());
|
2023-06-22 19:06:01 +01:00
|
|
|
} else {
|
2023-08-12 21:18:22 +01:00
|
|
|
setWindowTitle(tr("Export CurseForge Pack"));
|
|
|
|
ui->summaryLabel->setText(tr("&Author"));
|
|
|
|
ui->summary->setText(instance->settings()->get("ExportAuthor").toString());
|
2023-06-22 19:06:01 +01:00
|
|
|
}
|
2023-03-02 11:25:36 +00:00
|
|
|
|
2023-06-05 17:52:48 +01:00
|
|
|
// ensure a valid pack is generated
|
|
|
|
// the name and version fields mustn't be empty
|
2023-06-24 12:54:39 +01:00
|
|
|
connect(ui->name, &QLineEdit::textEdited, this, &ExportPackDialog::validate);
|
|
|
|
connect(ui->version, &QLineEdit::textEdited, this, &ExportPackDialog::validate);
|
2023-06-05 17:52:48 +01:00
|
|
|
// the instance name can technically be empty
|
|
|
|
validate();
|
|
|
|
|
2023-05-15 18:48:30 +01:00
|
|
|
QFileSystemModel* model = new QFileSystemModel(this);
|
2023-06-03 13:39:42 +01:00
|
|
|
model->setIconProvider(&icons);
|
|
|
|
|
2023-03-02 11:25:36 +00:00
|
|
|
// use the game root - everything outside cannot be exported
|
2023-05-15 18:48:30 +01:00
|
|
|
const QDir root(instance->gameRoot());
|
|
|
|
proxy = new FileIgnoreProxy(instance->gameRoot(), this);
|
2023-07-27 00:42:14 +01:00
|
|
|
proxy->ignoreFilesWithPath().insert({ "logs", "crash-reports", ".cache", ".fabric", ".quilt" });
|
2023-06-29 22:01:36 +01:00
|
|
|
proxy->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" });
|
2023-03-02 11:25:36 +00:00
|
|
|
proxy->setSourceModel(model);
|
2023-03-08 18:10:52 +00:00
|
|
|
|
2023-05-15 18:48:30 +01:00
|
|
|
const QDir::Filters filter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
2023-03-08 18:10:52 +00:00
|
|
|
|
2023-05-15 18:48:30 +01:00
|
|
|
for (const QString& file : root.entryList(filter)) {
|
2023-03-08 18:10:52 +00:00
|
|
|
if (!(file == "mods" || file == "coremods" || file == "datapacks" || file == "config" || file == "options.txt" ||
|
|
|
|
file == "servers.dat"))
|
|
|
|
proxy->blockedPaths().insert(file);
|
|
|
|
}
|
|
|
|
|
2023-05-15 18:48:30 +01:00
|
|
|
MinecraftInstance* mcInstance = dynamic_cast<MinecraftInstance*>(instance.get());
|
|
|
|
if (mcInstance) {
|
2023-05-15 19:15:56 +01:00
|
|
|
const QDir index = mcInstance->loaderModList()->indexDir();
|
|
|
|
if (index.exists())
|
2023-08-28 17:25:07 +01:00
|
|
|
proxy->ignoreFilesWithPath().insert(root.relativeFilePath(index.absolutePath()));
|
2023-05-15 18:48:30 +01:00
|
|
|
}
|
2023-03-08 18:10:52 +00:00
|
|
|
|
2023-08-12 21:18:22 +01:00
|
|
|
ui->files->setModel(proxy);
|
|
|
|
ui->files->setRootIndex(proxy->mapFromSource(model->index(instance->gameRoot())));
|
|
|
|
ui->files->sortByColumn(0, Qt::AscendingOrder);
|
2023-03-08 18:10:52 +00:00
|
|
|
|
|
|
|
model->setFilter(filter);
|
2023-05-15 18:48:30 +01:00
|
|
|
model->setRootPath(instance->gameRoot());
|
2023-03-08 18:10:52 +00:00
|
|
|
|
2023-08-12 21:18:22 +01:00
|
|
|
QHeaderView* headerView = ui->files->header();
|
2023-03-02 11:25:36 +00:00
|
|
|
headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
headerView->setSectionResizeMode(0, QHeaderView::Stretch);
|
2023-03-01 21:11:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:54:39 +01:00
|
|
|
ExportPackDialog::~ExportPackDialog()
|
2023-03-02 16:40:49 +00:00
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-06-24 12:54:39 +01:00
|
|
|
void ExportPackDialog::done(int result)
|
2023-03-02 11:25:36 +00:00
|
|
|
{
|
2023-08-12 21:18:22 +01:00
|
|
|
auto settings = instance->settings();
|
|
|
|
settings->set("ExportName", ui->name->text());
|
|
|
|
settings->set("ExportVersion", ui->version->text());
|
|
|
|
settings->set(m_provider == ModPlatform::ResourceProvider::FLAME ? "ExportAuthor" : "ExportSummary", ui->summary->text());
|
|
|
|
settings->set("ExportOptionalFiles", ui->optionalFiles->isChecked());
|
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
if (result == Accepted) {
|
2023-08-12 21:18:22 +01:00
|
|
|
const QString name = ui->name->text().isEmpty() ? instance->name() : ui->name->text();
|
|
|
|
const QString filename = FS::RemoveInvalidFilenameChars(name);
|
|
|
|
|
2023-06-22 19:06:01 +01:00
|
|
|
QString output;
|
2023-08-12 21:18:22 +01:00
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH) {
|
|
|
|
output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
|
|
|
|
"Modrinth pack (*.mrpack *.zip)", nullptr);
|
2023-08-28 17:09:40 +01:00
|
|
|
if (output.isEmpty())
|
|
|
|
return;
|
2023-08-12 21:18:22 +01:00
|
|
|
if (!(output.endsWith(".zip") || output.endsWith(".mrpack")))
|
|
|
|
output.append(".mrpack");
|
|
|
|
} else {
|
|
|
|
output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + ".zip"),
|
|
|
|
"CurseForge pack (*.zip)", nullptr);
|
2023-08-28 17:09:40 +01:00
|
|
|
if (output.isEmpty())
|
|
|
|
return;
|
2023-08-12 21:18:22 +01:00
|
|
|
if (!output.endsWith(".zip"))
|
|
|
|
output.append(".zip");
|
|
|
|
}
|
2023-03-02 16:40:49 +00:00
|
|
|
|
2023-06-22 18:03:44 +01:00
|
|
|
Task* task;
|
2023-08-12 21:18:22 +01:00
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH) {
|
|
|
|
task = new ModrinthPackExportTask(name, ui->version->text(), ui->summary->text(), ui->optionalFiles->isChecked(), instance,
|
|
|
|
output, std::bind(&FileIgnoreProxy::filterFile, proxy, std::placeholders::_1));
|
|
|
|
} else {
|
|
|
|
task = new FlamePackExportTask(name, ui->version->text(), ui->summary->text(), ui->optionalFiles->isChecked(), instance, output,
|
2023-07-02 10:50:45 +01:00
|
|
|
std::bind(&FileIgnoreProxy::filterFile, proxy, std::placeholders::_1));
|
2023-08-12 21:18:22 +01:00
|
|
|
}
|
2023-06-22 18:03:44 +01:00
|
|
|
|
|
|
|
connect(task, &Task::failed,
|
2023-03-18 12:21:13 +00:00
|
|
|
[this](const QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); });
|
2023-06-22 18:03:44 +01:00
|
|
|
connect(task, &Task::aborted, [this] {
|
2023-03-18 12:21:13 +00:00
|
|
|
CustomMessageBox::selectable(this, tr("Task aborted"), tr("The task has been aborted by the user."), QMessageBox::Information)
|
|
|
|
->show();
|
|
|
|
});
|
2023-06-22 18:03:44 +01:00
|
|
|
connect(task, &Task::finished, [task] { task->deleteLater(); });
|
2023-03-18 12:21:13 +00:00
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
ProgressDialog progress(this);
|
|
|
|
progress.setSkipButton(true, tr("Abort"));
|
2023-06-22 18:03:44 +01:00
|
|
|
if (progress.execWithTask(task) != QDialog::Accepted)
|
2023-03-04 11:07:07 +00:00
|
|
|
return;
|
2023-03-03 11:14:57 +00:00
|
|
|
}
|
2023-03-02 16:40:49 +00:00
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
QDialog::done(result);
|
2023-04-17 10:16:03 +01:00
|
|
|
}
|
2023-06-05 17:52:48 +01:00
|
|
|
|
2023-06-24 12:54:39 +01:00
|
|
|
void ExportPackDialog::validate()
|
2023-06-05 17:52:48 +01:00
|
|
|
{
|
2023-08-12 21:18:22 +01:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)
|
|
|
|
->setDisabled(m_provider == ModPlatform::ResourceProvider::MODRINTH && ui->version->text().isEmpty());
|
2023-06-05 17:52:48 +01:00
|
|
|
}
|