From f3dd55278bea06981645fddd57be31a80c394d77 Mon Sep 17 00:00:00 2001 From: Scighost Date: Wed, 22 Nov 2023 21:36:50 +0800 Subject: [PATCH] modify location of config file #503 --- src/Starward/AppConfig.cs | 177 ++++++------------ .../Pages/Welcome/SelectDirectoryPage.xaml.cs | 68 +++---- src/Starward/Services/UrlProtocolService.cs | 8 +- 3 files changed, 101 insertions(+), 152 deletions(-) diff --git a/src/Starward/AppConfig.cs b/src/Starward/AppConfig.cs index b59451260..495d29515 100644 --- a/src/Starward/AppConfig.cs +++ b/src/Starward/AppConfig.cs @@ -2,7 +2,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Microsoft.Win32; using Serilog; using Starward.Core; using Starward.Core.Gacha.Genshin; @@ -31,14 +30,10 @@ internal static class AppConfig { -#if (DEBUG || DEV) && !DISABLE_DEV - private const string REG_KEY_NAME = @"HKEY_CURRENT_USER\Software\Starward_Dev"; -#else - private const string REG_KEY_NAME = @"HKEY_CURRENT_USER\Software\Starward"; -#endif + public static string? AppVersion { get; private set; } - public static string? AppVersion { get; private set; } + public static bool IsPortable { get; private set; } public static IConfigurationRoot Configuration { get; private set; } @@ -53,12 +48,9 @@ internal static class AppConfig private static IServiceProvider _serviceProvider; - private static bool reg; - - static AppConfig() { - LoadConfiguration(); + Initialize(); } @@ -78,7 +70,7 @@ static AppConfig() - #region UserData + #region Ini Config private static int windowSizeMode; @@ -122,7 +114,8 @@ public static string UserDataFolder public static bool DisableGameAccountSwitcher { get; set; } - private static void LoadConfiguration() + + private static void Initialize() { try { @@ -132,51 +125,43 @@ private static void LoadConfiguration() string? baseDir = Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('\\')); string exe = Path.Join(baseDir, "Starward.exe"); - var builder = new ConfigurationBuilder(); if (File.Exists(exe)) { - string ini = Path.Join(baseDir, "config.ini"); - if (File.Exists(ini)) + IsPortable = true; + } + else + { + baseDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Starward"); + Directory.CreateDirectory(baseDir); + } + string? iniPath = Path.Join(baseDir, "config.ini"); + var builder = new ConfigurationBuilder().AddCommandLine(Environment.GetCommandLineArgs()); + if (File.Exists(iniPath)) + { + builder.AddIniFile(iniPath); + } + Configuration = builder.Build(); + + windowSizeMode = Configuration.GetValue(nameof(WindowSizeMode)); + language = Configuration.GetValue(nameof(Language)); + DisableNavigationShortcut = Configuration.GetValue(nameof(DisableNavigationShortcut)); + DisableGameNoticeRedHot = Configuration.GetValue(nameof(DisableGameNoticeRedHot)); + DisableGameAccountSwitcher = Configuration.GetValue(nameof(DisableGameAccountSwitcher)); + string? dir = Configuration.GetValue(nameof(UserDataFolder)); + if (!string.IsNullOrWhiteSpace(dir)) + { + string folder; + if (Path.IsPathFullyQualified(dir)) { - builder.AddIniFile(ini); + folder = dir; } - Configuration = builder.AddCommandLine(Environment.GetCommandLineArgs()).Build(); - windowSizeMode = Configuration.GetValue(nameof(WindowSizeMode)); - language = Configuration.GetValue(nameof(Language)); - DisableNavigationShortcut = Configuration.GetValue(nameof(DisableNavigationShortcut)); - DisableGameNoticeRedHot = Configuration.GetValue(nameof(DisableGameNoticeRedHot)); - DisableGameAccountSwitcher = Configuration.GetValue(nameof(DisableGameAccountSwitcher)); - string? dir = Configuration.GetValue(nameof(UserDataFolder)); - if (!string.IsNullOrWhiteSpace(dir)) + else { - string folder; - if (Path.IsPathFullyQualified(dir)) - { - folder = dir; - } - else - { - folder = Path.Join(baseDir, dir); - } - if (Directory.Exists(folder)) - { - userDataFolder = Path.GetFullPath(folder); - } + folder = Path.Join(baseDir, dir); } - } - else - { - reg = true; - Configuration = builder.AddCommandLine(Environment.GetCommandLineArgs()).Build(); - windowSizeMode = Registry.GetValue(REG_KEY_NAME, nameof(WindowSizeMode), null) as int? ?? 0; - language = Registry.GetValue(REG_KEY_NAME, nameof(Language), null) as string; - DisableNavigationShortcut = Registry.GetValue(REG_KEY_NAME, nameof(DisableNavigationShortcut), 0) is 1; - DisableGameNoticeRedHot = Registry.GetValue(REG_KEY_NAME, nameof(DisableGameNoticeRedHot), 0) is 1; - DisableGameAccountSwitcher = Registry.GetValue(REG_KEY_NAME, nameof(DisableGameAccountSwitcher), 0) is 1; - string? dir = Registry.GetValue(REG_KEY_NAME, nameof(UserDataFolder), null) as string; - if (Directory.Exists(dir)) + if (Directory.Exists(folder)) { - userDataFolder = Path.GetFullPath(dir); + userDataFolder = Path.GetFullPath(folder); } } } @@ -192,29 +177,28 @@ private static void SaveConfiguration() { try { - if (reg) + string dataFolder = UserDataFolder; + string baseDir; + if (IsPortable) { - Registry.SetValue(REG_KEY_NAME, nameof(WindowSizeMode), WindowSizeMode); - Registry.SetValue(REG_KEY_NAME, nameof(Language), Language ?? ""); - Registry.SetValue(REG_KEY_NAME, nameof(UserDataFolder), UserDataFolder); + baseDir = Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('\\'))!; } else { - string dataFolder = UserDataFolder; - string baseDir = Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('\\'))!; - if (dataFolder?.StartsWith(baseDir) ?? false) - { - dataFolder = Path.GetRelativePath(baseDir, dataFolder); - } - File.WriteAllText(Path.Combine(baseDir, "config.ini"), $""" - {nameof(WindowSizeMode)}={WindowSizeMode} - {nameof(Language)}={Language} - {nameof(UserDataFolder)}={dataFolder} - {nameof(DisableNavigationShortcut)}={DisableNavigationShortcut} - {nameof(DisableGameNoticeRedHot)}={DisableGameNoticeRedHot} - {nameof(DisableGameAccountSwitcher)}={DisableGameAccountSwitcher} - """); + baseDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Starward"); + } + if (dataFolder?.StartsWith(baseDir) ?? false) + { + dataFolder = Path.GetRelativePath(baseDir, dataFolder); } + File.WriteAllText(Path.Combine(baseDir, "config.ini"), $""" + {nameof(WindowSizeMode)}={WindowSizeMode} + {nameof(Language)}={Language} + {nameof(UserDataFolder)}={dataFolder} + {nameof(DisableNavigationShortcut)}={DisableNavigationShortcut} + {nameof(DisableGameNoticeRedHot)}={DisableGameNoticeRedHot} + {nameof(DisableGameAccountSwitcher)}={DisableGameAccountSwitcher} + """); } catch { } } @@ -561,15 +545,8 @@ private static void InitializeSettingProvider() DatabaseService ??= GetService(); if (cache is null) { - if (reg) - { - cache = new(); - } - else - { - using var dapper = DatabaseService.CreateConnection(); - cache = dapper.Query<(string Key, string? Value)>("SELECT Key, Value FROM Setting;").ToDictionary(x => x.Key, x => x.Value); - } + using var dapper = DatabaseService.CreateConnection(); + cache = dapper.Query<(string Key, string? Value)>("SELECT Key, Value FROM Setting;").ToDictionary(x => x.Key, x => x.Value); } } catch { } @@ -594,15 +571,8 @@ private static void InitializeSettingProvider() { return ConvertFromString(value, defaultValue); } - if (reg) - { - value = Registry.GetValue(REG_KEY_NAME, key, null) as string; - } - else - { - using var dapper = DatabaseService.CreateConnection(); - value = dapper.QueryFirstOrDefault("SELECT Value FROM Setting WHERE Key=@key LIMIT 1;", new { key }); - } + using var dapper = DatabaseService.CreateConnection(); + value = dapper.QueryFirstOrDefault("SELECT Value FROM Setting WHERE Key=@key LIMIT 1;", new { key }); cache[key] = value; return ConvertFromString(value, defaultValue); } @@ -647,26 +617,8 @@ private static void SetValue(T? value, [CallerMemberName] string? key = null) return; } cache[key] = val; - if (reg) - { - if (val is null) - { -#if (DEBUG || DEV) && !DISABLE_DEV - Registry.CurrentUser.OpenSubKey(@"Software\Starward_Dev", true)?.DeleteValue(key, false); -#else - Registry.CurrentUser.OpenSubKey(@"Software\Starward", true)?.DeleteValue(key, false); -#endif - } - else - { - Registry.SetValue(REG_KEY_NAME, key, val); - } - } - else - { - using var dapper = DatabaseService.CreateConnection(); - dapper.Execute("INSERT OR REPLACE INTO Setting (Key, Value) VALUES (@key, @val);", new { key, val }); - } + using var dapper = DatabaseService.CreateConnection(); + dapper.Execute("INSERT OR REPLACE INTO Setting (Key, Value) VALUES (@key, @val);", new { key, val }); } catch { } } @@ -675,19 +627,12 @@ private static void SetValue(T? value, [CallerMemberName] string? key = null) public static void DeleteAllSettings() { - if (reg) - { -#if (DEBUG || DEV) && !DISABLE_DEV - Registry.CurrentUser.OpenSubKey(@"Software", true)?.DeleteSubKeyTree("Starward_Dev"); -#else - Registry.CurrentUser.OpenSubKey(@"Software", true)?.DeleteSubKeyTree("Starward"); -#endif - } - else + try { using var dapper = DatabaseService.CreateConnection(); dapper.Execute("DELETE FROM Setting WHERE TRUE;"); } + catch { } } diff --git a/src/Starward/Pages/Welcome/SelectDirectoryPage.xaml.cs b/src/Starward/Pages/Welcome/SelectDirectoryPage.xaml.cs index e743e1ddd..256c1c0c0 100644 --- a/src/Starward/Pages/Welcome/SelectDirectoryPage.xaml.cs +++ b/src/Starward/Pages/Welcome/SelectDirectoryPage.xaml.cs @@ -57,52 +57,52 @@ private async Task SelectDirectoryAsync() { try { - var parentFolder = Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('/', '\\')); - var exe = Path.Join(parentFolder, "Starward.exe"); + string folder; + if (AppConfig.IsPortable) + { + folder = Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('/', '\\'))!; + } + else + { + folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Starward"); + } string? selectFolder = null; - if (File.Exists(exe)) + var dialog = new ContentDialog { - var dialog = new ContentDialog - { - // 选择文件夹 - Title = Lang.SelectDirectoryPage_SelectFolder, - // 推荐您将数据保存在软件所在的文件夹中: - // 是否选择此文件夹? - Content = $""" + // 选择文件夹 + Title = Lang.SelectDirectoryPage_SelectFolder, + // 推荐您将数据保存在软件所在的文件夹中: + // 是否选择此文件夹? + Content = $""" {Lang.SelectDirectoryPage_RecommendFolder} - {parentFolder} + {folder} {Lang.SelectDirectoryPage_WouldYouLikeToChooseThisFolder} """, - DefaultButton = ContentDialogButton.Primary, - // 好的 - PrimaryButtonText = Lang.SelectDirectoryPage_OK, - // 自己选 - SecondaryButtonText = Lang.SelectDirectoryPage_ChooseAnother, - // 不想选 - CloseButtonText = Lang.SelectDirectoryPage_Cancel, - XamlRoot = this.XamlRoot - }; - var result = await dialog.ShowAsync(); - if (result is ContentDialogResult.Primary) - { - selectFolder = parentFolder; - } - if (result is ContentDialogResult.Secondary) - { - selectFolder = await FileDialogHelper.PickFolderAsync(MainWindow.Current.HWND); - } - if (result is ContentDialogResult.None) - { - return; - } + DefaultButton = ContentDialogButton.Primary, + // 好的 + PrimaryButtonText = Lang.SelectDirectoryPage_OK, + // 自己选 + SecondaryButtonText = Lang.SelectDirectoryPage_ChooseAnother, + // 取消 + CloseButtonText = Lang.Common_Cancel, + XamlRoot = this.XamlRoot + }; + var result = await dialog.ShowAsync(); + if (result is ContentDialogResult.Primary) + { + selectFolder = folder; } - else + if (result is ContentDialogResult.Secondary) { selectFolder = await FileDialogHelper.PickFolderAsync(MainWindow.Current.HWND); } + if (result is ContentDialogResult.None) + { + return; + } if (Directory.Exists(selectFolder)) { diff --git a/src/Starward/Services/UrlProtocolService.cs b/src/Starward/Services/UrlProtocolService.cs index 62fb7d583..5f76eeafb 100644 --- a/src/Starward/Services/UrlProtocolService.cs +++ b/src/Starward/Services/UrlProtocolService.cs @@ -19,8 +19,12 @@ internal class UrlProtocolService public static void RegisterProtocol() { UnregisterProtocol(); - string exe = Path.Join(Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('\\', '/')), "Starward.exe"); - if (!File.Exists(exe)) + string exe; + if (AppConfig.IsPortable) + { + exe = Path.Join(Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd('\\', '/')), "Starward.exe"); + } + else { exe = Path.Join(AppContext.BaseDirectory, "Starward.exe"); }