aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/dialogs/ExportMrPackDialog.cpp
blob: 14518783f1c4db7fb19c336041b50067472eb2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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"
#include <QJsonDocument>
#include "FileSystem.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "ui_ExportMrPackDialog.h"

#include <QFileDialog>
#include <QFileSystemModel>
#include <QMessageBox>
#include "MMCZip.h"

ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent)
    : QDialog(parent), instance(instance), ui(new Ui::ExportMrPackDialog)
{
    ui->setupUi(this);
    ui->name->setText(instance->name());

    auto model = new QFileSystemModel(this);
    // use the game root - everything outside cannot be exported
    QString root = instance->gameRoot();
    proxy = new PackIgnoreProxy(root, this);
    proxy->setSourceModel(model);
    ui->treeView->setModel(proxy);
    ui->treeView->setRootIndex(proxy->mapFromSource(model->index(root)));
    ui->treeView->sortByColumn(0, Qt::AscendingOrder);
    model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
    model->setRootPath(root);
    auto headerView = ui->treeView->header();
    headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(0, QHeaderView::Stretch);
}

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

void ExportMrPackDialog::done(int result)
{
    if (result == Accepted)
        runExport();

    QDialog::done(result);
}

void ExportMrPackDialog::runExport()
{
    const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
    const QString output =
        QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()), FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
                                     "Modrinth modpack (*.mrpack *.zip)", nullptr);

    if (output.isEmpty())
        return;

    QFileInfoList files;
    if (!MMCZip::collectFileListRecursively(instance->gameRoot(), nullptr, &files,
                                            [this](const QString& path) { return proxy->blockedPaths().covers(path); })) {
        QMessageBox::warning(this, tr("Unable to export modpack"), tr("Could not collect list of files"));
        return;
    }

    QuaZip zip(output);
    if (!zip.open(QuaZip::mdCreate)) {
        QFile::remove(output);
        QMessageBox::warning(this, tr("Unable to export modpack"), tr("Could not create file"));
        return;
    }

    {
        QuaZipFile indexFile(&zip);
        if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo("modrinth.index.json"))) {
            QFile::remove(output);
            QMessageBox::warning(this, tr("Unable to export modpack"), tr("Could not create index"));
            return;
        }
        indexFile.write(generateIndex());
    }

    // should exist
    QDir dotMinecraft(instance->gameRoot());

    for (const QFileInfo& file : files)
        if (!JlCompress::compressFile(&zip, file.absoluteFilePath(), "overrides/" + dotMinecraft.relativeFilePath(file.absoluteFilePath())))
            QMessageBox::warning(this, tr("Unable to export modpack"), tr("Could not compress %1").arg(file.absoluteFilePath()));

    zip.close();

    if (zip.getZipError() != 0) {
        QFile::remove(output);
        QMessageBox::warning(this, tr("Unable to export modpack"), tr("A zip error occured"));
        return;
    }
}

QByteArray ExportMrPackDialog::generateIndex()
{
    QJsonObject obj;
    obj["formatVersion"] = 1;
    obj["game"] = "minecraft";
    obj["name"] = ui->name->text();
    obj["versionId"] = ui->version->text();
    obj["summary"] = ui->summary->text();

    MinecraftInstance* mc = dynamic_cast<MinecraftInstance*>(instance.get());
    if (mc) {
        auto profile = mc->getPackProfile();
        auto minecraft = profile->getComponent("net.minecraft");

        QJsonObject dependencies;
        dependencies["minecraft"] = minecraft->m_version;
        obj["dependencies"] = dependencies;
    }

    return QJsonDocument(obj).toJson(QJsonDocument::Compact);
}