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
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-03-01 21:11:04 +00:00
|
|
|
ui->name->setText(instance->name());
|
2023-06-22 19:06:01 +01:00
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH) {
|
2023-06-22 18:03:44 +01:00
|
|
|
ui->summary->setText(instance->notes().split(QRegularExpression("\\r?\\n"))[0]);
|
2023-06-22 19:06:01 +01:00
|
|
|
} else {
|
2023-06-23 17:35:41 +01:00
|
|
|
setWindowTitle("Export CurseForge Pack");
|
|
|
|
ui->version->setText("");
|
2023-06-24 20:15:18 +01:00
|
|
|
ui->summaryLabel->setText("Author");
|
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-03-02 11:25:36 +00:00
|
|
|
proxy->setSourceModel(model);
|
2023-06-23 08:38:26 +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-06-24 21:48:18 +01:00
|
|
|
mcInstance->loaderModList()->update();
|
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-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-03-03 11:14:57 +00:00
|
|
|
if (result == Accepted) {
|
|
|
|
const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
|
2023-06-22 19:06:01 +01:00
|
|
|
QString output;
|
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH)
|
|
|
|
output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()),
|
|
|
|
FS::PathCombine(QDir::homePath(), filename + ".mrpack"), "Modrinth pack (*.mrpack *.zip)",
|
|
|
|
nullptr);
|
|
|
|
else
|
|
|
|
output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()),
|
2023-06-24 12:50:05 +01:00
|
|
|
FS::PathCombine(QDir::homePath(), filename + ".zip"), "CurseForge pack (*.zip)", nullptr);
|
2023-03-02 16:40:49 +00:00
|
|
|
|
2023-03-03 11:14:57 +00:00
|
|
|
if (output.isEmpty())
|
|
|
|
return;
|
2023-06-22 18:03:44 +01:00
|
|
|
Task* task;
|
|
|
|
if (m_provider == ModPlatform::ResourceProvider::MODRINTH)
|
|
|
|
task = new ModrinthPackExportTask(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
|
|
|
|
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
|
|
|
|
else
|
2023-06-24 20:15:18 +01:00
|
|
|
task = new FlamePackExportTask(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
|
2023-06-22 18:03:44 +01:00
|
|
|
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
|
|
|
|
|
|
|
|
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-06-23 17:35:41 +01:00
|
|
|
const bool invalid =
|
|
|
|
ui->name->text().isEmpty() || ((m_provider == ModPlatform::ResourceProvider::MODRINTH) && ui->version->text().isEmpty());
|
2023-06-05 17:52:48 +01:00
|
|
|
ui->buttonBox->button(QDialogButtonBox::Ok)->setDisabled(invalid);
|
|
|
|
}
|