From d5670aff7721e710ad5f4157827d5716b82bc329 Mon Sep 17 00:00:00 2001 From: Elise Date: Wed, 25 Mar 2020 17:17:54 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20unhandled=20UnauthorizedAccessException?= =?UTF-8?q?=20causing=20crash=20while=20listing=E2=80=A6=20(#1025)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix unhandled UnauthorizedAccessException causing crash while listing directories * Actually handle not having privileges for a directory * Fix log message when not having privileges for a directory * Remove unneccesary empty lines * Remove unneccecssary space --- Ryujinx/Ui/ApplicationLibrary.cs | 46 ++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/Ryujinx/Ui/ApplicationLibrary.cs b/Ryujinx/Ui/ApplicationLibrary.cs index fc3d6122..3ff7a54c 100644 --- a/Ryujinx/Ui/ApplicationLibrary.cs +++ b/Ryujinx/Ui/ApplicationLibrary.cs @@ -40,6 +40,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: \"{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; @@ -53,6 +94,7 @@ namespace Ryujinx.Ui List applications = new List(); foreach (string appDir in appDirs) { + if (!Directory.Exists(appDir)) { Logger.PrintWarning(LogClass.Application, $"The \"game_dirs\" section in \"Config.json\" contains an invalid directory: \"{appDir}\""); @@ -60,10 +102,10 @@ namespace Ryujinx.Ui continue; } - foreach (string app in Directory.GetFiles(appDir, "*.*", SearchOption.AllDirectories)) + foreach (string app in GetFilesInDirectory(appDir)) { if ((Path.GetExtension(app).ToLower() == ".nsp") || - (Path.GetExtension(app).ToLower() == ".pfs0")|| + (Path.GetExtension(app).ToLower() == ".pfs0") || (Path.GetExtension(app).ToLower() == ".xci") || (Path.GetExtension(app).ToLower() == ".nca") || (Path.GetExtension(app).ToLower() == ".nro") ||