chore(clang-tidy): modernize the code

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2026-05-10 20:51:56 +03:00
parent 2e45d135c5
commit effa8bedb1
No known key found for this signature in database
GPG key ID: 55EF5DA53DB36318
42 changed files with 854 additions and 786 deletions

View file

@ -55,13 +55,13 @@
#include "settings/INISettingsObject.h"
#include "SysInfo.h"
#include "tasks/ConcurrentTask.h"
#include "ui/dialogs/BlockedModsDialog.h"
#include "ui/dialogs/CustomMessageBox.h"
#include <QDebug>
#include <QFileInfo>
#include <algorithm>
#include "HardwareInfo.h"
#include "meta/Index.h"
@ -70,163 +70,170 @@
#include "net/ApiDownload.h"
#include "ui/pages/modplatform/OptionalModDialog.h"
static const FlameAPI api;
bool FlameCreationTask::abort()
{
if (!canAbort())
if (!canAbort()) {
return false;
}
if (m_processUpdateFileInfoJob)
if (m_processUpdateFileInfoJob) {
m_processUpdateFileInfoJob->abort();
if (m_filesJob)
}
if (m_filesJob) {
m_filesJob->abort();
if (m_modIdResolver)
}
if (m_modIdResolver) {
m_modIdResolver->abort();
}
return InstanceCreationTask::abort();
}
bool FlameCreationTask::updateInstance()
{
auto instance_list = APPLICATION->instances();
auto* instanceList = APPLICATION->instances();
// FIXME: How to handle situations when there's more than one install already for a given modpack?
BaseInstance* inst;
if (auto original_id = originalInstanceID(); !original_id.isEmpty()) {
inst = instance_list->getInstanceById(original_id);
BaseInstance* inst = nullptr;
if (auto originalId = originalInstanceID(); !originalId.isEmpty()) {
inst = instanceList->getInstanceById(originalId);
Q_ASSERT(inst);
} else {
inst = instance_list->getInstanceByManagedName(originalName());
inst = instanceList->getInstanceByManagedName(originalName());
if (!inst) {
inst = instance_list->getInstanceById(originalName());
inst = instanceList->getInstanceById(originalName());
if (!inst)
if (!inst) {
return false;
}
}
}
QString index_path(FS::PathCombine(m_stagingPath, "manifest.json"));
QString indexPath(FS::PathCombine(m_stagingPath, "manifest.json"));
try {
Flame::loadManifest(m_pack, index_path);
Flame::loadManifest(m_pack, indexPath);
} catch (const JSONValidationError& e) {
setError(tr("Could not understand pack manifest:\n") + e.cause());
return false;
}
auto version_id = inst->getManagedPackVersionName();
auto version_str = !version_id.isEmpty() ? tr(" (version %1)").arg(version_id) : "";
auto versionId = inst->getManagedPackVersionName();
auto versionStr = !versionId.isEmpty() ? tr(" (version %1)").arg(versionId) : "";
if (shouldConfirmUpdate()) {
auto should_update = askIfShouldUpdate(m_parent, version_str);
if (should_update == ShouldUpdate::SkipUpdating)
auto shouldUpdate = askIfShouldUpdate(m_parent, versionStr);
if (shouldUpdate == ShouldUpdate::SkipUpdating) {
return false;
if (should_update == ShouldUpdate::Cancel) {
}
if (shouldUpdate == ShouldUpdate::Cancel) {
m_abort = true;
return false;
}
}
QDir old_inst_dir(inst->instanceRoot());
QDir oldInstDir(inst->instanceRoot());
QString old_index_folder(FS::PathCombine(old_inst_dir.absolutePath(), "flame"));
QString old_index_path(FS::PathCombine(old_index_folder, "manifest.json"));
QString oldIndexFolder(FS::PathCombine(oldInstDir.absolutePath(), "flame"));
QString oldIndexPath(FS::PathCombine(oldIndexFolder, "manifest.json"));
QFileInfo old_index_file(old_index_path);
if (old_index_file.exists()) {
Flame::Manifest old_pack;
Flame::loadManifest(old_pack, old_index_path);
QFileInfo oldIndexFile(oldIndexPath);
if (oldIndexFile.exists()) {
Flame::Manifest oldPack;
Flame::loadManifest(oldPack, oldIndexPath);
auto& old_files = old_pack.files;
auto& oldFiles = oldPack.files;
auto& files = m_pack.files;
// Remove repeated files, we don't need to download them!
auto files_iterator = files.begin();
while (files_iterator != files.end()) {
const auto& file = files_iterator;
auto filesIterator = files.begin();
while (filesIterator != files.end()) {
const auto& file = filesIterator;
auto old_file = old_files.find(file.key());
if (old_file != old_files.end()) {
auto oldFile = oldFiles.find(file.key());
if (oldFile != oldFiles.end()) {
// We found a match, but is it a different version?
if (old_file->fileId == file->fileId) {
if (oldFile->fileId == file->fileId) {
qDebug() << "Removed file at" << file->targetFolder << "with id" << file->fileId << "from list of downloads";
old_files.remove(file.key());
files_iterator = files.erase(files_iterator);
oldFiles.remove(file.key());
filesIterator = files.erase(filesIterator);
if (files_iterator != files.begin())
files_iterator--;
if (filesIterator != files.begin()) {
filesIterator--;
}
}
}
files_iterator++;
filesIterator++;
}
QDir old_minecraft_dir(inst->gameRoot());
QDir oldMinecraftDir(inst->gameRoot());
// We will remove all the previous overrides, to prevent duplicate files!
// TODO: Currently 'overrides' will always override the stuff on update. How do we preserve unchanged overrides?
// FIXME: We may want to do something about disabled mods.
auto old_overrides = Override::readOverrides("overrides", old_index_folder);
for (const auto& entry : old_overrides) {
scheduleToDelete(m_parent, old_minecraft_dir, entry);
auto oldOverrides = Override::readOverrides("overrides", oldIndexFolder);
for (const auto& entry : oldOverrides) {
scheduleToDelete(m_parent, oldMinecraftDir, entry);
}
// Remove remaining old files (we need to do an API request to know which ids are which files...)
QStringList fileIds;
for (auto& file : old_files) {
for (auto& file : oldFiles) {
fileIds.append(QString::number(file.fileId));
}
auto [job, raw_response] = api.getFiles(fileIds);
auto [job, rawResponse] = FlameAPI::getFiles(fileIds);
QEventLoop loop;
connect(job.get(), &Task::succeeded, this, [this, raw_response, fileIds, old_inst_dir, &old_files, old_minecraft_dir] {
connect(job.get(), &Task::succeeded, this, [this, rawResponse, fileIds, oldInstDir, &oldFiles, oldMinecraftDir] {
// Parse the API response
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*raw_response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Flame files task at" << parse_error.offset
<< "reason:" << parse_error.errorString();
qWarning() << *raw_response;
QJsonParseError parseError{};
auto doc = QJsonDocument::fromJson(*rawResponse, &parseError);
if (parseError.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Flame files task at" << parseError.offset
<< "reason:" << parseError.errorString();
qWarning() << *rawResponse;
return;
}
try {
QJsonArray entries;
if (fileIds.size() == 1)
if (fileIds.size() == 1) {
entries = { Json::requireObject(Json::requireObject(doc), "data") };
else
} else {
entries = Json::requireArray(Json::requireObject(doc), "data");
}
for (auto entry : entries) {
auto entry_obj = Json::requireObject(entry);
auto entryObj = Json::requireObject(entry);
Flame::File file;
// We don't care about blocked mods, we just need local data to delete the file
file.version = FlameMod::loadIndexedPackVersion(entry_obj);
auto id = Json::requireInteger(entry_obj, "id");
old_files.insert(id, file);
file.version = FlameMod::loadIndexedPackVersion(entryObj);
auto id = Json::requireInteger(entryObj, "id");
oldFiles.insert(id, file);
}
} catch (Json::JsonException& e) {
qCritical() << e.cause() << e.what();
}
// Delete the files
for (auto& file : old_files) {
if (file.version.fileName.isEmpty() || file.targetFolder.isEmpty())
for (auto& file : oldFiles) {
if (file.version.fileName.isEmpty() || file.targetFolder.isEmpty()) {
continue;
}
QString relative_path(FS::PathCombine(file.targetFolder, file.version.fileName));
scheduleToDelete(m_parent, old_minecraft_dir, relative_path, true);
QString relativePath(FS::PathCombine(file.targetFolder, file.version.fileName));
scheduleToDelete(m_parent, oldMinecraftDir, relativePath, true);
}
});
connect(job.get(), &Task::failed, this, [](QString reason) { qCritical() << "Failed to get files:" << reason; });
connect(job.get(), &Task::failed, this, [](const QString& reason) { qCritical() << "Failed to get files:" << reason; });
connect(job.get(), &Task::finished, &loop, &QEventLoop::quit);
m_processUpdateFileInfoJob = job;
@ -237,10 +244,10 @@ bool FlameCreationTask::updateInstance()
m_processUpdateFileInfoJob = nullptr;
} else {
// We don't have an old index file, so we may duplicate stuff!
auto dialog = CustomMessageBox::selectable(m_parent, tr("No index file."),
tr("We couldn't find a suitable index file for the older version. This may cause some "
"of the files to be duplicated. Do you want to continue?"),
QMessageBox::Warning, QMessageBox::Ok | QMessageBox::Cancel);
auto* dialog = CustomMessageBox::selectable(m_parent, tr("No index file."),
tr("We couldn't find a suitable index file for the older version. This may cause some "
"of the files to be duplicated. Do you want to continue?"),
QMessageBox::Warning, QMessageBox::Ok | QMessageBox::Cancel);
if (dialog->exec() == QDialog::DialogCode::Rejected) {
m_abort = true;
@ -257,7 +264,10 @@ bool FlameCreationTask::updateInstance()
return false;
}
QString FlameCreationTask::getVersionForLoader(QString uid, QString loaderType, QString loaderVersion, QString mcVersion)
QString FlameCreationTask::getVersionForLoader(const QString& uid,
const QString& loaderType,
QString loaderVersion,
const QString& mcVersion)
{
if (loaderVersion == "recommended") {
auto vlist = APPLICATION->metadataIndex()->get(uid);
@ -270,27 +280,29 @@ QString FlameCreationTask::getVersionForLoader(QString uid, QString loaderType,
QEventLoop loadVersionLoop;
auto task = vlist->getLoadTask();
connect(task.get(), &Task::finished, &loadVersionLoop, &QEventLoop::quit);
if (!task->isRunning())
if (!task->isRunning()) {
task->start();
}
loadVersionLoop.exec();
}
for (auto version : vlist->versions()) {
for (const auto& version : vlist->versions()) {
// first recommended build we find, we use.
if (!version->isRecommended())
if (!version->isRecommended()) {
continue;
}
auto reqs = version->requiredSet();
// filter by minecraft version, if the loader depends on a certain version.
// not all mod loaders depend on a given Minecraft version, so we won't do this
// filtering for those loaders.
if (loaderType == "forge" || loaderType == "neoforge") {
auto iter = std::find_if(reqs.begin(), reqs.end(), [mcVersion](const Meta::Require& req) {
return req.uid == "net.minecraft" && req.equalsVersion == mcVersion;
});
if (iter == reqs.end())
auto iter = std::ranges::find_if(
reqs, [mcVersion](const Meta::Require& req) { return req.uid == "net.minecraft" && req.equalsVersion == mcVersion; });
if (iter == reqs.end()) {
continue;
}
}
return version->descriptor();
}
@ -311,17 +323,18 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
{
QEventLoop loop;
QString parent_folder(FS::PathCombine(m_stagingPath, "flame"));
QString parentFolder(FS::PathCombine(m_stagingPath, "flame"));
try {
QString index_path(FS::PathCombine(m_stagingPath, "manifest.json"));
if (!m_pack.is_loaded)
Flame::loadManifest(m_pack, index_path);
QString indexPath(FS::PathCombine(m_stagingPath, "manifest.json"));
if (!m_pack.isLoaded) {
Flame::loadManifest(m_pack, indexPath);
}
// Keep index file in case we need it some other time (like when changing versions)
QString new_index_place(FS::PathCombine(parent_folder, "manifest.json"));
FS::ensureFilePathExists(new_index_place);
FS::move(index_path, new_index_place);
QString newIndexPlace(FS::PathCombine(parentFolder, "manifest.json"));
FS::ensureFilePathExists(newIndexPlace);
FS::move(indexPath, newIndexPlace);
} catch (const JSONValidationError& e) {
setError(tr("Could not understand pack manifest:\n") + e.cause());
@ -332,7 +345,7 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
QString overridePath = FS::PathCombine(m_stagingPath, m_pack.overrides);
if (QFile::exists(overridePath)) {
// Create a list of overrides in "overrides.txt" inside flame/
Override::createOverrides("overrides", parent_folder, overridePath);
Override::createOverrides("overrides", parentFolder, overridePath);
QString mcPath = FS::PathCombine(m_stagingPath, "minecraft");
if (!FS::move(overridePath, mcPath)) {
@ -353,8 +366,9 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
auto id = loader.id;
if (id.startsWith("neoforge-")) {
id.remove("neoforge-");
if (id.startsWith("1.20.1-"))
if (id.startsWith("1.20.1-")) {
id.remove("1.20.1-"); // this is a mess for curseforge
}
loaderType = "neoforge";
loaderUid = "net.neoforged";
} else if (id.startsWith("forge-")) {
@ -388,13 +402,14 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
logWarning(tr("Mysterious trailing dots removed from Minecraft version while importing pack."));
}
auto components = instance->getPackProfile();
auto* components = instance->getPackProfile();
components->buildingFromScratch();
components->setComponentVersion("net.minecraft", mcVersion, true);
if (!loaderType.isEmpty()) {
auto version = getVersionForLoader(loaderUid, loaderType, loaderVersion, mcVersion);
if (version.isEmpty())
if (version.isEmpty()) {
return nullptr;
}
components->setComponentVersion(loaderUid, version);
}
@ -415,13 +430,13 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
// only set memory if this is a fresh instance
if (!m_instance && recommendedRAM > 0) {
const uint64_t sysMiB = HardwareInfo::totalRamMiB();
const uint64_t max = sysMiB * 0.9;
const auto max = sysMiB * 9 / 10;
if (static_cast<uint64_t>(recommendedRAM) > max) {
logWarning(tr("The recommended memory of the modpack exceeds 90% of your system RAM—reducing it from %1 MiB to %2 MiB!")
.arg(recommendedRAM)
.arg(max));
recommendedRAM = max;
recommendedRAM = static_cast<int>(max);
}
instance->settings()->set("OverrideMemory", true);
@ -439,23 +454,24 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
qDebug() << info.fileName();
jarMods.push_back(info.absoluteFilePath());
}
auto profile = instance->getPackProfile();
auto* profile = instance->getPackProfile();
profile->installJarMods(jarMods);
// nuke the original files
FS::deletePath(jarmodsPath);
}
// Don't add managed info to packs without an ID (most likely imported from ZIP)
if (!m_managedId.isEmpty())
if (!m_managedId.isEmpty()) {
instance->setManagedPack("flame", m_managedId, m_pack.name, m_managedVersionId, m_pack.version);
else
} else {
instance->setManagedPack("flame", "", name(), "", "");
}
instance->setName(name());
m_modIdResolver.reset(new Flame::FileResolvingTask(m_pack));
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::succeeded, this, [this, &loop] { idResolverSucceeded(loop); });
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::failed, [this, &loop](QString reason) {
connect(m_modIdResolver.get(), &Flame::FileResolvingTask::failed, [this, &loop](const QString& reason) {
m_modIdResolver.reset();
setError(tr("Unable to resolve mod IDs:\n") + reason);
loop.quit();
@ -469,17 +485,17 @@ std::unique_ptr<MinecraftInstance> FlameCreationTask::createInstance()
loop.exec();
bool did_succeed = getError().isEmpty();
bool didSucceed = getError().isEmpty();
// Update information of the already installed instance, if any.
if (m_instance && did_succeed) {
if (m_instance && didSucceed) {
setAbortable(false);
auto inst = m_instance.value();
auto* inst = *m_instance;
inst->copyManagedPack(*instance);
}
if (did_succeed) {
if (didSucceed) {
return instance;
}
return nullptr;
@ -508,7 +524,7 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
}
// first check for blocked mods
QList<BlockedMod> blocked_mods;
QList<BlockedMod> blockedMods;
auto anyBlocked = false;
for (const auto& result : results.values()) {
if (result.pack.resourceType != ModPlatform::ResourceType::Mod) {
@ -517,19 +533,19 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
// skip optional mods that were not selected
if (result.version.downloadUrl.isEmpty()) {
BlockedMod blocked_mod;
blocked_mod.name = result.version.fileName;
blocked_mod.websiteUrl = QString("%1/download/%2").arg(result.pack.websiteUrl, QString::number(result.fileId));
blocked_mod.hash = result.version.hash;
blocked_mod.matched = false;
blocked_mod.localPath = "";
blocked_mod.targetFolder = result.targetFolder;
BlockedMod blockedMod;
blockedMod.name = result.version.fileName;
blockedMod.websiteUrl = QString("%1/download/%2").arg(result.pack.websiteUrl, QString::number(result.fileId));
blockedMod.hash = result.version.hash;
blockedMod.matched = false;
blockedMod.localPath = "";
blockedMod.targetFolder = result.targetFolder;
auto fileName = result.version.fileName;
fileName = FS::RemoveInvalidPathChars(fileName);
auto relpath = FS::PathCombine(result.targetFolder, fileName);
blocked_mod.disabled = !result.required && !m_selectedOptionalMods.contains(relpath);
blockedMod.disabled = !result.required && !m_selectedOptionalMods.contains(relpath);
blocked_mods.append(blocked_mod);
blockedMods.append(blockedMod);
anyBlocked = true;
}
@ -537,16 +553,16 @@ void FlameCreationTask::idResolverSucceeded(QEventLoop& loop)
if (anyBlocked) {
qWarning() << "Blocked mods found, displaying mod list";
BlockedModsDialog message_dialog(m_parent, tr("Blocked mods found"),
tr("The following files are not available for download in third party launchers.<br/>"
"You will need to manually download them and add them to the instance."),
blocked_mods);
BlockedModsDialog messageDialog(m_parent, tr("Blocked mods found"),
tr("The following files are not available for download in third party launchers.<br/>"
"You will need to manually download them and add them to the instance."),
blockedMods);
message_dialog.setModal(true);
messageDialog.setModal(true);
if (message_dialog.exec()) {
qDebug() << "Post dialog blocked mods list:" << blocked_mods;
copyBlockedMods(blocked_mods);
if (messageDialog.exec() != 0) {
qDebug() << "Post dialog blocked mods list:" << blockedMods;
copyBlockedMods(blockedMods);
setupDownloadJob(loop);
} else {
m_modIdResolver.reset();
@ -586,7 +602,7 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
m_filesJob.reset();
validateOtherResources(loop);
});
connect(m_filesJob.get(), &NetJob::failed, [this](QString reason) {
connect(m_filesJob.get(), &NetJob::failed, [this](const QString& reason) {
m_filesJob.reset();
setError(reason);
});
@ -602,22 +618,23 @@ void FlameCreationTask::setupDownloadJob(QEventLoop& loop)
/// @brief copy the matched blocked mods to the instance staging area
/// @param blocked_mods list of the blocked mods and their matched paths
void FlameCreationTask::copyBlockedMods(const QList<BlockedMod>& blocked_mods)
void FlameCreationTask::copyBlockedMods(const QList<BlockedMod>& blockedMods)
{
setStatus(tr("Copying Blocked Mods..."));
setAbortable(false);
int i = 0;
int total = blocked_mods.length();
auto total = blockedMods.length();
setProgress(i, total);
for (const auto& mod : blocked_mods) {
for (const auto& mod : blockedMods) {
if (!mod.matched) {
qDebug() << mod.name << "was not matched to a local file, skipping copy";
continue;
}
auto destPath = FS::PathCombine(m_stagingPath, "minecraft", mod.targetFolder, mod.name);
if (mod.disabled)
if (mod.disabled) {
destPath += ".disabled";
}
setStatus(tr("Copying Blocked Mods (%1 out of %2 are done)").arg(QString::number(i), QString::number(total)));
@ -644,13 +661,13 @@ void FlameCreationTask::validateOtherResources(QEventLoop& loop)
{
qDebug() << "Validating whether other resources are in the right place";
QStringList zipMods;
for (auto [fileName, targetFolder] : m_otherResources) {
for (const auto& [fileName, targetFolder] : m_otherResources) {
qDebug() << "Checking" << fileName << "...";
auto localPath = FS::PathCombine(m_stagingPath, "minecraft", targetFolder, fileName);
/// @brief check the target and move the the file
/// @return path where file can now be found
auto validatePath = [&localPath, this](QString fileName, QString targetFolder, QString realTarget) {
auto validatePath = [&localPath, this](const QString& fileName, const QString& targetFolder, const QString& realTarget) {
if (targetFolder != realTarget) {
qDebug() << "Target folder of" << fileName << "is incorrect, it belongs in" << realTarget;
auto destPath = FS::PathCombine(m_stagingPath, "minecraft", realTarget, fileName);
@ -664,7 +681,7 @@ void FlameCreationTask::validateOtherResources(QEventLoop& loop)
return localPath;
};
auto installWorld = [this](QString worldPath) {
auto installWorld = [this](const QString& worldPath) {
qDebug() << "Installing World from" << worldPath;
QFileInfo worldFileInfo(worldPath);
World w(worldFileInfo);
@ -714,7 +731,7 @@ void FlameCreationTask::validateOtherResources(QEventLoop& loop)
auto task = makeShared<ConcurrentTask>("CreateModMetadata", APPLICATION->settings()->get("NumberOfConcurrentTasks").toInt());
auto results = m_modIdResolver->getResults().files;
auto folder = FS::PathCombine(m_stagingPath, "minecraft", "mods", ".index");
for (auto file : results) {
for (const auto& file : results) {
if (file.targetFolder != "mods" || (file.version.fileName.endsWith(".zip") && !zipMods.contains(file.version.fileName))) {
continue;
}