From f449895e6d8af90f727de6590fd6120038c73986 Mon Sep 17 00:00:00 2001
From: Ac_K <Acoustik666@gmail.com>
Date: Wed, 18 Jan 2023 14:50:42 +0100
Subject: HOS: Load RomFs by pid (#4301)

We currently loading only one RomFs at a time, which could be wrong if one day we want to load more than one guest at time.
This PR fixes that by loading romfs by pid.
---
 Ryujinx.HLE/FileSystem/VirtualFileSystem.cs | 38 +++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 7 deletions(-)

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

diff --git a/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs b/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
index c958c6e8..95a2fcda 100644
--- a/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
+++ b/Ryujinx.HLE/FileSystem/VirtualFileSystem.cs
@@ -16,6 +16,7 @@ using Ryujinx.Common.Logging;
 using Ryujinx.HLE.HOS;
 using System;
 using System.Buffers.Text;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.CompilerServices;
@@ -35,7 +36,8 @@ namespace Ryujinx.HLE.FileSystem
         public EmulatedGameCard GameCard  { get; private set; }
         public EmulatedSdCard   SdCard    { get; private set; }
         public ModLoader        ModLoader { get; private set; }
-        public Stream           RomFs     { get; private set; }
+
+        private readonly ConcurrentDictionary<ulong, Stream> _romFsByPid;
 
         private static bool _isInitialized = false;
 
@@ -55,17 +57,34 @@ namespace Ryujinx.HLE.FileSystem
         {
             ReloadKeySet();
             ModLoader = new ModLoader(); // Should only be created once
+            _romFsByPid = new ConcurrentDictionary<ulong, Stream>();
+        }
+
+        public void LoadRomFs(ulong pid, string fileName)
+        {
+            var romfsStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
+
+            _romFsByPid.AddOrUpdate(pid, romfsStream, (pid, oldStream) =>
+            {
+                oldStream.Close();
+
+                return romfsStream;
+            });
         }
 
-        public void LoadRomFs(string fileName)
+        public void SetRomFs(ulong pid, Stream romfsStream)
         {
-            RomFs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
+            _romFsByPid.AddOrUpdate(pid, romfsStream, (pid, oldStream) =>
+            {
+                oldStream.Close();
+
+                return romfsStream;
+            });
         }
 
-        public void SetRomFs(Stream romfsStream)
+        public Stream GetRomFs(ulong pid)
         {
-            RomFs?.Close();
-            RomFs = romfsStream;
+            return _romFsByPid[pid];
         }
 
         public string GetFullPath(string basePath, string fileName)
@@ -583,7 +602,12 @@ namespace Ryujinx.HLE.FileSystem
         {
             if (disposing)
             {
-                RomFs?.Dispose();
+                foreach (var stream in _romFsByPid.Values)
+                {
+                    stream.Close();
+                }
+
+                _romFsByPid.Clear();
             }
         }
     }
-- 
cgit v1.2.3-70-g09d2