diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 2f72e0d3..bc0cce66 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -778,7 +778,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 @@ -876,6 +877,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/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/CreateShortcutDialog.cpp b/launcher/ui/dialogs/CreateShortcutDialog.cpp new file mode 100644 index 00000000..f6711397 --- /dev/null +++ b/launcher/ui/dialogs/CreateShortcutDialog.cpp @@ -0,0 +1,248 @@ +/* + * 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()); + } + + // check if instance is affected by a bug causing it to crash when trying to join a server on launch + // TODO: implement this check in meta + if (m_instance->typeName() == "Minecraft") + { + try + { + MinecraftInstancePtr minecraftInstance = qobject_pointer_cast(m_instance); + minecraftInstance->getPackProfile()->reload(Net::Mode::Online); + QDateTime versionDate = minecraftInstance->getPackProfile()->getComponent("net.minecraft")->getReleaseDateTime(); + bool enableJoinServer = (versionDate < MC_145102_START) + || (versionDate >= MC_145102_END && versionDate < MC_228828_START) + || (versionDate >= MC_228828_END); + ui->joinServerCheckBox->setEnabled(enableJoinServer); + ui->joinServer->setEnabled(enableJoinServer); + } + catch (const Exception &e) + { + QMessageBox::critical(this, tr("Error"), e.cause()); + } + catch (...) + { + QMessageBox::critical(this, tr("Error"), tr("Failed to load pack profile to check version!")); + } + } + + // 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 " -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" + "cd \"" + QCoreApplication::applicationDirPath() + "\"\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(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.png")) + { + QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); + iconPixmap.save(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.png"); + } + + shortcutText = "[Desktop Entry]\n" + "Type=Application\n" + "Name=" + m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME + "\n" + + "Exec=" + getLaunchCommand() + "\n" + + "Path=" + QCoreApplication::applicationDirPath() + "\n" + + "Icon=" + QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.png\n"; + + } +#endif +#ifdef Q_OS_WIN + // Windows batch script implementation + shortcutText = "@ECHO OFF\r\n" + "CD \"" + QDir::toNativeSeparators(QCoreApplication::applicationDirPath()) + "\"\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(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.ico")) + { + QPixmap iconPixmap = QIcon(":/logo.svg").pixmap(64, 64); + iconPixmap.save(QCoreApplication::applicationDirPath() + "/icons/shortcut-icon.ico"); + } + + createWindowsLink(QDir::toNativeSeparators(QCoreApplication::applicationFilePath()).toStdString().c_str(), + QDir::toNativeSeparators(QCoreApplication::applicationDirPath()).toStdString().c_str(), + getLaunchArgs().toStdString().c_str(), + ui->shortcutPath->text().toStdString().c_str(), + (m_instance->name() + " - " + BuildConfig.LAUNCHER_DISPLAYNAME).toStdString().c_str(), + QDir::toNativeSeparators(QCoreApplication::applicationDirPath() + "/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..d27e94cd --- /dev/null +++ b/launcher/ui/dialogs/CreateShortcutDialog.h @@ -0,0 +1,56 @@ +/* + * 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 + +// Dates when the game was affected by bugs that crashed the game when using the option to join a server on startup +const QDateTime MC_145102_START = timeFromS3Time("2019-02-20T14:56:58+00:00"); +const QDateTime MC_145102_END = timeFromS3Time("2020-05-14T08:16:26+00:00"); +const QDateTime MC_228828_START = timeFromS3Time("2021-03-10T15:24:38+00:00"); +const QDateTime MC_228828_END = timeFromS3Time("2021-06-18T12:24:40+00:00"); + +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 + + + + +