Save account id per remote to make it easier to use other servers

This commit is contained in:
Liza 2022-10-31 16:21:27 +01:00
parent 187a3a47f2
commit 90088ca846
3 changed files with 36 additions and 24 deletions

View File

@ -1,17 +1,25 @@
using Dalamud.Configuration; using Dalamud.Configuration;
using System;
using System.Collections.Generic;
using System.Numerics; using System.Numerics;
namespace Pal.Client namespace Pal.Client
{ {
public class Configuration : IPluginConfiguration public class Configuration : IPluginConfiguration
{ {
public int Version { get; set; } public int Version { get; set; } = 2;
#region Saved configuration values #region Saved configuration values
public bool FirstUse { get; set; } = true; public bool FirstUse { get; set; } = true;
public EMode Mode { get; set; } = EMode.Offline; 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<string, Guid> AccountIds { get; set; } = new();
public bool ShowTraps { get; set; } = true; public bool ShowTraps { get; set; } = true;
public Vector4 TrapColor { get; set; } = new Vector4(1, 0, 0, 0.4f); public Vector4 TrapColor { get; set; } = new Vector4(1, 0, 0, 0.4f);
@ -29,9 +37,25 @@ namespace Pal.Client
public delegate void OnSaved(); public delegate void OnSaved();
public event OnSaved? Saved; 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() public void Save()
{ {
Version = 1;
Service.PluginInterface.SavePluginConfig(this); Service.PluginInterface.SavePluginConfig(this);
Saved?.Invoke(); Saved?.Invoke();
} }

View File

@ -59,6 +59,7 @@ namespace Pal.Client
pluginInterface.Create<Service>(); pluginInterface.Create<Service>();
Service.Plugin = this; Service.Plugin = this;
Service.Configuration = (Configuration?)pluginInterface.GetPluginConfig() ?? pluginInterface.Create<Configuration>()!; Service.Configuration = (Configuration?)pluginInterface.GetPluginConfig() ?? pluginInterface.Create<Configuration>()!;
Service.Configuration.Migrate();
var agreementWindow = pluginInterface.Create<AgreementWindow>(); var agreementWindow = pluginInterface.Create<AgreementWindow>();
if (agreementWindow is not null) if (agreementWindow is not null)

View File

@ -47,43 +47,30 @@ namespace Pal.Client
} }
var accountClient = new AccountService.AccountServiceClient(_channel); var accountClient = new AccountService.AccountServiceClient(_channel);
#if DEBUG Guid? accountId = Service.Configuration.AccountIds[remoteUrl];
string? accountId = Service.Configuration.DebugAccountId; if (accountId == null)
#else
string? accountId = Service.Configuration.AccountId;
#endif
if (string.IsNullOrEmpty(accountId))
{ {
var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken);
if (createAccountReply.Success) if (createAccountReply.Success)
{ {
accountId = createAccountReply.AccountId; accountId = Guid.Parse(createAccountReply.AccountId);
#if DEBUG Service.Configuration.AccountIds[remoteUrl] = accountId.Value;
Service.Configuration.DebugAccountId = accountId;
#else
Service.Configuration.AccountId = accountId;
#endif
Service.Configuration.Save(); Service.Configuration.Save();
} }
} }
if (string.IsNullOrEmpty(accountId)) if (accountId == null)
return false; return false;
if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now) 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.Success)
{ {
if (_lastLoginReply.Error == LoginError.InvalidAccountId) if (_lastLoginReply.Error == LoginError.InvalidAccountId)
{ {
accountId = null; accountId = null;
#if DEBUG Service.Configuration.AccountIds.Remove(remoteUrl);
Service.Configuration.DebugAccountId = accountId;
#else
Service.Configuration.AccountId = accountId;
#endif
Service.Configuration.Save(); Service.Configuration.Save();
if (retry) if (retry)
return await Connect(cancellationToken, retry: false); return await Connect(cancellationToken, retry: false);