From 26f9fe4f3b2758f1e529890b5a2cf02c2c1449c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaro=20Mart=C3=ADnez?= Date: Wed, 13 Sep 2023 14:06:43 -0500 Subject: [PATCH 1/3] Do not force loading additional dictionaries If the path does not exist, simply do not load from that path. --- RetroBar/Extensions/ManagementExtensions.cs | 24 ++++++++++++++++++ RetroBar/Utilities/DictionaryManager.cs | 27 +++------------------ 2 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 RetroBar/Extensions/ManagementExtensions.cs diff --git a/RetroBar/Extensions/ManagementExtensions.cs b/RetroBar/Extensions/ManagementExtensions.cs new file mode 100644 index 00000000..d6f13531 --- /dev/null +++ b/RetroBar/Extensions/ManagementExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.IO; + +internal static class ManagementExtensions +{ + public static void AddFrom(this List list, string path, string extension, bool skipExisting = false) + { + if (!Directory.Exists(path)) + { + return; + } + + foreach (string file in Directory.GetFiles(path, $"*.{extension}")) + { + string fileName = Path.GetFileNameWithoutExtension(file); + if (skipExisting && list.Contains(fileName)) + { + continue; + } + list.Add(fileName); + } + } +} \ No newline at end of file diff --git a/RetroBar/Utilities/DictionaryManager.cs b/RetroBar/Utilities/DictionaryManager.cs index c6c72625..a3aa13fb 100644 --- a/RetroBar/Utilities/DictionaryManager.cs +++ b/RetroBar/Utilities/DictionaryManager.cs @@ -179,37 +179,18 @@ private List GetDictionaries(string dictDefault, string dictFolder, stri List dictionaries = new List { dictDefault }; // Built-in dictionaries - foreach (string subStr in Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dictFolder)) - .Where(s => Path.GetExtension(s).Contains(dictExtension))) - { - dictionaries.Add(Path.GetFileNameWithoutExtension(subStr)); - } + string builtInDictDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dictFolder); + dictionaries.AddFrom(builtInDictDir, dictExtension); // Installed AppData dictionaries string installedDictDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", dictFolder); - - if (Directory.Exists(installedDictDir)) - { - foreach (string subStr in Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", dictFolder)) - .Where(s => Path.GetExtension(s).Contains(dictExtension))) - { - dictionaries.Add(Path.GetFileNameWithoutExtension(subStr)); - } - } + dictionaries.AddFrom(installedDictDir, dictExtension); // Same-folder dictionaries // Because RetroBar is published as a single-file app, it gets extracted to a temp directory, so custom dictionaries won't be there. // Get the executable path to find the custom dictionaries directory when not a debug build. string customDictDir = Path.Combine(Path.GetDirectoryName(ExePath.GetExecutablePath()), dictFolder); - - if (Directory.Exists(customDictDir)) - { - foreach (string subStr in Directory.GetFiles(customDictDir) - .Where(s => Path.GetExtension(s).Contains(dictExtension) && !dictionaries.Contains(Path.GetFileNameWithoutExtension(s)))) - { - dictionaries.Add(Path.GetFileNameWithoutExtension(subStr)); - } - } + dictionaries.AddFrom(customDictDir, dictExtension, skipExisting: true); return dictionaries; } From f598939b5a2d3ba208a20adc7619717faf8d04f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaro=20Mart=C3=ADnez?= Date: Wed, 13 Sep 2023 16:44:08 -0500 Subject: [PATCH 2/3] Simplify the application-specific data path --- RetroBar/Extensions/ManagementExtensions.cs | 5 +++++ RetroBar/Utilities/DictionaryManager.cs | 6 ++---- RetroBar/Utilities/ManagedShellLogger.cs | 2 +- RetroBar/Utilities/Settings.cs | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/RetroBar/Extensions/ManagementExtensions.cs b/RetroBar/Extensions/ManagementExtensions.cs index d6f13531..bd84b20b 100644 --- a/RetroBar/Extensions/ManagementExtensions.cs +++ b/RetroBar/Extensions/ManagementExtensions.cs @@ -4,6 +4,11 @@ internal static class ManagementExtensions { + public static string InLocalAppData(this string path1, string path2 = "") + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", path1, path2); + } + public static void AddFrom(this List list, string path, string extension, bool skipExisting = false) { if (!Directory.Exists(path)) diff --git a/RetroBar/Utilities/DictionaryManager.cs b/RetroBar/Utilities/DictionaryManager.cs index a3aa13fb..ae4625c7 100644 --- a/RetroBar/Utilities/DictionaryManager.cs +++ b/RetroBar/Utilities/DictionaryManager.cs @@ -130,9 +130,7 @@ private void SetDictionary(string dictionary, string dictFolder, string dictDefa if (!File.Exists(dictFilePath)) { // Installed dictionary in AppData directory - dictFilePath = - Path.ChangeExtension(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", dictFolder, dictionary), - dictExtension); + dictFilePath = Path.ChangeExtension(dictFolder.InLocalAppData(dictionary), dictExtension); if (!File.Exists(dictFilePath)) { @@ -183,7 +181,7 @@ private List GetDictionaries(string dictDefault, string dictFolder, stri dictionaries.AddFrom(builtInDictDir, dictExtension); // Installed AppData dictionaries - string installedDictDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar", dictFolder); + string installedDictDir = dictFolder.InLocalAppData(); dictionaries.AddFrom(installedDictDir, dictExtension); // Same-folder dictionaries diff --git a/RetroBar/Utilities/ManagedShellLogger.cs b/RetroBar/Utilities/ManagedShellLogger.cs index d485ccce..8785c17c 100644 --- a/RetroBar/Utilities/ManagedShellLogger.cs +++ b/RetroBar/Utilities/ManagedShellLogger.cs @@ -7,7 +7,7 @@ namespace RetroBar.Utilities { class ManagedShellLogger : IDisposable { - private string _logPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar"), "Logs"); + private string _logPath = "Logs".InLocalAppData(); private string _logName = DateTime.Now.ToString("yyyy-MM-dd_HHmmssfff"); private string _logExt = "log"; private TimeSpan _logRetention = new TimeSpan(7, 0, 0); diff --git a/RetroBar/Utilities/Settings.cs b/RetroBar/Utilities/Settings.cs index ce9b1df9..03aaf25a 100644 --- a/RetroBar/Utilities/Settings.cs +++ b/RetroBar/Utilities/Settings.cs @@ -25,7 +25,7 @@ public static Settings Instance } } - private static string _settingsPath = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "RetroBar"), "settings.json"); + private static string _settingsPath = "settings.json".InLocalAppData(); private static bool _isInitializing = true; private static SettingsManager _settingsManager = new SettingsManager(_settingsPath, new Settings()); From 0e37f95199c5f7faf44c98878b14a315403c8210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaro=20Mart=C3=ADnez?= Date: Sun, 17 Sep 2023 11:00:30 -0500 Subject: [PATCH 3/3] Update DictionaryManager.cs --- RetroBar/Utilities/DictionaryManager.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/RetroBar/Utilities/DictionaryManager.cs b/RetroBar/Utilities/DictionaryManager.cs index ae4625c7..7c7cdd8b 100644 --- a/RetroBar/Utilities/DictionaryManager.cs +++ b/RetroBar/Utilities/DictionaryManager.cs @@ -177,18 +177,15 @@ private List GetDictionaries(string dictDefault, string dictFolder, stri List dictionaries = new List { dictDefault }; // Built-in dictionaries - string builtInDictDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dictFolder); - dictionaries.AddFrom(builtInDictDir, dictExtension); + dictionaries.AddFrom(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dictFolder), dictExtension); // Installed AppData dictionaries - string installedDictDir = dictFolder.InLocalAppData(); - dictionaries.AddFrom(installedDictDir, dictExtension); + dictionaries.AddFrom(dictFolder.InLocalAppData(), dictExtension); // Same-folder dictionaries // Because RetroBar is published as a single-file app, it gets extracted to a temp directory, so custom dictionaries won't be there. // Get the executable path to find the custom dictionaries directory when not a debug build. - string customDictDir = Path.Combine(Path.GetDirectoryName(ExePath.GetExecutablePath()), dictFolder); - dictionaries.AddFrom(customDictDir, dictExtension, skipExisting: true); + dictionaries.AddFrom(Path.Combine(Path.GetDirectoryName(ExePath.GetExecutablePath()), dictFolder), dictExtension, true); return dictionaries; }