diff --git a/ChatContext/ChatEnricher.cs b/ChatContext/ChatEnricher.cs index 1dbe170..fdbc271 100644 --- a/ChatContext/ChatEnricher.cs +++ b/ChatContext/ChatEnricher.cs @@ -3,19 +3,23 @@ using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Plugin.Services; using System; +using System.Linq; namespace ChatContext; + public class ChatEnricher : IDisposable { private IChatGui ChatGui { get; init; } private NearbyPlayers NearbyPlayers { get; init; } private Configuration Configuration { get; init; } + private IPluginLog PluginLog { get; init; } - public ChatEnricher(IChatGui chatGui, NearbyPlayers nearbyPlayers, Configuration configuration) + public ChatEnricher(IChatGui chatGui, NearbyPlayers nearbyPlayers, Configuration configuration, IPluginLog pluginLog) { ChatGui = chatGui; NearbyPlayers = nearbyPlayers; Configuration = configuration; + PluginLog = pluginLog; ChatGui.ChatMessage += OnChatMessage; } @@ -25,33 +29,51 @@ public void Dispose() ChatGui.ChatMessage -= OnChatMessage; } - void OnChatMessage(XivChatType type, int a2, ref SeString sender, ref SeString message, ref bool isHandled) + private void OnChatMessage(XivChatType type, int a2, ref SeString sender, ref SeString message, ref bool isHandled) { if (Configuration.Enabled && Configuration.FormatValid() && Configuration.Types.Contains(type)) { - string name; - if (sender.Payloads.Count > 0 && sender.Payloads[0] is PlayerPayload) - { - // cross world player format - name = ((PlayerPayload)sender.Payloads[0]).PlayerName; - } - else - { - name = sender.TextValue; - } - - var targetName = NearbyPlayers.GetTargetName(name); + var senderName = GetSenderName(sender); + PluginLog.Verbose($"Matched Type: {type}, Sender Name: {senderName}"); + var targetName = NearbyPlayers.GetTargetName(senderName); if (targetName != null) { + PluginLog.Verbose($"Successful Target Lookup: {senderName} => {targetName}"); var suffix = new SeStringBuilder() .Append(" ") .AddUiForeground((ushort)Configuration.Color) .Append(string.Format(Configuration.Format, targetName)) .AddUiForegroundOff() .Build(); - message.Append(suffix); } + else + { + PluginLog.Verbose($"Failed Target Lookup: {senderName}"); + } + } + } + + private static string GetSenderName(SeString sender) + { + // Cross-world + foreach (var payload in sender.Payloads) + { + if (payload is PlayerPayload playerPayload) + { + return playerPayload.PlayerName; + } } + + // Reverse to ignore prefixes (party number, etc.) + foreach (var payload in sender.Payloads.Reverse()) + { + if (payload is TextPayload rawPayload) + { + return rawPayload.Text!; + } + } + + return string.Empty; } } diff --git a/ChatContext/Configuration.cs b/ChatContext/Configuration.cs index fdfc3e8..78eea5a 100644 --- a/ChatContext/Configuration.cs +++ b/ChatContext/Configuration.cs @@ -6,19 +6,20 @@ namespace ChatContext; [Serializable] + public class Configuration : IPluginConfiguration { public int Version { get; set; } = 0; public bool Enabled { get; set; } = true; - public HashSet Types { get; set; } = new() { + public HashSet Types { get; set; } = [ XivChatType.Alliance, XivChatType.Yell, XivChatType.Party, XivChatType.Say, - XivChatType.Shout - }; + XivChatType.Shout + ]; public string Format { get; set; } = "[ {0}]"; diff --git a/ChatContext/NearbyPlayers.cs b/ChatContext/NearbyPlayers.cs index 768e602..437d99a 100644 --- a/ChatContext/NearbyPlayers.cs +++ b/ChatContext/NearbyPlayers.cs @@ -14,7 +14,6 @@ public class NearbyPlayers : IDisposable private Configuration Configuration { get; init; } public ImmutableDictionary TargetNameByName { get; private set; } = ImmutableDictionary.Create(); - public NearbyPlayers(IClientState clientState, IObjectTable objectTable, IFramework framework, Configuration configuration) { ClientState = clientState; diff --git a/ChatContext/Plugin.cs b/ChatContext/Plugin.cs index eab1b39..b17a4f8 100644 --- a/ChatContext/Plugin.cs +++ b/ChatContext/Plugin.cs @@ -4,6 +4,7 @@ using Dalamud.Interface.Windowing; using Dalamud.Plugin.Services; using ChatContext.Windows; +using Dalamud.Interface; namespace ChatContext; @@ -15,6 +16,7 @@ public sealed class Plugin : IDalamudPlugin [PluginService] internal static IFramework Framework { get; private set; } = null!; [PluginService] internal static IObjectTable ObjectTable { get; private set; } = null!; [PluginService] internal static IClientState ClientState { get; private set; } = null!; + [PluginService] internal static IPluginLog PluginLog { get; private set; } = null!; private const string CommandName = "/chatcontext"; private const string CommandHelpMessage = $"Available subcommands for {CommandName} are main, config, disable and enable"; @@ -32,10 +34,24 @@ public Plugin() Configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); NearbyPlayers = new NearbyPlayers(ClientState, ObjectTable, Framework, Configuration); - ChatEnricher = new ChatEnricher(ChatGui, NearbyPlayers, Configuration); + ChatEnricher = new ChatEnricher(ChatGui, NearbyPlayers, Configuration, PluginLog); - MainWindow = new MainWindow(this, NearbyPlayers); - ConfigWindow = new ConfigWindow(this); + MainWindow = new MainWindow(this, NearbyPlayers) + { + TitleBarButtons = [new() + { + Icon = FontAwesomeIcon.Cog, + Click = (_) => ToggleConfigUI() + }] + }; + ConfigWindow = new ConfigWindow(this) + { + TitleBarButtons = [new() + { + Icon = FontAwesomeIcon.ListAlt, + Click = (_) => ToggleMainUI() + }] + }; WindowSystem.AddWindow(MainWindow); WindowSystem.AddWindow(ConfigWindow); @@ -55,9 +71,7 @@ public void Dispose() { NearbyPlayers.Dispose(); ChatEnricher.Dispose(); - WindowSystem.RemoveAllWindows(); - ConfigWindow.Dispose(); CommandManager.RemoveHandler(CommandName); } diff --git a/ChatContext/Windows/ConfigWindow.cs b/ChatContext/Windows/ConfigWindow.cs index ae85175..425e071 100644 --- a/ChatContext/Windows/ConfigWindow.cs +++ b/ChatContext/Windows/ConfigWindow.cs @@ -7,13 +7,10 @@ namespace ChatContext.Windows; -public class ConfigWindow : Window, IDisposable +public class ConfigWindow : Window { private Configuration Configuration { get; init; } - // We give this window a constant ID using ### - // This allows for labels being dynamic, like "{FPS Counter}fps###XYZ counter window", - // and the window ID will always be "###XYZ counter window" for ImGui public ConfigWindow(Plugin plugin) : base("Chat Context Config##configWindow") { SizeConstraints = new WindowSizeConstraints @@ -25,8 +22,6 @@ public ConfigWindow(Plugin plugin) : base("Chat Context Config##configWindow") Configuration = plugin.Configuration; } - public void Dispose() { } - public override void Draw() { var enabled = Configuration.Enabled; diff --git a/ChatContext/Windows/MainWindow.cs b/ChatContext/Windows/MainWindow.cs index cd489c7..1225469 100644 --- a/ChatContext/Windows/MainWindow.cs +++ b/ChatContext/Windows/MainWindow.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Numerics; @@ -7,9 +6,9 @@ namespace ChatContext.Windows; -public class MainWindow : Window, IDisposable +public class MainWindow : Window { - public class TableComparer(ImGuiTableColumnSortSpecsPtr specs) : IComparer> + private class TableComparer(ImGuiTableColumnSortSpecsPtr specs) : IComparer> { private ImGuiTableColumnSortSpecsPtr Specs { get; init; } = specs; @@ -25,11 +24,11 @@ public int Compare(KeyValuePair lhs, KeyValuePair