diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 6beea1ce..2218b1b8 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -742,6 +742,8 @@ SET(LAUNCHER_SOURCES ui/pages/modplatform/modrinth/ModrinthData.h ui/pages/modplatform/modrinth/ModrinthModel.cpp ui/pages/modplatform/modrinth/ModrinthModel.h + ui/pages/modplatform/modrinth/ModrinthDocument.cpp + ui/pages/modplatform/modrinth/ModrinthDocument.h ui/pages/modplatform/modrinth/ModrinthPage.cpp ui/pages/modplatform/modrinth/ModrinthPage.h @@ -790,7 +792,8 @@ SET(LAUNCHER_SOURCES ui/dialogs/VersionSelectDialog.h ui/dialogs/SkinUploadDialog.cpp ui/dialogs/SkinUploadDialog.h - + ui/dialogs/CreateShortcutDialog.cpp + ui/dialogs/CreateShortcutDialog.h # GUI - widgets ui/widgets/Common.cpp @@ -888,6 +891,7 @@ qt5_wrap_ui(LAUNCHER_UI ui/dialogs/AboutDialog.ui ui/dialogs/LoginDialog.ui ui/dialogs/EditAccountDialog.ui + ui/dialogs/CreateShortcutDialog.ui ) qt5_add_resources(LAUNCHER_RESOURCES diff --git a/launcher/HoeDown.h b/launcher/HoeDown.h index b9e06ffb..a8b831a1 100644 --- a/launcher/HoeDown.h +++ b/launcher/HoeDown.h @@ -57,7 +57,7 @@ public: HoeDown() { renderer = hoedown_html_renderer_new((hoedown_html_flags) 0,0); - document = hoedown_document_new(renderer, (hoedown_extensions) 0, 8); + document = hoedown_document_new(renderer, (hoedown_extensions) HOEDOWN_EXT_TABLES, 8); } ~HoeDown() { diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index c0ba8839..139ca780 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -83,6 +83,7 @@ #include "ui/dialogs/UpdateDialog.h" #include "ui/dialogs/EditAccountDialog.h" #include "ui/dialogs/NotificationDialog.h" +#include "ui/dialogs/CreateShortcutDialog.h" #include "ui/dialogs/ExportInstanceDialog.h" #include "UpdateController.h" @@ -222,6 +223,7 @@ public: TranslatedAction actionLaunchInstanceOffline; TranslatedAction actionScreenshots; TranslatedAction actionExportInstance; + TranslatedAction actionCreateShortcut; QVector all_actions; LabeledToolButton *renameButton = nullptr; @@ -594,6 +596,13 @@ public: instanceToolBar->addSeparator(); + actionCreateShortcut = TranslatedAction(MainWindow); + actionCreateShortcut->setObjectName(QStringLiteral("actionCreateShortcut")); + actionCreateShortcut.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Create Shortcut")); + actionCreateShortcut.setTooltipId(QT_TRANSLATE_NOOP("MainWindow", "Create a shortcut that launches the selected instance")); + all_actions.append(&actionCreateShortcut); + instanceToolBar->addAction(actionCreateShortcut); + actionExportInstance = TranslatedAction(MainWindow); actionExportInstance->setObjectName(QStringLiteral("actionExportInstance")); actionExportInstance.setTextId(QT_TRANSLATE_NOOP("MainWindow", "Export Instance")); @@ -1844,6 +1853,13 @@ void MainWindow::on_actionLaunchInstance_triggered() } } +void MainWindow::on_actionCreateShortcut_triggered() { + if (m_selectedInstance) + { + CreateShortcutDialog(this, m_selectedInstance).exec(); + } +} + void MainWindow::activateInstance(InstancePtr instance) { APPLICATION->launch(instance); diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index e462c524..685adba9 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -141,6 +141,8 @@ private slots: void on_actionScreenshots_triggered(); + void on_actionCreateShortcut_triggered(); + void taskEnd(); /** diff --git a/launcher/ui/dialogs/AboutDialog.cpp b/launcher/ui/dialogs/AboutDialog.cpp index 9795c38b..ae51809b 100644 --- a/launcher/ui/dialogs/AboutDialog.cpp +++ b/launcher/ui/dialogs/AboutDialog.cpp @@ -28,27 +28,30 @@ namespace { // This is a hack, but I can't think of a better way to do this easily without screwing with QTextDocument... QString getCreditsHtml(QStringList patrons) { - QString patronsHeading = QObject::tr("Patrons", "About Credits"); QString output; QTextStream stream(&output); stream.setCodec(QTextCodec::codecForName("UTF-8")); stream << "
\n"; - // TODO: possibly retrieve from git history at build time? - stream << "

" << QObject::tr("Developers", "About Credits") << "

\n"; - stream << "

Andrew Okin <forkk@forkk.net>

\n"; - stream << "

Petr Mrázek <peterix@gmail.com>

\n"; - stream << "

Sky Welch <multimc@bunnies.io>

\n"; - stream << "

Jan (02JanDal) <02jandal@gmail.com>

\n"; - stream << "

RoboSky <@RoboSky_>

\n"; - stream << "
\n"; - stream << "

" << QObject::tr("With thanks to", "About Credits") << "

\n"; - stream << "

Orochimarufan <orochimarufan.x3@gmail.com>

\n"; - stream << "

TakSuyu <taksuyu@gmail.com>

\n"; - stream << "

Kilobyte <stiepen22@gmx.de>

\n"; - stream << "

Rootbear75 <@rootbear75>

\n"; - stream << "

Zeker Zhayard <@Zeker_Zhayard>

\n"; - stream << "
\n"; + stream << "

" << QObject::tr("Original Author", "About Credits") << "

\n"; + stream << "

Andrew Okin <forkk@forkk.net>

\n"; + + stream << "

" << QObject::tr("Maintainer", "About Credits") << "

\n"; + stream << "

Petr Mrázek <peterix@gmail.com>

\n"; + + stream << "

Dedicated to Erika

\n"; + stream << "

You gave all the work put into this meaning.

\n"; + + // TODO: grab contributors from git history + /* + if(!contributors.isEmpty()) { + stream << "

" << QObject::tr("Contributors", "About Credits") << "

\n"; + for (auto &contributor : contributors) + { + stream << "

" << contributor << "

\n"; + } + } + */ if(!patrons.isEmpty()) { stream << "

" << QObject::tr("Patrons", "About Credits") << "

\n"; @@ -57,6 +60,7 @@ QString getCreditsHtml(QStringList patrons) stream << "

" << patron << "

\n"; } } + stream << "
\n"; return output; } diff --git a/launcher/ui/dialogs/CreateShortcutDialog.cpp b/launcher/ui/dialogs/CreateShortcutDialog.cpp new file mode 100644 index 00000000..85397fea --- /dev/null +++ b/launcher/ui/dialogs/CreateShortcutDialog.cpp @@ -0,0 +1,227 @@ +/* + * Copyright 2022 arthomnix + * + * This source is subject to the Microsoft Public License (MS-PL). + * Please see the COPYING.md file for more information. + */ + +#include +#include +#include +#include "CreateShortcutDialog.h" +#include "ui_CreateShortcutDialog.h" +#include "Application.h" +#include "minecraft/auth/AccountList.h" +#include "minecraft/MinecraftInstance.h" +#include "minecraft/PackProfile.h" +#include "icons/IconList.h" + +#ifdef Q_OS_WIN +#include +#include +#endif + +CreateShortcutDialog::CreateShortcutDialog(QWidget *parent, InstancePtr instance) + :QDialog(parent), ui(new Ui::CreateShortcutDialog), m_instance(instance) +{ + ui->setupUi(this); + + QStringList accountNameList; + auto accounts = APPLICATION->accounts(); + + for (int i = 0; i < accounts->count(); i++) + { + accountNameList.append(accounts->at(i)->profileName()); + } + + ui->profileComboBox->addItems(accountNameList); + + if (accounts->defaultAccount()) + { + ui->profileComboBox->setCurrentText(accounts->defaultAccount()->profileName()); + } + + // TODO: check if version is affected by crashing when joining servers on launch, ideally in meta + + // Macs don't have any concept of a desktop shortcut, so force-enable the option to generate a shell script instead +#if defined(Q_OS_UNIX) && !defined(Q_OS_LINUX) + ui->createScriptCheckBox->setEnabled(false); + ui->createScriptCheckBox->setChecked(true); +#endif + + updateDialogState(); +} + +CreateShortcutDialog::~CreateShortcutDialog() +{ + delete ui; +} + +void CreateShortcutDialog::on_shortcutPathBrowse_clicked() +{ + QString linkExtension; +#ifdef Q_OS_UNIX + linkExtension = ui->createScriptCheckBox->isChecked() ? "sh" : "desktop"; +#endif +#ifdef Q_OS_WIN + linkExtension = ui->createScriptCheckBox->isChecked() ? "bat" : "lnk"; +#endif + QFileDialog fileDialog(this, tr("Select shortcut path"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); + fileDialog.setDefaultSuffix(linkExtension); + fileDialog.setAcceptMode(QFileDialog::AcceptSave); + fileDialog.setFileMode(QFileDialog::AnyFile); + fileDialog.selectFile(m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "." + linkExtension); + if (fileDialog.exec()) + { + ui->shortcutPath->setText(fileDialog.selectedFiles().at(0)); + } + updateDialogState(); +} + +void CreateShortcutDialog::accept() +{ + createShortcut(); + QDialog::accept(); +} + + +void CreateShortcutDialog::updateDialogState() +{ + + ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok)->setEnabled( + !ui->shortcutPath->text().isEmpty() + && (!ui->joinServerCheckBox->isChecked() || !ui->joinServer->text().isEmpty()) + && (!ui->offlineUsernameCheckBox->isChecked() || !ui->offlineUsername->text().isEmpty()) + && (!ui->useProfileCheckBox->isChecked() || !ui->profileComboBox->currentText().isEmpty()) + ); + ui->joinServer->setEnabled(ui->joinServerCheckBox->isChecked()); + ui->profileComboBox->setEnabled(ui->useProfileCheckBox->isChecked()); + ui->offlineUsernameCheckBox->setEnabled(ui->launchOfflineCheckBox->isChecked()); + ui->offlineUsername->setEnabled(ui->launchOfflineCheckBox->isChecked() && ui->offlineUsernameCheckBox->isChecked()); + if (!ui->launchOfflineCheckBox->isChecked()) + { + ui->offlineUsernameCheckBox->setChecked(false); + } +} + +QString CreateShortcutDialog::getLaunchCommand() +{ + return "\"" + QDir::toNativeSeparators(QCoreApplication::applicationFilePath()) + "\"" + + getLaunchArgs(); +} + +QString CreateShortcutDialog::getLaunchArgs() +{ + return " -d \"" + QDir::toNativeSeparators(QDir::currentPath()) + "\"" + + " -l " + m_instance->id() + + (ui->joinServerCheckBox->isChecked() ? " -s " + ui->joinServer->text() : "") + + (ui->useProfileCheckBox->isChecked() ? " -a " + ui->profileComboBox->currentText() : "") + + (ui->launchOfflineCheckBox->isChecked() ? " -o" : "") + + (ui->offlineUsernameCheckBox->isChecked() ? " -n " + ui->offlineUsername->text() : ""); +} + +void CreateShortcutDialog::createShortcut() +{ +#ifdef Q_OS_WIN + if (ui->createScriptCheckBox->isChecked()) // on windows, creating .lnk shortcuts requires specific win32 api stuff + // rather than just writing a text file + { +#endif + QString shortcutText; +#ifdef Q_OS_UNIX + // Unix shell script + if (ui->createScriptCheckBox->isChecked()) + { + shortcutText = "#!/bin/sh\n" + // FIXME: is there a way to use the launcher script instead of the raw binary here? + "cd \"" + QDir::currentPath() + "\"\n" + + getLaunchCommand() + " &\n"; + } else + // freedesktop.org desktop entry + { + // save the launcher icon to a file so we can use it in the shortcut + if (!QFileInfo::exists(QDir::currentPath() + "/icons/shortcut-icon.png")) + { + QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); + iconPixmap.save(QDir::currentPath() + "/icons/shortcut-icon.png"); + } + + shortcutText = "[Desktop Entry]\n" + "Type=Application\n" + "Name=" + m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "\n" + + "Exec=" + getLaunchCommand() + "\n" + + "Path=" + QDir::currentPath() + "\n" + + "Icon=" + QDir::currentPath() + "/icons/shortcut-icon.png\n"; + + } +#endif +#ifdef Q_OS_WIN + // Windows batch script implementation + shortcutText = "@ECHO OFF\r\n" + "CD \"" + QDir::toNativeSeparators(QDir::currentPath()) + "\"\r\n" + "START /B " + getLaunchCommand() + "\r\n"; +#endif + QFile shortcutFile(ui->shortcutPath->text()); + if (shortcutFile.open(QIODevice::WriteOnly)) + { + QTextStream stream(&shortcutFile); + stream << shortcutText; + shortcutFile.setPermissions(QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther + | QFile::WriteOwner | QFile::ExeOwner | QFile::ExeGroup); + shortcutFile.close(); + } +#ifdef Q_OS_WIN + } + else + { + if (!QFileInfo::exists(QDir::currentPath() + "/icons/shortcut-icon.ico")) + { + QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); + iconPixmap.save(QDir::currentPath() + "/icons/shortcut-icon.ico"); + } + + createWindowsLink(QDir::toNativeSeparators(QCoreApplication::applicationFilePath()).toStdString().c_str(), + QDir::toNativeSeparators(QDir::currentPath()).toStdString().c_str(), + getLaunchArgs().toStdString().c_str(), + ui->shortcutPath->text().toStdString().c_str(), + (m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME).toStdString().c_str(), + QDir::toNativeSeparators(QDir::currentPath() + "/icons/shortcut-icon.ico").toStdString().c_str() + ); + } +#endif +} + +#ifdef Q_OS_WIN +void CreateShortcutDialog::createWindowsLink(LPCSTR target, LPCSTR workingDir, LPCSTR args, LPCSTR filename, + LPCSTR desc, LPCSTR iconPath) +{ + HRESULT result; + IShellLink *link; + + CoInitialize(nullptr); + result = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &link); + if (SUCCEEDED(result)) + { + IPersistFile *file; + + link->SetPath(target); + link->SetWorkingDirectory(workingDir); + link->SetArguments(args); + link->SetDescription(desc); + link->SetIconLocation(iconPath, 0); + + result = link->QueryInterface(IID_IPersistFile, (LPVOID *) &file); + + if (SUCCEEDED(result)) + { + WCHAR path[MAX_PATH]; + MultiByteToWideChar(CP_ACP, 0, filename, -1, path, MAX_PATH); + + file->Save(path, TRUE); + file->Release(); + } + link->Release(); + } + CoUninitialize(); +} +#endif \ No newline at end of file diff --git a/launcher/ui/dialogs/CreateShortcutDialog.h b/launcher/ui/dialogs/CreateShortcutDialog.h new file mode 100644 index 00000000..a2497dd6 --- /dev/null +++ b/launcher/ui/dialogs/CreateShortcutDialog.h @@ -0,0 +1,50 @@ +/* + * Copyright 2022 arthomnix + * + * This source is subject to the Microsoft Public License (MS-PL). + * Please see the COPYING.md file for more information. + */ + +#pragma once + +#include +#include "minecraft/auth/MinecraftAccount.h" +#include "BaseInstance.h" +#include "minecraft/ParseUtils.h" + +#ifdef Q_OS_WIN +#include +#endif + +namespace Ui +{ + class CreateShortcutDialog; +} + +class CreateShortcutDialog : public QDialog +{ + Q_OBJECT + +public: + explicit CreateShortcutDialog(QWidget *parent = nullptr, InstancePtr instance = nullptr); + ~CreateShortcutDialog() override; + +private +slots: + void on_shortcutPathBrowse_clicked(); + void updateDialogState(); + void accept() override; + +private: + Ui::CreateShortcutDialog *ui; + InstancePtr m_instance; + + QString getLaunchCommand(); + QString getLaunchArgs(); + + void createShortcut(); + +#ifdef Q_OS_WIN + void createWindowsLink(LPCSTR target, LPCSTR workingDir, LPCSTR args, LPCSTR filename, LPCSTR desc, LPCSTR iconPath); +#endif +}; \ No newline at end of file diff --git a/launcher/ui/dialogs/CreateShortcutDialog.ui b/launcher/ui/dialogs/CreateShortcutDialog.ui new file mode 100644 index 00000000..568bf0a6 --- /dev/null +++ b/launcher/ui/dialogs/CreateShortcutDialog.ui @@ -0,0 +1,288 @@ + + + + CreateShortcutDialog + + + + 0 + 0 + 796 + 232 + + + + + 796 + 230 + + + + Create Shortcut + + + + + + QLayout::SetDefaultConstraint + + + + + Launch in offline mode + + + + + + + Shortcut path: + + + + + + + Browse + + + + + + + + + + + + + + + + Set offline mode username: + + + + + + + Join server on launch: + + + + + + + Use specific profile: + + + + + + + + + + + + Qt::Vertical + + + + 0 + 0 + + + + + + + + 0 + + + + + Create script instead of shortcut + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + CreateShortcutDialog + accept() + + + 397 + 207 + + + 397 + 114 + + + + + buttonBox + rejected() + CreateShortcutDialog + reject() + + + 397 + 207 + + + 397 + 114 + + + + + joinServer + textChanged(QString) + CreateShortcutDialog + updateDialogState() + + + 471 + 61 + + + 397 + 114 + + + + + joinServerCheckBox + stateChanged(int) + CreateShortcutDialog + updateDialogState() + + + 122 + 61 + + + 397 + 114 + + + + + launchOfflineCheckBox + stateChanged(int) + CreateShortcutDialog + updateDialogState() + + + 122 + 130 + + + 397 + 114 + + + + + offlineUsername + textChanged(QString) + CreateShortcutDialog + updateDialogState() + + + 471 + 162 + + + 397 + 114 + + + + + offlineUsernameCheckBox + stateChanged(int) + CreateShortcutDialog + updateDialogState() + + + 122 + 162 + + + 397 + 114 + + + + + profileComboBox + currentTextChanged(QString) + CreateShortcutDialog + updateDialogState() + + + 471 + 98 + + + 397 + 114 + + + + + shortcutPath + textChanged(QString) + CreateShortcutDialog + updateDialogState() + + + 471 + 23 + + + 397 + 114 + + + + + useProfileCheckBox + stateChanged(int) + CreateShortcutDialog + updateDialogState() + + + 122 + 98 + + + 397 + 114 + + + + + diff --git a/launcher/ui/dialogs/NewInstanceDialog.cpp b/launcher/ui/dialogs/NewInstanceDialog.cpp index 0b933fb5..28be8fee 100644 --- a/launcher/ui/dialogs/NewInstanceDialog.cpp +++ b/launcher/ui/dialogs/NewInstanceDialog.cpp @@ -103,6 +103,8 @@ NewInstanceDialog::NewInstanceDialog(const QString & initialGroup, const QString importPage->setUrl(url); } + connect(APPLICATION, &QApplication::focusChanged, this, &NewInstanceDialog::onFocusChanged); + updateDialogState(); restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get("NewInstanceGeometry").toByteArray())); @@ -150,8 +152,15 @@ NewInstanceDialog::~NewInstanceDialog() void NewInstanceDialog::setSuggestedPack(const QString& name, InstanceTask* task) { creationTask.reset(task); + + defaultInstName = name; ui->instNameTextBox->setPlaceholderText(name); + if (!instNameChanged) + { + ui->instNameTextBox->setText(name); + } + if(!task) { ui->iconButton->setIcon(APPLICATION->icons()->getIcon("default")); @@ -238,11 +247,29 @@ void NewInstanceDialog::on_iconButton_clicked() } } +void NewInstanceDialog::on_resetNameButton_clicked() +{ + ui->instNameTextBox->setText(defaultInstName); + instNameChanged = false; +} + void NewInstanceDialog::on_instNameTextBox_textChanged(const QString &arg1) { updateDialogState(); } +void NewInstanceDialog::on_instNameTextBox_textEdited(const QString &text) +{ + instNameChanged = true; +} + +void NewInstanceDialog::onFocusChanged(QWidget *, QWidget *newWidget) +{ + if (newWidget == ui->instNameTextBox && !instNameChanged) { + QTimer::singleShot(0, ui->instNameTextBox, &QLineEdit::selectAll); + } +} + void NewInstanceDialog::importIconNow() { if(importIcon) { diff --git a/launcher/ui/dialogs/NewInstanceDialog.h b/launcher/ui/dialogs/NewInstanceDialog.h index 3a100b77..a001acea 100644 --- a/launcher/ui/dialogs/NewInstanceDialog.h +++ b/launcher/ui/dialogs/NewInstanceDialog.h @@ -56,10 +56,13 @@ public: public slots: void accept() override; void reject() override; + void onFocusChanged(QWidget *, QWidget *newWidget); private slots: void on_iconButton_clicked(); + void on_resetNameButton_clicked(); void on_instNameTextBox_textChanged(const QString &arg1); + void on_instNameTextBox_textEdited(const QString &text); private: Ui::NewInstanceDialog *ui = nullptr; @@ -75,4 +78,7 @@ private: QString importIconName; void importIconNow(); + + QString defaultInstName; + bool instNameChanged = false; }; diff --git a/launcher/ui/dialogs/NewInstanceDialog.ui b/launcher/ui/dialogs/NewInstanceDialog.ui index 7fb19ff5..90d6bf39 100644 --- a/launcher/ui/dialogs/NewInstanceDialog.ui +++ b/launcher/ui/dialogs/NewInstanceDialog.ui @@ -26,6 +26,9 @@ + + + @@ -33,6 +36,16 @@ + + + + &Name: + + + instNameTextBox + + + @@ -43,19 +56,6 @@ - - - - - - - &Name: - - - instNameTextBox - - - @@ -66,6 +66,13 @@ + + + + Reset + + + diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.cpp new file mode 100644 index 00000000..f734610a --- /dev/null +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2022 Petr Mrázek + * + * This source is subject to the Microsoft Permissive License (MS-PL). + * Please see the COPYING.md file for more information. + */ + +#include "ModrinthDocument.h" + +#include +#include +#include +#include +#include + +Modrinth::ModrinthDocument::ModrinthDocument(const QString &markdown, QObject* parent) : QTextDocument(parent) { + HoeDown hoedown; + // 100 MiB + QPixmapCache::setCacheLimit(102400); + setHtml(hoedown.process(markdown.toUtf8())); +} + +QVariant Modrinth::ModrinthDocument::loadResource(int type, const QUrl& name) { + if(type == QTextDocument::ResourceType::ImageResource) { + auto pixmap = QPixmapCache::find(name.toString()); + if(!pixmap) { + requestResource(name); + return QVariant(); + } + return QVariant(*pixmap); + } + return QTextDocument::loadResource(type, name); +} + +void Modrinth::ModrinthDocument::downloadFinished(const QString& key, const QPixmap& out) { + m_loading.remove(key); + QPixmapCache::insert(key, out); + emit layoutUpdateRequired(); +} + +void Modrinth::ModrinthDocument::downloadFailed(const QString& key) { + m_failed.append(key); + m_loading.remove(key); +} + +void Modrinth::ModrinthDocument::requestResource(const QUrl& url) { + QString key = url.toString(); + if(m_loading.contains(key) || m_failed.contains(key)) + { + return; + } + + qDebug() << "Loading resource" << key; + + ImageLoad *load = new ImageLoad; + load->job = new NetJob(QString("Modrinth Image Download %1").arg(key), APPLICATION->network()); + load->job->addNetAction(Net::Download::makeByteArray(url, &load->output)); + load->key = key; + + QObject::connect(load->job.get(), &NetJob::succeeded, this, [this, load] { + QPixmap pixmap; + if(!pixmap.loadFromData(load->output)) { + qDebug() << load->output; + downloadFailed(load->key); + } + if(pixmap.width() > 800) { + pixmap = pixmap.scaledToWidth(800); + } + downloadFinished(load->key, pixmap); + }); + + QObject::connect(load->job.get(), &NetJob::failed, this, [this, load] + { + downloadFailed(load->key); + }); + + load->job->start(); + + m_loading[key] = load; +} diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.h b/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.h new file mode 100644 index 00000000..218a59a7 --- /dev/null +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthDocument.h @@ -0,0 +1,45 @@ +/* + * Copyright 2022 Petr Mrázek + * + * This source is subject to the Microsoft Permissive License (MS-PL). + * Please see the COPYING.md file for more information. + */ + +#pragma once + +#include +#include + +namespace Modrinth { + +using Callback = std::function; +struct ImageLoad { + QString key; + NetJob::Ptr job; + QByteArray output; + Callback handler; +}; + +class ModrinthDocument: public QTextDocument { + Q_OBJECT +public: + ModrinthDocument(const QString &markdown, QObject * parent = nullptr); + +signals: + void layoutUpdateRequired(); + +protected: + QVariant loadResource(int type, const QUrl & name) override; + +private: + void downloadFailed(const QString &key); + void downloadFinished(const QString &key, const QPixmap &out); + + void requestResource(const QUrl &url); + +private: + QMap m_loading; + QStringList m_failed; +}; + +} diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp index 7aebc2fa..70793185 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp @@ -17,12 +17,12 @@ #include "ModrinthModel.h" #include "ModrinthPage.h" +#include "ModrinthDocument.h" #include "ui/dialogs/NewInstanceDialog.h" #include "ui_ModrinthPage.h" #include -#include #include ModrinthPage::ModrinthPage(NewInstanceDialog *dialog, QWidget *parent) : QWidget(parent), ui(new Ui::ModrinthPage), dialog(dialog) @@ -133,14 +133,6 @@ void ModrinthPage::onPackDataChanged(const QString& id) } } -namespace { -QString processMarkdown(QString input) -{ - HoeDown hoedown; - return hoedown.process(input.toUtf8()); -} -} - QString versionToString(const Modrinth::Version& version) { switch(version.type) { case Modrinth::VersionType::Alpha: { @@ -171,7 +163,9 @@ void ModrinthPage::updateCurrentPackUI() break; } case Modrinth::LoadState::Loaded: { - ui->packDescription->setText(processMarkdown(current.body)); + auto document = new Modrinth::ModrinthDocument(current.body); + connect(document, &Modrinth::ModrinthDocument::layoutUpdateRequired, this, &ModrinthPage::forceDocumentLayout); + ui->packDescription->setDocument(document); break; } } @@ -199,3 +193,7 @@ void ModrinthPage::updateCurrentPackUI() } suggestCurrent(); } + +void ModrinthPage::forceDocumentLayout() { + ui->packDescription->document()->adjustSize(); +} diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h index 8829ce2a..ddaa2db0 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h @@ -64,6 +64,7 @@ private slots: void onSelectionChanged(QModelIndex first, QModelIndex second); void onVersionSelectionChanged(const QString & version); void onPackDataChanged(const QString &id); + void forceDocumentLayout(); private: void updateCurrentPackUI(); diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui index 7ef099d3..08dcc392 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.ui @@ -36,6 +36,9 @@ Qt::ScrollBarAlwaysOff + + QAbstractScrollArea::AdjustToContents + true diff --git a/libraries/launcher/org/multimc/onesix/OneSixLauncher.java b/libraries/launcher/org/multimc/onesix/OneSixLauncher.java index ea445995..d341cad7 100644 --- a/libraries/launcher/org/multimc/onesix/OneSixLauncher.java +++ b/libraries/launcher/org/multimc/onesix/OneSixLauncher.java @@ -21,6 +21,7 @@ import java.applet.Applet; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -143,12 +144,14 @@ public class OneSixLauncher implements Launcher String[] paramsArray = mcparams.toArray(new String[mcparams.size()]); try { - mc.getMethod("main", String[].class).invoke(null, (Object) paramsArray); + Method meth = mc.getMethod("main", String[].class); + meth.setAccessible(true); + meth.invoke(null, (Object) paramsArray); return 0; } catch (Exception e) { Utils.log("Failed to invoke the Minecraft main class:", "Fatal"); - e.printStackTrace(System.err); + (e instanceof InvocationTargetException ? e.getCause() : e).printStackTrace(System.err); return -1; } } @@ -195,7 +198,8 @@ public class OneSixLauncher implements Launcher try { meth = mc.getMethod("main", String[].class); - } catch (NoSuchMethodException e) + meth.setAccessible(true); + } catch (NoSuchMethodException | SecurityException e) { System.err.println("Failed to acquire the main method:"); e.printStackTrace(System.err); @@ -211,7 +215,7 @@ public class OneSixLauncher implements Launcher } catch (Exception e) { System.err.println("Failed to start Minecraft:"); - e.printStackTrace(System.err); + (e instanceof InvocationTargetException ? e.getCause() : e).printStackTrace(System.err); return -1; } return 0;