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

rendering
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 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<string, Guid> 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();
}

View File

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

View File

@ -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);