diff --git a/src/MaaWpfGui/Helper/ConfigurationHelper.cs b/src/MaaWpfGui/Helper/ConfigurationHelper.cs index b20b9fb16a..0a98f957f2 100644 --- a/src/MaaWpfGui/Helper/ConfigurationHelper.cs +++ b/src/MaaWpfGui/Helper/ConfigurationHelper.cs @@ -85,6 +85,13 @@ namespace MaaWpfGui.Helper return bool.TryParse(value, out var result) ? result : defaultValue; } + public static TOut GetValue(string key, TOut defaultValue) + where TOut : struct, Enum + { + var value = GetValue(key, defaultValue.ToString()); + return Enum.TryParse(value, out var result) ? result : defaultValue; + } + public static string GetGlobalValue(string key, string defaultValue) { var hasValue = _globalKvs.TryGetValue(key, out var value); @@ -100,7 +107,7 @@ namespace MaaWpfGui.Helper { _logger.Information("Read global configuration key {Key} with current configuration value {Value}, configuration hit: {HasValue}, configuration value {Value}", key, value, true, value); SetGlobalValue(key, value); - DeleteValue(key); + DeleteValue(key, out _); return value; } @@ -167,20 +174,25 @@ namespace MaaWpfGui.Helper /// Deletes a configuration /// /// The configuration key. + /// The old value. /// The return value of . // ReSharper disable once UnusedMember.Global - public static bool DeleteValue(string key) + public static bool DeleteValue(string key, out string value) { - var old = string.Empty; + value = string.Empty; if (_kvs.Remove(key, out var kv)) { - old = kv; + value = kv; + } + else + { + return false; } var result = Save(); if (result) { - ConfigurationUpdateEvent?.Invoke(key, old, string.Empty); + ConfigurationUpdateEvent?.Invoke(key, value, string.Empty); _logger.Debug("Configuration {Key} has been deleted", key); } else @@ -191,18 +203,22 @@ namespace MaaWpfGui.Helper return result; } - public static bool DeleteGlobalValue(string key) + public static bool DeleteGlobalValue(string key, out string value) { - var old = string.Empty; + value = string.Empty; if (_globalKvs.Remove(key, out var kv)) { - old = kv; + value = kv; + } + else + { + return false; } var result = Save(); if (result) { - ConfigurationUpdateEvent?.Invoke(key, old, string.Empty); + ConfigurationUpdateEvent?.Invoke(key, value, string.Empty); _logger.Debug("Global configuration {Key} has been deleted", key); } else