配置文件功能完善

This commit is contained in:
rmbgame
2021-03-06 11:01:13 +08:00
parent 3cbcabeedf
commit ca93b3f9c8
29 changed files with 906 additions and 77 deletions
@@ -24,18 +24,4 @@ namespace System.Application.Models
/// </summary>
public Dictionary<Platform, string>? TextReaderProvider { get; set; }
}
#if DEBUG
[Obsolete("use AppUserSettings", true)]
public class SettingsModel
{
[Obsolete("use AppUserSettings.StartupArgs", true)]
public string? SteamRunParam { get; set; }
[Obsolete("use AppUserSettings.EnableTextLog", true)]
public bool EnableTextLog { get; set; }
}
#endif
}
@@ -0,0 +1,46 @@
using System;
using System.Application.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace System.Application.Models.Settings
{
public static class AuthSettings
{
static AuthSettings()
{
Authenticators.ValueChanged += Authenticators_ValueChanged;
}
private static void Authenticators_ValueChanged(object sender, ValueChangedEventArgs<string> e)
{
if (e.NewValue != e.OldValue)
{
if (AuthSettings.IsCurrentDirectorySaveAuthData.Value)
{
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, Constants.AUTHDATA_FILE), e.NewValue);
}
}
}
/// <summary>
/// 令牌数据(压缩存储)
/// </summary>
public static SerializableProperty<string> Authenticators { get; }
= new SerializableProperty<string>(GetKey(), Providers.Local) { AutoSave = true };
/// <summary>
/// 是否将令牌数据存储在程序路径下
/// </summary>
public static SerializableProperty<bool> IsCurrentDirectorySaveAuthData { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
private static string GetKey([CallerMemberName] string propertyName = "")
{
return nameof(AuthSettings) + "." + propertyName;
}
}
}
@@ -0,0 +1,76 @@
using System;
using System.Application.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace System.Application.Models.Settings
{
public static class GeneralSettings
{
static GeneralSettings()
{
WindowsStartupAutoRun.ValueChanged += WindowsStartupAutoRun_ValueChanged;
IsEnableLogRecord.ValueChanged += IsEnableLogRecord_ValueChanged;
//CreateDesktopShortcut.ValueChanged += CreateDesktopShortcut_ValueChanged;
}
private static void IsEnableLogRecord_ValueChanged(object sender, ValueChangedEventArgs<bool> e)
{
}
private static void WindowsStartupAutoRun_ValueChanged(object sender, ValueChangedEventArgs<bool> e)
{
}
/// <summary>
/// 程序是否开机自启动
/// </summary>
public static SerializableProperty<bool> WindowsStartupAutoRun { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
///// <summary>
///// 创建桌面快捷方式
///// </summary>
//public static SerializableProperty<bool> CreateDesktopShortcut { get; }
// = new SerializableProperty<bool>(GetKey(), Providers.Roaming, false) { AutoSave = true };
/// <summary>
/// 程序启动时最小化
/// </summary>
public static SerializableProperty<bool> IsStartupAppMinimized { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
/// <summary>
/// 是否显示起始页
/// </summary>
public static SerializableProperty<bool> IsShowStartPage { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, true) { AutoSave = true };
/// <summary>
/// 启用游戏列表本地缓存
/// </summary>
public static SerializableProperty<bool> IsSteamAppListLocalCache { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, true) { AutoSave = true };
/// <summary>
/// 自动检查更新
/// </summary>
public static SerializableProperty<bool> IsAutoCheckUpdate { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, true) { AutoSave = true };
/// <summary>
/// 启用错误日志记录
/// </summary>
public static SerializableProperty<bool> IsEnableLogRecord { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
private static string GetKey([CallerMemberName] string propertyName = "")
{
return nameof(GeneralSettings) + "." + propertyName;
}
}
}
@@ -0,0 +1,90 @@
using System;
using System.Application.Serialization;
using System.Application.Services;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
namespace System.Application.Models.Settings
{
public abstract class SettingsHost
{
private static readonly Dictionary<Type, SettingsHost> instances = new();
private readonly Dictionary<string, object> cachedProperties = new();
protected virtual string CategoryName => this.GetType().Name;
protected SettingsHost()
{
instances[this.GetType()] = this;
}
/// <summary>
/// 请参阅 <see cref="SerializableProperty{T}"/> 缓存在当前实例中获取
/// 如果没有缓存,根据<see cref="create" />创建它
/// </summary>
/// <returns></returns>
protected SerializableProperty<T> Cache<T>(Func<string, SerializableProperty<T>> create, [CallerMemberName] string propertyName = "")
{
var key = this.CategoryName + "." + propertyName;
if (this.cachedProperties.TryGetValue(key, out object obj) && obj is SerializableProperty<T> property1)
return property1;
var property = create(key);
this.cachedProperties[key] = property;
return property;
}
#region Load / Save
public static void Load()
{
try
{
Providers.Local.Load();
}
catch (Exception ex)
{
Log.Error(nameof(SettingsHost), "Config Load", ex);
File.Delete(Providers.LocalFilePath);
Providers.Local.Load();
}
}
public static void Save()
{
try
{
Providers.Local.Save();
}
catch (Exception ex)
{
Log.Error(nameof(SettingsHost), "Config Save", ex);
throw;
}
}
#endregion
/// <summary>
/// <typeparamref name ="T" />获取类型的配置对象的唯一实例。
/// </summary>
public static T Instance<T>() where T : SettingsHost, new()
{
return instances.TryGetValue(typeof(T), out SettingsHost host) ? (T)host : new T();
}
}
public class Providers
{
public static string LocalFilePath => Path.Combine(IOPath.AppDataDirectory, IConfigFileService.ConfigFileName);
public static ISerializationProvider Local { get; } = new FileSettingsProvider(LocalFilePath);
}
}
@@ -0,0 +1,41 @@
using System;
using System.Application.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace System.Application.Models.Settings
{
public static class SteamSettings
{
/// <summary>
/// Steam启动参数
/// </summary>
public static SerializableProperty<string> SteamStratParameter { get; }
= new SerializableProperty<string>(GetKey(), Providers.Local, string.Empty) { AutoSave = true };
/// <summary>
/// Steam皮肤
/// </summary>
public static SerializableProperty<string> SteamSkin { get; }
= new SerializableProperty<string>(GetKey(), Providers.Local, string.Empty) { AutoSave = true };
/// <summary>
/// 自动运行Steam
/// </summary>
public static SerializableProperty<bool> IsAutoRunSteam { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
/// <summary>
/// 检测到Steam启动时弹出消息通知
/// </summary>
public static SerializableProperty<bool> IsEnableSteamLaunchNotification { get; }
= new SerializableProperty<bool>(GetKey(), Providers.Local, false) { AutoSave = true };
private static string GetKey([CallerMemberName] string propertyName = "")
{
return nameof(SteamSettings) + "." + propertyName;
}
}
}
@@ -0,0 +1,31 @@
using System;
using System.Application.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace System.Application.Models.Settings
{
public static class UISettings
{
/// <summary>
/// Ö÷Ìâ
/// </summary>
public static SerializableProperty<short> Theme { get; }
= new SerializableProperty<short>(GetKey(), Providers.Local) { AutoSave = true };
/// <summary>
/// ÓïÑÔ
/// </summary>
public static SerializableProperty<string> Language { get; }
= new SerializableProperty<string>(GetKey(), Providers.Local) { AutoSave = true };
private static string GetKey([CallerMemberName] string propertyName = "")
{
return nameof(UISettings) + "." + propertyName;
}
}
}