Skip to content

Commit

Permalink
modify location of config file #503
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Nov 22, 2023
1 parent 736705b commit f3dd552
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 152 deletions.
177 changes: 61 additions & 116 deletions src/Starward/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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; }
Expand All @@ -53,12 +48,9 @@ internal static class AppConfig
private static IServiceProvider _serviceProvider;


private static bool reg;


static AppConfig()
{
LoadConfiguration();
Initialize();
}


Expand All @@ -78,7 +70,7 @@ static AppConfig()



#region UserData
#region Ini Config


private static int windowSizeMode;
Expand Down Expand Up @@ -122,7 +114,8 @@ public static string UserDataFolder
public static bool DisableGameAccountSwitcher { get; set; }


private static void LoadConfiguration()

private static void Initialize()
{
try
{
Expand All @@ -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<int>(nameof(WindowSizeMode));
language = Configuration.GetValue<string>(nameof(Language));
DisableNavigationShortcut = Configuration.GetValue<bool>(nameof(DisableNavigationShortcut));
DisableGameNoticeRedHot = Configuration.GetValue<bool>(nameof(DisableGameNoticeRedHot));
DisableGameAccountSwitcher = Configuration.GetValue<bool>(nameof(DisableGameAccountSwitcher));
string? dir = Configuration.GetValue<string>(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<int>(nameof(WindowSizeMode));
language = Configuration.GetValue<string>(nameof(Language));
DisableNavigationShortcut = Configuration.GetValue<bool>(nameof(DisableNavigationShortcut));
DisableGameNoticeRedHot = Configuration.GetValue<bool>(nameof(DisableGameNoticeRedHot));
DisableGameAccountSwitcher = Configuration.GetValue<bool>(nameof(DisableGameAccountSwitcher));
string? dir = Configuration.GetValue<string>(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);
}
}
}
Expand All @@ -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 { }
}
Expand Down Expand Up @@ -561,15 +545,8 @@ private static void InitializeSettingProvider()
DatabaseService ??= GetService<DatabaseService>();
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 { }
Expand All @@ -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<string>("SELECT Value FROM Setting WHERE Key=@key LIMIT 1;", new { key });
}
using var dapper = DatabaseService.CreateConnection();
value = dapper.QueryFirstOrDefault<string>("SELECT Value FROM Setting WHERE Key=@key LIMIT 1;", new { key });
cache[key] = value;
return ConvertFromString(value, defaultValue);
}
Expand Down Expand Up @@ -647,26 +617,8 @@ private static void SetValue<T>(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 { }
}
Expand All @@ -675,19 +627,12 @@ private static void SetValue<T>(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 { }
}


Expand Down
68 changes: 34 additions & 34 deletions src/Starward/Pages/Welcome/SelectDirectoryPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
8 changes: 6 additions & 2 deletions src/Starward/Services/UrlProtocolService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit f3dd552

Please sign in to comment.