Files
minecraft-launcher/LauncherWindow.cpp

114 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "LauncherWindow.h"
#include <QVBoxLayout>
#include <QDir>
#include <QStandardPaths>
#include <QDesktopServices>
#include <QMessageBox>
#include <QDebug>
#include <QFile>
LauncherWindow::LauncherWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUI();
networkManager = new QNetworkAccessManager(this);
connect(networkManager, &QNetworkAccessManager::finished, this, &LauncherWindow::onDownloadFinished);
}
LauncherWindow::~LauncherWindow()
{
}
void LauncherWindow::setupUI()
{
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
usernameLineEdit = new QLineEdit(this);
usernameLineEdit->setPlaceholderText("Введите ваш никнейм");
layout->addWidget(usernameLineEdit);
launchButton = new QPushButton("Запустить Minecraft", this);
connect(launchButton, &QPushButton::clicked, this, &LauncherWindow::launchMinecraft);
layout->addWidget(launchButton);
modsFolderButton = new QPushButton("Открыть папку с модами", this);
connect(modsFolderButton, &QPushButton::clicked, this, &LauncherWindow::openModsFolder);
layout->addWidget(modsFolderButton);
downloadModsButton = new QPushButton("Загрузить моды с GitHub", this);
connect(downloadModsButton, &QPushButton::clicked, this, &LauncherWindow::downloadMods);
layout->addWidget(downloadModsButton);
setWindowTitle("Minecraft Launcher");
resize(300, 200);
}
QString LauncherWindow::getMinecraftPath()
{
#ifdef Q_OS_WIN
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/.minecraft";
#elif defined(Q_OS_MAC)
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/Library/Application Support/minecraft";
#else
return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.minecraft";
#endif
}
void LauncherWindow::launchMinecraft()
{
QString username = usernameLineEdit->text();
if (username.isEmpty()) {
QMessageBox::warning(this, "Ошибка", "Пожалуйста, введите никнейм.");
return;
}
// Это упрощенная команда запуска. Для современных версий Minecraft
// может потребоваться более сложная команда с указанием версии,
// аутентификацией и путями к библиотекам.
// Здесь мы используем гипотетическую команду для демонстрации.
QString command = "java -jar \"" + getMinecraftPath() + "/versions/1.19.2/1.19.2.jar\" --username " + username;
QProcess *process = new QProcess(this);
process->startDetached(command);
}
void LauncherWindow::openModsFolder()
{
QString modsPath = getMinecraftPath() + "/mods";
QDir modsDir(modsPath);
if (!modsDir.exists()) {
modsDir.mkpath(".");
}
QDesktopServices::openUrl(QUrl::fromLocalFile(modsPath));
}
void LauncherWindow::downloadMods()
{
// URL для загрузки ZIP-архива с модами из репозитория GitHub
// Замените на URL вашего репозитория
QUrl url("https://github.com/user/repo/archive/refs/heads/main.zip");
QNetworkRequest request(url);
networkManager->get(request);
}
void LauncherWindow::onDownloadFinished(QNetworkReply *reply)
{
if (reply->error()) {
QMessageBox::critical(this, "Ошибка загрузки", "Не удалось загрузить моды: " + reply->errorString());
return;
}
QFile *file = new QFile(getMinecraftPath() + "/mods/mods.zip");
if (file->open(QIODevice::WriteOnly)) {
file->write(reply->readAll());
file->close();
QMessageBox::information(this, "Загрузка завершена", "Моды успешно загружены в папку mods. Распакуйте архив.");
} else {
QMessageBox::critical(this, "Ошибка", "Не удалось сохранить архив с модами.");
}
delete file;
reply->deleteLater();
}