From 8c6dd455f2c45ff29373a85cb2bd4fdcf927af38 Mon Sep 17 00:00:00 2001
From: Daniil Vinogradov <xitrix@bk.ru>
Date: Sun, 16 Feb 2025 15:07:14 +0100
Subject: [PATCH] Game update path fix

---
 .../Updates/GameUpdateManagerSheet.swift      | 36 ++++++++++---------
 src/Ryujinx.Headless.SDL2/Program.cs          |  2 +-
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/src/MeloNX/MeloNX/App/Views/Updates/GameUpdateManagerSheet.swift b/src/MeloNX/MeloNX/App/Views/Updates/GameUpdateManagerSheet.swift
index 90211058b..0d7e8fa68 100644
--- a/src/MeloNX/MeloNX/App/Views/Updates/GameUpdateManagerSheet.swift
+++ b/src/MeloNX/MeloNX/App/Views/Updates/GameUpdateManagerSheet.swift
@@ -26,7 +26,7 @@ struct UpdateManagerSheet: View {
                         Text(item.lastPathComponent)
                             .foregroundStyle(Color(uiColor: .label))
                         Spacer()
-                        if selectedItem == "\(game!.titleId)/\(item.lastPathComponent)" {
+                        if selectedItem == "updates/\(game!.titleId)/\(item.lastPathComponent)" {
                             Image(systemName: "checkmark.circle.fill")
                                 .foregroundStyle(Color.accentColor)
                                 .font(.system(size: 24))
@@ -86,7 +86,7 @@ struct UpdateManagerSheet: View {
                     let destinationURL = romUpdatedDirectory.appendingPathComponent(url.lastPathComponent)
                     try? fileManager.copyItem(at: url, to: destinationURL)
 
-                    items.append(gameInfo.titleId + "/" + url.lastPathComponent)
+                    items.append("updates/" + gameInfo.titleId + "/" + url.lastPathComponent)
                     selectItem(url.lastPathComponent)
                     Ryujinx.shared.games = Ryujinx.shared.loadGames()
                     loadJSON(jsonURL!)
@@ -100,7 +100,7 @@ struct UpdateManagerSheet: View {
     }
     
     func removeUpdate(_ game: URL) {
-        let gameString = "\(self.game!.titleId)/\(game.lastPathComponent)"
+        let gameString = "updates/\(self.game!.titleId)/\(game.lastPathComponent)"
         paths.removeAll { $0 == game }
         items.removeAll { $0 == gameString }
         
@@ -131,23 +131,25 @@ struct UpdateManagerSheet: View {
     
     func loadJSON(_ json: URL) {
         self.jsonURL = json
-        print("Failed to read JSO")
         
-        guard let jsonURL = jsonURL else { return }
-        print("Failed to read JSOK")
+        guard let jsonURL else { return }
         
         do {
             let data = try Data(contentsOf: jsonURL)
             if let jsonDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
                let list = jsonDict["paths"] as? [String]
             {
-                var urls: [URL] = []
-                
-                for path in list {
-                    urls.append(URL.documentsDirectory.appendingPathComponent("updates").appendingPathComponent(path))
+
+                let filteredList = list.filter { relativePath in
+                    let path = URL.documentsDirectory.appendingPathComponent(relativePath)
+                    return FileManager.default.fileExists(atPath: path.path)
                 }
-                
-                items = list
+
+                let urls: [URL] = filteredList.map { relativePath in
+                    URL.documentsDirectory.appendingPathComponent(relativePath)
+                }
+
+                items = filteredList
                 paths = urls
                 selectedItem = jsonDict["selected"] as? String
             }
@@ -171,9 +173,9 @@ struct UpdateManagerSheet: View {
     }
     
     func selectItem(_ item: String) {
-        let newSelection = "\(game!.titleId)/\(item)"
-        
-        guard let jsonURL = jsonURL else { return }
+        let newSelection = "updates/\(game!.titleId)/\(item)"
+
+        guard let jsonURL else { return }
         
         do {
             let data = try Data(contentsOf: jsonURL)
@@ -183,12 +185,12 @@ struct UpdateManagerSheet: View {
                 jsonDict["selected"] = ""
                 selectedItem = ""
             } else {
-                jsonDict["selected"] = newSelection
+                jsonDict["selected"] = "\(newSelection)"
                 selectedItem = newSelection
             }
             
             jsonDict["paths"] = items
-            
+
             let newData = try JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)
             try newData.write(to: jsonURL)
             Ryujinx.shared.games = Ryujinx.shared.loadGames()
diff --git a/src/Ryujinx.Headless.SDL2/Program.cs b/src/Ryujinx.Headless.SDL2/Program.cs
index ad78c40ea..5c6b84c11 100644
--- a/src/Ryujinx.Headless.SDL2/Program.cs
+++ b/src/Ryujinx.Headless.SDL2/Program.cs
@@ -752,7 +752,7 @@ namespace Ryujinx.Headless.SDL2
                     if (File.Exists(titleUpdateMetadataPath))
                     {
                         string updatePathRelative = JsonHelper.DeserializeFromFile(titleUpdateMetadataPath, _titleSerializerContext.TitleUpdateMetadata).Selected;
-                        updatePath = Path.Combine(AppDataManager.BaseDirPath, "updates", updatePathRelative);
+                        updatePath = Path.Combine(AppDataManager.BaseDirPath, updatePathRelative);
 
                         if (File.Exists(updatePath))
                         {