From b1ffc8ddab7d9aff10b6195a75e6e58c43b233ca Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Fri, 12 May 2023 16:37:45 -0700 Subject: [PATCH] refactor: normalize url fn & cleanup Signed-off-by: Rachel Powers <508861+Ryex@users.noreply.github.com> --- launcher/Application.cpp | 45 ++++++------------ launcher/Application.h | 2 +- launcher/InstanceImportTask.cpp | 2 +- launcher/ui/pages/modplatform/ImportPage.cpp | 50 ++++++++------------ 4 files changed, 36 insertions(+), 63 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 5effc01be..edaccadf6 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -235,12 +235,12 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) m_instanceIdToShowWindowOf = parser.value("show"); for (auto url : parser.values("import")){ - addImportUrl(url); + m_urlsToImport.append(normalizeImportUrl(url)); } // treat unspecified positional arguments as import urls for (auto url : parser.positionalArguments()) { - addImportUrl(url); + m_urlsToImport.append(normalizeImportUrl(url)); } // error if --launch is missing with --server or --profile @@ -345,12 +345,11 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) activate.command = "activate"; m_peerInstance->sendMessage(activate.serialize(), timeout); - if(!m_urlsToImport.isEmpty()) - { + if (!m_urlsToImport.isEmpty()) { for (auto url : m_urlsToImport) { ApplicationMessage import; import.command = "import"; - import.args.insert("path", url.toString()); + import.args.insert("url", url.toString()); m_peerInstance->sendMessage(import.serialize(), timeout); } } @@ -1071,27 +1070,16 @@ void Application::messageReceived(const QByteArray& message) auto & command = received.command; - if(command == "activate") - { + if (command == "activate") { showMainWindow(); - } - else if(command == "import") - { - QString path = received.args["path"]; - if(path.isEmpty()) - { + } else if (command == "import") { + QString url = received.args["url"]; + if (url.isEmpty()) { qWarning() << "Received" << command << "message without a zip path/URL."; return; } - auto local_file = QFileInfo(path); - if (local_file.exists()) { - m_mainWindow->processURLs({ QUrl::fromLocalFile(local_file.absoluteFilePath()) }); - } else { - m_mainWindow->processURLs({ QUrl::fromUserInput(path) }); - } - } - else if(command == "launch") - { + m_mainWindow->processURLs({ normalizeImportUrl(url) }); + } else if (command == "launch") { QString id = received.args["id"]; QString server = received.args["server"]; QString profile = received.args["profile"]; @@ -1131,9 +1119,7 @@ void Application::messageReceived(const QByteArray& message) serverObject, accountObject ); - } - else - { + } else { qWarning() << "Received invalid message" << message; } } @@ -1741,13 +1727,12 @@ void Application::triggerUpdateCheck() } } -void Application::addImportUrl(QString const& url) +QUrl Application::normalizeImportUrl(QString const& url) { auto local_file = QFileInfo(url); - if (local_file.exists()){ - m_urlsToImport.append(QUrl::fromLocalFile(local_file.absoluteFilePath())); + if (local_file.exists()) { + return QUrl::fromLocalFile(local_file.absoluteFilePath()); } else { - m_urlsToImport.append(QUrl::fromUserInput(url)); + return QUrl::fromUserInput(url); } } - diff --git a/launcher/Application.h b/launcher/Application.h index 83bfa9efd..97d33830c 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -211,7 +211,7 @@ public: int suitableMaxMem(); - void addImportUrl(QString const& url); + QUrl normalizeImportUrl(QString const& url); signals: void updateAllowedChanged(bool status); diff --git a/launcher/InstanceImportTask.cpp b/launcher/InstanceImportTask.cpp index 41b7898cb..badcf0a22 100644 --- a/launcher/InstanceImportTask.cpp +++ b/launcher/InstanceImportTask.cpp @@ -90,7 +90,7 @@ void InstanceImportTask::executeTask() setStatus(tr("Downloading modpack:\n%1").arg(m_sourceUrl.toString())); m_downloadRequired = true; - downloadFromUrl(); + downloadFromUrl(); } } diff --git a/launcher/ui/pages/modplatform/ImportPage.cpp b/launcher/ui/pages/modplatform/ImportPage.cpp index 78b7d1d56..315f6555b 100644 --- a/launcher/ui/pages/modplatform/ImportPage.cpp +++ b/launcher/ui/pages/modplatform/ImportPage.cpp @@ -102,16 +102,13 @@ void ImportPage::openedImpl() void ImportPage::updateState() { - if(!isOpened) - { + if (!isOpened) { return; } - if(ui->modpackEdit->hasAcceptableInput()) - { + if (ui->modpackEdit->hasAcceptableInput()) { QString input = ui->modpackEdit->text(); auto url = QUrl::fromUserInput(input); - if(url.isLocalFile()) - { + if (url.isLocalFile()) { // FIXME: actually do some validation of what's inside here... this is fake AF QFileInfo fi(input); @@ -120,15 +117,12 @@ void ImportPage::updateState() // mrpack is a modrinth pack bool isMRPack = fi.suffix() == "mrpack"; - if(fi.exists() && (isZip || isMRPack)) - { + if (fi.exists() && (isZip || isMRPack)) { QFileInfo fi(url.fileName()); - dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url,this)); + dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this)); dialog->setSuggestedIcon("default"); } - } - else if (url.scheme() == "curseforge") - { + } else if (url.scheme() == "curseforge") { // need to find the download link for the modpack // format of url curseforge://install?addonId=IDHERE&fileId=IDHERE QUrlQuery query(url); @@ -139,12 +133,9 @@ void ImportPage::updateState() req->addNetAction( Net::Download::makeByteArray(QUrl(QString("https://api.curseforge.com/v1/mods/%1/files/%2").arg(addonId, fileId)), array)); - connect(req.get(), &NetJob::finished, [array] { - delete array; - }); - connect(req.get(), &NetJob::failed, this, [this](QString reason){ - CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); - }); + connect(req.get(), &NetJob::finished, [array] { delete array; }); + connect(req.get(), &NetJob::failed, this, + [this](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); }); connect(req.get(), &NetJob::succeeded, this, [this, array, addonId, fileId] { qDebug() << "Returned CFURL Json:\n" << array->toStdString().c_str(); auto doc = Json::requireDocument(*array); @@ -156,12 +147,15 @@ void ImportPage::updateState() auto dl_url = QUrl( Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "downloadUrl", "", "downloadUrl")); if (!dl_url.isValid()) { - CustomMessageBox::selectable(this, tr("Error"), tr("The modpack is blocked ! Please download it manually"), QMessageBox::Critical)->show(); + CustomMessageBox::selectable(this, tr("Error"), tr("The modpack is blocked ! Please download it manually"), + QMessageBox::Critical) + ->show(); return; } QFileInfo dl_file(dl_url.fileName()); - QString pack_name = Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "displayName", dl_file.completeBaseName(), "displayName"); + QString pack_name = Json::ensureString(Json::ensureObject(Json::ensureObject(doc.object()), "data"), "displayName", + dl_file.completeBaseName(), "displayName"); QMap extra_info; extra_info.insert("pack_id", addonId); @@ -169,7 +163,7 @@ void ImportPage::updateState() dialog->setSuggestedPack(pack_name, new InstanceImportTask(dl_url, this, std::move(extra_info))); dialog->setSuggestedIcon("default"); - + } else { CustomMessageBox::selectable(this, tr("Error"), tr("This url isn't a valid modpack !"), QMessageBox::Critical)->show(); } @@ -178,24 +172,18 @@ void ImportPage::updateState() dlUrlDialod.setSkipButton(true, tr("Abort")); dlUrlDialod.execWithTask(req.get()); return; - } - else - { - - - if(input.endsWith("?client=y")) { + } else { + if (input.endsWith("?client=y")) { input.chop(9); input.append("/file"); url = QUrl::fromUserInput(input); } // hook, line and sinker. QFileInfo fi(url.fileName()); - dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url,this)); + dialog->setSuggestedPack(fi.completeBaseName(), new InstanceImportTask(url, this)); dialog->setSuggestedIcon("default"); } - } - else - { + } else { dialog->setSuggestedPack(); } }