From edf157200d2cc75e18a8e8486c6d944c06d4b1ae Mon Sep 17 00:00:00 2001
From: Steveice10 <1269164+Steveice10@users.noreply.github.com>
Date: Thu, 24 Aug 2023 03:57:11 -0700
Subject: [PATCH] qt: Support updating an installed app image. (#6915)

---
 src/citra_qt/updater/updater.cpp | 23 ++++++++++++++++++-----
 src/citra_qt/updater/updater_p.h |  1 +
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/citra_qt/updater/updater.cpp b/src/citra_qt/updater/updater.cpp
index d88e2cb0f..47828ef2d 100644
--- a/src/citra_qt/updater/updater.cpp
+++ b/src/citra_qt/updater/updater.cpp
@@ -14,6 +14,7 @@
 #include "citra_qt/uisettings.h"
 #include "citra_qt/updater/updater.h"
 #include "citra_qt/updater/updater_p.h"
+#include "common/file_util.h"
 #include "common/logging/log.h"
 
 #ifdef Q_OS_OSX
@@ -110,9 +111,22 @@ QString UpdaterPrivate::ToSystemExe(QString base_path) {
 #endif
 }
 
+QFileInfo UpdaterPrivate::GetMaintenanceTool() const {
+#if defined(Q_OS_UNIX) && !defined(Q_OS_OSX)
+    const auto appimage_path = QProcessEnvironment::systemEnvironment()
+                                   .value(QStringLiteral("APPIMAGE"), {})
+                                   .toStdString();
+    if (!appimage_path.empty()) {
+        const auto appimage_dir = FileUtil::GetParentPath(appimage_path);
+        LOG_DEBUG(Frontend, "Detected app image directory: {}", appimage_dir);
+        return QFileInfo(QString::fromStdString(std::string(appimage_dir)), tool_path);
+    }
+#endif
+    return QFileInfo(QCoreApplication::applicationDirPath(), tool_path);
+}
+
 bool UpdaterPrivate::HasUpdater() const {
-    QFileInfo tool_info(QCoreApplication::applicationDirPath(), tool_path);
-    return tool_info.exists();
+    return GetMaintenanceTool().exists();
 }
 
 bool UpdaterPrivate::StartUpdateCheck() {
@@ -125,9 +139,8 @@ bool UpdaterPrivate::StartUpdateCheck() {
     last_error_code = EXIT_SUCCESS;
     last_error_log.clear();
 
-    QFileInfo tool_info(QCoreApplication::applicationDirPath(), tool_path);
     main_process = new QProcess(this);
-    main_process->setProgram(tool_info.absoluteFilePath());
+    main_process->setProgram(GetMaintenanceTool().absoluteFilePath());
     main_process->setArguments({QStringLiteral("--checkupdates"), QStringLiteral("-v")});
 
     connect(main_process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this,
@@ -271,7 +284,7 @@ void UpdaterPrivate::LaunchWithArguments(const QStringList& args) {
         return;
     }
 
-    QFileInfo tool_info(QCoreApplication::applicationDirPath(), tool_path);
+    QFileInfo tool_info = GetMaintenanceTool();
 
     if (!QProcess::startDetached(tool_info.absoluteFilePath(), args, tool_info.absolutePath())) {
         LOG_WARNING(Frontend, "Unable to start program {}",
diff --git a/src/citra_qt/updater/updater_p.h b/src/citra_qt/updater/updater_p.h
index 09fbb4a4b..549c98e88 100644
--- a/src/citra_qt/updater/updater_p.h
+++ b/src/citra_qt/updater/updater_p.h
@@ -26,6 +26,7 @@ public:
 
     static QString ToSystemExe(QString base_path);
 
+    QFileInfo GetMaintenanceTool() const;
     bool HasUpdater() const;
 
     bool StartUpdateCheck();