Files
BiLauncher/launcherbackend.cpp
galeon 98344a1870
tests / package-smoke-linux (push) Skipped
tests / package-smoke-windows (push) Skipped
tests / package-smoke-macos (push) Skipped
tests / linux (push) Successful in 2m22s
tests / macos (push) Successful in 2m29s
tests / windows (push) Successful in 4m31s
cleanups
Signed-off-by: galeon <dertiellis@gmail.com>
2026-11-03 09:52:33 +03:00

2449 lines
88 KiB
C++

#include "launcherbackend.h"
#include "localization.h"
#include "authservice.h"
#include "gamelauncher.h"
#include "javainstaller.h"
#include "javalocator.h"
#include "javaruntime.h"
#include "javaruntimeservice.h"
#include "launcherpaths.h"
#include "versioninstaller.h"
#include "versionmanifestservice.h"
#include "modloaderinstaller.h"
#include "msaauthservice.h"
#include "modloaderversionservice.h"
#include "buildswitcher.h"
#include "gamedirimport.h"
#include "seasonalbuildservice.h"
#include "seasonalpackdownloader.h"
#include <QCoreApplication>
#include <QDate>
#include <QDateTime>
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QHash>
#include <QRegularExpression>
#include <QSaveFile>
#include <QSet>
#include <QTimer>
#include <QUrl>
#include <algorithm>
namespace {
const QString kOffline = QStringLiteral("offline");
const QString kElyBy = QStringLiteral("elyby");
const QString kMicrosoft = QStringLiteral("microsoft");
const int kTokenRenewMarginSecs = 120;
const QString kSeasonalBaseUrl = QStringLiteral("https://launcher-builds.galeonworx.com");
QString normalizedAuthType(const QString &value)
{
if (value == kElyBy)
return kElyBy;
if (value == kMicrosoft)
return kMicrosoft;
return kOffline;
}
QString formatSeasonDate(const QDate &date)
{
return date.isValid() ? date.toString(QStringLiteral("dd.MM.yyyy")) : QStringLiteral("—");
}
int settingInt(const QVariantMap &settings, const QString &key, int fallback)
{
const QVariant value = settings.value(key);
bool ok = false;
const int parsed = value.toInt(&ok);
return ok ? parsed : fallback;
}
} // namespace
LauncherBackend::LauncherBackend(QObject *parent)
: QObject(parent)
, m_auth(new AuthService(this))
, m_msa(new MsaAuthService(this))
, m_launcher(new GameLauncher(this))
, m_manifest(new VersionManifestService(this))
, m_installer(new VersionInstaller(m_manifest, this))
, m_loaderMeta(new ModLoaderVersionService(this))
, m_loaderInstaller(new ModLoaderInstaller(m_loaderMeta, m_installer, this))
, m_switcher(new BuildSwitcher(this))
, m_javaMeta(new JavaRuntimeService(this))
, m_javaInstaller(new JavaInstaller(this))
, m_seasonalMeta(new SeasonalBuildService(this))
, m_packDownloader(new SeasonalPackDownloader(this))
{
QString storageError;
if (!LauncherPaths::ensureRootExists(&storageError))
m_storageIssues << Loc::text("storage.error.launcherDirFailed").arg(storageError);
const bool firstRun = !QFile::exists(LauncherPaths::settingsFile())
&& !QFile::exists(LauncherPaths::customBuildsFile())
&& !QFile::exists(LauncherPaths::legacyCustomBuildsFile());
loadSettings();
migrateLegacyBuildsFile();
loadData();
const QString importedBuild = firstRun && m_builds.isEmpty() ? importGameDir() : QString();
ensureDataFiles();
const QString interrupted = m_switcher->interruptedSwitchWarning();
if (!interrupted.isEmpty()) {
m_storageIssues << interrupted;
QTimer::singleShot(0, this, [this] { m_switcher->resumeInterrupted(gameDir()); });
}
connect(m_auth, &AuthService::progress, this, &LauncherBackend::launchProgress);
connect(m_msa, &MsaAuthService::progress, this, &LauncherBackend::launchProgress);
connect(m_launcher, &GameLauncher::progress, this, &LauncherBackend::launchProgress);
connect(m_launcher, &GameLauncher::output, this, &LauncherBackend::gameOutput);
connect(m_launcher, &GameLauncher::gameStarted, this, [this](const QString &commandLine) {
emit gameOutput(commandLine);
emit gameRunningChanged();
});
connect(m_launcher, &GameLauncher::gameFinished, this, [this](int exitCode, bool crashed) {
emit gameRunningChanged();
emit gameFinished(exitCode, crashed);
});
connect(m_manifest, &VersionManifestService::versionsChanged,
this, &LauncherBackend::invalidateCatalog);
connect(m_manifest, &VersionManifestService::refreshingChanged,
this, &LauncherBackend::catalogLoadingChanged);
connect(m_javaMeta, &JavaRuntimeService::catalogChanged,
this, &LauncherBackend::invalidateJavaCatalog);
connect(m_javaMeta, &JavaRuntimeService::refreshingChanged,
this, &LauncherBackend::javaCatalogLoadingChanged);
connect(m_javaInstaller, &JavaInstaller::started, this, [this](const QString &label) {
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("java.progress.downloading").arg(label));
});
connect(m_javaInstaller, &JavaInstaller::progressChanged,
this, &LauncherBackend::downloadChanged);
connect(m_javaInstaller, &JavaInstaller::finished, this,
[this](const QString &runtimeId, const QString &javaPath) {
invalidateJavaCatalog();
emit downloadChanged();
emit busyChanged();
emit javaRuntimeInstalled(runtimeId);
emit launchProgress(Loc::text("java.status.installed")
.arg(QDir::toNativeSeparators(javaPath)));
if (m_season.stage == SeasonStage::Java)
seasonAdvance();
});
connect(m_javaInstaller, &JavaInstaller::failed, this,
[this](const QString &label, const QString &message) {
invalidateJavaCatalog();
emit downloadChanged();
emit busyChanged();
emit launchError(Loc::text("java.error.installFailed").arg(label, message));
if (m_season.stage == SeasonStage::Java)
seasonFail({});
});
connect(m_javaInstaller, &JavaInstaller::canceled, this, [this](const QString &label) {
invalidateJavaCatalog();
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("java.status.downloadCancelled").arg(label));
if (m_season.stage == SeasonStage::Java)
seasonFail({});
});
connect(m_installer, &VersionInstaller::started, this, [this](const QString &id) {
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("version.progress.downloading").arg(id));
});
connect(m_installer, &VersionInstaller::progressChanged,
this, &LauncherBackend::downloadChanged);
connect(m_installer, &VersionInstaller::finished, this, [this](const QString &id) {
invalidateCatalog();
emit installedVersionsChanged();
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("version.status.downloaded").arg(id));
if (m_season.stage == SeasonStage::Loader && !m_loaderInstaller->isRunning())
seasonAdvance();
});
connect(m_installer, &VersionInstaller::failed, this, [this](const QString &id,
const QString &message) {
invalidateCatalog();
emit installedVersionsChanged();
emit downloadChanged();
emit busyChanged();
emit launchError(Loc::text("version.error.downloadFailed").arg(id, message));
if (m_season.stage == SeasonStage::Loader && !m_loaderInstaller->isRunning())
seasonFail({});
});
connect(m_switcher, &BuildSwitcher::progressChanged, this, [this] {
emit switchChanged();
emit busyChanged();
});
connect(m_switcher, &BuildSwitcher::finished, this, [this](int toBuildId) {
m_settings.insert(QStringLiteral("activeBuildId"), toBuildId);
saveSettings();
invalidateCatalog();
emit activeBuildChanged();
emit installedVersionsChanged();
emit switchChanged();
emit busyChanged();
const int index = indexOfBuildId(toBuildId);
emit launchProgress(Loc::text("build.status.active")
.arg(index >= 0 ? m_builds[index].name : QString()));
const QJsonObject note = m_switcher->lastPackNote();
if (!note.isEmpty())
recordSeasonalPack(toBuildId, note, m_switcher->lastPackEntries());
if (m_season.stage == SeasonStage::Applying)
seasonFinish();
else if (m_season.stage == SeasonStage::Switching)
seasonAdvance();
});
connect(m_switcher, &BuildSwitcher::failed, this,
[this](int, const QString &message, bool gameDirIntact) {
invalidateCatalog();
emit installedVersionsChanged();
emit switchChanged();
emit busyChanged();
if (m_season.stage == SeasonStage::Applying) {
emit launchError(gameDirIntact
? Loc::text("build.error.filesNotInstalled").arg(message)
: Loc::text("build.error.filesIncomplete")
.arg(message));
} else {
emit launchError(gameDirIntact
? Loc::text("build.error.switchFailed").arg(message)
: Loc::text("build.error.switchFailedDirty")
.arg(message));
}
if (m_season.stage == SeasonStage::Switching
|| m_season.stage == SeasonStage::Applying) {
seasonFail({});
}
});
connect(m_loaderMeta, &ModLoaderVersionService::versionsChanged,
this, &LauncherBackend::loaderVersionsChanged);
connect(m_loaderMeta, &ModLoaderVersionService::refreshingChanged,
this, &LauncherBackend::downloadChanged);
connect(m_loaderInstaller, &ModLoaderInstaller::started, this, [this](const QString &label) {
emit busyChanged();
emit downloadChanged();
emit launchProgress(Loc::text("loader.progress.installingShort").arg(label));
});
connect(m_loaderInstaller, &ModLoaderInstaller::progressChanged,
this, &LauncherBackend::downloadChanged);
connect(m_loaderInstaller, &ModLoaderInstaller::log, this, &LauncherBackend::gameOutput);
connect(m_loaderInstaller, &ModLoaderInstaller::finished, this,
[this](const QString &loader, const QString &gameVersion,
const QString &loaderVersion, const QString &producedVersionId) {
const int index = indexOfBuildId(m_loaderInstallBuildId);
m_loaderInstallBuildId = -1;
if (index >= 0) {
updateCustomBuild(index,
{{QStringLiteral("loader"), loader},
{QStringLiteral("loaderVersion"), loaderVersion},
{QStringLiteral("resolvedVersionId"), producedVersionId}});
}
invalidateCatalog();
emit installedVersionsChanged();
emit busyChanged();
emit downloadChanged();
emit launchProgress(Loc::text("loader.status.done")
.arg(producedVersionId, gameVersion));
if (m_season.stage == SeasonStage::Loader)
seasonAdvance();
});
connect(m_loaderInstaller, &ModLoaderInstaller::failed, this,
[this](const QString &label, const QString &message) {
m_loaderInstallBuildId = -1;
invalidateCatalog();
emit installedVersionsChanged();
emit busyChanged();
emit downloadChanged();
emit launchError(Loc::text("common.status.pair").arg(label, message));
if (m_season.stage == SeasonStage::Loader)
seasonFail({});
});
connect(m_loaderInstaller, &ModLoaderInstaller::canceled, this, [this](const QString &label) {
m_loaderInstallBuildId = -1;
invalidateCatalog();
emit installedVersionsChanged();
emit busyChanged();
emit downloadChanged();
emit launchProgress(Loc::text("loader.status.installCancelled").arg(label));
if (m_season.stage == SeasonStage::Loader)
seasonFail({});
});
connect(m_installer, &VersionInstaller::canceled, this, [this](const QString &id) {
invalidateCatalog();
emit installedVersionsChanged();
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("version.status.downloadCancelled").arg(id));
if (m_season.stage == SeasonStage::Loader && !m_loaderInstaller->isRunning())
seasonFail({});
});
m_seasonalMeta->setBaseUrl(seasonalBaseUrl());
connect(m_seasonalMeta, &SeasonalBuildService::buildsChanged,
this, &LauncherBackend::invalidateSeasonalCatalog);
connect(m_seasonalMeta, &SeasonalBuildService::refreshingChanged,
this, &LauncherBackend::seasonalCatalogLoadingChanged);
connect(this, &LauncherBackend::customBuildsChanged,
this, &LauncherBackend::invalidateSeasonalCatalog);
connect(m_packDownloader, &SeasonalPackDownloader::started, this, [this](const QString &label) {
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("seasonal.progress.downloadingBuild").arg(label));
});
connect(m_packDownloader, &SeasonalPackDownloader::progressChanged,
this, &LauncherBackend::downloadChanged);
connect(m_packDownloader, &SeasonalPackDownloader::finished, this, [this](const QString &path) {
emit downloadChanged();
emit busyChanged();
if (m_season.stage != SeasonStage::Downloading)
return;
m_season.packPath = path;
seasonAdvance();
});
connect(m_packDownloader, &SeasonalPackDownloader::failed, this,
[this](const QString &label, const QString &message) {
emit downloadChanged();
emit busyChanged();
emit launchError(Loc::text("build.status.named").arg(label, message));
if (m_season.stage == SeasonStage::Downloading)
seasonFail({});
});
connect(m_packDownloader, &SeasonalPackDownloader::canceled, this, [this](const QString &label) {
emit downloadChanged();
emit busyChanged();
emit launchProgress(Loc::text("seasonal.status.downloadCancelled").arg(label));
if (m_season.stage == SeasonStage::Downloading)
seasonFail({});
});
if (!importedBuild.isEmpty()) {
QTimer::singleShot(0, this, [this, importedBuild] { emit gameDirImported(importedBuild); });
}
if (!m_storageIssues.isEmpty()) {
QTimer::singleShot(0, this, [this] {
emit launchError(m_storageIssues.join(u'\n'));
});
}
}
LauncherBackend::~LauncherBackend() = default;
QStringList LauncherBackend::profileNames() const
{
QStringList names;
for (const auto &p : m_profiles)
names << p.name;
return names;
}
QStringList LauncherBackend::customBuildNames() const
{
QStringList names;
for (const auto &b : m_builds)
names << b.name;
return names;
}
QStringList LauncherBackend::installedVersions() const
{
return VersionLoader::installedVersions(gameDir());
}
bool LauncherBackend::gameRunning() const
{
return m_launcher->isRunning();
}
void LauncherBackend::addProfile(const QString &name,
const QString &login,
const QString &password,
const QString &authType)
{
Profile profile;
profile.id = m_nextProfileId++;
profile.name = name;
profile.login = login;
profile.password = password;
profile.authType = normalizedAuthType(authType);
profile.clientToken = AuthService::generateClientToken();
m_profiles.append(profile);
saveProfiles();
emit profilesChanged();
}
void LauncherBackend::addCustomBuild(const QString &name,
const QString &serverUrl,
const QString &minecraftVersion)
{
CustomBuild build;
build.id = m_nextBuildId++;
build.name = name;
build.serverUrl = serverUrl;
build.minecraftVersion = minecraftVersion;
m_builds.append(build);
saveCustomBuilds();
emit customBuildsChanged();
if (!minecraftVersion.trimmed().isEmpty())
maybeInstall(minecraftVersion.trimmed());
}
void LauncherBackend::updateProfile(int index,
const QString &name,
const QString &login,
const QString &password,
const QString &authType)
{
if (index < 0 || index >= m_profiles.size())
return;
Profile &profile = m_profiles[index];
const QString type = normalizedAuthType(authType);
if (profile.authType == kElyBy && (profile.login != login || profile.password != password)) {
profile.accessToken.clear();
profile.uuid.clear();
profile.resolvedName.clear();
}
if (profile.authType != type) {
profile.accessToken.clear();
profile.refreshToken.clear();
profile.uuid.clear();
profile.resolvedName.clear();
profile.xuid.clear();
profile.tokenExpiry.clear();
profile.userType.clear();
}
profile.name = name;
profile.login = login;
profile.password = password;
profile.authType = type;
if (profile.clientToken.isEmpty())
profile.clientToken = AuthService::generateClientToken();
saveProfiles();
emit profilesChanged();
}
void LauncherBackend::updateCustomBuild(int index, const QVariantMap &fields)
{
if (index < 0 || index >= m_builds.size())
return;
CustomBuild &build = m_builds[index];
const auto take = [&fields](const char *key, QString &target) {
const auto it = fields.constFind(QLatin1String(key));
if (it == fields.constEnd())
return false;
target = it->toString();
return true;
};
const QString previousMinecraft = build.minecraftVersion;
take("name", build.name);
take("serverUrl", build.serverUrl);
take("minecraftVersion", build.minecraftVersion);
take("loader", build.loader);
take("loaderVersion", build.loaderVersion);
take("resolvedVersionId", build.resolvedVersionId);
take("seasonalId", build.seasonalId);
if (fields.contains(QStringLiteral("seasonalRevision")))
build.seasonalRevision = fields.value(QStringLiteral("seasonalRevision")).toInt();
if (build.minecraftVersion != previousMinecraft
&& !fields.contains(QStringLiteral("loaderVersion"))) {
build.loaderVersion.clear();
build.resolvedVersionId.clear();
}
saveCustomBuilds();
emit customBuildsChanged();
emit activeBuildChanged();
}
QVariantMap LauncherBackend::profileAt(int index) const
{
if (index < 0 || index >= m_profiles.size())
return {};
const auto &p = m_profiles[index];
return {{"name", p.name},
{"login", p.login},
{"password", p.password},
{"authType", p.authType},
{"resolvedName", p.resolvedName},
{"hasMicrosoftSession", !p.refreshToken.isEmpty()}};
}
QVariantMap LauncherBackend::customBuildAt(int index) const
{
if (index < 0 || index >= m_builds.size())
return {};
const auto &b = m_builds[index];
return {{"name", b.name},
{"serverUrl", b.serverUrl},
{"minecraftVersion", b.minecraftVersion},
{"loader", b.loader},
{"loaderVersion", b.loaderVersion},
{"resolvedVersionId", b.resolvedVersionId},
{"seasonalId", b.seasonalId},
{"seasonalRevision", b.seasonalRevision},
{"launchVersionId", launchVersionIdOf(b)}};
}
void LauncherBackend::removeProfile(int index)
{
if (index < 0 || index >= m_profiles.size())
return;
m_profiles.removeAt(index);
saveProfiles();
emit profilesChanged();
}
QVariantMap LauncherBackend::customBuildRemovalInfo(int index) const
{
if (index < 0 || index >= m_builds.size())
return {};
const CustomBuild &build = m_builds[index];
const bool active = m_settings.value(QStringLiteral("activeBuildId")).toInt() == build.id
|| m_builds.size() == 1;
return {{QStringLiteral("name"), build.name},
{QStringLiteral("hasArchive"), m_switcher->hasArchive(build.id)},
{QStringLiteral("active"), active},
{QStringLiteral("lastOne"), m_builds.size() == 1}};
}
void LauncherBackend::removeCustomBuild(int index)
{
if (index < 0 || index >= m_builds.size())
return;
if (m_switcher->isRunning() || m_busy || m_installer->isRunning()
|| m_loaderInstaller->isRunning()) {
emit launchError(Loc::text("common.error.busyOperation"));
return;
}
if (m_launcher->isRunning()) {
emit launchError(Loc::text("common.error.closeGameFirst"));
return;
}
const CustomBuild removed = m_builds.takeAt(index);
const bool wasActive = m_settings.value(QStringLiteral("activeBuildId")).toInt() == removed.id
|| m_builds.isEmpty();
saveCustomBuilds();
if (!m_switcher->forgetBuild(removed.id)) {
emit launchError(Loc::text("build.warning.archiveNotRemoved")
.arg(removed.name,
QDir::toNativeSeparators(LauncherPaths::buildStorageDir())));
}
emit customBuildsChanged();
emit activeBuildChanged();
if (!wasActive)
return;
m_settings.remove(QStringLiteral("activeBuildId"));
saveSettings();
if (m_builds.isEmpty()) {
emit activeBuildChanged();
return;
}
const CustomBuild &next = m_builds.first();
emit launchProgress(Loc::text("build.progress.activating").arg(next.name));
m_switcher->switchTo(-1, QString(), next.id, next.name, gameDir());
emit switchChanged();
emit busyChanged();
}
int LauncherBackend::indexOfBuildId(int buildId) const
{
for (int i = 0; i < m_builds.size(); ++i) {
if (m_builds[i].id == buildId)
return i;
}
return -1;
}
int LauncherBackend::activeBuildIndex() const
{
if (m_builds.isEmpty())
return -1;
const int index = indexOfBuildId(m_settings.value(QStringLiteral("activeBuildId")).toInt());
return index >= 0 ? index : 0;
}
void LauncherBackend::setActiveBuildIndex(int index)
{
const int current = activeBuildIndex();
if (index < 0 || index >= m_builds.size() || index == current)
return;
if (m_switcher->isRunning()) {
emit launchError(Loc::text("common.error.switchInProgress"));
return;
}
if (m_launcher->isRunning()) {
emit launchError(Loc::text("build.error.closeGameFilesMoving"));
return;
}
if (m_busy || m_installer->isRunning() || m_loaderInstaller->isRunning()) {
emit launchError(Loc::text("common.error.busyOperation"));
return;
}
const CustomBuild &target = m_builds[index];
emit launchProgress(Loc::text("build.progress.switchingTo").arg(target.name));
m_switcher->switchTo(current >= 0 ? m_builds[current].id : -1,
current >= 0 ? m_builds[current].name : QString(),
target.id,
target.name,
gameDir());
emit switchChanged();
emit busyChanged();
}
QString LauncherBackend::activeBuildName() const
{
const int index = activeBuildIndex();
return index < 0 ? QString() : m_builds[index].name;
}
void LauncherBackend::installCustomBuild(int index)
{
installLoaderForBuild(index);
}
QString LauncherBackend::launchVersionIdOf(const CustomBuild &build) const
{
if (!build.resolvedVersionId.isEmpty())
return build.resolvedVersionId;
return build.minecraftVersion.isEmpty() ? build.name : build.minecraftVersion;
}
QStringList LauncherBackend::checkInstallation(int buildIndex) const
{
if (buildIndex < 0 || buildIndex >= m_builds.size())
return {Loc::text("common.status.noBuildSelected")};
const CustomBuild &entry = m_builds[buildIndex];
const QString id = launchVersionIdOf(entry);
if (id.trimmed().isEmpty())
return {Loc::text("build.error.noMinecraftVersion")
.arg(QDir::toNativeSeparators(LauncherPaths::customBuildsFile()))};
QString error;
const MinecraftVersion version = VersionLoader::load(gameDir(), id, {}, &error);
if (!version.isValid()) {
return {error.isEmpty()
? Loc::text("build.error.versionNotInstalled")
.arg(id,
entry.name,
QDir::toNativeSeparators(gameDir() + QStringLiteral("/versions")))
: error};
}
LaunchOptions options;
options.gameDir = gameDir();
options.versionId = id;
return GameLauncher::missingFiles(options, version);
}
// ── Каталог версий ─────────────────────────────────────────────────────────
void LauncherBackend::invalidateCatalog()
{
m_catalogDirty = true;
emit versionCatalogChanged();
}
bool LauncherBackend::catalogLoading() const
{
return m_manifest->isRefreshing();
}
void LauncherBackend::refreshVersionCatalog(bool force)
{
invalidateCatalog();
m_manifest->ensureLoaded([this](bool ok, const QString &warning) {
if (!ok && !warning.isEmpty())
emit launchError(warning);
}, force);
}
bool LauncherBackend::isVersionInstalled(const QString &versionId) const
{
if (versionId.trimmed().isEmpty())
return false;
return VersionLoader::installedVersions(gameDir()).contains(versionId);
}
QVariantMap LauncherBackend::versionRemovalInfo(const QString &versionId) const
{
const QString id = versionId.trimmed();
if (id.isEmpty())
return {};
const qint64 bytes = VersionLoader::installedSize(gameDir(), id);
QStringList builds;
for (const CustomBuild &build : m_builds) {
if (build.minecraftVersion == id || build.resolvedVersionId == id
|| launchVersionIdOf(build) == id) {
builds << build.name;
}
}
return {{QStringLiteral("id"), id},
{QStringLiteral("installed"), isVersionInstalled(id)},
{QStringLiteral("sizeMb"), static_cast<int>((bytes + 1024 * 1024 - 1) / (1024 * 1024))},
{QStringLiteral("dependents"), VersionLoader::dependentsOf(gameDir(), id)},
{QStringLiteral("builds"), builds}};
}
void LauncherBackend::removeVersion(const QString &versionId)
{
const QString id = versionId.trimmed();
if (id.isEmpty())
return;
if (m_installer->versionId() == id || m_installer->isQueued(id)) {
emit launchError(Loc::text("version.error.currentlyDownloading"));
return;
}
if (m_switcher->isRunning() || m_busy || m_installer->isRunning()
|| m_loaderInstaller->isRunning()) {
emit launchError(Loc::text("common.error.busyOperation"));
return;
}
if (m_launcher->isRunning()) {
emit launchError(Loc::text("common.error.closeGameFirst"));
return;
}
QString error;
if (!VersionLoader::remove(gameDir(), id, &error)) {
emit launchError(Loc::text("version.error.removeFailed").arg(error));
return;
}
invalidateCatalog();
emit installedVersionsChanged();
emit launchProgress(Loc::text("version.status.removed").arg(id));
}
QVariantList LauncherBackend::versionCatalog() const
{
if (m_catalogDirty) {
rebuildCatalog();
m_catalogDirty = false;
}
return m_catalog;
}
void LauncherBackend::rebuildCatalog() const
{
m_catalog.clear();
const QList<RemoteVersionEntry> remote = m_manifest->versions();
const QStringList installed = VersionLoader::installedVersions(gameDir());
const QSet<QString> installedSet(installed.cbegin(), installed.cend());
const auto category = [](const QString &type) {
if (type == QLatin1String("release") || type == QLatin1String("snapshot")
|| type == QLatin1String("old_beta") || type == QLatin1String("old_alpha"))
return type;
return QStringLiteral("other");
};
const auto makeVersionRow = [&category](const QString &id, const QString &type, bool isInstalled) {
return QVariantMap{{QStringLiteral("id"), id},
{QStringLiteral("type"), type},
{QStringLiteral("category"), category(type)},
{QStringLiteral("label"), id},
{QStringLiteral("installed"), isInstalled},
{QStringLiteral("search"), (id + u' ' + type).toLower()}};
};
m_catalog.reserve(remote.size() + installed.size());
QSet<QString> known;
known.reserve(remote.size());
for (const RemoteVersionEntry &entry : remote) {
known.insert(entry.id);
m_catalog.append(makeVersionRow(entry.id, entry.type, installedSet.contains(entry.id)));
}
QStringList unknown;
for (const QString &id : installed) {
if (!known.contains(id))
unknown << id;
}
std::sort(unknown.begin(), unknown.end());
for (const QString &id : std::as_const(unknown))
m_catalog.append(makeVersionRow(id, QString(), true));
}
// ── Загрузка версий ────────────────────────────────────────────────────────
void LauncherBackend::maybeInstall(const QString &versionId)
{
const QString id = versionId.trimmed();
if (id.isEmpty() || isVersionInstalled(id))
return;
if (m_installer->versionId() == id || m_installer->isQueued(id))
return;
m_installer->install(gameDir(), id);
}
void LauncherBackend::installVersion(const QString &versionId)
{
const QString id = versionId.trimmed();
if (id.isEmpty())
return;
if (m_installer->versionId() == id || m_installer->isQueued(id))
return;
m_installer->install(gameDir(), id);
}
void LauncherBackend::cancelDownload()
{
if (m_loaderInstaller->isRunning())
m_loaderInstaller->cancel();
else if (m_installer->isRunning())
m_installer->cancel();
else if (m_javaInstaller->isRunning())
m_javaInstaller->cancel();
else
m_packDownloader->cancel();
}
bool LauncherBackend::downloading() const
{
return m_loaderInstaller->isRunning() || m_installer->isRunning()
|| m_javaInstaller->isRunning() || m_packDownloader->isRunning();
}
bool LauncherBackend::javaDownloadShown() const
{
return m_javaInstaller->isRunning() && !m_installer->isRunning()
&& !m_loaderInstaller->isRunning();
}
bool LauncherBackend::packDownloadShown() const
{
return m_packDownloader->isRunning() && !m_installer->isRunning()
&& !m_loaderInstaller->isRunning() && !m_javaInstaller->isRunning();
}
double LauncherBackend::downloadProgress() const
{
if (packDownloadShown())
return m_packDownloader->fraction();
if (javaDownloadShown())
return m_javaInstaller->fraction();
if (!m_loaderInstaller->isRunning())
return m_installer->fraction();
return m_installer->isRunning() ? m_installer->fraction() : m_loaderInstaller->fraction();
}
QString LauncherBackend::downloadVersion() const
{
if (packDownloadShown())
return m_packDownloader->label();
if (javaDownloadShown())
return m_javaInstaller->label();
if (m_loaderInstaller->isRunning())
return m_loaderInstaller->label();
return m_installer->versionId();
}
QString LauncherBackend::downloadStatus() const
{
QString stage = m_installer->stage();
QString file = m_installer->currentFile();
if (packDownloadShown()) {
stage = m_packDownloader->stage();
file = m_packDownloader->currentFile();
} else if (javaDownloadShown()) {
stage = m_javaInstaller->stage();
file = m_javaInstaller->currentFile();
} else if (m_loaderInstaller->isRunning() && !m_installer->isRunning()) {
stage = m_loaderInstaller->stage();
file = m_loaderInstaller->currentFile();
}
if (stage.isEmpty())
return file;
if (file.isEmpty())
return stage;
return QStringLiteral("%1 — %2").arg(stage, file);
}
qint64 LauncherBackend::downloadBytesDone() const
{
if (packDownloadShown())
return m_packDownloader->bytesDone();
if (javaDownloadShown())
return m_javaInstaller->bytesDone();
if (m_loaderInstaller->isRunning() && !m_installer->isRunning())
return m_loaderInstaller->bytesDone();
return m_installer->bytesDone();
}
qint64 LauncherBackend::downloadBytesTotal() const
{
if (packDownloadShown())
return m_packDownloader->bytesTotal();
if (javaDownloadShown())
return m_javaInstaller->bytesTotal();
if (m_loaderInstaller->isRunning() && !m_installer->isRunning())
return m_loaderInstaller->bytesTotal();
return m_installer->bytesTotal();
}
// ── Модлоадеры ─────────────────────────────────────────────────────────────
QVariantList LauncherBackend::loaderVersions(const QString &loaderKey,
const QString &gameVersion) const
{
const auto loader = loaderFromKey(loaderKey);
if (!loader)
return {};
QVariantList result;
const QList<LoaderVersionEntry> entries = m_loaderMeta->versions(*loader, gameVersion);
result.reserve(entries.size());
for (const LoaderVersionEntry &entry : entries) {
QString label = entry.loaderVersion;
if (entry.recommended)
label += Loc::text("loader.label.recommended");
else if (!entry.stable)
label += Loc::text("loader.label.unstable");
result.append(QVariantMap{{QStringLiteral("version"), entry.loaderVersion},
{QStringLiteral("label"), label},
{QStringLiteral("recommended"), entry.recommended},
{QStringLiteral("stable"), entry.stable}});
}
return result;
}
void LauncherBackend::refreshLoaderVersions(const QString &loaderKey,
const QString &gameVersion,
bool force)
{
const auto loader = loaderFromKey(loaderKey);
if (!loader)
return;
m_loaderMeta->ensureLoaded(*loader, gameVersion, [this, loaderKey, gameVersion](
bool ok, const QString &warning) {
if (!ok && !warning.isEmpty())
emit launchError(warning);
emit loaderVersionsChanged(loaderKey, gameVersion);
}, force);
}
bool LauncherBackend::loaderVersionsLoading(const QString &loaderKey,
const QString &gameVersion) const
{
const auto loader = loaderFromKey(loaderKey);
return loader && m_loaderMeta->isRefreshing(*loader, gameVersion);
}
void LauncherBackend::installLoaderForBuild(int index)
{
if (index < 0 || index >= m_builds.size())
return;
if (busy()) {
emit launchError(Loc::text("common.error.busyDownload"));
return;
}
const CustomBuild &build = m_builds[index];
if (build.minecraftVersion.trimmed().isEmpty()) {
emit launchError(Loc::text("build.error.pickMinecraftVersion").arg(build.name));
return;
}
const auto loader = loaderFromKey(build.loader);
if (!loader) {
if (build.resolvedVersionId != build.minecraftVersion)
updateCustomBuild(index, {{QStringLiteral("resolvedVersionId"), QString()}});
maybeInstall(build.minecraftVersion);
return;
}
if (build.loaderVersion.trimmed().isEmpty()) {
emit launchError(Loc::text("build.error.pickLoaderVersion")
.arg(loaderTitle(*loader), build.name));
return;
}
m_loaderInstallBuildId = build.id;
m_loaderInstaller->install(gameDir(),
*loader,
build.minecraftVersion,
build.loaderVersion,
managedJavaPath().isEmpty()
? m_settings.value(QStringLiteral("javaPath")).toString()
: managedJavaPath());
}
void LauncherBackend::launchGame(int profileIndex, int buildIndex)
{
if (m_loaderInstaller->isRunning()) {
emit launchError(Loc::text("build.error.installBusy")
.arg(m_loaderInstaller->label()));
return;
}
if (m_installer->isRunning()) {
emit launchError(Loc::text("version.error.downloadBusy")
.arg(m_installer->versionId()));
return;
}
if (m_javaInstaller->isRunning()) {
emit launchError(Loc::text("java.error.installBusy")
.arg(m_javaInstaller->label()));
return;
}
if (m_busy) {
emit launchError(Loc::text("launch.error.alreadyLaunching"));
return;
}
if (m_launcher->isRunning()) {
emit launchError(Loc::text("common.error.gameRunning"));
return;
}
if (profileIndex < 0 || profileIndex >= m_profiles.size()) {
emit launchError(Loc::text("launch.error.noProfile"));
return;
}
if (buildIndex < 0 || buildIndex >= m_builds.size()) {
emit launchError(Loc::text("launch.error.noBuild"));
return;
}
const QString directory = gameDir();
if (!QDir(directory).exists()) {
emit launchError(Loc::text("launch.error.gameDirMissing")
.arg(QDir::toNativeSeparators(directory)));
return;
}
const CustomBuild &entry = m_builds[buildIndex];
const QString id = launchVersionIdOf(entry);
if (id.trimmed().isEmpty()) {
emit launchError(Loc::text("launch.error.buildNoVersion")
.arg(entry.name,
QDir::toNativeSeparators(LauncherPaths::customBuildsFile())));
return;
}
const QString versionsDir = QDir::toNativeSeparators(directory + QStringLiteral("/versions"));
const QStringList installed = VersionLoader::installedVersions(directory);
if (!installed.contains(id)) {
emit launchError(installed.isEmpty()
? Loc::text("launch.error.noVersionsInstalled").arg(versionsDir)
: Loc::text("launch.error.versionNotInstalled")
.arg(id,
entry.name,
versionsDir,
installed.join(QStringLiteral(", "))));
return;
}
setBusy(true);
emit launchProgress(Loc::text("launch.progress.readingVersion").arg(id));
QSet<QString> features;
if (settingInt(m_settings, QStringLiteral("windowWidth"), 0) > 0
&& settingInt(m_settings, QStringLiteral("windowHeight"), 0) > 0)
features << QStringLiteral("has_custom_resolution");
QString error;
m_pendingVersionData = VersionLoader::load(directory, id, features, &error);
if (!m_pendingVersionData.isValid()) {
failLaunch(error.isEmpty() ? Loc::text("common.error.versionReadFailed").arg(id) : error);
return;
}
LaunchOptions probe;
probe.gameDir = directory;
probe.versionId = id;
const QStringList missing = GameLauncher::missingFiles(probe, m_pendingVersionData);
if (!missing.isEmpty()) {
failLaunch(Loc::text("launch.error.gameFilesMissing")
.arg(missing.size())
.arg(missing.join(u'\n')));
return;
}
m_pendingProfile = profileIndex;
m_pendingBuild = entry.id;
m_pendingInjectorPath.clear();
beginAuthentication();
}
void LauncherBackend::startMicrosoftLogin(int profileIndex)
{
if (!microsoftAvailable()) {
emit microsoftLoginFailed(Loc::text("auth.msa.error.noWebEngine"));
return;
}
if (profileIndex >= m_profiles.size()) {
emit microsoftLoginFailed(Loc::text("profile.error.notFound"));
return;
}
m_msaLoginProfile = profileIndex < 0 ? -2 : profileIndex;
emit microsoftLoginUrlReady(MsaAuthService::authorizationUrl().toString());
}
void LauncherBackend::cancelMicrosoftLogin()
{
m_msaLoginProfile = -1;
}
QVariantMap LauncherBackend::inspectMicrosoftRedirect(const QString &url) const
{
QString code;
QString error;
const bool matched = MsaAuthService::matchRedirect(QUrl(url), &code, &error);
return {{QStringLiteral("matched"), matched},
{QStringLiteral("code"), code},
{QStringLiteral("error"), error}};
}
void LauncherBackend::finishMicrosoftLogin(const QString &code)
{
if (m_msaLoginProfile == -1) {
emit microsoftLoginFailed(Loc::text("auth.msa.status.alreadyCancelled"));
return;
}
setBusy(true);
m_msa->loginWithCode(code, [this](const AuthResult &result) {
setBusy(false);
const int target = m_msaLoginProfile;
m_msaLoginProfile = -1;
if (target == -1)
return;
if (!result.ok) {
emit microsoftLoginFailed(result.error);
return;
}
if (target == -2) {
Profile profile;
profile.id = m_nextProfileId++;
profile.name = result.playerName;
profile.authType = kMicrosoft;
profile.clientToken = AuthService::generateClientToken();
applyMicrosoftResult(profile, result);
m_profiles.append(profile);
} else if (target < m_profiles.size()) {
Profile &profile = m_profiles[target];
profile.authType = kMicrosoft;
applyMicrosoftResult(profile, result);
} else {
emit microsoftLoginFailed(Loc::text("profile.error.removedDuringLogin"));
return;
}
saveProfiles();
emit profilesChanged();
emit microsoftLoginSucceeded(result.playerName);
});
}
void LauncherBackend::applyMicrosoftResult(Profile &profile, const AuthResult &auth)
{
profile.accessToken = auth.accessToken;
if (!auth.refreshToken.isEmpty())
profile.refreshToken = auth.refreshToken;
if (!auth.uuid.isEmpty())
profile.uuid = auth.uuid;
if (!auth.playerName.isEmpty())
profile.resolvedName = auth.playerName;
profile.xuid = auth.xuid;
profile.userType = auth.userType;
profile.tokenExpiry = auth.expiresAt.isValid()
? auth.expiresAt.toUTC().toString(Qt::ISODate)
: QString();
}
void LauncherBackend::beginMicrosoftAuthentication()
{
const Profile &profile = m_profiles[m_pendingProfile];
if (profile.refreshToken.isEmpty()) {
const int index = m_pendingProfile;
failLaunch(Loc::text("auth.msa.error.profileNeedsLogin")
.arg(profile.name));
emit microsoftReloginRequired(index);
return;
}
const QDateTime expiry = QDateTime::fromString(profile.tokenExpiry, Qt::ISODate);
if (!profile.accessToken.isEmpty() && expiry.isValid()
&& QDateTime::currentDateTimeUtc().addSecs(kTokenRenewMarginSecs) < expiry) {
AuthResult stored;
stored.ok = true;
stored.playerName = profile.resolvedName;
stored.uuid = profile.uuid;
stored.accessToken = profile.accessToken;
stored.refreshToken = profile.refreshToken;
stored.clientToken = MsaAuthService::clientId();
stored.xuid = profile.xuid;
stored.userType = profile.userType.isEmpty() ? QStringLiteral("msa") : profile.userType;
stored.expiresAt = expiry;
continueLaunch(stored);
return;
}
m_msa->loginWithRefreshToken(profile.refreshToken, [this](const AuthResult &result) {
if (m_pendingProfile < 0)
return;
if (result.licenseMissing) {
failLaunch(result.error);
return;
}
if (!result.ok) {
const int index = m_pendingProfile;
failLaunch(Loc::text("auth.msa.error.sessionExpired")
.arg(result.error));
emit microsoftReloginRequired(index);
return;
}
continueLaunch(result);
});
}
void LauncherBackend::beginAuthentication()
{
const Profile &profile = m_profiles[m_pendingProfile];
if (profile.authType == kMicrosoft) {
beginMicrosoftAuthentication();
return;
}
if (profile.authType != kElyBy) {
const QString nickname = profile.login.isEmpty() ? profile.name : profile.login;
const AuthResult result = AuthService::offline(nickname);
if (!result.ok) {
failLaunch(result.error);
return;
}
continueLaunch(result);
return;
}
m_auth->ensureAuthlibInjector(LauncherPaths::runtimeDir(),
[this](const QString &path, const QString &error) {
if (m_pendingProfile < 0)
return;
if (!error.isEmpty()) {
failLaunch(error);
return;
}
m_pendingInjectorPath = path;
const Profile &pending = m_profiles[m_pendingProfile];
m_auth->loginElyBy(pending.login,
pending.password,
pending.clientToken,
pending.accessToken,
[this](const AuthResult &result) {
if (m_pendingProfile < 0)
return;
if (result.twoFactorRequired) {
setBusy(false);
emit twoFactorRequired(m_profiles[m_pendingProfile].name);
return;
}
if (!result.ok) {
failLaunch(result.error);
return;
}
continueLaunch(result);
});
});
}
void LauncherBackend::submitTwoFactorCode(const QString &code)
{
if (m_pendingProfile < 0 || m_pendingProfile >= m_profiles.size()) {
emit launchError(Loc::text("launch.status.alreadyCancelled"));
return;
}
if (code.trimmed().isEmpty()) {
emit launchError(Loc::text("auth.ely.error.enterCode"));
return;
}
setBusy(true);
const Profile &profile = m_profiles[m_pendingProfile];
m_auth->loginElyByWithTotp(profile.login,
profile.password,
code.trimmed(),
profile.clientToken,
[this](const AuthResult &result) {
if (m_pendingProfile < 0)
return;
if (!result.ok) {
failLaunch(result.error);
return;
}
continueLaunch(result);
});
}
void LauncherBackend::cancelPendingLaunch()
{
m_pendingProfile = -1;
m_pendingBuild = -1;
m_pendingInjectorPath.clear();
setBusy(false);
}
void LauncherBackend::continueLaunch(const AuthResult &auth)
{
if (m_pendingProfile < 0 || m_pendingProfile >= m_profiles.size() || m_pendingBuild < 0) {
failLaunch(Loc::text("launch.status.cancelled"));
return;
}
const int buildIndex = indexOfBuildId(m_pendingBuild);
if (buildIndex < 0) {
failLaunch(Loc::text("build.error.removedDuringAuth"));
return;
}
Profile &profile = m_profiles[m_pendingProfile];
const CustomBuild &build = m_builds[buildIndex];
if (profile.authType == kMicrosoft && auth.ok) {
applyMicrosoftResult(profile, auth);
saveProfiles();
} else if (!auth.accessToken.isEmpty() && profile.authType == kElyBy) {
profile.accessToken = auth.accessToken;
profile.clientToken = auth.clientToken;
if (!auth.uuid.isEmpty())
profile.uuid = auth.uuid;
if (!auth.playerName.isEmpty())
profile.resolvedName = auth.playerName;
profile.userType = auth.userType;
saveProfiles();
}
LaunchOptions options;
options.gameDir = gameDir();
options.versionId = m_pendingVersionData.id;
options.playerName = auth.playerName.isEmpty() ? profile.resolvedName : auth.playerName;
if (options.playerName.isEmpty())
options.playerName = profile.login.isEmpty() ? profile.name : profile.login;
options.uuid = auth.uuid.isEmpty() ? profile.uuid : auth.uuid;
options.accessToken = auth.accessToken;
options.clientToken = auth.clientToken;
options.userType = auth.userType;
options.xuid = auth.xuid;
options.javaPath = managedJavaPath();
if (options.javaPath.isEmpty()) {
if (!m_settings.value(QStringLiteral("javaRuntime")).toString().trimmed().isEmpty())
emit launchProgress(Loc::text("java.warning.runtimeMissing"));
options.javaPath = m_settings.value(QStringLiteral("javaPath")).toString();
}
options.minMemoryMb = settingInt(m_settings, QStringLiteral("minMemoryMb"), 512);
options.maxMemoryMb = settingInt(m_settings, QStringLiteral("maxMemoryMb"), 4096);
options.windowWidth = settingInt(m_settings, QStringLiteral("windowWidth"), 0);
options.windowHeight = settingInt(m_settings, QStringLiteral("windowHeight"), 0);
options.fullscreen = m_settings.value(QStringLiteral("fullscreen")).toBool();
options.serverAddress = build.serverUrl.trimmed();
options.launcherName = QCoreApplication::applicationName();
options.launcherVersion = QCoreApplication::applicationVersion();
const QString extra = m_settings.value(QStringLiteral("jvmArgs")).toString().trimmed();
if (!extra.isEmpty())
options.extraJvmArgs = extra.split(QRegularExpression(QStringLiteral("\\s+")),
Qt::SkipEmptyParts);
if (profile.authType == kElyBy)
options.authlibInjectorPath = m_pendingInjectorPath;
QString error;
const bool started = m_launcher->launch(options, m_pendingVersionData, &error);
const QString profileName = profile.name;
const QString buildName = build.name;
const QString serverUrl = build.serverUrl;
cancelPendingLaunch();
if (!started) {
emit launchError(error);
return;
}
emit gameRunningChanged();
emit launched(profileName, buildName, serverUrl);
}
void LauncherBackend::stopGame()
{
m_launcher->terminate();
}
void LauncherBackend::failLaunch(const QString &message)
{
cancelPendingLaunch();
emit launchError(message);
}
bool LauncherBackend::switching() const
{
return m_switcher->isRunning();
}
double LauncherBackend::switchProgress() const
{
return m_switcher->fraction();
}
QString LauncherBackend::switchStage() const
{
return m_switcher->stage();
}
QString LauncherBackend::switchStatus() const
{
return m_switcher->status();
}
bool LauncherBackend::busy() const
{
return m_busy || m_installer->isRunning() || m_loaderInstaller->isRunning()
|| m_javaInstaller->isRunning() || m_switcher->isRunning()
|| m_packDownloader->isRunning();
}
bool LauncherBackend::microsoftAvailable() const
{
#ifdef LAUNCHER_HAS_WEBENGINE
return true;
#else
return false;
#endif
}
void LauncherBackend::setBusy(bool busy)
{
if (m_busy == busy)
return;
m_busy = busy;
emit busyChanged();
}
QString LauncherBackend::gameDir() const
{
const QString configured = m_settings.value(QStringLiteral("gameDir")).toString().trimmed();
return configured.isEmpty() ? LauncherPaths::defaultMinecraftDir()
: QDir::fromNativeSeparators(configured);
}
QVariantMap LauncherBackend::settings() const
{
QVariantMap result = m_settings;
result[QStringLiteral("resolvedGameDir")] = QDir::toNativeSeparators(gameDir());
return result;
}
void LauncherBackend::updateSettings(const QVariantMap &values)
{
const QString previousGameDir = gameDir();
const QString previousLanguage = m_settings.value(QStringLiteral("language")).toString();
for (auto it = values.cbegin(); it != values.cend(); ++it)
m_settings.insert(it.key(), it.value());
saveSettings();
emit settingsChanged();
const QString language = m_settings.value(QStringLiteral("language")).toString();
if (language != previousLanguage)
Localization::instance().setLanguage(language);
if (gameDir() != previousGameDir) {
invalidateCatalog();
emit installedVersionsChanged();
}
}
QStringList LauncherBackend::detectedJava() const
{
QStringList result;
const QStringList candidates = JavaLocator::findAll(gameDir());
for (const QString &candidate : candidates) {
const int major = JavaLocator::majorVersion(candidate);
result << (major > 0 ? QStringLiteral("Java %1 — %2")
.arg(major)
.arg(QDir::toNativeSeparators(candidate))
: QDir::toNativeSeparators(candidate));
}
return result;
}
// ── Сборки Java ────────────────────────────────────────────────────────────
void LauncherBackend::invalidateJavaCatalog()
{
m_javaCatalogDirty = true;
emit javaCatalogChanged();
}
bool LauncherBackend::javaCatalogLoading() const
{
return m_javaMeta->isRefreshing();
}
void LauncherBackend::refreshJavaCatalog(bool force)
{
invalidateJavaCatalog();
m_javaMeta->ensureLoaded([this](bool ok, const QString &warning) {
if (!ok && !warning.isEmpty())
emit launchError(warning);
}, force);
}
QVariantList LauncherBackend::javaCatalog() const
{
if (m_javaCatalogDirty) {
rebuildJavaCatalog();
m_javaCatalogDirty = false;
}
return m_javaCatalog;
}
void LauncherBackend::rebuildJavaCatalog() const
{
m_javaCatalog.clear();
const QList<InstalledJavaRuntime> installed = JavaRuntimeStore::installed();
QHash<QString, qint64> installedSizes;
for (const InstalledJavaRuntime &runtime : installed)
installedSizes.insert(runtime.id, runtime.size);
const auto megabytes = [](qint64 bytes) {
return bytes > 0 ? int((bytes + 512 * 1024) / (1024 * 1024)) : 0;
};
const auto makeRow = [&megabytes](const QString &id,
const QString &kind,
int major,
const QString &version,
const QString &detail,
qint64 size,
bool isInstalled,
bool lts,
bool downloadable) {
return QVariantMap{
{QStringLiteral("id"), id},
{QStringLiteral("kind"), kind},
{QStringLiteral("major"), major},
{QStringLiteral("version"), version},
{QStringLiteral("label"), QStringLiteral("Java %1").arg(version)},
{QStringLiteral("detail"), detail},
{QStringLiteral("coverage"), JavaRequirement::coverage(major)},
{QStringLiteral("sizeMb"), megabytes(size)},
{QStringLiteral("installed"), isInstalled},
{QStringLiteral("lts"), lts},
{QStringLiteral("downloadable"), downloadable},
{QStringLiteral("search"),
QStringLiteral("%1 %2 %3 %4").arg(version, QString::number(major), kind, detail).toLower()}};
};
QSet<QString> listed;
for (const InstalledJavaRuntime &runtime : installed) {
listed.insert(runtime.id);
const auto remote = m_javaMeta->find(runtime.id);
const QString detail = remote ? (remote->kind == JavaRuntimeKind::Mojang
? Loc::text("java.label.mojang").arg(remote->component)
: Loc::text("java.label.temurin").arg(remote->architecture))
: Loc::text("java.label.downloadedByLauncher");
m_javaCatalog.append(makeRow(runtime.id,
runtime.kind,
runtime.major,
runtime.version,
detail,
runtime.size,
true,
remote && remote->lts,
remote.has_value()));
}
const QList<JavaRuntimeKind> kinds{JavaRuntimeKind::Mojang, JavaRuntimeKind::Jdk,
JavaRuntimeKind::Jre};
for (const JavaRuntimeKind kind : kinds) {
const QList<JavaRuntimeEntry> entries = m_javaMeta->entries(kind);
for (const JavaRuntimeEntry &entry : entries) {
if (listed.contains(entry.id))
continue;
const QString detail = kind == JavaRuntimeKind::Mojang
? Loc::text("java.label.mojang").arg(entry.component)
: Loc::text("java.label.temurin").arg(entry.architecture);
m_javaCatalog.append(makeRow(entry.id,
javaKindKey(kind),
entry.major,
entry.version,
detail,
entry.size,
false,
entry.lts,
true));
}
}
}
void LauncherBackend::installJavaRuntime(const QString &runtimeId)
{
if (m_javaInstaller->isRunning()) {
emit launchError(Loc::text("java.error.installBusy")
.arg(m_javaInstaller->label()));
return;
}
if (m_installer->isRunning() || m_loaderInstaller->isRunning() || m_switcher->isRunning()) {
emit launchError(Loc::text("common.error.busyOperation"));
return;
}
const auto entry = m_javaMeta->find(runtimeId);
if (!entry) {
emit launchError(Loc::text("java.error.buildNotInCatalog").arg(runtimeId));
return;
}
m_javaInstaller->install(*entry);
}
void LauncherBackend::removeJavaRuntime(const QString &runtimeId)
{
if (m_javaInstaller->isRunning() && m_javaInstaller->runtimeId() == runtimeId) {
emit launchError(Loc::text("java.error.currentlyDownloading"));
return;
}
QString error;
if (!JavaRuntimeStore::remove(runtimeId, &error)) {
emit launchError(Loc::text("java.error.removeFailed").arg(error));
return;
}
if (m_settings.value(QStringLiteral("javaRuntime")).toString() == runtimeId) {
m_settings.insert(QStringLiteral("javaRuntime"), QString());
saveSettings();
emit settingsChanged();
}
invalidateJavaCatalog();
emit launchProgress(Loc::text("java.status.removed"));
}
QVariantMap LauncherBackend::javaRuntimeInfo(const QString &runtimeId) const
{
if (runtimeId.trimmed().isEmpty())
return {};
const auto runtime = JavaRuntimeStore::find(runtimeId);
if (runtime) {
return QVariantMap{{QStringLiteral("id"), runtime->id},
{QStringLiteral("kind"), runtime->kind},
{QStringLiteral("major"), runtime->major},
{QStringLiteral("version"), runtime->version},
{QStringLiteral("label"), QStringLiteral("Java %1").arg(runtime->version)},
{QStringLiteral("javaPath"), QDir::toNativeSeparators(runtime->javaPath)},
{QStringLiteral("installed"), true}};
}
const auto entry = m_javaMeta->find(runtimeId);
if (!entry)
return QVariantMap{{QStringLiteral("id"), runtimeId},
{QStringLiteral("label"), runtimeId},
{QStringLiteral("installed"), false}};
return QVariantMap{{QStringLiteral("id"), entry->id},
{QStringLiteral("kind"), javaKindKey(entry->kind)},
{QStringLiteral("major"), entry->major},
{QStringLiteral("version"), entry->version},
{QStringLiteral("label"), QStringLiteral("Java %1").arg(entry->version)},
{QStringLiteral("javaPath"), QString()},
{QStringLiteral("installed"), false}};
}
int LauncherBackend::requiredJavaMajor(int buildIndex) const
{
if (buildIndex < 0 || buildIndex >= m_builds.size())
return 0;
return JavaRequirement::minimumFor(m_builds.at(buildIndex).minecraftVersion);
}
QString LauncherBackend::managedJavaPath() const
{
const QString id = m_settings.value(QStringLiteral("javaRuntime")).toString();
if (id.trimmed().isEmpty())
return {};
const auto runtime = JavaRuntimeStore::find(id);
return runtime ? runtime->javaPath : QString();
}
// ── Сезонные сборки ────────────────────────────────────────────────────────
QUrl LauncherBackend::seasonalBaseUrl() const
{
const QString configured =
m_settings.value(QStringLiteral("seasonalBaseUrl")).toString().trimmed();
QUrl url(configured.isEmpty() ? kSeasonalBaseUrl : configured);
while (url.path().endsWith(u'/'))
url.setPath(url.path().chopped(1));
return url;
}
void LauncherBackend::invalidateSeasonalCatalog()
{
m_seasonalCatalogDirty = true;
emit seasonalCatalogChanged();
}
int LauncherBackend::indexOfSeasonalBuild(const QString &seasonalId) const
{
if (seasonalId.isEmpty())
return -1;
for (int i = 0; i < m_builds.size(); ++i) {
if (m_builds[i].seasonalId == seasonalId)
return i;
}
return -1;
}
void LauncherBackend::rebuildSeasonalCatalog() const
{
m_seasonalCatalog.clear();
const QList<SeasonalBuildEntry> builds = m_seasonalMeta->builds();
m_seasonalCatalog.reserve(builds.size());
for (const SeasonalBuildEntry &entry : builds) {
int installedRevision = 0;
bool installed = false;
for (const CustomBuild &build : m_builds) {
if (build.seasonalId != entry.id)
continue;
installed = true;
installedRevision = build.seasonalRevision;
break;
}
const bool updateAvailable = installed && installedRevision < entry.revision;
QString status;
if (!installed)
status = Loc::text("seasonal.status.notInstalled");
else if (updateAvailable)
status = Loc::text("seasonal.status.updateAvailable");
else
status = Loc::text("seasonal.status.installed");
const auto loader = loaderFromKey(entry.loader);
m_seasonalCatalog.append(QVariantMap{
{QStringLiteral("id"), entry.id},
{QStringLiteral("name"), entry.name},
{QStringLiteral("minecraftVersion"), entry.minecraftVersion},
{QStringLiteral("loader"), entry.loader},
{QStringLiteral("loaderTitle"), loader ? loaderTitle(*loader) : Loc::text("loader.label.vanilla")},
{QStringLiteral("loaderVersion"), entry.loaderVersion},
{QStringLiteral("modCount"), entry.modCount},
{QStringLiteral("seasonStart"), formatSeasonDate(entry.seasonStart)},
{QStringLiteral("seasonEnd"), formatSeasonDate(entry.seasonEnd)},
{QStringLiteral("sizeBytes"), entry.archiveSize},
{QStringLiteral("revision"), entry.revision},
{QStringLiteral("description"), entry.description},
{QStringLiteral("serverUrl"), entry.serverUrl},
{QStringLiteral("installed"), installed},
{QStringLiteral("installedRevision"), installedRevision},
{QStringLiteral("updateAvailable"), updateAvailable},
{QStringLiteral("status"), status}});
}
m_seasonalCatalogDirty = false;
}
QVariantList LauncherBackend::seasonalCatalog() const
{
if (m_seasonalCatalogDirty)
rebuildSeasonalCatalog();
return m_seasonalCatalog;
}
bool LauncherBackend::seasonalCatalogLoading() const
{
return m_seasonalMeta->isRefreshing();
}
QString LauncherBackend::seasonalCatalogError() const
{
return m_seasonalMeta->lastError();
}
bool LauncherBackend::seasonalInstalling() const
{
return m_season.stage != SeasonStage::Idle;
}
void LauncherBackend::refreshSeasonalCatalog(bool force)
{
m_seasonalMeta->setBaseUrl(seasonalBaseUrl());
m_seasonalMeta->ensureLoaded(nullptr, force);
}
// ── Установка сезонной сборки ──────────────────────────────────────────────
void LauncherBackend::installSeasonalBuild(const QString &seasonalId)
{
if (m_season.stage != SeasonStage::Idle) {
emit launchError(Loc::text("seasonal.error.installInProgress"));
return;
}
if (m_launcher->isRunning()) {
emit launchError(Loc::text("build.error.closeGameFilesMoving"));
return;
}
if (busy()) {
emit launchError(Loc::text("common.error.busyOperation"));
return;
}
const auto entry = m_seasonalMeta->find(seasonalId);
if (!entry) {
emit launchError(Loc::text("seasonal.error.notInCatalog").arg(seasonalId));
return;
}
m_season = {};
m_season.entry = *entry;
int index = indexOfSeasonalBuild(seasonalId);
if (index < 0) {
CustomBuild build;
build.id = m_nextBuildId++;
build.name = entry->name;
build.serverUrl = entry->serverUrl;
build.minecraftVersion = entry->minecraftVersion;
build.loader = entry->loader;
build.loaderVersion = entry->loaderVersion;
build.seasonalId = entry->id;
m_builds.append(build);
index = m_builds.size() - 1;
saveCustomBuilds();
emit customBuildsChanged();
} else {
updateCustomBuild(index,
{{QStringLiteral("name"), entry->name},
{QStringLiteral("serverUrl"), entry->serverUrl},
{QStringLiteral("minecraftVersion"), entry->minecraftVersion},
{QStringLiteral("loader"), entry->loader},
{QStringLiteral("loaderVersion"), entry->loaderVersion}});
}
m_season.buildId = m_builds[index].id;
setBusy(true);
emit launchProgress(Loc::text("seasonal.progress.installing").arg(entry->name));
seasonAdvance();
emit seasonalInstallingChanged();
}
void LauncherBackend::seasonAdvance()
{
if (indexOfBuildId(m_season.buildId) < 0) {
seasonFail(Loc::text("seasonal.error.entryGone"));
return;
}
switch (m_season.stage) {
case SeasonStage::Idle:
if (seasonBeginSwitch())
return;
[[fallthrough]];
case SeasonStage::Switching:
if (seasonBeginJava())
return;
[[fallthrough]];
case SeasonStage::Java:
if (seasonBeginLoader())
return;
[[fallthrough]];
case SeasonStage::Loader:
if (seasonBeginDownload())
return;
[[fallthrough]];
case SeasonStage::Downloading:
if (seasonBeginApply())
return;
[[fallthrough]];
case SeasonStage::Applying:
seasonFinish();
return;
}
}
bool LauncherBackend::seasonBeginSwitch()
{
const int index = indexOfBuildId(m_season.buildId);
const int current = activeBuildIndex();
if (current == index)
return false;
m_season.stage = SeasonStage::Switching;
emit launchProgress(Loc::text("build.progress.switchingTo").arg(m_builds[index].name));
m_switcher->switchTo(current >= 0 ? m_builds[current].id : -1,
current >= 0 ? m_builds[current].name : QString(),
m_builds[index].id,
m_builds[index].name,
gameDir());
emit switchChanged();
emit busyChanged();
return true;
}
bool LauncherBackend::seasonBeginJava()
{
const int major = m_season.entry.javaMajor > 0
? m_season.entry.javaMajor
: JavaRequirement::minimumFor(m_season.entry.minecraftVersion);
if (major <= 0)
return false;
QString error;
if (!JavaLocator::select(gameDir(), major, QString(), &error).isEmpty())
return false;
m_season.stage = SeasonStage::Java;
emit launchProgress(Loc::text("seasonal.progress.installingJava").arg(major));
m_javaMeta->ensureLoaded([this, major](bool ok, const QString &warning) {
if (m_season.stage != SeasonStage::Java)
return;
if (!ok) {
seasonFail(warning.isEmpty() ? Loc::text("seasonal.error.javaCatalogFailed") : warning);
return;
}
const auto entry = m_javaMeta->bestFor(major);
if (!entry) {
seasonFail(Loc::text("seasonal.error.noJavaBuild").arg(major));
return;
}
m_javaInstaller->install(*entry);
});
return true;
}
bool LauncherBackend::seasonBeginLoader()
{
const int index = indexOfBuildId(m_season.buildId);
const CustomBuild &build = m_builds[index];
const auto loader = loaderFromKey(build.loader);
if (!loader) {
if (checkInstallation(index).isEmpty())
return false;
m_season.stage = SeasonStage::Loader;
m_installer->install(gameDir(), build.minecraftVersion);
return true;
}
if (!build.resolvedVersionId.isEmpty() && checkInstallation(index).isEmpty())
return false;
m_season.stage = SeasonStage::Loader;
m_loaderInstallBuildId = build.id;
m_loaderInstaller->install(gameDir(),
*loader,
build.minecraftVersion,
build.loaderVersion,
managedJavaPath().isEmpty()
? m_settings.value(QStringLiteral("javaPath")).toString()
: managedJavaPath());
return true;
}
bool LauncherBackend::seasonBeginDownload()
{
const SeasonalBuildEntry &entry = m_season.entry;
const QString path = LauncherPaths::buildDir(m_season.buildId)
+ QStringLiteral("/.pack-r") + QString::number(entry.revision)
+ QStringLiteral(".zip");
m_season.stage = SeasonStage::Downloading;
m_packDownloader->download(entry.archiveUrl,
path,
entry.archiveSha256,
entry.archiveSize,
entry.name);
return true;
}
bool LauncherBackend::seasonBeginApply()
{
if (m_season.packPath.isEmpty()) {
seasonFail(Loc::text("seasonal.error.filesNotDownloaded"));
return true;
}
const QStringList remove = seasonalPackEntries(m_season.buildId);
const QJsonObject note{{QStringLiteral("seasonalId"), m_season.entry.id},
{QStringLiteral("revision"), m_season.entry.revision},
{QStringLiteral("sha256"), m_season.entry.archiveSha256}};
const int index = indexOfBuildId(m_season.buildId);
m_season.stage = SeasonStage::Applying;
m_switcher->applyPack(m_season.buildId,
m_builds[index].name,
m_season.packPath,
remove,
note,
gameDir());
emit switchChanged();
emit busyChanged();
return true;
}
void LauncherBackend::seasonFinish()
{
const int index = indexOfBuildId(m_season.buildId);
const QString name = index >= 0 ? m_builds[index].name : m_season.entry.name;
const QString seasonalId = m_season.entry.id;
m_season = {};
setBusy(false);
invalidateSeasonalCatalog();
emit seasonalInstallingChanged();
emit launchProgress(Loc::text("seasonal.status.buildInstalled").arg(name));
emit seasonalInstallFinished(seasonalId, name);
}
void LauncherBackend::seasonFail(const QString &message)
{
const int index = indexOfBuildId(m_season.buildId);
const QString name = index >= 0 ? m_builds[index].name : m_season.entry.name;
if (!m_season.packPath.isEmpty())
QFile::remove(m_season.packPath);
m_season = {};
setBusy(false);
emit seasonalInstallingChanged();
emit launchError(message.isEmpty()
? Loc::text("seasonal.error.installAborted").arg(name)
: Loc::text("seasonal.error.installAbortedWhy").arg(name, message));
}
void LauncherBackend::cancelSeasonalInstall()
{
switch (m_season.stage) {
case SeasonStage::Idle:
return;
case SeasonStage::Java:
m_javaInstaller->cancel();
return;
case SeasonStage::Loader:
if (m_loaderInstaller->isRunning())
m_loaderInstaller->cancel();
else
m_installer->cancel();
return;
case SeasonStage::Downloading:
m_packDownloader->cancel();
return;
case SeasonStage::Switching:
case SeasonStage::Applying:
emit launchError(Loc::text("switch.error.cannotCancel"));
return;
}
}
// ── Состояние установленной сезонной сборки ────────────────────────────────
QStringList LauncherBackend::seasonalPackEntries(int buildId) const
{
QFile file(LauncherPaths::seasonalStateFile(buildId));
if (!file.open(QIODevice::ReadOnly))
return {};
const QJsonObject root = QJsonDocument::fromJson(file.readAll()).object();
QStringList entries;
const QJsonArray array = root.value(QStringLiteral("entries")).toArray();
entries.reserve(array.size());
for (const QJsonValue &value : array)
entries << value.toString();
return entries;
}
void LauncherBackend::recordSeasonalPack(int buildId,
const QJsonObject &note,
const QStringList &entries)
{
const QString seasonalId = note.value(QStringLiteral("seasonalId")).toString();
const int revision = note.value(QStringLiteral("revision")).toInt();
if (seasonalId.isEmpty() || revision <= 0)
return;
const QJsonObject state{{QStringLiteral("seasonalId"), seasonalId},
{QStringLiteral("revision"), revision},
{QStringLiteral("sha256"),
note.value(QStringLiteral("sha256")).toString()},
{QStringLiteral("installedAt"),
QDateTime::currentDateTimeUtc().toString(Qt::ISODate)},
{QStringLiteral("entries"), QJsonArray::fromStringList(entries)}};
QDir().mkpath(LauncherPaths::buildDir(buildId));
QSaveFile file(LauncherPaths::seasonalStateFile(buildId));
if (file.open(QIODevice::WriteOnly)) {
const QByteArray payload = QJsonDocument(state).toJson(QJsonDocument::Indented);
if (file.write(payload) == payload.size())
file.commit();
else
file.cancelWriting();
}
const int index = indexOfBuildId(buildId);
if (index < 0)
return;
updateCustomBuild(index,
{{QStringLiteral("seasonalId"), seasonalId},
{QStringLiteral("seasonalRevision"), revision}});
}
void LauncherBackend::openMinecraftFolder()
{
const QString modsDir = gameDir() + QStringLiteral("/mods");
QDir().mkpath(modsDir);
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(modsDir)))
emit launchError(Loc::text("launch.error.openModsFailed"));
}
void LauncherBackend::openGameFolder()
{
const QString directory = gameDir();
QDir().mkpath(directory);
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(directory)))
emit launchError(Loc::text("launch.error.openGameDirFailed"));
}
bool LauncherBackend::moveAsideBroken(const QString &path, const QString &reason)
{
const QString backup = path + QStringLiteral(".bak");
QFile::remove(backup);
if (!QFile::rename(path, backup)) {
m_storageIssues << Loc::text("storage.error.corruptNotSetAside")
.arg(QDir::toNativeSeparators(path), reason);
return false;
}
m_storageIssues << Loc::text("storage.warning.corruptRecreated")
.arg(QDir::toNativeSeparators(path),
reason,
QDir::toNativeSeparators(backup));
return true;
}
QJsonArray LauncherBackend::readArrayFile(const QString &path)
{
QFile file(path);
if (!file.exists())
return {};
if (!file.open(QIODevice::ReadOnly)) {
m_storageIssues << Loc::text("common.error.readFailed")
.arg(QDir::toNativeSeparators(path), file.errorString());
return {};
}
QJsonParseError parseError;
const QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &parseError);
if (parseError.error != QJsonParseError::NoError) {
file.close();
moveAsideBroken(path, parseError.errorString());
return {};
}
if (!document.isArray()) {
file.close();
moveAsideBroken(path, Loc::text("storage.error.expectedArray"));
return {};
}
return document.array();
}
QJsonObject LauncherBackend::readObjectFile(const QString &path)
{
QFile file(path);
if (!file.exists())
return {};
if (!file.open(QIODevice::ReadOnly)) {
m_storageIssues << Loc::text("common.error.readFailed")
.arg(QDir::toNativeSeparators(path), file.errorString());
return {};
}
QJsonParseError parseError;
const QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &parseError);
if (parseError.error != QJsonParseError::NoError) {
file.close();
moveAsideBroken(path, parseError.errorString());
return {};
}
if (!document.isObject()) {
file.close();
moveAsideBroken(path, Loc::text("storage.error.expectedObject"));
return {};
}
return document.object();
}
bool LauncherBackend::writeJsonFile(const QString &path, const QJsonDocument &document)
{
QString storageError;
if (!LauncherPaths::ensureRootExists(&storageError)) {
emit launchError(Loc::text("storage.error.launcherDirFailed").arg(storageError));
return false;
}
const QByteArray payload = document.toJson();
QFile file(path);
if (!file.open(QIODevice::WriteOnly) || file.write(payload) != payload.size()) {
emit launchError(Loc::text("storage.error.saveFailedWhy")
.arg(QDir::toNativeSeparators(path), file.errorString()));
return false;
}
return true;
}
void LauncherBackend::loadData()
{
auto loadFile = [this](const QString &path, auto handler) {
for (const auto &val : readArrayFile(path))
handler(val.toObject());
};
loadFile(LauncherPaths::profilesFile(), [this](const QJsonObject &o) {
Profile profile;
profile.id = o["id"].toInt();
profile.name = o["name"].toString();
profile.login = o["login"].toString();
profile.password = o["password"].toString();
profile.authType = o.value("authType").toString(kOffline);
profile.clientToken = o.value("clientToken").toString();
profile.accessToken = o.value("accessToken").toString();
profile.uuid = o.value("uuid").toString();
profile.resolvedName = o.value("resolvedName").toString();
profile.userType = o.value("userType").toString();
profile.refreshToken = o.value("refreshToken").toString();
profile.xuid = o.value("xuid").toString();
profile.tokenExpiry = o.value("tokenExpiry").toString();
if (profile.clientToken.isEmpty())
profile.clientToken = AuthService::generateClientToken();
m_profiles.append(profile);
if (profile.id >= m_nextProfileId)
m_nextProfileId = profile.id + 1;
});
std::sort(m_profiles.begin(), m_profiles.end(),
[](const Profile &a, const Profile &b) { return a.id < b.id; });
int skippedBuilds = 0;
loadFile(LauncherPaths::customBuildsFile(), [this, &skippedBuilds](const QJsonObject &o) {
CustomBuild build;
build.id = o["id"].toInt();
build.name = o["name"].toString();
build.serverUrl = o["serverUrl"].toString();
build.minecraftVersion = o.value("minecraftVersion").toString();
if (build.minecraftVersion.isEmpty())
build.minecraftVersion = o.value("versionId").toString();
build.loader = o.value("loader").toString();
build.loaderVersion = o.value("loaderVersion").toString();
build.resolvedVersionId = o.value("resolvedVersionId").toString();
build.seasonalId = o.value("seasonalId").toString();
build.seasonalRevision = o.value("seasonalRevision").toInt();
if (build.name.trimmed().isEmpty() && build.minecraftVersion.trimmed().isEmpty()) {
++skippedBuilds;
return;
}
m_builds.append(build);
if (build.id >= m_nextBuildId)
m_nextBuildId = build.id + 1;
});
if (skippedBuilds > 0) {
m_storageIssues << Loc::text("storage.warning.buildsSkipped")
.arg(QDir::toNativeSeparators(LauncherPaths::customBuildsFile()))
.arg(skippedBuilds);
}
std::sort(m_builds.begin(), m_builds.end(),
[](const CustomBuild &a, const CustomBuild &b) { return a.id < b.id; });
}
bool LauncherBackend::migrateLegacyBuildsFile()
{
if (QFile::exists(LauncherPaths::customBuildsFile()))
return false;
if (!QFile::exists(LauncherPaths::legacyCustomBuildsFile()))
return false;
const QJsonArray legacy = readArrayFile(LauncherPaths::legacyCustomBuildsFile());
if (legacy.isEmpty() && !QFile::exists(LauncherPaths::legacyCustomBuildsFile()))
return false;
QJsonArray migrated;
for (const auto &value : legacy) {
const QJsonObject o = value.toObject();
migrated.append(QJsonObject{{"id", o.value("id").toInt()},
{"name", o.value("name").toString()},
{"serverUrl", o.value("serverUrl").toString()},
{"minecraftVersion", o.value("versionId").toString()},
{"loader", QString()},
{"loaderVersion", QString()},
{"resolvedVersionId", QString()}});
}
if (!writeJsonFile(LauncherPaths::customBuildsFile(), QJsonDocument(migrated)))
return false;
const QString retired = LauncherPaths::legacyCustomBuildsFile() + QStringLiteral(".migrated");
QFile::remove(retired);
if (!QFile::rename(LauncherPaths::legacyCustomBuildsFile(), retired)) {
m_storageIssues << Loc::text("storage.warning.migrationLeftover")
.arg(QDir::toNativeSeparators(LauncherPaths::customBuildsFile()),
QDir::toNativeSeparators(
LauncherPaths::legacyCustomBuildsFile()));
}
return true;
}
QString LauncherBackend::importGameDir()
{
const std::optional<GameDirImport::Detected> found = GameDirImport::detect(gameDir());
if (!found)
return {};
CustomBuild build;
build.id = m_nextBuildId++;
build.minecraftVersion = found->minecraftVersion;
build.resolvedVersionId = found->resolvedVersionId;
build.loader = found->loader;
build.loaderVersion = found->loaderVersion;
if (build.minecraftVersion.isEmpty()) {
build.name = Loc::text("build.title.fromGameDir");
} else if (const auto loader = loaderFromKey(build.loader)) {
build.name = loaderTitle(*loader) + u' ' + build.minecraftVersion;
} else {
build.name = QStringLiteral("Minecraft ") + build.minecraftVersion;
}
m_builds.append(build);
m_settings.insert(QStringLiteral("activeBuildId"), build.id);
saveSettings();
saveCustomBuilds();
return build.name;
}
void LauncherBackend::ensureDataFiles()
{
if (!QFile::exists(LauncherPaths::settingsFile()))
saveSettings();
if (!QFile::exists(LauncherPaths::profilesFile()))
saveProfiles();
if (!QFile::exists(LauncherPaths::customBuildsFile()))
saveCustomBuilds();
}
void LauncherBackend::saveProfiles()
{
QJsonArray arr;
for (const auto &p : m_profiles)
arr.append(QJsonObject{{"id", p.id},
{"name", p.name},
{"login", p.login},
{"password", p.password},
{"authType", p.authType},
{"clientToken", p.clientToken},
{"accessToken", p.accessToken},
{"uuid", p.uuid},
{"resolvedName", p.resolvedName},
{"userType", p.userType},
{"refreshToken", p.refreshToken},
{"xuid", p.xuid},
{"tokenExpiry", p.tokenExpiry}});
writeJsonFile(LauncherPaths::profilesFile(), QJsonDocument(arr));
}
void LauncherBackend::saveCustomBuilds()
{
QJsonArray arr;
for (const auto &b : m_builds)
arr.append(QJsonObject{{"id", b.id},
{"name", b.name},
{"serverUrl", b.serverUrl},
{"minecraftVersion", b.minecraftVersion},
{"loader", b.loader},
{"loaderVersion", b.loaderVersion},
{"resolvedVersionId", b.resolvedVersionId},
{"seasonalId", b.seasonalId},
{"seasonalRevision", b.seasonalRevision}});
writeJsonFile(LauncherPaths::customBuildsFile(), QJsonDocument(arr));
}
void LauncherBackend::loadSettings()
{
m_settings = {{QStringLiteral("gameDir"), QString()},
{QStringLiteral("javaPath"), QString()},
{QStringLiteral("javaRuntime"), QString()},
{QStringLiteral("minMemoryMb"), 512},
{QStringLiteral("maxMemoryMb"), 4096},
{QStringLiteral("jvmArgs"), QString()},
{QStringLiteral("windowWidth"), 0},
{QStringLiteral("windowHeight"), 0},
{QStringLiteral("fullscreen"), false},
{QStringLiteral("language"), QStringLiteral("system")},
{QStringLiteral("activeBuildId"), 0},
{QStringLiteral("seasonalBaseUrl"), kSeasonalBaseUrl}};
const QJsonObject stored = readObjectFile(LauncherPaths::settingsFile());
for (auto it = stored.constBegin(); it != stored.constEnd(); ++it)
m_settings.insert(it.key(), it.value().toVariant());
m_settings.remove(QStringLiteral("launcherName"));
m_settings.remove(QStringLiteral("launcherVersion"));
}
void LauncherBackend::saveSettings()
{
writeJsonFile(LauncherPaths::settingsFile(),
QJsonDocument(QJsonObject::fromVariantMap(m_settings)));
}