From 0de53673697ea773fbf4b498f3a5b7758d53a6fd Mon Sep 17 00:00:00 2001
From: mageven <62494521+mageven@users.noreply.github.com>
Date: Thu, 13 Aug 2020 23:01:13 +0530
Subject: [PATCH] PPTC GUI: Purge all versioned caches on click (#1454)

* PPTC GUI: Purge all versioned caches on click

* Address AcK's comments

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* nit: Explicit declarations

Co-authored-by: Ac_K <Acoustik666@gmail.com>
---
 Ryujinx/Ui/GameTableContextMenu.cs | 36 +++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/Ryujinx/Ui/GameTableContextMenu.cs b/Ryujinx/Ui/GameTableContextMenu.cs
index 1f8d9ba83c..afa2182d07 100644
--- a/Ryujinx/Ui/GameTableContextMenu.cs
+++ b/Ryujinx/Ui/GameTableContextMenu.cs
@@ -14,6 +14,7 @@ using Ryujinx.Common.Utilities;
 using Ryujinx.HLE.FileSystem;
 using System;
 using System.Buffers;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Globalization;
 using System.IO;
@@ -664,30 +665,39 @@ namespace Ryujinx.Ui
         
         private void PurgePtcCache_Clicked(object sender, EventArgs args)
         {
-            string titleId       = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n")[1].ToLower();
-            string cacheFileName = _gameTableStore.GetValue(_rowIter, 4) + ".cache";
+            string[] tableEntry = _gameTableStore.GetValue(_rowIter, 2).ToString().Split("\n");
+            string titleId = tableEntry[1].ToLower();
             
-            string mainPath   = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", titleId, "cache", "cpu", "0", cacheFileName);
-            string backupPath = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", titleId, "cache", "cpu", "1", cacheFileName);
+            DirectoryInfo mainDir   = new DirectoryInfo(System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", titleId, "cache", "cpu", "0"));
+            DirectoryInfo backupDir = new DirectoryInfo(System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "games", titleId, "cache", "cpu", "1"));
             
             MessageDialog warningDialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Warning, ButtonsType.YesNo, null)
             {
                 Title          = "Ryujinx - Warning",
-                Text           = "You are about to delete the PPTC cache. Are you sure you want to proceed?",
+                Text           = $"You are about to delete the PPTC cache for '{tableEntry[0]}'. Are you sure you want to proceed?",
                 WindowPosition = WindowPosition.Center
             };
-            
-            if (warningDialog.Run() == (int)ResponseType.Yes)
+
+            List<FileInfo> cacheFiles = new List<FileInfo>();
+
+            if (mainDir.Exists)   { cacheFiles.AddRange(mainDir.EnumerateFiles("*.cache")); }
+            if (backupDir.Exists) { cacheFiles.AddRange(backupDir.EnumerateFiles("*.cache")); }
+
+            if (cacheFiles.Count > 0 && warningDialog.Run() == (int)ResponseType.Yes)
             {
-                if (File.Exists(mainPath))
+                foreach (FileInfo file in cacheFiles)
                 {
-                    File.Delete(mainPath);
-                }
-                if (File.Exists(backupPath))
-                {
-                    File.Delete(backupPath);
+                    try 
+                    { 
+                        file.Delete(); 
+                    }
+                    catch(Exception e)
+                    {
+                        Logger.Error?.Print(LogClass.Application, $"Error purging PPTC cache {file.Name}: {e}");
+                    }
                 }
             }
+
             warningDialog.Dispose();
         }
     }