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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ExportMrPackDialog.h"
|
2023-05-15 18:48:30 +01:00
|
|
|
#include "minecraft/mod/ModFolderModel.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-03-01 19:38:27 +00:00
|
|
|
#include "ui_ExportMrPackDialog.h"
|
|
|
|
|
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-03-01 19:38:27 +00:00
|
|
|
ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent)
|
|
|
|
: QDialog(parent), instance(instance), ui(new Ui::ExportMrPackDialog)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-03-01 21:11:04 +00:00
|
|
|
ui->name->setText(instance->name());
|
2023-03-19 11:49:09 +00:00
|
|
|
ui->summary->setText(instance->notes().split(QRegularExpression("\\r?\\n"))[0]);
|
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
|
|
|
|
connect(ui->name, &QLineEdit::textEdited, this, &ExportMrPackDialog::validate);
|
|
|
|
connect(ui->version, &QLineEdit::textEdited, this, &ExportMrPackDialog::validate);
|
|
|
|
// 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-03-02 11:25:36 +00:00
|
|
|
proxy->setSourceModel(model);
|
2023-06-24 05:40:55 +01:00
|
|
|
proxy->setFilterRegularExpression("^(?!(\\.DS_Store)|([tT]humbs\\.db)).+$");
|
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())
|
|
|
|
proxy->blockedPaths().insert(root.relativeFilePath(index.absolutePath()));
|
2023-05-15 18:48:30 +01:00
|
|
|
}
|
2023-03-08 18:10:52 +00:00
|
|
|
|
2023-03-02 11:25:36 +00:00
|
|
|
ui->treeView->setModel(proxy);
|
2023-05-15 18:48:30 +01:00
|
|
|
ui->treeView->setRootIndex(proxy->mapFromSource(model->index(instance->gameRoot())));
|
2023-03-02 11:25:36 +00:00
|
|
|
ui->treeView->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-05-15 18:48:30 +01:00
|
|
|
QHeaderView* headerView = ui->treeView->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-03-02 16:40:49 +00:00
|
|
|
ExportMrPackDialog::~ExportMrPackDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-03-02 11:25:36 +00:00
|
|
|
void ExportMrPackDialog::done(int result)
|
|
|
|
{
|
2023-03-03 11:14:57 +00:00
|
|
|
if (result == Accepted) {
|
|
|
|
const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
|
2023-03-04 19:55:38 +00:00
|
|
|
const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()),
|
|
|
|
FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
|
|
|
|
"Modrinth pack (*.mrpack *.zip)", nullptr);
|
2023-03-02 16:40:49 +00:00
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
if (output.isEmpty())
|
|
|
|
return;
|
2023-03-01 19:38:27 +00:00
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
|
|
|
|
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
|
2023-03-18 12:21:13 +00:00
|
|
|
|
|
|
|
connect(&task, &Task::failed,
|
|
|
|
[this](const QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); });
|
|
|
|
connect(&task, &Task::aborted, [this] {
|
|
|
|
CustomMessageBox::selectable(this, tr("Task aborted"), tr("The task has been aborted by the user."), QMessageBox::Information)
|
|
|
|
->show();
|
|
|
|
});
|
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
ProgressDialog progress(this);
|
|
|
|
progress.setSkipButton(true, tr("Abort"));
|
2023-03-04 11:07:07 +00:00
|
|
|
if (progress.execWithTask(&task) != QDialog::Accepted)
|
|
|
|
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
|
|
|
|
|
|
|
void ExportMrPackDialog::validate()
|
|
|
|
{
|
|
|
|
const bool invalid = ui->name->text().isEmpty() || ui->version->text().isEmpty();
|
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(invalid);
|
|
|
|
}
|