diff --git a/Pal.Client/Configuration.cs b/Pal.Client/Configuration.cs index 88147b5..67275aa 100644 --- a/Pal.Client/Configuration.cs +++ b/Pal.Client/Configuration.cs @@ -1,17 +1,25 @@ using Dalamud.Configuration; +using System; +using System.Collections.Generic; using System.Numerics; namespace Pal.Client { public class Configuration : IPluginConfiguration { - public int Version { get; set; } + public int Version { get; set; } = 2; #region Saved configuration values public bool FirstUse { get; set; } = true; public EMode Mode { get; set; } = EMode.Offline; - public string? DebugAccountId { get; set; } - public string? AccountId { get; set; } + + [Obsolete] + public string? DebugAccountId { private get; set; } + + [Obsolete] + public string? AccountId { private get; set; } + + public Dictionary AccountIds { get; set; } = new(); public bool ShowTraps { get; set; } = true; public Vector4 TrapColor { get; set; } = new Vector4(1, 0, 0, 0.4f); @@ -29,9 +37,25 @@ namespace Pal.Client public delegate void OnSaved(); public event OnSaved? Saved; +#pragma warning disable CS0612 // Type or member is obsolete + public void Migrate() + { + if (Version == 1) + { + if (DebugAccountId != null && Guid.TryParse(DebugAccountId, out Guid debugAccountId)) + AccountIds["http://localhost:5145"] = debugAccountId; + + if (AccountId != null && Guid.TryParse(AccountId, out Guid accountId)) + AccountIds["https://pal.μ.tv"] = accountId; + + Version = 2; + Save(); + } + } +#pragma warning restore CS0612 // Type or member is obsolete + public void Save() { - Version = 1; Service.PluginInterface.SavePluginConfig(this); Saved?.Invoke(); } diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index 16d8ad0..10366c1 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -59,6 +59,7 @@ namespace Pal.Client pluginInterface.Create(); Service.Plugin = this; Service.Configuration = (Configuration?)pluginInterface.GetPluginConfig() ?? pluginInterface.Create()!; + Service.Configuration.Migrate(); var agreementWindow = pluginInterface.Create(); if (agreementWindow is not null) diff --git a/Pal.Client/RemoteApi.cs b/Pal.Client/RemoteApi.cs index a0f4614..3e48024 100644 --- a/Pal.Client/RemoteApi.cs +++ b/Pal.Client/RemoteApi.cs @@ -47,43 +47,30 @@ namespace Pal.Client } var accountClient = new AccountService.AccountServiceClient(_channel); -#if DEBUG - string? accountId = Service.Configuration.DebugAccountId; -#else - string? accountId = Service.Configuration.AccountId; -#endif - if (string.IsNullOrEmpty(accountId)) + Guid? accountId = Service.Configuration.AccountIds[remoteUrl]; + if (accountId == null) { var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); if (createAccountReply.Success) { - accountId = createAccountReply.AccountId; -#if DEBUG - Service.Configuration.DebugAccountId = accountId; -#else - Service.Configuration.AccountId = accountId; - -#endif + accountId = Guid.Parse(createAccountReply.AccountId); + Service.Configuration.AccountIds[remoteUrl] = accountId.Value; Service.Configuration.Save(); } } - if (string.IsNullOrEmpty(accountId)) + if (accountId == null) return false; if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now) { - _lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = accountId }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); + _lastLoginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = accountId.ToString() }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); if (!_lastLoginReply.Success) { if (_lastLoginReply.Error == LoginError.InvalidAccountId) { accountId = null; -#if DEBUG - Service.Configuration.DebugAccountId = accountId; -#else - Service.Configuration.AccountId = accountId; -#endif + Service.Configuration.AccountIds.Remove(remoteUrl); Service.Configuration.Save(); if (retry) return await Connect(cancellationToken, retry: false);