From 6de83dea9c8373654a9a1d7370fc3c02191bbb0b Mon Sep 17 00:00:00 2001 From: EliseZeroTwo Date: Wed, 25 Mar 2020 13:01:42 +0100 Subject: [PATCH] Actually handle not having privileges for a directory --- Ryujinx/Ui/ApplicationLibrary.cs | 84 ++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 21 deletions(-) diff --git a/Ryujinx/Ui/ApplicationLibrary.cs b/Ryujinx/Ui/ApplicationLibrary.cs index f7d226686d..5eb03ac03a 100644 --- a/Ryujinx/Ui/ApplicationLibrary.cs +++ b/Ryujinx/Ui/ApplicationLibrary.cs @@ -39,6 +39,47 @@ namespace Ryujinx.Ui private static Language _desiredTitleLanguage; private static bool _loadingError; + public static IEnumerable GetFilesInDirectory(string directory) + { + Stack stack = new Stack(); + stack.Push(directory); + while (stack.Count > 0) + { + string dir = stack.Pop(); + string[] content = { }; + + try + { + content = Directory.GetFiles(dir, "*"); + } + catch (UnauthorizedAccessException) + { + Logger.PrintWarning(LogClass.Application, $"Failed to get access to directory to list files: \"{dir}\""); + } + + if (content.Length > 0) + { + foreach (string file in content) + yield return file; + } + + try + { + content = Directory.GetDirectories(dir); + } + catch (UnauthorizedAccessException) + { + Logger.PrintWarning(LogClass.Application, $"Failed to get access to directory: \"{dir}\""); + } + + if (content.Length > 0) + { + foreach (string subdir in content) + stack.Push(subdir); + } + } + } + public static void LoadApplications(List appDirs, VirtualFileSystem virtualFileSystem, Language desiredTitleLanguage) { int numApplicationsFound = 0; @@ -52,31 +93,32 @@ namespace Ryujinx.Ui List applications = new List(); foreach (string appDir in appDirs) { - try + + if (!Directory.Exists(appDir)) { - if (!Directory.Exists(appDir)) + Logger.PrintWarning(LogClass.Application, $"The \"game_dirs\" section in \"Config.json\" contains an invalid directory: \"{appDir}\""); + + continue; + } + + + + foreach (string app in GetFilesInDirectory(appDir)) + { + + if ((Path.GetExtension(app).ToLower() == ".nsp") || + (Path.GetExtension(app).ToLower() == ".pfs0") || + (Path.GetExtension(app).ToLower() == ".xci") || + (Path.GetExtension(app).ToLower() == ".nca") || + (Path.GetExtension(app).ToLower() == ".nro") || + (Path.GetExtension(app).ToLower() == ".nso")) { - Logger.PrintWarning(LogClass.Application, $"The \"game_dirs\" section in \"Config.json\" contains an invalid directory: \"{appDir}\""); - continue; + applications.Add(app); + numApplicationsFound++; + } - - foreach (string app in Directory.GetFiles(appDir, "*.*", SearchOption.AllDirectories)) - { - if ((Path.GetExtension(app).ToLower() == ".nsp") || - (Path.GetExtension(app).ToLower() == ".pfs0") || - (Path.GetExtension(app).ToLower() == ".xci") || - (Path.GetExtension(app).ToLower() == ".nca") || - (Path.GetExtension(app).ToLower() == ".nro") || - (Path.GetExtension(app).ToLower() == ".nso")) - { - - applications.Add(app); - numApplicationsFound++; - - } - } - } catch (UnauthorizedAccessException) { } + } } // Loops through applications list, creating a struct and then firing an event containing the struct for each application