Added plantxt export

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-06-25 14:11:41 +03:00
parent ef0752972a
commit 84c63f4f01
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
14 changed files with 282 additions and 176 deletions

View File

@ -490,8 +490,8 @@ set(API_SOURCES
modplatform/helpers/OverrideUtils.h
modplatform/helpers/OverrideUtils.cpp
modplatform/helpers/ExportModsToStringTask.h
modplatform/helpers/ExportModsToStringTask.cpp
modplatform/helpers/ExportToModList.h
modplatform/helpers/ExportToModList.cpp
)
set(FTB_SOURCES
@ -913,8 +913,8 @@ SET(LAUNCHER_SOURCES
ui/dialogs/ExportInstanceDialog.h
ui/dialogs/ExportMrPackDialog.cpp
ui/dialogs/ExportMrPackDialog.h
ui/dialogs/ExportModsToStringDialog.cpp
ui/dialogs/ExportModsToStringDialog.h
ui/dialogs/ExportToModListDialog.cpp
ui/dialogs/ExportToModListDialog.h
ui/dialogs/IconPickerDialog.cpp
ui/dialogs/IconPickerDialog.h
ui/dialogs/ImportResourceDialog.cpp
@ -1062,7 +1062,7 @@ qt_wrap_ui(LAUNCHER_UI
ui/dialogs/SkinUploadDialog.ui
ui/dialogs/ExportInstanceDialog.ui
ui/dialogs/ExportMrPackDialog.ui
ui/dialogs/ExportModsToStringDialog.ui
ui/dialogs/ExportToModListDialog.ui
ui/dialogs/IconPickerDialog.ui
ui/dialogs/ImportResourceDialog.ui
ui/dialogs/MSALoginDialog.ui

View File

@ -166,6 +166,13 @@ auto Mod::homeurl() const -> QString
return details().homeurl;
}
auto Mod::metaurl() const -> QString
{
if (metadata() == nullptr)
return homeurl();
return ModPlatform::getMetaURL(metadata()->provider, metadata()->project_id);
}
auto Mod::description() const -> QString
{
return details().description;

View File

@ -70,6 +70,7 @@ public:
auto provider() const -> std::optional<QString>;
auto licenses() const -> const QList<ModLicense>&;
auto issueTracker() const -> QString;
auto metaurl() const -> QString;
/** Get the intneral path to the mod's icon file*/
QString iconPath() const { return m_local_details.icon_file; };

View File

@ -77,4 +77,10 @@ auto ProviderCapabilities::hash(ResourceProvider p, QIODevice* device, QString t
return { hash.result().toHex() };
}
QString getMetaURL(ResourceProvider provider, QVariant projectID)
{
return ((provider == ModPlatform::ResourceProvider::FLAME) ? "https://www.curseforge.com/projects/" : "https://modrinth.com/mod/") +
projectID.toString();
}
} // namespace ModPlatform

View File

@ -144,6 +144,7 @@ inline auto getOverrideDeps() -> QList<OverrideDep>
{ "qvIfYCYJ", "P7dR8mSH", "API", ModPlatform::ResourceProvider::MODRINTH },
{ "lwVhp9o5", "Ha28R6CL", "KotlinLibraries", ModPlatform::ResourceProvider::MODRINTH } };
};
QString getMetaURL(ResourceProvider provider, QVariant projectID);
} // namespace ModPlatform

View File

@ -15,11 +15,10 @@
* 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 "ExportModsToStringTask.h"
#include "modplatform/ModIndex.h"
#include "ExportToModList.h"
namespace ExportToString {
QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData extraData)
namespace ExportToModList {
QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData)
{
switch (format) {
case HTML: {
@ -28,12 +27,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
auto meta = mod->metadata();
auto modName = mod->name();
if (extraData & Url) {
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
if (!url.isEmpty())
modName = QString("<a href=\"%1\">%2</a>").arg(url, modName);
}
@ -49,7 +43,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
line += " by " + mod->authors().join(", ");
lines.append(QString("<ul>%1</ul>").arg(line));
}
return QString("<html><body>\n\t%1\n</body></html>").arg(lines.join("\n\t"));
return QString("<html><body><li>\n\t%1\n</li></body></html>").arg(lines.join("\n\t"));
}
case MARKDOWN: {
QStringList lines;
@ -57,12 +51,7 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
auto meta = mod->metadata();
auto modName = mod->name();
if (extraData & Url) {
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
if (!url.isEmpty())
modName = QString("[%1](%2)").arg(modName, url);
}
@ -76,6 +65,31 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
}
if (extraData & Authors && !mod->authors().isEmpty())
line += " by " + mod->authors().join(", ");
lines << "- " + line;
}
return lines.join("\n");
}
case PLAINTXT: {
QStringList lines;
for (auto mod : mods) {
auto meta = mod->metadata();
auto modName = mod->name();
auto line = "name: " + modName + ";";
if (extraData & Url) {
auto url = mod->metaurl();
if (!url.isEmpty())
line += " url: " + url + ";";
}
if (extraData & Version) {
auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString();
if (!ver.isEmpty())
line += " version: " + QString("[%1]").arg(ver) + ";";
}
if (extraData & Authors && !mod->authors().isEmpty())
line += " authors " + mod->authors().join(", ") + ";";
lines << line;
}
return lines.join("\n");
@ -86,19 +100,13 @@ QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData ex
}
}
QString ExportModsToStringTask(QList<Mod*> mods, QString lineTemplate)
QString ExportToModList(QList<Mod*> mods, QString lineTemplate)
{
QStringList lines;
for (auto mod : mods) {
auto meta = mod->metadata();
auto modName = mod->name();
auto url = mod->homeurl();
if (meta != nullptr) {
url = (meta->provider == ModPlatform::ResourceProvider::FLAME ? "https://www.curseforge.com/minecraft/mc-mods/"
: "https://modrinth.com/mod/") +
meta->slug.remove(".pw.toml");
}
auto url = mod->metaurl();
auto ver = mod->version();
if (ver.isEmpty() && meta != nullptr)
ver = meta->version().toString();
@ -111,4 +119,4 @@ QString ExportModsToStringTask(QList<Mod*> mods, QString lineTemplate)
}
return lines.join("\n");
}
} // namespace ExportToString
} // namespace ExportToModList

View File

@ -20,14 +20,14 @@
#include <QString>
#include "minecraft/mod/Mod.h"
namespace ExportToString {
namespace ExportToModList {
enum Formats { HTML, MARKDOWN };
enum Formats { HTML, MARKDOWN, PLAINTXT, CUSTOM };
enum OptionalData {
Authors = 1 << 0,
Url = 1 << 1,
Version = 1 << 2,
};
QString ExportModsToStringTask(QList<Mod*> mods, Formats format, OptionalData extraData);
QString ExportModsToStringTask(QList<Mod*> mods, QString lineTemplate);
} // namespace ExportToString
QString ExportToModList(QList<Mod*> mods, Formats format, OptionalData extraData);
QString ExportToModList(QList<Mod*> mods, QString lineTemplate);
} // namespace ExportToModList

View File

@ -43,7 +43,7 @@
#include "FileSystem.h"
#include "MainWindow.h"
#include "ui/dialogs/ExportModsToStringDialog.h"
#include "ui/dialogs/ExportToModListDialog.h"
#include "ui_MainWindow.h"
#include <QVariant>
@ -206,7 +206,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
auto exportInstanceMenu = new QMenu(this);
exportInstanceMenu->addAction(ui->actionExportInstanceZip);
exportInstanceMenu->addAction(ui->actionExportInstanceMrPack);
exportInstanceMenu->addAction(ui->actionExportInstanceToString);
exportInstanceMenu->addAction(ui->actionExportInstanceToModList);
ui->actionExportInstance->setMenu(exportInstanceMenu);
}
@ -1418,10 +1418,10 @@ void MainWindow::on_actionExportInstanceMrPack_triggered()
}
}
void MainWindow::on_actionExportInstanceToString_triggered()
void MainWindow::on_actionExportInstanceToModList_triggered()
{
if (m_selectedInstance) {
ExportModsToStringDialog dlg(m_selectedInstance, this);
ExportToModListDialog dlg(m_selectedInstance, this);
dlg.exec();
}
}

View File

@ -157,7 +157,7 @@ private slots:
inline void on_actionExportInstance_triggered() { on_actionExportInstanceZip_triggered(); }
void on_actionExportInstanceZip_triggered();
void on_actionExportInstanceMrPack_triggered();
void on_actionExportInstanceToString_triggered();
void on_actionExportInstanceToModList_triggered();
void on_actionRenameInstance_triggered();

View File

@ -479,12 +479,12 @@
<string>Modrinth (mrpack)</string>
</property>
</action>
<action name="actionExportInstanceToString">
<action name="actionExportInstanceToModList">
<property name="icon">
<iconset theme="new"/>
</property>
<property name="text">
<string>Text</string>
<string>ModList (txt)</string>
</property>
</action>
<action name="actionCreateInstanceShortcut">

View File

@ -1,122 +0,0 @@
// 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 "ExportModsToStringDialog.h"
#include <QCheckBox>
#include <QComboBox>
#include <QTextEdit>
#include "minecraft/MinecraftInstance.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/helpers/ExportModsToStringTask.h"
#include "ui_ExportModsToStringDialog.h"
#include <QFileDialog>
#include <QFileSystemModel>
#include <QJsonDocument>
#include <QMessageBox>
#include <QPushButton>
ExportModsToStringDialog::ExportModsToStringDialog(InstancePtr instance, QWidget* parent)
: QDialog(parent), m_template_selected(false), ui(new Ui::ExportModsToStringDialog)
{
ui->setupUi(this);
ui->templateGroup->setDisabled(true);
MinecraftInstance* mcInstance = dynamic_cast<MinecraftInstance*>(instance.get());
if (mcInstance) {
mcInstance->loaderModList()->update();
connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this, mcInstance]() {
m_allMods = mcInstance->loaderModList()->allMods();
triggerImp();
});
}
connect(ui->formatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ExportModsToStringDialog::formatChanged);
connect(ui->authorsCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->versionCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->urlCheckBox, &QCheckBox::stateChanged, this, &ExportModsToStringDialog::trigger);
connect(ui->templateText, &QTextEdit::textChanged, this, &ExportModsToStringDialog::triggerImp);
connect(ui->copyButton, &QPushButton::clicked, this, [this](bool) {
this->ui->finalText->selectAll();
this->ui->finalText->copy();
});
}
ExportModsToStringDialog::~ExportModsToStringDialog()
{
delete ui;
}
void ExportModsToStringDialog::formatChanged(int index)
{
switch (index) {
case 0: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
break;
}
case 1: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
break;
}
case 2: {
ui->templateGroup->setDisabled(false);
ui->optionsGroup->setDisabled(true);
break;
}
}
triggerImp();
}
void ExportModsToStringDialog::triggerImp()
{
auto format = ExportToString::HTML;
switch (ui->formatComboBox->currentIndex()) {
case 2: {
m_template_selected = true;
ui->finalText->setPlainText(ExportToString::ExportModsToStringTask(m_allMods, ui->templateText->toPlainText()));
return;
}
case 0: {
format = ExportToString::HTML;
break;
}
case 1: {
format = ExportToString::MARKDOWN;
break;
}
default: {
return;
}
}
auto opt = 0;
if (ui->authorsCheckBox->isChecked())
opt |= ExportToString::Authors;
if (ui->versionCheckBox->isChecked())
opt |= ExportToString::Version;
if (ui->urlCheckBox->isChecked())
opt |= ExportToString::Url;
ui->finalText->setPlainText(ExportToString::ExportModsToStringTask(m_allMods, format, static_cast<ExportToString::OptionalData>(opt)));
if (!m_template_selected) {
auto exampleLine = format == ExportToString::HTML ? "<ul><a href=\"{url}\">{name}</a>[{version}] by {authors}</ul>"
: "[{name}]({url})[{version}] by {authors}";
if (ui->templateText->toPlainText() != exampleLine)
ui->templateText->setPlainText(exampleLine);
}
}

View File

@ -0,0 +1,171 @@
// 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 "ExportToModListDialog.h"
#include <QCheckBox>
#include <QComboBox>
#include <QTextEdit>
#include "FileSystem.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/mod/ModFolderModel.h"
#include "modplatform/helpers/ExportToModList.h"
#include "ui_ExportToModListDialog.h"
#include <QFileDialog>
#include <QFileSystemModel>
#include <QJsonDocument>
#include <QMessageBox>
#include <QPushButton>
ExportToModListDialog::ExportToModListDialog(InstancePtr instance, QWidget* parent)
: QDialog(parent), m_template_selected(false), name(instance->name()), ui(new Ui::ExportToModListDialog)
{
ui->setupUi(this);
ui->templateGroup->setDisabled(true);
MinecraftInstance* mcInstance = dynamic_cast<MinecraftInstance*>(instance.get());
if (mcInstance) {
mcInstance->loaderModList()->update();
connect(mcInstance->loaderModList().get(), &ModFolderModel::updateFinished, this, [this, mcInstance]() {
m_allMods = mcInstance->loaderModList()->allMods();
triggerImp();
});
}
connect(ui->formatComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ExportToModListDialog::formatChanged);
connect(ui->authorsCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->versionCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->urlCheckBox, &QCheckBox::stateChanged, this, &ExportToModListDialog::trigger);
connect(ui->templateText, &QTextEdit::textChanged, this, &ExportToModListDialog::triggerImp);
connect(ui->copyButton, &QPushButton::clicked, this, [this](bool) {
this->ui->finalText->selectAll();
this->ui->finalText->copy();
});
}
ExportToModListDialog::~ExportToModListDialog()
{
delete ui;
}
void ExportToModListDialog::formatChanged(int index)
{
switch (index) {
case 0: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->show();
format = ExportToModList::HTML;
break;
}
case 1: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->show();
format = ExportToModList::MARKDOWN;
break;
}
case 2: {
ui->templateGroup->setDisabled(true);
ui->optionsGroup->setDisabled(false);
ui->resultText->hide();
format = ExportToModList::PLAINTXT;
break;
}
case 3: {
ui->templateGroup->setDisabled(false);
ui->optionsGroup->setDisabled(true);
ui->resultText->hide();
format = ExportToModList::CUSTOM;
break;
}
}
triggerImp();
}
void ExportToModListDialog::triggerImp()
{
if (format == ExportToModList::CUSTOM) {
m_template_selected = true;
ui->finalText->setPlainText(ExportToModList::ExportToModList(m_allMods, ui->templateText->toPlainText()));
return;
}
auto opt = 0;
if (ui->authorsCheckBox->isChecked())
opt |= ExportToModList::Authors;
if (ui->versionCheckBox->isChecked())
opt |= ExportToModList::Version;
if (ui->urlCheckBox->isChecked())
opt |= ExportToModList::Url;
auto txt = ExportToModList::ExportToModList(m_allMods, format, static_cast<ExportToModList::OptionalData>(opt));
ui->finalText->setPlainText(txt);
QString exampleLine;
switch (format) {
case ExportToModList::HTML: {
exampleLine = "<ul><a href=\"{url}\">{name}</a>[{version}] by {authors}</ul>";
ui->resultText->setHtml(txt);
break;
}
case ExportToModList::MARKDOWN: {
exampleLine = "[{name}]({url})[{version}] by {authors}";
ui->resultText->setMarkdown(txt);
break;
}
case ExportToModList::PLAINTXT: {
exampleLine = "name: {name}; url: {url}; version: {version}; authors: {authors};";
break;
}
case ExportToModList::CUSTOM:
return;
}
if (!m_template_selected) {
if (ui->templateText->toPlainText() != exampleLine)
ui->templateText->setPlainText(exampleLine);
}
}
void ExportToModListDialog::done(int result)
{
if (result == Accepted) {
const QString filename = FS::RemoveInvalidFilenameChars(name);
const QString output =
QFileDialog::getSaveFileName(this, tr("Export %1").arg(name), FS::PathCombine(QDir::homePath(), filename + extension()),
"File (*.txt *.html *.md)", nullptr);
if (output.isEmpty())
return;
FS::write(output, ui->finalText->toPlainText().toUtf8());
}
QDialog::done(result);
}
QString ExportToModListDialog::extension()
{
switch (format) {
case ExportToModList::HTML:
return ".html";
case ExportToModList::MARKDOWN:
return ".md";
case ExportToModList::PLAINTXT:
return ".txt";
case ExportToModList::CUSTOM:
return ".txt";
}
return ".txt";
}

View File

@ -22,17 +22,20 @@
#include <QList>
#include "BaseInstance.h"
#include "minecraft/mod/Mod.h"
#include "modplatform/helpers/ExportToModList.h"
namespace Ui {
class ExportModsToStringDialog;
class ExportToModListDialog;
}
class ExportModsToStringDialog : public QDialog {
class ExportToModListDialog : public QDialog {
Q_OBJECT
public:
explicit ExportModsToStringDialog(InstancePtr instance, QWidget* parent = nullptr);
~ExportModsToStringDialog();
explicit ExportToModListDialog(InstancePtr instance, QWidget* parent = nullptr);
~ExportToModListDialog();
void done(int result) override;
protected slots:
void formatChanged(int index);
@ -40,7 +43,10 @@ class ExportModsToStringDialog : public QDialog {
void trigger(int) { triggerImp(); };
private:
QString extension();
QList<Mod*> m_allMods;
bool m_template_selected;
Ui::ExportModsToStringDialog* ui;
QString name;
ExportToModList::Formats format = ExportToModList::Formats::HTML;
Ui::ExportToModListDialog* ui;
};

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExportModsToStringDialog</class>
<widget class="QDialog" name="ExportModsToStringDialog">
<class>ExportToModListDialog</class>
<widget class="QDialog" name="ExportToModListDialog">
<property name="geometry">
<rect>
<x>0</x>
@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Export Modrinth Pack</string>
<string>Export Pack to ModList</string>
</property>
<property name="sizeGripEnabled">
<bool>true</bool>
@ -53,6 +53,11 @@
<string>Markdown</string>
</property>
</item>
<item>
<property name="text">
<string>Plaintext</string>
</property>
</item>
<item>
<property name="text">
<string>Custom</string>
@ -124,6 +129,13 @@
</property>
</widget>
</item>
<item>
<widget class="QTextBrowser" name="resultText">
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -141,7 +153,7 @@
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
</property>
</widget>
</item>
@ -154,7 +166,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ExportModsToStringDialog</receiver>
<receiver>ExportToModListDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
@ -167,5 +179,21 @@
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ExportToModListDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>324</x>
<y>390</y>
</hint>
<hint type="destinationlabel">
<x>324</x>
<y>206</y>
</hint>
</hints>
</connection>
</connections>
</ui>