Files
BiLauncher/javainstaller.h
T

130 lines
3.8 KiB
C++
Raw Normal View History

2026-08-27 19:35:11 +03:00
#pragma once
#include "javaruntime.h"
#include <QCryptographicHash>
#include <QList>
#include <QObject>
#include <QPair>
#include <QQueue>
#include <QString>
#include <QStringList>
#include <memory>
class QJsonObject;
class QNetworkAccessManager;
class QNetworkReply;
class QProcess;
class QSaveFile;
class QTimer;
struct ZipExtraction;
struct JavaFileTask
{
QUrl url;
QString path; // абсолютный путь назначения
QString sha1;
qint64 size = 0;
bool executable = false;
int attempts = 0;
};
struct JavaActiveDownload
{
JavaFileTask task;
QNetworkReply *reply = nullptr;
std::unique_ptr<QSaveFile> file;
QCryptographicHash hash{QCryptographicHash::Sha1};
qint64 received = 0;
};
class JavaInstaller : public QObject
{
Q_OBJECT
public:
explicit JavaInstaller(QObject *parent = nullptr);
~JavaInstaller() override;
bool isRunning() const { return m_running; }
QString runtimeId() const { return m_entry.id; }
QString label() const { return m_label; }
QString stage() const { return m_stage; }
QString currentFile() const { return m_currentFile; }
qint64 bytesDone() const;
qint64 bytesTotal() const { return m_bytesTotal; }
double fraction() const; // 0..1, либо -1 пока итог неизвестен
void install(const JavaRuntimeEntry &entry);
void cancel();
signals:
void started(const QString &label);
void progressChanged();
void finished(const QString &runtimeId, const QString &javaPath);
void failed(const QString &label, const QString &message);
void canceled(const QString &label);
private:
// ── Общее ───────────────────────────────────────────────────────────
void beginArchive();
void beginMojang();
void complete();
void fail(const QString &message);
void reset();
void setStage(const QString &stage);
void scheduleProgress();
void flushProgress();
// ── Temurin ─────────────────────────────────────────────────────────
void downloadArchive();
void handleArchiveFinished(QNetworkReply *reply);
void startExtraction();
void extractZipChunk();
void startTarExtraction();
// ── Mojang ──────────────────────────────────────────────────────────
void handleManifest(const QByteArray &payload);
bool prepareTree(const QJsonObject &files);
void pump();
void startTask(JavaFileTask task);
void handleFileFinished(const std::shared_ptr<JavaActiveDownload> &context);
bool retryOrFail(JavaFileTask task, const QString &reason);
bool applyLinks();
QNetworkAccessManager *m_network;
JavaRuntimeEntry m_entry;
QString m_label;
QString m_targetDir;
QString m_archivePath;
QString m_stage;
QString m_currentFile;
qint64 m_bytesSettled = 0;
qint64 m_bytesTotal = 0;
int m_stepsDone = 0;
int m_stepsTotal = 0;
QNetworkReply *m_reply = nullptr; // архив или манифест
std::unique_ptr<QSaveFile> m_archiveFile;
QCryptographicHash m_archiveHash{QCryptographicHash::Sha256};
QProcess *m_tar = nullptr;
QTimer *m_extractTimer;
QTimer *m_progressTimer;
bool m_progressDirty = false;
std::unique_ptr<ZipExtraction> m_zip;
QQueue<JavaFileTask> m_queue;
QList<std::shared_ptr<JavaActiveDownload>> m_active;
2026-09-29 09:38:12 +03:00
QList<QPair<QString, QString>> m_links;
2026-08-27 19:35:11 +03:00
bool m_running = false;
bool m_canceling = false;
};