From 199a7df807994ded1469cc893e6c68c21307444f Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Wed, 25 Jan 2023 10:43:23 +0100 Subject: [PATCH] refactor: add error handling to component import Signed-off-by: Sefa Eyeoglu --- launcher/minecraft/PackProfile.cpp | 56 +++++++++++----------- launcher/minecraft/PackProfile.h | 3 +- launcher/ui/pages/instance/VersionPage.cpp | 8 +++- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/launcher/minecraft/PackProfile.cpp b/launcher/minecraft/PackProfile.cpp index 270f3d22..54fbf7f3 100644 --- a/launcher/minecraft/PackProfile.cpp +++ b/launcher/minecraft/PackProfile.cpp @@ -733,21 +733,47 @@ void PackProfile::invalidateLaunchProfile() void PackProfile::installJarMods(QStringList selectedFiles) { + // FIXME: get rid of _internal installJarMods_internal(selectedFiles); } void PackProfile::installCustomJar(QString selectedFile) { + // FIXME: get rid of _internal installCustomJar_internal(selectedFile); } -void PackProfile::installComponents(QStringList selectedFiles) +bool PackProfile::installComponents(QStringList selectedFiles) { - installComponents_internal(selectedFiles); + const QString patchDir = FS::PathCombine(d->m_instance->instanceRoot(), "patches"); + if (!FS::ensureFolderPathExists(patchDir)) + return false; + + bool result = true; + for (const QString& source : selectedFiles) { + const QFileInfo sourceInfo(source); + + auto versionFile = ProfileUtils::parseJsonFile(sourceInfo, false); + const QString target = FS::PathCombine(patchDir, versionFile->uid + ".json"); + + if (!QFile::copy(source, target)) { + qWarning() << "Component" << source << "could not be copied to target" << target; + result = false; + continue; + } + + appendComponent(new Component(this, versionFile->uid, versionFile)); + } + + scheduleSave(); + invalidateLaunchProfile(); + + return result; } void PackProfile::installAgents(QStringList selectedFiles) { + // FIXME: get rid of _internal installAgents_internal(selectedFiles); } @@ -948,32 +974,6 @@ bool PackProfile::installCustomJar_internal(QString filepath) return true; } -bool PackProfile::installComponents_internal(QStringList filepaths) -{ - const QString patchDir = FS::PathCombine(d->m_instance->instanceRoot(), "patches"); - if (!FS::ensureFolderPathExists(patchDir)) - return false; - - for (const QString& source : filepaths) { - const QFileInfo sourceInfo(source); - - auto versionFile = ProfileUtils::parseJsonFile(sourceInfo, false); - const QString target = FS::PathCombine(patchDir, versionFile->uid + ".json"); - - if (!QFile::copy(source, target)) - { - return false; - } - - appendComponent(new Component(this, versionFile->uid, versionFile)); - } - - scheduleSave(); - invalidateLaunchProfile(); - - return true; -} - bool PackProfile::installAgents_internal(QStringList filepaths) { // FIXME code duplication diff --git a/launcher/minecraft/PackProfile.h b/launcher/minecraft/PackProfile.h index 8b885aa8..e5b398db 100644 --- a/launcher/minecraft/PackProfile.h +++ b/launcher/minecraft/PackProfile.h @@ -90,7 +90,7 @@ public: void installCustomJar(QString selectedFile); /// install MMC/Prism component files - void installComponents(QStringList selectedFiles); + bool installComponents(QStringList selectedFiles); /// install Java agent files void installAgents(QStringList selectedFiles); @@ -177,7 +177,6 @@ private: bool load(); bool installJarMods_internal(QStringList filepaths); bool installCustomJar_internal(QString filepath); - bool installComponents_internal(QStringList filepaths); bool installAgents_internal(QStringList filepaths); bool removeComponent_internal(ComponentPtr patch); diff --git a/launcher/ui/pages/instance/VersionPage.cpp b/launcher/ui/pages/instance/VersionPage.cpp index bce50a09..f92a7660 100644 --- a/launcher/ui/pages/instance/VersionPage.cpp +++ b/launcher/ui/pages/instance/VersionPage.cpp @@ -384,8 +384,12 @@ void VersionPage::on_actionImport_Components_triggered() QStringList list = GuiUtil::BrowseForFiles("component", tr("Select components"), tr("Components (*.json)"), APPLICATION->settings()->get("CentralModsDir").toString(), this->parentWidget()); - if (!list.isEmpty()) - m_profile->installComponents(list); + if (!list.isEmpty()) { + if (!m_profile->installComponents(list)) { + QMessageBox::warning(this, tr("Failed to import components"), + tr("Some components could not be imported. Check logs for details")); + } + } updateButtons(); }