From d6b9babe1d73a78e963455a5a6ea3e7431a44ece Mon Sep 17 00:00:00 2001
From: Thog <me@thog.eu>
Date: Tue, 21 Jan 2020 23:23:11 +0100
Subject: Keep the GUI alive when closing a game (#888)

* Keep the GUI alive when closing a game

Make HLE.Switch init when starting a game and dispose it when closing
the GlScreen.

This also make HLE in charge of disposing the audio and gpu backend.

* Address Ac_k's comments

* Make sure to dispose the Discord module and use GTK quit method

Also update Discord Precense when closing a game.

* Make sure to dispose MainWindow

* Address gdk's comments
---
 Ryujinx.HLE/FileSystem/VirtualFileSystem.cs | 79 ++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 1 deletion(-)

(limited to 'Ryujinx.HLE/FileSystem/VirtualFileSystem.cs')

diff --git a/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs b/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
index 257a55a2..070ec3bc 100644
--- a/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
+++ b/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
@@ -1,3 +1,7 @@
+using LibHac;
+using LibHac.Fs;
+using LibHac.FsService;
+using LibHac.FsSystem;
 using Ryujinx.HLE.FileSystem.Content;
 using Ryujinx.HLE.HOS;
 using System;
@@ -16,6 +20,16 @@ namespace Ryujinx.HLE.FileSystem
         public static string SystemNandPath = Path.Combine(NandPath, "system");
         public static string UserNandPath   = Path.Combine(NandPath, "user");
 
+        public Keyset           KeySet   { get; private set; }
+        public FileSystemServer FsServer { get; private set; }
+        public FileSystemClient FsClient { get; private set; }
+        public EmulatedGameCard GameCard { get; private set; }
+
+        public VirtualFileSystem()
+        {
+            Reload();
+        }
+
         public Stream RomFs { get; private set; }
 
         public void LoadRomFs(string fileName)
@@ -183,6 +197,69 @@ namespace Ryujinx.HLE.FileSystem
             return Path.Combine(appDataPath, BasePath);
         }
 
+        public void Reload()
+        {
+            ReloadKeySet();
+
+            LocalFileSystem serverBaseFs = new LocalFileSystem(GetBasePath());
+
+            DefaultFsServerObjects fsServerObjects = DefaultFsServerObjects.GetDefaultEmulatedCreators(serverBaseFs, KeySet);
+
+            GameCard = fsServerObjects.GameCard;
+
+            FileSystemServerConfig fsServerConfig = new FileSystemServerConfig
+            {
+                FsCreators     = fsServerObjects.FsCreators,
+                DeviceOperator = fsServerObjects.DeviceOperator,
+                ExternalKeySet = KeySet.ExternalKeySet
+            };
+
+            FsServer = new FileSystemServer(fsServerConfig);
+            FsClient = FsServer.CreateFileSystemClient();
+        }
+
+
+        private void ReloadKeySet()
+        {
+            string keyFile        = null;
+            string titleKeyFile   = null;
+            string consoleKeyFile = null;
+
+            string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
+
+            LoadSetAtPath(Path.Combine(home, ".switch"));
+            LoadSetAtPath(GetSystemPath());
+
+            void LoadSetAtPath(string basePath)
+            {
+                string localKeyFile        = Path.Combine(basePath, "prod.keys");
+                string localTitleKeyFile   = Path.Combine(basePath, "title.keys");
+                string localConsoleKeyFile = Path.Combine(basePath, "console.keys");
+
+                if (File.Exists(localKeyFile))
+                {
+                    keyFile = localKeyFile;
+                }
+
+                if (File.Exists(localTitleKeyFile))
+                {
+                    titleKeyFile = localTitleKeyFile;
+                }
+
+                if (File.Exists(localConsoleKeyFile))
+                {
+                    consoleKeyFile = localConsoleKeyFile;
+                }
+            }
+
+            KeySet = ExternalKeyReader.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile);
+        }
+
+        public void Unload()
+        {
+            RomFs?.Dispose();
+        }
+
         public void Dispose()
         {
             Dispose(true);
@@ -192,7 +269,7 @@ namespace Ryujinx.HLE.FileSystem
         {
             if (disposing)
             {
-                RomFs?.Dispose();
+                Unload();
             }
         }
     }
-- 
cgit v1.2.3-70-g09d2