Merge branch 'MultiMC:main' into main

This commit is contained in:
Sebastian
2022-07-19 19:18:53 -05:00
committed by GitHub
17 changed files with 807 additions and 45 deletions

View File

@@ -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

View File

@@ -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()
{

View File

@@ -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<TranslatedAction *> 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);

View File

@@ -141,6 +141,8 @@ private slots:
void on_actionScreenshots_triggered();
void on_actionCreateShortcut_triggered();
void taskEnd();
/**

View File

@@ -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 << "<center>\n";
// TODO: possibly retrieve from git history at build time?
stream << "<h3>" << QObject::tr("Developers", "About Credits") << "</h3>\n";
stream << "<p>Andrew Okin &lt;<a href='mailto:forkk@forkk.net'>forkk@forkk.net</a>&gt;</p>\n";
stream << "<p>Petr Mrázek &lt;<a href='mailto:peterix@gmail.com'>peterix@gmail.com</a>&gt;</p>\n";
stream << "<p>Sky Welch &lt;<a href='mailto:multimc@bunnies.io'>multimc@bunnies.io</a>&gt;</p>\n";
stream << "<p>Jan (02JanDal) &lt;<a href='mailto:02jandal@gmail.com'>02jandal@gmail.com</a>&gt;</p>\n";
stream << "<p>RoboSky &lt;<a href='https://twitter.com/RoboSky_'>@RoboSky_</a>&gt;</p>\n";
stream << "<br />\n";
stream << "<h3>" << QObject::tr("With thanks to", "About Credits") << "</h3>\n";
stream << "<p>Orochimarufan &lt;<a href='mailto:orochimarufan.x3@gmail.com'>orochimarufan.x3@gmail.com</a>&gt;</p>\n";
stream << "<p>TakSuyu &lt;<a href='mailto:taksuyu@gmail.com'>taksuyu@gmail.com</a>&gt;</p>\n";
stream << "<p>Kilobyte &lt;<a href='mailto:stiepen22@gmx.de'>stiepen22@gmx.de</a>&gt;</p>\n";
stream << "<p>Rootbear75 &lt;<a href='https://twitter.com/rootbear75'>@rootbear75</a>&gt;</p>\n";
stream << "<p>Zeker Zhayard &lt;<a href='https://twitter.com/zeker_zhayard'>@Zeker_Zhayard</a>&gt;</p>\n";
stream << "<br />\n";
stream << "<h3>" << QObject::tr("Original Author", "About Credits") << "</h3>\n";
stream << "<p>Andrew Okin &lt;<a href='mailto:forkk@forkk.net'>forkk@forkk.net</a>&gt;</p>\n";
stream << "<h3>" << QObject::tr("Maintainer", "About Credits") << "</h3>\n";
stream << "<p>Petr Mr&aacute;zek &lt;<a href='mailto:peterix@gmail.com'>peterix@gmail.com</a>&gt;</p>\n";
stream << "<h3>Dedicated to Erika</h3>\n";
stream << "<p>You gave all the work put into this meaning.</p>\n";
// TODO: grab contributors from git history
/*
if(!contributors.isEmpty()) {
stream << "<h3>" << QObject::tr("Contributors", "About Credits") << "</h3>\n";
for (auto &contributor : contributors)
{
stream << "<p>" << contributor << "</p>\n";
}
}
*/
if(!patrons.isEmpty()) {
stream << "<h3>" << QObject::tr("Patrons", "About Credits") << "</h3>\n";
@@ -57,6 +60,7 @@ QString getCreditsHtml(QStringList patrons)
stream << "<p>" << patron << "</p>\n";
}
}
stream << "</center>\n";
return output;
}

View File

@@ -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 <QFileDialog>
#include <BuildConfig.h>
#include <QMessageBox>
#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 <shobjidl.h>
#include <shlguid.h>
#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

View File

@@ -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 <QDialog>
#include "minecraft/auth/MinecraftAccount.h"
#include "BaseInstance.h"
#include "minecraft/ParseUtils.h"
#ifdef Q_OS_WIN
#include <windows.h>
#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
};

View File

@@ -0,0 +1,288 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2022 arthomnix
This source is subject to the Microsoft Public License (MS-PL).
Please see the COPYING.md file for more information.
-->
<ui version="4.0">
<class>CreateShortcutDialog</class>
<widget class="QDialog" name="CreateShortcutDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>796</width>
<height>232</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>796</width>
<height>230</height>
</size>
</property>
<property name="windowTitle">
<string>Create Shortcut</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item row="3" column="0">
<widget class="QCheckBox" name="launchOfflineCheckBox">
<property name="text">
<string>Launch in offline mode</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="shortcutPathLabel">
<property name="text">
<string>Shortcut path:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="shortcutPathBrowse">
<property name="text">
<string>Browse</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="shortcutPath"/>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="profileComboBox"/>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="offlineUsername"/>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="offlineUsernameCheckBox">
<property name="text">
<string>Set offline mode username:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="joinServerCheckBox">
<property name="text">
<string>Join server on launch:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="useProfileCheckBox">
<property name="text">
<string>Use specific profile:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="joinServer"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="createScriptCheckBox">
<property name="text">
<string>Create script instead of shortcut</string>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>397</x>
<y>207</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>397</x>
<y>207</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>joinServer</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>61</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>joinServerCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>61</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>launchOfflineCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>130</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>offlineUsername</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>162</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>offlineUsernameCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>162</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>profileComboBox</sender>
<signal>currentTextChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>shortcutPath</sender>
<signal>textChanged(QString)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>471</x>
<y>23</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
<connection>
<sender>useProfileCheckBox</sender>
<signal>stateChanged(int)</signal>
<receiver>CreateShortcutDialog</receiver>
<slot>updateDialogState()</slot>
<hints>
<hint type="sourcelabel">
<x>122</x>
<y>98</y>
</hint>
<hint type="destinationlabel">
<x>397</x>
<y>114</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -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) {

View File

@@ -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;
};

View File

@@ -26,6 +26,9 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="2">
<widget class="QLineEdit" name="instNameTextBox"/>
</item>
<item row="1" column="2">
<widget class="QComboBox" name="groupBox">
<property name="editable">
@@ -33,6 +36,16 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>instNameTextBox</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="groupLabel">
<property name="text">
@@ -43,19 +56,6 @@
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLineEdit" name="instNameTextBox"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="nameLabel">
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>instNameTextBox</cstring>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QToolButton" name="iconButton">
<property name="iconSize">
@@ -66,6 +66,13 @@
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QPushButton" name="resetNameButton">
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
</layout>
</item>
<item>

View File

@@ -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 <HoeDown.h>
#include <QPixmapCache>
#include <QDebug>
#include <net/NetJob.h>
#include <Application.h>
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;
}

View File

@@ -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 <QTextDocument>
#include <net/NetJob.h>
namespace Modrinth {
using Callback = std::function<void(QString)>;
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<QString, ImageLoad *> m_loading;
QStringList m_failed;
};
}

View File

@@ -17,12 +17,12 @@
#include "ModrinthModel.h"
#include "ModrinthPage.h"
#include "ModrinthDocument.h"
#include "ui/dialogs/NewInstanceDialog.h"
#include "ui_ModrinthPage.h"
#include <QKeyEvent>
#include <HoeDown.h>
#include <InstanceImportTask.h>
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();
}

View File

@@ -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();

View File

@@ -36,6 +36,9 @@
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="sizeAdjustPolicy">
<enum>QAbstractScrollArea::AdjustToContents</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>

View File

@@ -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;