From a3dd04deef7d2555726378293df891f3877f4489 Mon Sep 17 00:00:00 2001
From: JavidPack <javidpack@gmail.com>
Date: Wed, 2 Mar 2022 01:51:37 -0700
Subject: [PATCH] Implement -p or --profile command line argument (#2947)

* Implement -p or --profile command line argument

* Put command line logic all in Program and reference it elsewhere

* Address feedback
---
 .../HOS/Services/Account/Acc/AccountManager.cs      | 12 ++++++++++--
 Ryujinx/Program.cs                                  | 13 +++++++++++++
 Ryujinx/Ui/MainWindow.cs                            |  2 +-
 3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/Ryujinx.HLE/HOS/Services/Account/Acc/AccountManager.cs b/Ryujinx.HLE/HOS/Services/Account/Acc/AccountManager.cs
index e28fe1061..9d9cb6ff8 100644
--- a/Ryujinx.HLE/HOS/Services/Account/Acc/AccountManager.cs
+++ b/Ryujinx.HLE/HOS/Services/Account/Acc/AccountManager.cs
@@ -3,6 +3,7 @@ using LibHac.Common;
 using LibHac.Fs;
 using LibHac.Fs.Shim;
 using Ryujinx.Common;
+using Ryujinx.Common.Logging;
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
@@ -25,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
 
         public UserProfile LastOpenedUser { get; private set; }
 
-        public AccountManager(HorizonClient horizonClient)
+        public AccountManager(HorizonClient horizonClient, string initialProfileName = null)
         {
             _horizonClient = horizonClient;
 
@@ -43,7 +44,14 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
             }
             else
             {
-                OpenUser(_accountSaveDataManager.LastOpened);
+                UserId commandLineUserProfileOverride = default; 
+                if (!string.IsNullOrEmpty(initialProfileName))
+                { 
+                    commandLineUserProfileOverride = _profiles.Values.FirstOrDefault(x => x.Name == initialProfileName)?.UserId ?? default;
+                    if (commandLineUserProfileOverride.IsNull)
+                        Logger.Warning?.Print(LogClass.Application, $"The command line specified profile named '{initialProfileName}' was not found");
+                }
+                OpenUser(commandLineUserProfileOverride.IsNull ? _accountSaveDataManager.LastOpened : commandLineUserProfileOverride);
             }
         }
 
diff --git a/Ryujinx/Program.cs b/Ryujinx/Program.cs
index 8cd5a9969..26b89d06f 100644
--- a/Ryujinx/Program.cs
+++ b/Ryujinx/Program.cs
@@ -27,6 +27,8 @@ namespace Ryujinx
 
         public static string ConfigurationPath { get; set; }
 
+        public static string CommandLineProfile { get; set; }
+
         [DllImport("libX11")]
         private extern static int XInitThreads();
 
@@ -52,6 +54,17 @@ namespace Ryujinx
 
                     baseDirPathArg = args[++i];
                 }
+                else if (arg == "-p" || arg == "--profile")
+                {
+                    if (i + 1 >= args.Length)
+                    {
+                        Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");
+
+                        continue;
+                    }
+
+                    CommandLineProfile = args[++i];
+                }
                 else if (arg == "-f" || arg == "--fullscreen")
                 {
                     startFullscreenArg = true;
diff --git a/Ryujinx/Ui/MainWindow.cs b/Ryujinx/Ui/MainWindow.cs
index 926893071..0c94fc673 100644
--- a/Ryujinx/Ui/MainWindow.cs
+++ b/Ryujinx/Ui/MainWindow.cs
@@ -178,7 +178,7 @@ namespace Ryujinx.Ui
             VirtualFileSystem.FixExtraData(_libHacHorizonManager.RyujinxClient);
 
             _contentManager         = new ContentManager(_virtualFileSystem);
-            _accountManager         = new AccountManager(_libHacHorizonManager.RyujinxClient);
+            _accountManager         = new AccountManager(_libHacHorizonManager.RyujinxClient, Program.CommandLineProfile);
             _userChannelPersistence = new UserChannelPersistence();
 
             // Instantiate GUI objects.