From bb4a28b5254a964a99af14d6a8bd473ccbbf5778 Mon Sep 17 00:00:00 2001
From: Isaac Marovitz <42140194+IsaacMarovitz@users.noreply.github.com>
Date: Mon, 29 Jan 2024 22:14:19 +0000
Subject: Ava UI: Mod Manager Fixes (Again) (#6187)

* Fix typo + Fix deleting from old dir

* Avoid double enumeration

* Break when parentDir is found

* Fix deleting non subdirectory mods

* Typo
---
 src/Ryujinx.Ava/UI/Models/ModModel.cs              |  4 +-
 .../UI/ViewModels/ModManagerViewModel.cs           | 54 ++++++++++++++--------
 2 files changed, 37 insertions(+), 21 deletions(-)

(limited to 'src')

diff --git a/src/Ryujinx.Ava/UI/Models/ModModel.cs b/src/Ryujinx.Ava/UI/Models/ModModel.cs
index f68e1593..ee28ca5f 100644
--- a/src/Ryujinx.Ava/UI/Models/ModModel.cs
+++ b/src/Ryujinx.Ava/UI/Models/ModModel.cs
@@ -17,14 +17,16 @@ namespace Ryujinx.Ava.UI.Models
             }
         }
 
+        public bool InSd { get; }
         public string Path { get; }
         public string Name { get; }
 
-        public ModModel(string path, string name, bool enabled)
+        public ModModel(string path, string name, bool enabled, bool inSd)
         {
             Path = path;
             Name = name;
             Enabled = enabled;
+            InSd = inSd;
         }
     }
 }
diff --git a/src/Ryujinx.Ava/UI/ViewModels/ModManagerViewModel.cs b/src/Ryujinx.Ava/UI/ViewModels/ModManagerViewModel.cs
index 2c29660b..8321bf89 100644
--- a/src/Ryujinx.Ava/UI/ViewModels/ModManagerViewModel.cs
+++ b/src/Ryujinx.Ava/UI/ViewModels/ModManagerViewModel.cs
@@ -102,13 +102,14 @@ namespace Ryujinx.Ava.UI.ViewModels
 
             foreach (var path in modsBasePaths)
             {
+                var inSd = path == ModLoader.GetSdModsBasePath();
                 var modCache = new ModLoader.ModCache();
 
                 ModLoader.QueryContentsDir(modCache, new DirectoryInfo(Path.Combine(path, "contents")), applicationId);
 
                 foreach (var mod in modCache.RomfsDirs)
                 {
-                    var modModel = new ModModel(mod.Path.Parent.FullName, mod.Name, mod.Enabled);
+                    var modModel = new ModModel(mod.Path.Parent.FullName, mod.Name, mod.Enabled, inSd);
                     if (Mods.All(x => x.Path != mod.Path.Parent.FullName))
                     {
                         Mods.Add(modModel);
@@ -117,12 +118,12 @@ namespace Ryujinx.Ava.UI.ViewModels
 
                 foreach (var mod in modCache.RomfsContainers)
                 {
-                    Mods.Add(new ModModel(mod.Path.FullName, mod.Name, mod.Enabled));
+                    Mods.Add(new ModModel(mod.Path.FullName, mod.Name, mod.Enabled, inSd));
                 }
 
                 foreach (var mod in modCache.ExefsDirs)
                 {
-                    var modModel = new ModModel(mod.Path.Parent.FullName, mod.Name, mod.Enabled);
+                    var modModel = new ModModel(mod.Path.Parent.FullName, mod.Name, mod.Enabled, inSd);
                     if (Mods.All(x => x.Path != mod.Path.Parent.FullName))
                     {
                         Mods.Add(modModel);
@@ -131,7 +132,7 @@ namespace Ryujinx.Ava.UI.ViewModels
 
                 foreach (var mod in modCache.ExefsContainers)
                 {
-                    Mods.Add(new ModModel(mod.Path.FullName, mod.Name, mod.Enabled));
+                    Mods.Add(new ModModel(mod.Path.FullName, mod.Name, mod.Enabled, inSd));
                 }
             }
 
@@ -183,30 +184,43 @@ namespace Ryujinx.Ava.UI.ViewModels
 
         public void Delete(ModModel model)
         {
-            var modsDir = ModLoader.GetApplicationDir(ModLoader.GetSdModsBasePath(), _applicationId.ToString("x16"));
-            var parentDir = String.Empty;
+            var isSubdir = true;
+            var pathToDelete = model.Path;
+            var basePath = model.InSd ? ModLoader.GetSdModsBasePath() : ModLoader.GetModsBasePath();
+            var modsDir = ModLoader.GetApplicationDir(basePath, _applicationId.ToString("x16"));
 
-            foreach (var dir in Directory.GetDirectories(modsDir, "*", SearchOption.TopDirectoryOnly))
+            if (new DirectoryInfo(model.Path).Parent?.FullName == modsDir)
             {
-                if (Directory.GetDirectories(dir, "*", SearchOption.AllDirectories).Contains(model.Path))
-                {
-                    parentDir = dir;
-                }
+                isSubdir = false;
             }
 
-            if (parentDir == String.Empty)
+            if (isSubdir)
             {
-                Dispatcher.UIThread.Post(async () =>
+                var parentDir = String.Empty;
+
+                foreach (var dir in Directory.GetDirectories(modsDir, "*", SearchOption.TopDirectoryOnly))
                 {
-                    await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(
-                        LocaleKeys.DialogModDeleteNoParentMessage,
-                        parentDir));
-                });
-                return;
+                    if (Directory.GetDirectories(dir, "*", SearchOption.AllDirectories).Contains(model.Path))
+                    {
+                        parentDir = dir;
+                        break;
+                    }
+                }
+
+                if (parentDir == String.Empty)
+                {
+                    Dispatcher.UIThread.Post(async () =>
+                    {
+                        await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(
+                            LocaleKeys.DialogModDeleteNoParentMessage,
+                            model.Path));
+                    });
+                    return;
+                }
             }
 
-            Logger.Info?.Print(LogClass.Application, $"Deleting mod at \"{model.Path}\"");
-            Directory.Delete(parentDir, true);
+            Logger.Info?.Print(LogClass.Application, $"Deleting mod at \"{pathToDelete}\"");
+            Directory.Delete(pathToDelete, true);
 
             Mods.Remove(model);
             OnPropertyChanged(nameof(ModCount));
-- 
cgit v1.2.3-70-g09d2