diff --git a/Daybreak/Configuration/Options/CredentialManagerOptions.cs b/Daybreak/Configuration/Options/CredentialManagerOptions.cs index 6436448d..86dfd426 100644 --- a/Daybreak/Configuration/Options/CredentialManagerOptions.cs +++ b/Daybreak/Configuration/Options/CredentialManagerOptions.cs @@ -6,7 +6,7 @@ namespace Daybreak.Configuration.Options; [OptionsIgnore] -[OptionsSynchronizationIgnore] +[OptionsName(Name = "Credentials")] internal sealed class CredentialManagerOptions { [JsonProperty(nameof(ProtectedLoginCredentials))] diff --git a/Daybreak/Configuration/ProjectConfiguration.cs b/Daybreak/Configuration/ProjectConfiguration.cs index 1b5d3fa9..e10cf802 100644 --- a/Daybreak/Configuration/ProjectConfiguration.cs +++ b/Daybreak/Configuration/ProjectConfiguration.cs @@ -347,6 +347,7 @@ public override void RegisterStartupActions(IStartupActionProducer startupAction startupActionProducer.RegisterAction(); startupActionProducer.RegisterAction(); startupActionProducer.RegisterAction(); + startupActionProducer.RegisterAction(); } public override void RegisterPostUpdateActions(IPostUpdateActionProducer postUpdateActionProducer) diff --git a/Daybreak/Models/ProtectedLoginCredentials.cs b/Daybreak/Models/ProtectedLoginCredentials.cs index 2f21008a..d0f557b3 100644 --- a/Daybreak/Models/ProtectedLoginCredentials.cs +++ b/Daybreak/Models/ProtectedLoginCredentials.cs @@ -10,6 +10,4 @@ public sealed class ProtectedLoginCredentials public string? ProtectedUsername { get; set; } [JsonProperty(nameof(ProtectedPassword))] public string? ProtectedPassword { get; set; } - [JsonProperty(nameof(CharacterName))] - public string? CharacterName { get; set; } } diff --git a/Daybreak/Services/Options/IOptionsProvider.cs b/Daybreak/Services/Options/IOptionsProvider.cs index 82063a60..845dc136 100644 --- a/Daybreak/Services/Options/IOptionsProvider.cs +++ b/Daybreak/Services/Options/IOptionsProvider.cs @@ -6,6 +6,7 @@ namespace Daybreak.Services.Options; public interface IOptionsProvider { + JObject? TryGetKeyedOptions(string key); IEnumerable GetRegisteredOptions(); IEnumerable GetRegisteredOptionsTypes(); void SaveRegisteredOptions(object options); diff --git a/Daybreak/Services/Options/OptionsManager.cs b/Daybreak/Services/Options/OptionsManager.cs index 0463206d..15a573cb 100644 --- a/Daybreak/Services/Options/OptionsManager.cs +++ b/Daybreak/Services/Options/OptionsManager.cs @@ -141,6 +141,16 @@ public void SaveRegisteredOptions(string name, JObject options) this.SaveOptions(registeredType, options); } + public JObject? TryGetKeyedOptions(string key) + { + if (!this.optionsCache.TryGetValue(key, out var value)) + { + return default; + } + + return JsonConvert.DeserializeObject(value); + } + private void SaveOptions(Type type, object value) { var optionsName = GetOptionsName(type); diff --git a/Daybreak/Services/Startup/Actions/CredentialsOptionsMigrator.cs b/Daybreak/Services/Startup/Actions/CredentialsOptionsMigrator.cs new file mode 100644 index 00000000..0a4d3dde --- /dev/null +++ b/Daybreak/Services/Startup/Actions/CredentialsOptionsMigrator.cs @@ -0,0 +1,42 @@ +using Daybreak.Attributes; +using Daybreak.Configuration.Options; +using Daybreak.Services.Options; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Linq; +using System; +using System.Core.Extensions; +using System.Reflection; + +namespace Daybreak.Services.Startup.Actions; +internal sealed class CredentialsOptionsMigrator : StartupActionBase +{ + private const string OldOptionsKey = "CredentialManagerOptions"; + + private readonly IOptionsProvider optionsProvider; + private readonly ILogger logger; + + public CredentialsOptionsMigrator( + IOptionsProvider optionsProvider, + ILogger logger) + { + this.optionsProvider = optionsProvider.ThrowIfNull(); + this.logger = logger.ThrowIfNull(); + } + + public override void ExecuteOnStartup() + { + if (this.optionsProvider.TryGetKeyedOptions(OldOptionsKey) is not JObject options) + { + return; + } + + var newOptionsKey = GetNewOptionsKey(); + this.logger.LogInformation($"Found [{OldOptionsKey}]. Migrating options to [{newOptionsKey}]"); + this.optionsProvider.SaveRegisteredOptions(newOptionsKey, options); + } + + private static string GetNewOptionsKey() + { + return typeof(CredentialManagerOptions).GetCustomAttribute()?.Name ?? throw new InvalidOperationException("Unable to retrieve new credentials options key"); + } +}