diff --git a/MareSynchronos/Plugin.cs b/MareSynchronos/Plugin.cs index 138dacd..ac6e3fd 100644 --- a/MareSynchronos/Plugin.cs +++ b/MareSynchronos/Plugin.cs @@ -91,30 +91,11 @@ public sealed class Plugin : IDalamudPlugin collection.AddSingleton((s) => new ServerTagConfigService(pluginInterface.ConfigDirectory.FullName)); collection.AddSingleton((s) => new TransientConfigService(pluginInterface.ConfigDirectory.FullName)); collection.AddSingleton((s) => new ConfigurationMigrator(s.GetRequiredService>(), pluginInterface)); - collection.AddSingleton((s) => new HubFactory(s.GetRequiredService>(), s.GetRequiredService(), - s.GetRequiredService(), s.GetRequiredService(), - s.GetRequiredService(), pluginLog)); - - // func factory method singletons - collection.AddSingleton(s => - new Func((pair) => - new StandaloneProfileUi(s.GetRequiredService>(), - s.GetRequiredService(), - s.GetRequiredService(), - s.GetRequiredService(), - s.GetRequiredService(), - s.GetRequiredService(), pair))); - collection.AddSingleton(s => - new Func((dto) => - new SyncshellAdminUI(s.GetRequiredService>(), - s.GetRequiredService(), - dto, - s.GetRequiredService(), - s.GetRequiredService(), - s.GetRequiredService()))); + collection.AddSingleton(); // add scoped services collection.AddScoped(); + collection.AddScoped(); collection.AddScoped(); collection.AddScoped(); collection.AddScoped(); @@ -135,10 +116,9 @@ public sealed class Plugin : IDalamudPlugin collection.AddScoped(); collection.AddScoped(); collection.AddScoped(); - collection.AddScoped((s) => new UiService(s.GetRequiredService>(), pluginInterface, s.GetRequiredService(), - s.GetRequiredService(), s.GetServices(), - s.GetRequiredService>(), - s.GetRequiredService>(), + collection.AddScoped((s) => new UiService(s.GetRequiredService>(), pluginInterface.UiBuilder, s.GetRequiredService(), + s.GetRequiredService(), s.GetServices(), + s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService())); collection.AddScoped((s) => new CommandManagerService(commandManager, s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), s.GetRequiredService(), diff --git a/MareSynchronos/Services/UiFactory.cs b/MareSynchronos/Services/UiFactory.cs new file mode 100644 index 0000000..8d611b2 --- /dev/null +++ b/MareSynchronos/Services/UiFactory.cs @@ -0,0 +1,46 @@ +using MareSynchronos.API.Dto.Group; +using MareSynchronos.PlayerData.Pairs; +using MareSynchronos.Services.Mediator; +using MareSynchronos.Services.ServerConfiguration; +using MareSynchronos.UI; +using MareSynchronos.UI.Components.Popup; +using MareSynchronos.WebAPI; +using Microsoft.Extensions.Logging; + +namespace MareSynchronos.Services; + +public class UiFactory +{ + private readonly ILoggerFactory _loggerFactory; + private readonly MareMediator _mareMediator; + private readonly ApiController _apiController; + private readonly UiSharedService _uiSharedService; + private readonly PairManager _pairManager; + private readonly ServerConfigurationManager _serverConfigManager; + private readonly MareProfileManager _mareProfileManager; + + public UiFactory(ILoggerFactory loggerFactory, MareMediator mareMediator, ApiController apiController, + UiSharedService uiSharedService, PairManager pairManager, ServerConfigurationManager serverConfigManager, + MareProfileManager mareProfileManager) + { + _loggerFactory = loggerFactory; + _mareMediator = mareMediator; + _apiController = apiController; + _uiSharedService = uiSharedService; + _pairManager = pairManager; + _serverConfigManager = serverConfigManager; + _mareProfileManager = mareProfileManager; + } + + public SyncshellAdminUI CreateSyncshellAdminUi(GroupFullInfoDto dto) + { + return new SyncshellAdminUI(_loggerFactory.CreateLogger(), _mareMediator, + _apiController, _uiSharedService, _pairManager, dto); + } + + public StandaloneProfileUi CreateStandaloneProfileUi(Pair pair) + { + return new StandaloneProfileUi(_loggerFactory.CreateLogger(), _mareMediator, + _uiSharedService, _serverConfigManager, _mareProfileManager, _pairManager, pair); + } +} diff --git a/MareSynchronos/Services/UiService.cs b/MareSynchronos/Services/UiService.cs index eb72605..a8151e5 100644 --- a/MareSynchronos/Services/UiService.cs +++ b/MareSynchronos/Services/UiService.cs @@ -1,9 +1,7 @@ -using Dalamud.Interface.ImGuiFileDialog; +using Dalamud.Interface; +using Dalamud.Interface.ImGuiFileDialog; using Dalamud.Interface.Windowing; -using Dalamud.Plugin; -using MareSynchronos.API.Dto.Group; using MareSynchronos.MareConfiguration; -using MareSynchronos.PlayerData.Pairs; using MareSynchronos.Services.Mediator; using MareSynchronos.UI; using MareSynchronos.UI.Components.Popup; @@ -14,32 +12,31 @@ namespace MareSynchronos.Services; public sealed class UiService : DisposableMediatorSubscriberBase { private readonly List _createdWindows = []; - private readonly DalamudPluginInterface _dalamudPluginInterface; + private readonly UiBuilder _uiBuilder; private readonly FileDialogManager _fileDialogManager; private readonly ILogger _logger; private readonly MareConfigService _mareConfigService; private readonly WindowSystem _windowSystem; - private readonly Func _syncshellAdminUiFactory; + private readonly UiFactory _uiFactory; - public UiService(ILogger logger, DalamudPluginInterface dalamudPluginInterface, + public UiService(ILogger logger, UiBuilder uiBuilder, MareConfigService mareConfigService, WindowSystem windowSystem, IEnumerable windows, - Func standaloneProfileUiFactory, - Func syncshellAdminUiFactory, - FileDialogManager fileDialogManager, MareMediator mareMediator) : base(logger, mareMediator) + UiFactory uiFactory, FileDialogManager fileDialogManager, + MareMediator mareMediator) : base(logger, mareMediator) { _logger = logger; _logger.LogTrace("Creating {type}", GetType().Name); - _dalamudPluginInterface = dalamudPluginInterface; + _uiBuilder = uiBuilder; _mareConfigService = mareConfigService; _windowSystem = windowSystem; - _syncshellAdminUiFactory = syncshellAdminUiFactory; + _uiFactory = uiFactory; _fileDialogManager = fileDialogManager; - _dalamudPluginInterface.UiBuilder.DisableGposeUiHide = true; - _dalamudPluginInterface.UiBuilder.Draw += Draw; - _dalamudPluginInterface.UiBuilder.OpenConfigUi += ToggleUi; - _dalamudPluginInterface.UiBuilder.OpenMainUi += ToggleMainUi; + _uiBuilder.DisableGposeUiHide = true; + _uiBuilder.Draw += Draw; + _uiBuilder.OpenConfigUi += ToggleUi; + _uiBuilder.OpenMainUi += ToggleMainUi; foreach (var window in windows) { @@ -51,7 +48,7 @@ public sealed class UiService : DisposableMediatorSubscriberBase if (!_createdWindows.Exists(p => p is StandaloneProfileUi ui && string.Equals(ui.Pair.UserData.AliasOrUID, msg.Pair.UserData.AliasOrUID, StringComparison.Ordinal))) { - var window = standaloneProfileUiFactory(msg.Pair); + var window = _uiFactory.CreateStandaloneProfileUi(msg.Pair); _createdWindows.Add(window); _windowSystem.AddWindow(window); } @@ -62,7 +59,7 @@ public sealed class UiService : DisposableMediatorSubscriberBase if (!_createdWindows.Exists(p => p is SyncshellAdminUI ui && string.Equals(ui.GroupFullInfo.GID, msg.GroupInfo.GID, StringComparison.Ordinal))) { - var window = _syncshellAdminUiFactory(msg.GroupInfo); + var window = _uiFactory.CreateSyncshellAdminUi(msg.GroupInfo); _createdWindows.Add(window); _windowSystem.AddWindow(window); } @@ -105,9 +102,9 @@ public sealed class UiService : DisposableMediatorSubscriberBase window.Dispose(); } - _dalamudPluginInterface.UiBuilder.Draw -= Draw; - _dalamudPluginInterface.UiBuilder.OpenConfigUi -= ToggleUi; - _dalamudPluginInterface.UiBuilder.OpenMainUi -= ToggleMainUi; + _uiBuilder.Draw -= Draw; + _uiBuilder.OpenConfigUi -= ToggleUi; + _uiBuilder.OpenMainUi -= ToggleMainUi; } private void Draw() diff --git a/MareSynchronos/UI/CompactUI.cs b/MareSynchronos/UI/CompactUI.cs index cd0a177..cf73dac 100644 --- a/MareSynchronos/UI/CompactUI.cs +++ b/MareSynchronos/UI/CompactUI.cs @@ -115,21 +115,22 @@ public class CompactUi : WindowMediatorSubscriberBase $"It is highly recommended to keep Mare Synchronos up to date. Open /xlplugins and update the plugin.", ImGuiColors.DalamudRed); } - UiSharedService.DrawWithID("header", DrawUIDHeader); + using (ImRaii.PushId("header")) DrawUIDHeader(); ImGui.Separator(); - UiSharedService.DrawWithID("serverstatus", DrawServerStatus); + using (ImRaii.PushId("serverstatus")) DrawServerStatus(); ImGui.Separator(); if (_apiController.ServerState is ServerState.Connected) { - UiSharedService.DrawWithID("global-topmenu", () => _tabMenu.Draw()); - UiSharedService.DrawWithID("pairlist", DrawPairList); + using (ImRaii.PushId("global-topmenu")) _tabMenu.Draw(); + using (ImRaii.PushId("pairlist")) DrawPairs(); + TransferPartHeight = ImGui.GetCursorPosY(); ImGui.Separator(); - UiSharedService.DrawWithID("transfers", DrawTransfers); + using (ImRaii.PushId("transfers")) DrawTransfers(); TransferPartHeight = ImGui.GetCursorPosY() - TransferPartHeight - ImGui.GetTextLineHeight(); - UiSharedService.DrawWithID("group-user-popup", () => _selectPairsForGroupUi.Draw(_pairManager.DirectPairs)); - UiSharedService.DrawWithID("grouping-popup", () => _selectGroupForPairUi.Draw()); + using (ImRaii.PushId("group-user-popup")) _selectPairsForGroupUi.Draw(_pairManager.DirectPairs); + using (ImRaii.PushId("grouping-popup")) _selectGroupForPairUi.Draw(); } if (_configService.Current.OpenPopupOnAdd && _pairManager.LastAddedUser != null) @@ -173,12 +174,6 @@ public class CompactUi : WindowMediatorSubscriberBase } } - private void DrawPairList() - { - UiSharedService.DrawWithID("pairs", DrawPairs); - TransferPartHeight = ImGui.GetCursorPosY(); - } - private void DrawPairs() { var ySize = TransferPartHeight == 0 @@ -277,14 +272,16 @@ public class CompactUi : WindowMediatorSubscriberBase if (_apiController.ServerState is not (ServerState.Reconnecting or ServerState.Disconnecting)) { - ImGui.PushStyleColor(ImGuiCol.Text, color); - if (ImGuiComponents.IconButton(connectedIcon)) + using (ImRaii.PushColor(ImGuiCol.Text, color)) { - _serverManager.CurrentServer.FullPause = !_serverManager.CurrentServer.FullPause; - _serverManager.Save(); - _ = _apiController.CreateConnections(); + if (ImGuiComponents.IconButton(connectedIcon)) + { + _serverManager.CurrentServer.FullPause = !_serverManager.CurrentServer.FullPause; + _serverManager.Save(); + _ = _apiController.CreateConnections(); + } } - ImGui.PopStyleColor(); + UiSharedService.AttachToolTip(!_serverManager.CurrentServer.FullPause ? "Disconnect from " + _serverManager.CurrentServer.ServerName : "Connect to " + _serverManager.CurrentServer.ServerName); } } @@ -292,9 +289,7 @@ public class CompactUi : WindowMediatorSubscriberBase private void DrawTransfers() { var currentUploads = _fileTransferManager.CurrentUploads.ToList(); - ImGui.PushFont(UiBuilder.IconFont); - ImGui.TextUnformatted(FontAwesomeIcon.Upload.ToIconString()); - ImGui.PopFont(); + UiSharedService.FontText(FontAwesomeIcon.Upload.ToIconString(), UiBuilder.IconFont); ImGui.SameLine(35 * ImGuiHelpers.GlobalScale); if (currentUploads.Any()) @@ -317,9 +312,7 @@ public class CompactUi : WindowMediatorSubscriberBase } var currentDownloads = _currentDownloads.SelectMany(d => d.Value.Values).ToList(); - ImGui.PushFont(UiBuilder.IconFont); - ImGui.TextUnformatted(FontAwesomeIcon.Download.ToIconString()); - ImGui.PopFont(); + UiSharedService.FontText(FontAwesomeIcon.Download.ToIconString(), UiBuilder.IconFont); ImGui.SameLine(35 * ImGuiHelpers.GlobalScale); if (currentDownloads.Any()) diff --git a/MareSynchronos/UI/Components/DrawFolderBase.cs b/MareSynchronos/UI/Components/DrawFolderBase.cs index c3bfefe..c2a487f 100644 --- a/MareSynchronos/UI/Components/DrawFolderBase.cs +++ b/MareSynchronos/UI/Components/DrawFolderBase.cs @@ -99,10 +99,7 @@ public abstract class DrawFolderBase : IDrawFolder } if (ImGui.BeginPopup("User Flyout Menu")) { - UiSharedService.DrawWithID($"buttons-{_id}", () => - { - DrawMenu(_menuWidth); - }); + using (ImRaii.PushId($"buttons-{_id}")) DrawMenu(_menuWidth); _menuWidth = ImGui.GetWindowContentRegionMax().X - ImGui.GetWindowContentRegionMin().X; ImGui.EndPopup(); } diff --git a/MareSynchronos/UI/Components/DrawUserPair.cs b/MareSynchronos/UI/Components/DrawUserPair.cs index 2cf3be8..8752596 100644 --- a/MareSynchronos/UI/Components/DrawUserPair.cs +++ b/MareSynchronos/UI/Components/DrawUserPair.cs @@ -408,7 +408,7 @@ public class DrawUserPair } if (ImGui.BeginPopup("User Flyout Menu")) { - UiSharedService.DrawWithID($"buttons-{_pair.UserData.UID}", () => + using (ImRaii.PushId($"buttons-{_pair.UserData.UID}")) { ImGui.TextUnformatted("Common Pair Functions"); DrawCommonClientMenu(); @@ -418,7 +418,7 @@ public class DrawUserPair { _menuRenderWidth = ImGui.GetWindowContentRegionMax().X - ImGui.GetWindowContentRegionMin().X; } - }); + } ImGui.EndPopup(); } diff --git a/MareSynchronos/UI/Components/SelectTagForPairUi.cs b/MareSynchronos/UI/Components/SelectTagForPairUi.cs index 8157e7b..4e98da0 100644 --- a/MareSynchronos/UI/Components/SelectTagForPairUi.cs +++ b/MareSynchronos/UI/Components/SelectTagForPairUi.cs @@ -1,6 +1,7 @@ using Dalamud.Interface; using Dalamud.Interface.Components; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using Dalamud.Utility; using ImGuiNET; using MareSynchronos.PlayerData.Pairs; @@ -66,7 +67,7 @@ public class SelectTagForPairUi { foreach (var tag in tags) { - UiSharedService.DrawWithID($"groups-pair-{_pair.UserData.UID}-{tag}", () => DrawGroupName(_pair, tag)); + using (ImRaii.PushId($"groups-pair-{_pair.UserData.UID}-{tag}")) DrawGroupName(_pair, tag); } ImGui.EndChild(); } diff --git a/MareSynchronos/UI/Handlers/IdDisplayHandler.cs b/MareSynchronos/UI/Handlers/IdDisplayHandler.cs index 97484bf..8d95cad 100644 --- a/MareSynchronos/UI/Handlers/IdDisplayHandler.cs +++ b/MareSynchronos/UI/Handlers/IdDisplayHandler.cs @@ -95,9 +95,8 @@ public class IdDisplayHandler { ImGui.AlignTextToFramePadding(); - if (textIsUid) ImGui.PushFont(UiBuilder.MonoFont); - ImGui.TextUnformatted(playerText); - if (textIsUid) ImGui.PopFont(); + using (ImRaii.PushFont(UiBuilder.MonoFont, textIsUid)) ImGui.TextUnformatted(playerText); + if (ImGui.IsItemHovered()) { if (!string.Equals(_lastMouseOverUid, id)) diff --git a/MareSynchronos/UI/IntroUI.cs b/MareSynchronos/UI/IntroUI.cs index 696e093..8f64ada 100644 --- a/MareSynchronos/UI/IntroUI.cs +++ b/MareSynchronos/UI/IntroUI.cs @@ -1,5 +1,6 @@ using Dalamud.Interface.Colors; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using Dalamud.Utility; using ImGuiNET; using MareSynchronos.FileCache; @@ -62,9 +63,7 @@ public class IntroUi : WindowMediatorSubscriberBase if (!_configService.Current.AcceptedAgreement && !_readFirstPage) { - if (_uiShared.UidFontBuilt) ImGui.PushFont(_uiShared.UidFont); - ImGui.TextUnformatted("Welcome to Mare Synchronos"); - if (_uiShared.UidFontBuilt) ImGui.PopFont(); + _uiShared.BigText("Welcome to Mare Synchronos"); ImGui.Separator(); UiSharedService.TextWrapped("Mare Synchronos is a plugin that will replicate your full current character state including all Penumbra mods to other paired Mare Synchronos users. " + "Note that you will have to have Penumbra as well as Glamourer installed to use this plugin."); diff --git a/MareSynchronos/UI/PopoutProfileUi.cs b/MareSynchronos/UI/PopoutProfileUi.cs index a95ea87..4d9c85c 100644 --- a/MareSynchronos/UI/PopoutProfileUi.cs +++ b/MareSynchronos/UI/PopoutProfileUi.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Internal; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using ImGuiNET; using MareSynchronos.API.Data.Extensions; using MareSynchronos.MareConfiguration; @@ -110,9 +111,9 @@ public class PopoutProfileUi : WindowMediatorSubscriberBase var rectMin = drawList.GetClipRectMin(); var rectMax = drawList.GetClipRectMax(); - if (_uiSharedService.UidFontBuilt) ImGui.PushFont(_uiSharedService.UidFont); - UiSharedService.ColorText(_pair.UserData.AliasOrUID, ImGuiColors.HealerGreen); - if (_uiSharedService.UidFontBuilt) ImGui.PopFont(); + using (ImRaii.PushFont(_uiSharedService.UidFont, _uiSharedService.UidFontBuilt)) + UiSharedService.ColorText(_pair.UserData.AliasOrUID, ImGuiColors.HealerGreen); + ImGui.Dummy(new(spacing.Y, spacing.Y)); var textPos = ImGui.GetCursorPosY(); ImGui.Separator(); diff --git a/MareSynchronos/UI/SettingsUi.cs b/MareSynchronos/UI/SettingsUi.cs index eeda8ae..7da8a68 100644 --- a/MareSynchronos/UI/SettingsUi.cs +++ b/MareSynchronos/UI/SettingsUi.cs @@ -20,6 +20,7 @@ using MareSynchronos.WebAPI.Files.Models; using MareSynchronos.WebAPI.SignalR.Utils; using Microsoft.Extensions.Logging; using System.Collections.Concurrent; +using System.Globalization; using System.Numerics; using System.Text.Json; @@ -249,14 +250,14 @@ public class SettingsUi : WindowMediatorSubscriberBase foreach (var transfer in _fileTransferManager.CurrentUploads.ToArray()) { var color = UiSharedService.UploadColor((transfer.Transferred, transfer.Total)); - ImGui.PushStyleColor(ImGuiCol.Text, color); + var col = ImRaii.PushColor(ImGuiCol.Text, color); ImGui.TableNextColumn(); ImGui.TextUnformatted(transfer.Hash); ImGui.TableNextColumn(); ImGui.TextUnformatted(UiSharedService.ByteToString(transfer.Transferred)); ImGui.TableNextColumn(); ImGui.TextUnformatted(UiSharedService.ByteToString(transfer.Total)); - ImGui.PopStyleColor(); + col.Dispose(); ImGui.TableNextRow(); } @@ -282,13 +283,13 @@ public class SettingsUi : WindowMediatorSubscriberBase ImGui.TextUnformatted(userName); ImGui.TableNextColumn(); ImGui.TextUnformatted(entry.Key); - ImGui.PushStyleColor(ImGuiCol.Text, color); + var col = ImRaii.PushColor(ImGuiCol.Text, color); ImGui.TableNextColumn(); ImGui.TextUnformatted(entry.Value.TransferredFiles + "/" + entry.Value.TotalFiles); ImGui.TableNextColumn(); ImGui.TextUnformatted(UiSharedService.ByteToString(entry.Value.TransferredBytes) + "/" + UiSharedService.ByteToString(entry.Value.TotalBytes)); ImGui.TableNextColumn(); - ImGui.PopStyleColor(); + col.Dispose(); ImGui.TableNextRow(); } } @@ -352,7 +353,7 @@ public class SettingsUi : WindowMediatorSubscriberBase } UiSharedService.DrawHelpText("Enabling this can incur a (slight) performance impact. Enabling this for extended periods of time is not recommended."); - if (!logPerformance) ImGui.BeginDisabled(); + using var disabled = ImRaii.Disabled(!logPerformance); if (UiSharedService.IconTextButton(FontAwesomeIcon.StickyNote, "Print Performance Stats to /xllog")) { _performanceCollector.PrintPerformanceStats(); @@ -362,7 +363,6 @@ public class SettingsUi : WindowMediatorSubscriberBase { _performanceCollector.PrintPerformanceStats(60); } - if (!logPerformance) ImGui.EndDisabled(); } private void DrawFileStorageSettings() @@ -763,7 +763,7 @@ public class SettingsUi : WindowMediatorSubscriberBase } UiSharedService.DrawHelpText("Enabling this will show a small notification (type: Info) in the bottom right corner when pairs go online."); - if (!onlineNotifs) ImGui.BeginDisabled(); + using var disabled = ImRaii.Disabled(!onlineNotifs); if (ImGui.Checkbox("Notify only for individual pairs", ref onlineNotifsPairsOnly)) { _configService.Current.ShowOnlineNotificationsOnlyForIndividualPairs = onlineNotifsPairsOnly; @@ -776,7 +776,6 @@ public class SettingsUi : WindowMediatorSubscriberBase _configService.Save(); } UiSharedService.DrawHelpText("Enabling this will only show online notifications (type: Info) for pairs where you have set an individual note."); - if (!onlineNotifs) ImGui.EndDisabled(); } private void DrawServerConfiguration() @@ -885,59 +884,58 @@ public class SettingsUi : WindowMediatorSubscriberBase int i = 0; foreach (var item in selectedServer.Authentications.ToList()) { - UiSharedService.DrawWithID("selectedChara" + i, () => + using var charaId = ImRaii.PushId("selectedChara" + i); + + var worldIdx = (ushort)item.WorldId; + var data = _uiShared.WorldData.OrderBy(u => u.Value, StringComparer.Ordinal).ToDictionary(k => k.Key, k => k.Value); + if (!data.TryGetValue(worldIdx, out string? worldPreview)) { - var worldIdx = (ushort)item.WorldId; - var data = _uiShared.WorldData.OrderBy(u => u.Value, StringComparer.Ordinal).ToDictionary(k => k.Key, k => k.Value); - if (!data.TryGetValue(worldIdx, out string? worldPreview)) + worldPreview = data.First().Value; + } + + var secretKeyIdx = item.SecretKeyIdx; + var keys = selectedServer.SecretKeys; + if (!keys.TryGetValue(secretKeyIdx, out var secretKey)) + { + secretKey = new(); + } + var friendlyName = secretKey.FriendlyName; + + if (ImGui.TreeNode($"chara", $"Character: {item.CharacterName}, World: {worldPreview}, Secret Key: {friendlyName}")) + { + var charaName = item.CharacterName; + if (ImGui.InputText("Character Name", ref charaName, 64)) { - worldPreview = data.First().Value; + item.CharacterName = charaName; + _serverConfigurationManager.Save(); } - var secretKeyIdx = item.SecretKeyIdx; - var keys = selectedServer.SecretKeys; - if (!keys.TryGetValue(secretKeyIdx, out var secretKey)) - { - secretKey = new(); - } - var friendlyName = secretKey.FriendlyName; - - if (ImGui.TreeNode($"chara", $"Character: {item.CharacterName}, World: {worldPreview}, Secret Key: {friendlyName}")) - { - var charaName = item.CharacterName; - if (ImGui.InputText("Character Name", ref charaName, 64)) + _uiShared.DrawCombo("World##" + item.CharacterName + i, data, (w) => w.Value, + (w) => { - item.CharacterName = charaName; - _serverConfigurationManager.Save(); - } - - _uiShared.DrawCombo("World##" + item.CharacterName + i, data, (w) => w.Value, - (w) => + if (item.WorldId != w.Key) { - if (item.WorldId != w.Key) - { - item.WorldId = w.Key; - _serverConfigurationManager.Save(); - } - }, EqualityComparer>.Default.Equals(data.FirstOrDefault(f => f.Key == worldIdx), default) ? data.First() : data.First(f => f.Key == worldIdx)); + item.WorldId = w.Key; + _serverConfigurationManager.Save(); + } + }, EqualityComparer>.Default.Equals(data.FirstOrDefault(f => f.Key == worldIdx), default) ? data.First() : data.First(f => f.Key == worldIdx)); - _uiShared.DrawCombo("Secret Key##" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName, - (w) => + _uiShared.DrawCombo("Secret Key##" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName, + (w) => + { + if (w.Key != item.SecretKeyIdx) { - if (w.Key != item.SecretKeyIdx) - { - item.SecretKeyIdx = w.Key; - _serverConfigurationManager.Save(); - } - }, EqualityComparer>.Default.Equals(keys.FirstOrDefault(f => f.Key == item.SecretKeyIdx), default) ? keys.First() : keys.First(f => f.Key == item.SecretKeyIdx)); + item.SecretKeyIdx = w.Key; + _serverConfigurationManager.Save(); + } + }, EqualityComparer>.Default.Equals(keys.FirstOrDefault(f => f.Key == item.SecretKeyIdx), default) ? keys.First() : keys.First(f => f.Key == item.SecretKeyIdx)); - if (UiSharedService.IconTextButton(FontAwesomeIcon.Trash, "Delete Character") && UiSharedService.CtrlPressed()) - _serverConfigurationManager.RemoveCharacterFromServer(idx, item); - UiSharedService.AttachToolTip("Hold CTRL to delete this entry."); + if (UiSharedService.IconTextButton(FontAwesomeIcon.Trash, "Delete Character") && UiSharedService.CtrlPressed()) + _serverConfigurationManager.RemoveCharacterFromServer(idx, item); + UiSharedService.AttachToolTip("Hold CTRL to delete this entry."); - ImGui.TreePop(); - } - }); + ImGui.TreePop(); + } i++; } @@ -970,34 +968,32 @@ public class SettingsUi : WindowMediatorSubscriberBase { foreach (var item in selectedServer.SecretKeys.ToList()) { - UiSharedService.DrawWithID("key" + item.Key, () => + using var id = ImRaii.PushId("key" + item.Key); + var friendlyName = item.Value.FriendlyName; + if (ImGui.InputText("Secret Key Display Name", ref friendlyName, 255)) { - var friendlyName = item.Value.FriendlyName; - if (ImGui.InputText("Secret Key Display Name", ref friendlyName, 255)) + item.Value.FriendlyName = friendlyName; + _serverConfigurationManager.Save(); + } + var key = item.Value.Key; + if (ImGui.InputText("Secret Key", ref key, 64)) + { + item.Value.Key = key; + _serverConfigurationManager.Save(); + } + if (!selectedServer.Authentications.Exists(p => p.SecretKeyIdx == item.Key)) + { + if (UiSharedService.IconTextButton(FontAwesomeIcon.Trash, "Delete Secret Key") && UiSharedService.CtrlPressed()) { - item.Value.FriendlyName = friendlyName; + selectedServer.SecretKeys.Remove(item.Key); _serverConfigurationManager.Save(); } - var key = item.Value.Key; - if (ImGui.InputText("Secret Key", ref key, 64)) - { - item.Value.Key = key; - _serverConfigurationManager.Save(); - } - if (!selectedServer.Authentications.Exists(p => p.SecretKeyIdx == item.Key)) - { - if (UiSharedService.IconTextButton(FontAwesomeIcon.Trash, "Delete Secret Key") && UiSharedService.CtrlPressed()) - { - selectedServer.SecretKeys.Remove(item.Key); - _serverConfigurationManager.Save(); - } - UiSharedService.AttachToolTip("Hold CTRL to delete this secret key entry"); - } - else - { - UiSharedService.ColorTextWrapped("This key is in use and cannot be deleted", ImGuiColors.DalamudYellow); - } - }); + UiSharedService.AttachToolTip("Hold CTRL to delete this secret key entry"); + } + else + { + UiSharedService.ColorTextWrapped("This key is in use and cannot be deleted", ImGuiColors.DalamudYellow); + } if (item.Key != selectedServer.SecretKeys.Keys.LastOrDefault()) ImGui.Separator(); @@ -1136,7 +1132,21 @@ public class SettingsUi : WindowMediatorSubscriberBase private void DrawSettingsContent() { - _uiShared.PrintServerState(); + if (_apiController.ServerState is ServerState.Connected) + { + ImGui.TextUnformatted("Service " + _serverConfigurationManager.CurrentServer!.ServerName + ":"); + ImGui.SameLine(); + ImGui.TextColored(ImGuiColors.ParsedGreen, "Available"); + ImGui.SameLine(); + ImGui.TextUnformatted("("); + ImGui.SameLine(); + ImGui.TextColored(ImGuiColors.ParsedGreen, _apiController.OnlineUsers.ToString(CultureInfo.InvariantCulture)); + ImGui.SameLine(); + ImGui.TextUnformatted("Users Online"); + ImGui.SameLine(); + ImGui.TextUnformatted(")"); + } + ImGui.AlignTextToFramePadding(); ImGui.TextUnformatted("Community and Support:"); ImGui.SameLine(); diff --git a/MareSynchronos/UI/StandaloneProfileUi.cs b/MareSynchronos/UI/StandaloneProfileUi.cs index 6991e3f..e84abb5 100644 --- a/MareSynchronos/UI/StandaloneProfileUi.cs +++ b/MareSynchronos/UI/StandaloneProfileUi.cs @@ -2,6 +2,7 @@ using Dalamud.Interface.Internal; using Dalamud.Interface.Utility; +using Dalamud.Interface.Utility.Raii; using ImGuiNET; using MareSynchronos.API.Data.Extensions; using MareSynchronos.PlayerData.Pairs; @@ -76,9 +77,9 @@ public class StandaloneProfileUi : WindowMediatorSubscriberBase var rectMax = drawList.GetClipRectMax(); var headerSize = ImGui.GetCursorPosY() - ImGui.GetStyle().WindowPadding.Y; - if (_uiSharedService.UidFontBuilt) ImGui.PushFont(_uiSharedService.UidFont); - UiSharedService.ColorText(Pair.UserData.AliasOrUID, ImGuiColors.HealerGreen); - if (_uiSharedService.UidFontBuilt) ImGui.PopFont(); + using (ImRaii.PushFont(_uiSharedService.UidFont, _uiSharedService.UidFontBuilt)) + UiSharedService.ColorText(Pair.UserData.AliasOrUID, ImGuiColors.HealerGreen); + ImGuiHelpers.ScaledDummy(new Vector2(spacing.Y, spacing.Y)); var textPos = ImGui.GetCursorPosY() - headerSize; ImGui.Separator(); diff --git a/MareSynchronos/UI/SyncshellAdminUI.cs b/MareSynchronos/UI/SyncshellAdminUI.cs index 57f3a7a..95650b2 100644 --- a/MareSynchronos/UI/SyncshellAdminUI.cs +++ b/MareSynchronos/UI/SyncshellAdminUI.cs @@ -24,7 +24,8 @@ public class SyncshellAdminUI : WindowMediatorSubscriberBase private int _multiInvites; private string _newPassword; private bool _pwChangeSuccess; - public SyncshellAdminUI(ILogger logger, MareMediator mediator, GroupFullInfoDto groupFullInfo, ApiController apiController, UiSharedService uiSharedService, PairManager pairManager) + public SyncshellAdminUI(ILogger logger, MareMediator mediator, ApiController apiController, + UiSharedService uiSharedService, PairManager pairManager, GroupFullInfoDto groupFullInfo) : base(logger, mediator, "Syncshell Admin Panel (" + groupFullInfo.GID + ")") { GroupFullInfo = groupFullInfo; diff --git a/MareSynchronos/UI/UISharedService.cs b/MareSynchronos/UI/UISharedService.cs index a75ef59..bd3f22d 100644 --- a/MareSynchronos/UI/UISharedService.cs +++ b/MareSynchronos/UI/UISharedService.cs @@ -207,16 +207,14 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase public static void ColorText(string text, Vector4 color) { - ImGui.PushStyleColor(ImGuiCol.Text, color); + using var raiicolor = ImRaii.PushColor(ImGuiCol.Text, color); ImGui.TextUnformatted(text); - ImGui.PopStyleColor(); } public static void ColorTextWrapped(string text, Vector4 color) { - ImGui.PushStyleColor(ImGuiCol.Text, color); + using var raiicolor = ImRaii.PushColor(ImGuiCol.Text, color); TextWrapped(text); - ImGui.PopStyleColor(); } public static bool CtrlPressed() => (GetKeyState(0xA2) & 0x8000) != 0 || (GetKeyState(0xA3) & 0x8000) != 0; @@ -224,18 +222,19 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase public static void DrawHelpText(string helpText) { ImGui.SameLine(); - ImGui.PushFont(UiBuilder.IconFont); - ImGui.SetWindowFontScale(0.8f); - ImGui.TextDisabled(FontAwesomeIcon.Question.ToIconString()); - ImGui.SetWindowFontScale(1.0f); - ImGui.PopFont(); + using (ImRaii.PushFont(UiBuilder.IconFont)) + { + ImGui.SetWindowFontScale(0.8f); + ImGui.TextDisabled(FontAwesomeIcon.Question.ToIconString()); + ImGui.SetWindowFontScale(1.0f); + } + if (ImGui.IsItemHovered()) { - ImGui.BeginTooltip(); + using var tooltip = ImRaii.Tooltip(); ImGui.PushTextWrapPos(ImGui.GetFontSize() * 35.0f); ImGui.TextUnformatted(helpText); ImGui.PopTextWrapPos(); - ImGui.EndTooltip(); } } @@ -243,31 +242,34 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase { var original = ImGui.GetCursorPos(); - ImGui.PushStyleColor(ImGuiCol.Text, outlineColor); - ImGui.SetCursorPos(original with { Y = original.Y - thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X - thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { Y = original.Y + thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X + thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X - thickness, Y = original.Y - thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X + thickness, Y = original.Y + thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X - thickness, Y = original.Y + thickness }); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original with { X = original.X + thickness, Y = original.Y - thickness }); - ImGui.TextUnformatted(text); - ImGui.PopStyleColor(); + using (ImRaii.PushColor(ImGuiCol.Text, outlineColor)) + { + ImGui.PushStyleColor(ImGuiCol.Text, outlineColor); + ImGui.SetCursorPos(original with { Y = original.Y - thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X - thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { Y = original.Y + thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X + thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X - thickness, Y = original.Y - thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X + thickness, Y = original.Y + thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X - thickness, Y = original.Y + thickness }); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original with { X = original.X + thickness, Y = original.Y - thickness }); + ImGui.TextUnformatted(text); + } - ImGui.PushStyleColor(ImGuiCol.Text, fontColor); - ImGui.SetCursorPos(original); - ImGui.TextUnformatted(text); - ImGui.SetCursorPos(original); - ImGui.TextUnformatted(text); - ImGui.PopStyleColor(); + using (ImRaii.PushColor(ImGuiCol.Text, fontColor)) + { + ImGui.SetCursorPos(original); + ImGui.TextUnformatted(text); + ImGui.SetCursorPos(original); + ImGui.TextUnformatted(text); + } } public static void DrawOutlinedFont(ImDrawListPtr drawList, string text, Vector2 textPos, uint fontColor, uint outlineColor, int thickness) @@ -293,13 +295,6 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase drawList.AddText(textPos, fontColor, text); } - public static void DrawWithID(string id, Action drawSubSection) - { - ImGui.PushID(id); - drawSubSection.Invoke(); - ImGui.PopID(); - } - public static void FontText(string text, ImFontPtr font, Vector4? color = null) { using var pushedFont = ImRaii.PushFont(font); @@ -309,22 +304,17 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase public static Vector4 GetBoolColor(bool input) => input ? ImGuiColors.ParsedGreen : ImGuiColors.DalamudRed; - public static Vector4 GetCpuLoadColor(double input) => input < 50 ? ImGuiColors.ParsedGreen : - input < 90 ? ImGuiColors.DalamudYellow : ImGuiColors.DalamudRed; - public static Vector2 GetIconButtonSize(FontAwesomeIcon icon) { - ImGui.PushFont(UiBuilder.IconFont); + using var font = ImRaii.PushFont(UiBuilder.IconFont); var buttonSize = ImGuiHelpers.GetButtonSize(icon.ToIconString()); - ImGui.PopFont(); return buttonSize; } public static Vector2 GetIconSize(FontAwesomeIcon icon) { - ImGui.PushFont(UiBuilder.IconFont); + using var font = ImRaii.PushFont(UiBuilder.IconFont); var iconSize = ImGui.CalcTextSize(icon.ToIconString()); - ImGui.PopFont(); return iconSize; } @@ -449,23 +439,6 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase } } - public static void OutlineTextWrapped(string text, Vector4 textcolor, Vector4 outlineColor, float dist = 3) - { - var cursorPos = ImGui.GetCursorPos(); - ColorTextWrapped(text, outlineColor); - ImGui.SetCursorPos(new(cursorPos.X, cursorPos.Y + dist)); - ColorTextWrapped(text, outlineColor); - ImGui.SetCursorPos(new(cursorPos.X + dist, cursorPos.Y)); - ColorTextWrapped(text, outlineColor); - ImGui.SetCursorPos(new(cursorPos.X + dist, cursorPos.Y + dist)); - ColorTextWrapped(text, outlineColor); - - ImGui.SetCursorPos(new(cursorPos.X + dist / 2, cursorPos.Y + dist / 2)); - ColorTextWrapped(text, textcolor); - ImGui.SetCursorPos(new(cursorPos.X + dist / 2, cursorPos.Y + dist / 2)); - ColorTextWrapped(text, textcolor); - } - public static void SetScaledWindowSize(float width, bool centerWindow = true) { var newLineHeight = ImGui.GetCursorPosY(); @@ -537,9 +510,8 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase public void BigText(string text) { - if (UidFontBuilt) ImGui.PushFont(UidFont); + using var font = ImRaii.PushFont(UidFont, UidFontBuilt); ImGui.TextUnformatted(text); - if (UidFontBuilt) ImGui.PopFont(); } public void DrawCacheDirectorySetting() @@ -854,24 +826,6 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase Strings.ToS = new Strings.ToSStrings(); } - public void PrintServerState() - { - if (_apiController.ServerState is ServerState.Connected) - { - ImGui.TextUnformatted("Service " + _serverConfigurationManager.CurrentServer!.ServerName + ":"); - ImGui.SameLine(); - ImGui.TextColored(ImGuiColors.ParsedGreen, "Available"); - ImGui.SameLine(); - ImGui.TextUnformatted("("); - ImGui.SameLine(); - ImGui.TextColored(ImGuiColors.ParsedGreen, _apiController.OnlineUsers.ToString(CultureInfo.InvariantCulture)); - ImGui.SameLine(); - ImGui.TextUnformatted("Users Online"); - ImGui.SameLine(); - ImGui.TextUnformatted(")"); - } - } - public void RecalculateFileCacheSize() { _cacheScanner.InvokeScan(forced: true); @@ -903,7 +857,9 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase ImGui.SetWindowPos(new Vector2(center.X - width / 2, center.Y - height / 2), cond); } +#pragma warning disable MA0009 // Add regex evaluation timeout [GeneratedRegex(@"^(?:[a-zA-Z]:\\[\w\s\-\\]+?|\/(?:[\w\s\-\/])+?)$", RegexOptions.ECMAScript)] +#pragma warning restore MA0009 // Add regex evaluation timeout private static partial Regex PathRegex(); private void BuildFont() diff --git a/MareSynchronos/WebAPI/SignalR/HubFactory.cs b/MareSynchronos/WebAPI/SignalR/HubFactory.cs index e9c6562..51512bf 100644 --- a/MareSynchronos/WebAPI/SignalR/HubFactory.cs +++ b/MareSynchronos/WebAPI/SignalR/HubFactory.cs @@ -16,21 +16,19 @@ namespace MareSynchronos.WebAPI.SignalR; public class HubFactory : MediatorSubscriberBase { - private readonly MareConfigService _configService; - private readonly IPluginLog _pluginLog; + private readonly ILoggerProvider _loggingProvider; private readonly ServerConfigurationManager _serverConfigurationManager; private readonly TokenProvider _tokenProvider; private HubConnection? _instance; private bool _isDisposed = false; public HubFactory(ILogger logger, MareMediator mediator, - ServerConfigurationManager serverConfigurationManager, MareConfigService configService, - TokenProvider tokenProvider, IPluginLog pluginLog) : base(logger, mediator) + ServerConfigurationManager serverConfigurationManager, + TokenProvider tokenProvider, ILoggerProvider pluginLog) : base(logger, mediator) { _serverConfigurationManager = serverConfigurationManager; - _configService = configService; _tokenProvider = tokenProvider; - _pluginLog = pluginLog; + _loggingProvider = pluginLog; } public async Task DisposeHubAsync() @@ -92,7 +90,7 @@ public class HubFactory : MediatorSubscriberBase .WithAutomaticReconnect(new ForeverRetryPolicy(Mediator)) .ConfigureLogging(a => { - a.ClearProviders().AddProvider(new DalamudLoggingProvider(_configService, _pluginLog)); + a.ClearProviders().AddProvider(_loggingProvider); a.SetMinimumLevel(LogLevel.Information); }) .Build();