From c66b58d1564fe579c39fc9391f2de32ee2080846 Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Wed, 30 Oct 2024 10:13:22 +0100 Subject: [PATCH] add disable autologon on a per character basis add automatic notes population on empty notes adjust mare command help text --- .../Configurations/MareConfig.cs | 1 + .../Models/Authentication.cs | 1 + .../Factories/PairHandlerFactory.cs | 8 +++++-- .../PlayerData/Handlers/PairHandler.cs | 10 ++++++-- .../Services/CommandManagerService.cs | 15 +++++++++++- MareSynchronos/Services/DalamudUtilService.cs | 1 - .../ServerConfigurationManager.cs | 12 ++++++++++ MareSynchronos/UI/CompactUI.cs | 23 +++++++++++++++---- MareSynchronos/UI/SettingsUi.cs | 15 ++++++++++++ .../WebAPI/SignalR/ApiController.cs | 14 ++++++++++- .../WebAPI/SignalR/Utils/ServerState.cs | 3 ++- 11 files changed, 90 insertions(+), 13 deletions(-) diff --git a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs index 058d8c4..82a8b0f 100644 --- a/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs +++ b/MareSynchronos/MareConfiguration/Configurations/MareConfig.cs @@ -56,6 +56,7 @@ public class MareConfig : IMareConfiguration public bool UseAlternativeFileUpload { get; set; } = false; public bool UseCompactor { get; set; } = false; public bool DebugStopWhining { get; set; } = false; + public bool AutoPopulateEmptyNotesFromCharaName { get; set; } = false; public int Version { get; set; } = 1; public NotificationLocation WarningNotification { get; set; } = NotificationLocation.Both; } \ No newline at end of file diff --git a/MareSynchronos/MareConfiguration/Models/Authentication.cs b/MareSynchronos/MareConfiguration/Models/Authentication.cs index 575d8bd..95e6bdf 100644 --- a/MareSynchronos/MareConfiguration/Models/Authentication.cs +++ b/MareSynchronos/MareConfiguration/Models/Authentication.cs @@ -7,4 +7,5 @@ public record Authentication public uint WorldId { get; set; } = 0; public int SecretKeyIdx { get; set; } = -1; public string? UID { get; set; } + public bool AutoLogin { get; set; } = true; } \ No newline at end of file diff --git a/MareSynchronos/PlayerData/Factories/PairHandlerFactory.cs b/MareSynchronos/PlayerData/Factories/PairHandlerFactory.cs index 74b33ce..000a4b7 100644 --- a/MareSynchronos/PlayerData/Factories/PairHandlerFactory.cs +++ b/MareSynchronos/PlayerData/Factories/PairHandlerFactory.cs @@ -4,6 +4,7 @@ using MareSynchronos.PlayerData.Handlers; using MareSynchronos.PlayerData.Pairs; using MareSynchronos.Services; using MareSynchronos.Services.Mediator; +using MareSynchronos.Services.ServerConfiguration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -20,12 +21,14 @@ public class PairHandlerFactory private readonly ILoggerFactory _loggerFactory; private readonly MareMediator _mareMediator; private readonly PlayerPerformanceService _playerPerformanceService; + private readonly ServerConfigurationManager _serverConfigManager; private readonly PluginWarningNotificationService _pluginWarningNotificationManager; public PairHandlerFactory(ILoggerFactory loggerFactory, GameObjectHandlerFactory gameObjectHandlerFactory, IpcManager ipcManager, FileDownloadManagerFactory fileDownloadManagerFactory, DalamudUtilService dalamudUtilService, PluginWarningNotificationService pluginWarningNotificationManager, IHostApplicationLifetime hostApplicationLifetime, - FileCacheManager fileCacheManager, MareMediator mareMediator, PlayerPerformanceService playerPerformanceService) + FileCacheManager fileCacheManager, MareMediator mareMediator, PlayerPerformanceService playerPerformanceService, + ServerConfigurationManager serverConfigManager) { _loggerFactory = loggerFactory; _gameObjectHandlerFactory = gameObjectHandlerFactory; @@ -37,12 +40,13 @@ public class PairHandlerFactory _fileCacheManager = fileCacheManager; _mareMediator = mareMediator; _playerPerformanceService = playerPerformanceService; + _serverConfigManager = serverConfigManager; } public PairHandler Create(Pair pair) { return new PairHandler(_loggerFactory.CreateLogger(), pair, _gameObjectHandlerFactory, _ipcManager, _fileDownloadManagerFactory.Create(), _pluginWarningNotificationManager, _dalamudUtilService, _hostApplicationLifetime, - _fileCacheManager, _mareMediator, _playerPerformanceService); + _fileCacheManager, _mareMediator, _playerPerformanceService, _serverConfigManager); } } \ No newline at end of file diff --git a/MareSynchronos/PlayerData/Handlers/PairHandler.cs b/MareSynchronos/PlayerData/Handlers/PairHandler.cs index 8e2a9cb..a786250 100644 --- a/MareSynchronos/PlayerData/Handlers/PairHandler.cs +++ b/MareSynchronos/PlayerData/Handlers/PairHandler.cs @@ -6,6 +6,7 @@ using MareSynchronos.PlayerData.Pairs; using MareSynchronos.Services; using MareSynchronos.Services.Events; using MareSynchronos.Services.Mediator; +using MareSynchronos.Services.ServerConfiguration; using MareSynchronos.Utils; using MareSynchronos.WebAPI.Files; using Microsoft.Extensions.Hosting; @@ -27,6 +28,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase private readonly IpcManager _ipcManager; private readonly IHostApplicationLifetime _lifetime; private readonly PlayerPerformanceService _playerPerformanceService; + private readonly ServerConfigurationManager _serverConfigManager; private readonly PluginWarningNotificationService _pluginWarningNotificationManager; private CancellationTokenSource? _applicationCancellationTokenSource = new(); private Guid _applicationId; @@ -47,7 +49,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase PluginWarningNotificationService pluginWarningNotificationManager, DalamudUtilService dalamudUtil, IHostApplicationLifetime lifetime, FileCacheManager fileDbManager, MareMediator mediator, - PlayerPerformanceService playerPerformanceService) : base(logger, mediator) + PlayerPerformanceService playerPerformanceService, + ServerConfigurationManager serverConfigManager) : base(logger, mediator) { Pair = pair; _gameObjectHandlerFactory = gameObjectHandlerFactory; @@ -58,6 +61,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase _lifetime = lifetime; _fileDbManager = fileDbManager; _playerPerformanceService = playerPerformanceService; + _serverConfigManager = serverConfigManager; _penumbraCollection = _ipcManager.Penumbra.CreateTemporaryCollectionAsync(logger, Pair.UserData.UID).ConfigureAwait(false).GetAwaiter().GetResult(); Mediator.Subscribe(this, (_) => FrameworkUpdate()); @@ -451,7 +455,7 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase _applicationTask = ApplyCharacterDataAsync(applicationBase, charaData, updatedData, updateModdedPaths, updateManip, moddedPaths, token); } - private async Task ApplyCharacterDataAsync(Guid applicationBase, CharacterData charaData, Dictionary> updatedData, bool updateModdedPaths, bool updateManip, + private async Task ApplyCharacterDataAsync(Guid applicationBase, CharacterData charaData, Dictionary> updatedData, bool updateModdedPaths, bool updateManip, Dictionary<(string GamePath, string? Hash), string> moddedPaths, CancellationToken token) { try @@ -557,6 +561,8 @@ public sealed class PairHandler : DisposableMediatorSubscriberBase PlayerName = name; _charaHandler = _gameObjectHandlerFactory.Create(ObjectKind.Player, () => _dalamudUtil.GetPlayerCharacterFromCachedTableByIdent(Pair.Ident), isWatched: false).GetAwaiter().GetResult(); + _serverConfigManager.AutoPopulateNoteForUid(Pair.UserData.UID, name); + Mediator.Subscribe(this, async (_) => { if (string.IsNullOrEmpty(_cachedData?.HonorificData)) return; diff --git a/MareSynchronos/Services/CommandManagerService.cs b/MareSynchronos/Services/CommandManagerService.cs index 85631d6..7b5271f 100644 --- a/MareSynchronos/Services/CommandManagerService.cs +++ b/MareSynchronos/Services/CommandManagerService.cs @@ -36,7 +36,13 @@ public sealed class CommandManagerService : IDisposable _mareConfigService = mareConfigService; _commandManager.AddHandler(_commandName, new CommandInfo(OnCommand) { - HelpMessage = "Opens the Mare Synchronos UI" + HelpMessage = "Opens the Mare Synchronos UI" + Environment.NewLine + Environment.NewLine + + "Additionally possible commands:" + Environment.NewLine + + "\t /mare toggle - Disconnects from Mare, if connected. Connects to Mare, if disconnected" + Environment.NewLine + + "\t /mare toggle on|off - Connects or disconnects to Mare respectively" + Environment.NewLine + + "\t /mare gpose - Opens the GPose MCDF import window (only works in GPose)" + Environment.NewLine + + "\t /mare analyze - Opens the Mare Character Data Analysis window" + Environment.NewLine + + "\t /mare settings - Opens the Mare Settings window" }); } @@ -59,6 +65,9 @@ public sealed class CommandManagerService : IDisposable return; } + if (!_mareConfigService.Current.HasValidSetup()) + return; + if (string.Equals(splitArgs[0], "toggle", StringComparison.OrdinalIgnoreCase)) { if (_apiController.ServerState == WebAPI.SignalR.Utils.ServerState.Disconnecting) @@ -109,5 +118,9 @@ public sealed class CommandManagerService : IDisposable { _mediator.Publish(new UiToggleMessage(typeof(DataAnalysisUi))); } + else if (string.Equals(splitArgs[0], "settings", StringComparison.OrdinalIgnoreCase)) + { + _mediator.Publish(new UiToggleMessage(typeof(SettingsUi))); + } } } \ No newline at end of file diff --git a/MareSynchronos/Services/DalamudUtilService.cs b/MareSynchronos/Services/DalamudUtilService.cs index 211a878..d00666a 100644 --- a/MareSynchronos/Services/DalamudUtilService.cs +++ b/MareSynchronos/Services/DalamudUtilService.cs @@ -39,7 +39,6 @@ public class DalamudUtilService : IHostedService, IMediatorSubscriber private readonly List _notUpdatedCharas = []; private bool _sentBetweenAreas = false; - public DalamudUtilService(ILogger logger, IClientState clientState, IObjectTable objectTable, IFramework framework, IGameGui gameGui, ICondition condition, IDataManager gameData, ITargetManager targetManager, BlockedCharacterHandler blockedCharacterHandler, MareMediator mediator, PerformanceCollectorService performanceCollector) diff --git a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs index 0ae7c6e..a61a2a8 100644 --- a/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs +++ b/MareSynchronos/Services/ServerConfiguration/ServerConfigurationManager.cs @@ -17,6 +17,7 @@ public class ServerConfigurationManager { private readonly ServerConfigService _configService; private readonly DalamudUtilService _dalamudUtil; + private readonly MareConfigService _mareConfigService; private readonly ILogger _logger; private readonly MareMediator _mareMediator; private readonly NotesConfigService _notesConfig; @@ -24,6 +25,7 @@ public class ServerConfigurationManager public ServerConfigurationManager(ILogger logger, ServerConfigService configService, ServerTagConfigService serverTagConfig, NotesConfigService notesConfig, DalamudUtilService dalamudUtil, + MareConfigService mareConfigService, MareMediator mareMediator) { _logger = logger; @@ -31,6 +33,7 @@ public class ServerConfigurationManager _serverTagConfig = serverTagConfig; _notesConfig = notesConfig; _dalamudUtil = dalamudUtil; + _mareConfigService = mareConfigService; _mareMediator = mareMediator; EnsureMainExists(); } @@ -438,6 +441,15 @@ public class ServerConfigurationManager _notesConfig.Save(); } + internal void AutoPopulateNoteForUid(string uid, string note) + { + if (!_mareConfigService.Current.AutoPopulateEmptyNotesFromCharaName + || GetNoteForUid(uid) != null) + return; + + SetNoteForUid(uid, note, save: true); + } + private ServerNotesStorage CurrentNotesStorage() { TryCreateCurrentNotesStorage(); diff --git a/MareSynchronos/UI/CompactUI.cs b/MareSynchronos/UI/CompactUI.cs index b149092..150fee6 100644 --- a/MareSynchronos/UI/CompactUI.cs +++ b/MareSynchronos/UI/CompactUI.cs @@ -328,8 +328,9 @@ public class CompactUi : WindowMediatorSubscriberBase { ImGui.SetCursorPosY(ImGui.GetCursorPosY() - ((userSize.Y + textSize.Y) / 2 + shardTextSize.Y) / 2 - ImGui.GetStyle().ItemSpacing.Y + buttonSize.Y / 2); } - var color = UiSharedService.GetBoolColor(!_serverManager.CurrentServer!.FullPause); - var connectedIcon = !_serverManager.CurrentServer.FullPause ? FontAwesomeIcon.Link : FontAwesomeIcon.Unlink; + bool isConnectingOrConnected = _apiController.ServerState is ServerState.Connected or ServerState.Connecting or ServerState.Reconnecting; + var color = UiSharedService.GetBoolColor(!isConnectingOrConnected); + var connectedIcon = isConnectingOrConnected ? FontAwesomeIcon.Unlink : FontAwesomeIcon.Link; ImGui.SameLine(ImGui.GetWindowContentRegionMin().X + UiSharedService.GetWindowContentRegionWidth() - buttonSize.X); if (printShard) @@ -343,13 +344,22 @@ public class CompactUi : WindowMediatorSubscriberBase { if (_uiSharedService.IconButton(connectedIcon)) { - _serverManager.CurrentServer.FullPause = !_serverManager.CurrentServer.FullPause; - _serverManager.Save(); + if (isConnectingOrConnected && !_serverManager.CurrentServer.FullPause) + { + _serverManager.CurrentServer.FullPause = true; + _serverManager.Save(); + } + else if (!isConnectingOrConnected && _serverManager.CurrentServer.FullPause) + { + _serverManager.CurrentServer.FullPause = false; + _serverManager.Save(); + } + _ = _apiController.CreateConnectionsAsync(); } } - UiSharedService.AttachToolTip(!_serverManager.CurrentServer.FullPause ? "Disconnect from " + _serverManager.CurrentServer.ServerName : "Connect to " + _serverManager.CurrentServer.ServerName); + UiSharedService.AttachToolTip(isConnectingOrConnected ? "Disconnect from " + _serverManager.CurrentServer.ServerName : "Connect to " + _serverManager.CurrentServer.ServerName); } } @@ -611,6 +621,7 @@ public class CompactUi : WindowMediatorSubscriberBase ServerState.MultiChara => "Your Character Configuration has multiple characters configured with same name and world. You will not be able to connect until you fix this issue. Remove the duplicates from the configuration in Settings -> Service Settings -> Character Management and reconnect manually after.", ServerState.OAuthMisconfigured => "OAuth2 is enabled but not fully configured, verify in the Settings -> Service Settings that you have OAuth2 connected and, importantly, a UID assigned to your current character.", ServerState.OAuthLoginTokenStale => "Your OAuth2 login token is stale and cannot be used to renew. Go to the Settings -> Service Settings and unlink then relink your OAuth2 configuration.", + ServerState.NoAutoLogon => "This character has automatic login into Mare disabled. Press the connect button to connect to Mare.", _ => string.Empty }; } @@ -632,6 +643,7 @@ public class CompactUi : WindowMediatorSubscriberBase ServerState.MultiChara => ImGuiColors.DalamudYellow, ServerState.OAuthMisconfigured => ImGuiColors.DalamudRed, ServerState.OAuthLoginTokenStale => ImGuiColors.DalamudRed, + ServerState.NoAutoLogon => ImGuiColors.DalamudYellow, _ => ImGuiColors.DalamudRed }; } @@ -652,6 +664,7 @@ public class CompactUi : WindowMediatorSubscriberBase ServerState.MultiChara => "Duplicate Characters", ServerState.OAuthMisconfigured => "Misconfigured OAuth2", ServerState.OAuthLoginTokenStale => "Stale OAuth2", + ServerState.NoAutoLogon => "Auto Login disabled", ServerState.Connected => _apiController.DisplayName, _ => string.Empty }; diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index b570f27..c4ca949 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -759,6 +759,14 @@ public class SettingsUi : WindowMediatorSubscriberBase } _uiShared.DrawHelpText("This will open a popup that allows you to set the notes for a user after successfully adding them to your individual pairs."); + var autoPopulateNotes = _configService.Current.AutoPopulateEmptyNotesFromCharaName; + if (ImGui.Checkbox("Automatically populate notes using player names", ref autoPopulateNotes)) + { + _configService.Current.AutoPopulateEmptyNotesFromCharaName = autoPopulateNotes; + _configService.Save(); + } + _uiShared.DrawHelpText("This will automatically populate user notes using the first encountered player name if the note was not set prior"); + ImGui.Separator(); _uiShared.BigText("UI"); var showNameInsteadOfNotes = _configService.Current.ShowCharacterNameInsteadOfNotesForVisible; @@ -1369,6 +1377,13 @@ public class SettingsUi : WindowMediatorSubscriberBase { _uiShared.DrawUIDComboForAuthentication(i, item, selectedServer.ServerUri, _logger); } + bool isAutoLogin = item.AutoLogin; + if (ImGui.Checkbox("Automatically login to Mare", ref isAutoLogin)) + { + item.AutoLogin = isAutoLogin; + _serverConfigurationManager.Save(); + } + _uiShared.DrawHelpText("When enabled and logging into this character in XIV, Mare will automatically connect to the current service."); if (_uiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete Character") && UiSharedService.CtrlPressed()) _serverConfigurationManager.RemoveCharacterFromServer(idx, item); UiSharedService.AttachToolTip("Hold CTRL to delete this entry."); diff --git a/MareSynchronos/WebAPI/SignalR/ApiController.cs b/MareSynchronos/WebAPI/SignalR/ApiController.cs index b4ef28e..7f3ac77 100644 --- a/MareSynchronos/WebAPI/SignalR/ApiController.cs +++ b/MareSynchronos/WebAPI/SignalR/ApiController.cs @@ -371,7 +371,19 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM private void DalamudUtilOnLogIn() { - _ = Task.Run(() => CreateConnectionsAsync()); + var charaName = _dalamudUtil.GetPlayerNameAsync().GetAwaiter().GetResult(); + var worldId = _dalamudUtil.GetHomeWorldIdAsync().GetAwaiter().GetResult(); + var auth = _serverManager.CurrentServer.Authentications.Find(f => string.Equals(f.CharacterName, charaName, StringComparison.Ordinal) && f.WorldId == worldId); + if (auth?.AutoLogin ?? false) + { + Logger.LogInformation("Logging into {chara}", charaName); + _ = Task.Run(CreateConnectionsAsync); + } + else + { + Logger.LogInformation("Not logging into {chara}, auto login disabled", charaName); + _ = Task.Run(async () => await StopConnectionAsync(ServerState.NoAutoLogon).ConfigureAwait(false)); + } } private void DalamudUtilOnLogOut() diff --git a/MareSynchronos/WebAPI/SignalR/Utils/ServerState.cs b/MareSynchronos/WebAPI/SignalR/Utils/ServerState.cs index a5e654f..2520097 100644 --- a/MareSynchronos/WebAPI/SignalR/Utils/ServerState.cs +++ b/MareSynchronos/WebAPI/SignalR/Utils/ServerState.cs @@ -14,5 +14,6 @@ public enum ServerState NoSecretKey, MultiChara, OAuthMisconfigured, - OAuthLoginTokenStale + OAuthLoginTokenStale, + NoAutoLogon } \ No newline at end of file