From 49c63ea07779eb27674ae8c4a14e1dcf4b794a95 Mon Sep 17 00:00:00 2001
From: John <loneboco@gmail.com>
Date: Thu, 11 May 2023 17:14:29 -0700
Subject: [PATCH] Fix the restart after an update. (#4869)

* Fix the restart after an update.

* Fix the updater for the Ava UI too.

* Fixing up the code after some change requests.
Removed a line of code that was accidentally left in.

* Fix restarting on Linux Avalonia.

* Fix issues with escaped arguments.
---
 src/Ryujinx.Ava/Modules/Updater/Updater.cs  | 42 ++++++++++++++++-----
 src/Ryujinx/Modules/Updater/UpdateDialog.cs | 15 +++++++-
 2 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/src/Ryujinx.Ava/Modules/Updater/Updater.cs b/src/Ryujinx.Ava/Modules/Updater/Updater.cs
index 0542993513..77d77d7945 100644
--- a/src/Ryujinx.Ava/Modules/Updater/Updater.cs
+++ b/src/Ryujinx.Ava/Modules/Updater/Updater.cs
@@ -295,14 +295,7 @@ namespace Ryujinx.Modules
                 if (shouldRestart)
                 {
                     List<string> arguments = CommandLineState.Arguments.ToList();
-                    string ryuName = Path.GetFileName(Environment.ProcessPath);
                     string executableDirectory = AppDomain.CurrentDomain.BaseDirectory;
-                    string executablePath = Path.Combine(executableDirectory, ryuName);
-
-                    if (!Path.Exists(executablePath))
-                    {
-                        executablePath = Path.Combine(executableDirectory, OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx");
-                    }
 
                     // On macOS we perform the update at relaunch.
                     if (OperatingSystem.IsMacOS())
@@ -310,13 +303,42 @@ namespace Ryujinx.Modules
                         string baseBundlePath = Path.GetFullPath(Path.Combine(executableDirectory, "..", ".."));
                         string newBundlePath = Path.Combine(UpdateDir, "Ryujinx.app");
                         string updaterScriptPath = Path.Combine(newBundlePath, "Contents", "Resources", "updater.sh");
-                        string currentPid = Process.GetCurrentProcess().Id.ToString();
+                        string currentPid = Environment.ProcessId.ToString();
 
-                        executablePath = "/bin/bash";
                         arguments.InsertRange(0, new List<string> { updaterScriptPath, baseBundlePath, newBundlePath, currentPid });
+                        Process.Start("/bin/bash", arguments);
+                    }
+                    else
+                    {
+                        // Find the process name.
+                        string ryuName = Path.GetFileName(Environment.ProcessPath);
+
+                        // Some operating systems can see the renamed executable, so strip off the .ryuold if found.
+                        if (ryuName.EndsWith(".ryuold"))
+                        {
+                            ryuName = ryuName[..^7];
+                        }
+
+                        // Fallback if the executable could not be found.
+                        if (!Path.Exists(Path.Combine(executableDirectory, ryuName)))
+                        {
+                            ryuName = OperatingSystem.IsWindows() ? "Ryujinx.Ava.exe" : "Ryujinx.Ava";
+                        }
+
+                        ProcessStartInfo processStart = new(ryuName)
+                        {
+                            UseShellExecute = true,
+                            WorkingDirectory = executableDirectory
+                        };
+
+                        foreach (string argument in CommandLineState.Arguments)
+                        {
+                            processStart.ArgumentList.Add(argument);
+                        }
+
+                        Process.Start(processStart);
                     }
 
-                    Process.Start(executablePath, arguments);
                     Environment.Exit(0);
                 }
             }
diff --git a/src/Ryujinx/Modules/Updater/UpdateDialog.cs b/src/Ryujinx/Modules/Updater/UpdateDialog.cs
index 4957b681b5..e0a257fd66 100644
--- a/src/Ryujinx/Modules/Updater/UpdateDialog.cs
+++ b/src/Ryujinx/Modules/Updater/UpdateDialog.cs
@@ -1,5 +1,6 @@
 using Gdk;
 using Gtk;
+using Ryujinx.Common;
 using Ryujinx.Ui;
 using Ryujinx.Ui.Common.Configuration;
 using Ryujinx.Ui.Common.Helper;
@@ -47,9 +48,19 @@ namespace Ryujinx.Modules
             if (_restartQuery)
             {
                 string ryuName = OperatingSystem.IsWindows() ? "Ryujinx.exe" : "Ryujinx";
-                string ryuExe  = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ryuName);
 
-                Process.Start(ryuExe, CommandLineState.Arguments);
+                ProcessStartInfo processStart = new(ryuName)
+                {
+                    UseShellExecute = true,
+                    WorkingDirectory = ReleaseInformation.GetBaseApplicationDirectory()
+                };
+
+                foreach (string argument in CommandLineState.Arguments)
+                {
+                    processStart.ArgumentList.Add(argument);
+                }
+
+                Process.Start(processStart);
 
                 Environment.Exit(0);
             }