Версия с упрощённым GUI
This commit is contained in:
43
CMakeLists.txt
Normal file
43
CMakeLists.txt
Normal file
@@ -0,0 +1,43 @@
|
||||
cmake_minimum_required(VERSION 3.19)
|
||||
project(minecraft-launcher LANGUAGES CXX)
|
||||
|
||||
find_package(Qt6 6.5 REQUIRED COMPONENTS Core Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(minecraft-launcher
|
||||
WIN32 MACOSX_BUNDLE
|
||||
|
||||
|
||||
|
||||
|
||||
main.cpp
|
||||
mainwindow.h mainwindow.cpp
|
||||
settingsdialog.h settingsdialog.cpp
|
||||
settingsdialog.ui
|
||||
mainwindow.ui
|
||||
|
||||
|
||||
|
||||
)
|
||||
|
||||
target_link_libraries(minecraft-launcher
|
||||
PRIVATE
|
||||
Qt::Core
|
||||
Qt::Widgets
|
||||
)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
install(TARGETS minecraft-launcher
|
||||
BUNDLE DESTINATION .
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
|
||||
qt_generate_deploy_app_script(
|
||||
TARGET minecraft-launcher
|
||||
OUTPUT_SCRIPT deploy_script
|
||||
NO_UNSUPPORTED_PLATFORM_ERROR
|
||||
)
|
||||
install(SCRIPT ${deploy_script})
|
||||
10
main.cpp
Normal file
10
main.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "mainwindow.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
||||
170
mainwindow.cpp
Normal file
170
mainwindow.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h" // Подключаем сгенерированный заголовочный файл
|
||||
#include "settingsdialog.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QDesktopServices>
|
||||
#include <QUrl>
|
||||
#include <QDir>
|
||||
#include <QInputDialog> // Для диалога выбора
|
||||
#include <QFileInfo> // Для проверки файла
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, ui(new Ui::MainWindow) // Инициализируем указатель на UI
|
||||
{
|
||||
ui->setupUi(this); // Загружаем интерфейс из .ui файла
|
||||
|
||||
// Начальная настройка виджетов
|
||||
ui->progressBar->setRange(0, 0); // Неопределенный прогресс-бар
|
||||
ui->progressBar->setVisible(false);
|
||||
|
||||
// Инициализация логики
|
||||
settings = new QSettings("GaleonDev", "MinecraftLauncher", this);
|
||||
process = new QProcess(this);
|
||||
|
||||
// Подключаем сигналы от процесса (это делается вручную)
|
||||
connect(process, &QProcess::readyReadStandardOutput, this, &MainWindow::readProcessOutput);
|
||||
connect(process, &QProcess::readyReadStandardError, this, &MainWindow::readProcessOutput);
|
||||
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinished(int, QProcess::ExitStatus)));
|
||||
connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(onProcessError(QProcess::ProcessError)));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui; // Освобождаем память от объекта UI
|
||||
}
|
||||
|
||||
void MainWindow::on_launchButton_clicked()
|
||||
{
|
||||
QString minecraftPath = getMinecraftPath();
|
||||
if (minecraftPath.isEmpty()) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось найти директорию .minecraft.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Находим папку с версиями
|
||||
QString versionsPath = minecraftPath + "/versions";
|
||||
QDir versionsDir(versionsPath);
|
||||
if (!versionsDir.exists()) {
|
||||
QMessageBox::warning(this, "Ошибка", "Папка 'versions' не найдена!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Получаем список всех папок внутри 'versions' (это и есть наши версии)
|
||||
QStringList versionList = versionsDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||
if (versionList.isEmpty()) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не найдено ни одной установленной версии Minecraft.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Предлагаем пользователю выбрать версию
|
||||
bool ok;
|
||||
QString selectedVersion = QInputDialog::getItem(this, "Выбор версии",
|
||||
"Выберите версию Minecraft для запуска:",
|
||||
versionList, 0, false, &ok);
|
||||
|
||||
// 4. Если пользователь сделал выбор (нажал "OK" и версия не пустая)
|
||||
if (ok && !selectedVersion.isEmpty()) {
|
||||
// Формируем путь к .jar файлу. Например: /.minecraft/versions/1.19.2/1.19.2.jar
|
||||
QString jarPath = versionsPath + "/" + selectedVersion + "/" + selectedVersion + ".jar";
|
||||
|
||||
// Проверяем, что .jar файл действительно существует
|
||||
QFileInfo jarFile(jarPath);
|
||||
if (!jarFile.exists() || !jarFile.isFile()) {
|
||||
QMessageBox::critical(this, "Ошибка запуска",
|
||||
"Не удалось найти запускаемый .jar файл для версии " + selectedVersion + ".\n"
|
||||
"Проверьте целостность файлов игры.\n"
|
||||
"Ожидаемый путь: " + jarPath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 5. Запускаем игру
|
||||
QString javaPath = "java"; // Java должна быть в системной переменной PATH
|
||||
QStringList arguments;
|
||||
arguments << "-jar" << jarPath << "net.fabricmc.loader.impl.launch.knot.KnotClient";
|
||||
|
||||
ui->logOutput->appendPlainText("Запуск Minecraft версии: " + selectedVersion);
|
||||
process->start(javaPath, arguments);
|
||||
ui->progressBar->setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_modsFolderButton_clicked()
|
||||
{
|
||||
QString minecraftPath = getMinecraftPath();
|
||||
if (minecraftPath.isEmpty()) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось найти директорию .minecraft.");
|
||||
return;
|
||||
}
|
||||
QString modsPath = minecraftPath + "/mods";
|
||||
QDesktopServices::openUrl(QUrl::fromLocalFile(modsPath));
|
||||
}
|
||||
|
||||
void MainWindow::on_settingsButton_clicked()
|
||||
{
|
||||
SettingsDialog dialog(this);
|
||||
dialog.exec();
|
||||
}
|
||||
|
||||
void MainWindow::on_updateModsButton_clicked()
|
||||
{
|
||||
QString gitRepoUrl = settings->value("gitRepoUrl").toString();
|
||||
if (gitRepoUrl.isEmpty()) {
|
||||
QMessageBox::information(this, "Настройки", "Пожалуйста, укажите URL Git-репозитория в настройках.");
|
||||
return;
|
||||
}
|
||||
|
||||
QString minecraftPath = getMinecraftPath();
|
||||
if (minecraftPath.isEmpty()) {
|
||||
QMessageBox::warning(this, "Ошибка", "Не удалось найти директорию .minecraft.");
|
||||
return;
|
||||
}
|
||||
QString modsPath = minecraftPath + "/mods";
|
||||
QDir modsDir(modsPath);
|
||||
|
||||
if (!modsDir.exists(".git")) {
|
||||
ui->logOutput->appendPlainText("Клонирование модов из " + gitRepoUrl);
|
||||
process->start("git", QStringList() << "clone" << gitRepoUrl << modsPath);
|
||||
} else {
|
||||
ui->logOutput->appendPlainText("Обновление модов...");
|
||||
process->setWorkingDirectory(modsPath);
|
||||
process->start("git", QStringList() << "pull");
|
||||
}
|
||||
ui->progressBar->setVisible(true);
|
||||
}
|
||||
|
||||
void MainWindow::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
||||
{
|
||||
ui->progressBar->setVisible(false);
|
||||
if (exitStatus == QProcess::CrashExit) {
|
||||
ui->logOutput->appendPlainText("Процесс завершился с ошибкой.");
|
||||
} else {
|
||||
ui->logOutput->appendPlainText("Процесс успешно завершен с кодом " + QString::number(exitCode));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::onProcessError(QProcess::ProcessError error)
|
||||
{
|
||||
ui->progressBar->setVisible(false);
|
||||
ui->logOutput->appendPlainText("Ошибка запуска процесса: " + process->errorString());
|
||||
}
|
||||
|
||||
void MainWindow::readProcessOutput()
|
||||
{
|
||||
ui->logOutput->appendPlainText(process->readAllStandardOutput());
|
||||
ui->logOutput->appendPlainText(process->readAllStandardError());
|
||||
}
|
||||
|
||||
QString MainWindow::getMinecraftPath()
|
||||
{
|
||||
QString path;
|
||||
#if defined(Q_OS_WIN)
|
||||
path = QDir::homePath() + "/AppData/Roaming/.minecraft";
|
||||
#elif defined(Q_OS_MAC)
|
||||
path = QDir::homePath() + "/Library/Application Support/minecraft";
|
||||
#else
|
||||
path = QDir::homePath() + "/.minecraft";
|
||||
#endif
|
||||
return path;
|
||||
}
|
||||
41
mainwindow.h
Normal file
41
mainwindow.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QProcess>
|
||||
#include <QSettings>
|
||||
|
||||
// Предварительное объявление класса, сгенерированного из mainwindow.ui
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui { class MainWindow; }
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
private slots:
|
||||
// Слоты для кнопок (автоматически подключаются по имени: on_<имя_объекта>_<сигнал>)
|
||||
void on_launchButton_clicked();
|
||||
void on_modsFolderButton_clicked();
|
||||
void on_updateModsButton_clicked();
|
||||
void on_settingsButton_clicked();
|
||||
|
||||
// Слоты для QProcess (подключаются вручную)
|
||||
void onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||
void onProcessError(QProcess::ProcessError error);
|
||||
void readProcessOutput();
|
||||
|
||||
private:
|
||||
QString getMinecraftPath();
|
||||
|
||||
Ui::MainWindow *ui; // Указатель на объект интерфейса
|
||||
QProcess *process;
|
||||
QSettings *settings;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
||||
68
mainwindow.ui
Normal file
68
mainwindow.ui
Normal file
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MainWindow</class>
|
||||
<widget class="QMainWindow" name="MainWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>400</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Minecraft Лаунчер</string>
|
||||
</property>
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="launchButton">
|
||||
<property name="text">
|
||||
<string>Запустить Minecraft</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="modsFolderButton">
|
||||
<property name="text">
|
||||
<string>Открыть папку с модами</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="updateModsButton">
|
||||
<property name="text">
|
||||
<string>Обновить моды из Git</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="settingsButton">
|
||||
<property name="text">
|
||||
<string>Настройки</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QProgressBar" name="progressBar">
|
||||
<property name="value">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="textVisible">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="logOutput">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
27
settingsdialog.cpp
Normal file
27
settingsdialog.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "settingsdialog.h"
|
||||
#include "ui_settingsdialog.h"
|
||||
|
||||
SettingsDialog::SettingsDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::SettingsDialog)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
settings = new QSettings("MyCompany", "MinecraftLauncher", this);
|
||||
ui->gitRepoEdit->setText(settings->value("gitRepoUrl").toString());
|
||||
|
||||
// Сигналы от кнопок (OK, Cancel) автоматически подключены к слотам accept() и reject()
|
||||
// благодаря QDialogButtonBox и .ui файлу
|
||||
}
|
||||
|
||||
SettingsDialog::~SettingsDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
// Эта функция будет вызвана при нажатии кнопки "OK"
|
||||
void SettingsDialog::accept()
|
||||
{
|
||||
settings->setValue("gitRepoUrl", ui->gitRepoEdit->text());
|
||||
QDialog::accept(); // Вызываем базовую реализацию, которая закрывает диалог
|
||||
}
|
||||
27
settingsdialog.h
Normal file
27
settingsdialog.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef SETTINGSDIALOG_H
|
||||
#define SETTINGSDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QSettings>
|
||||
|
||||
namespace Ui {
|
||||
class SettingsDialog;
|
||||
}
|
||||
|
||||
class SettingsDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SettingsDialog(QWidget *parent = nullptr);
|
||||
~SettingsDialog();
|
||||
|
||||
private slots:
|
||||
void accept(); // Переопределяем слот, чтобы сохранить настройки
|
||||
|
||||
private:
|
||||
Ui::SettingsDialog *ui;
|
||||
QSettings *settings;
|
||||
};
|
||||
|
||||
#endif // SETTINGSDIALOG_H
|
||||
74
settingsdialog.ui
Normal file
74
settingsdialog.ui
Normal file
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>SettingsDialog</class>
|
||||
<widget class="QDialog" name="SettingsDialog">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>120</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Настройки</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>URL Git-репозитория с модами:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="gitRepoEdit"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>SettingsDialog</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>SettingsDialog</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user