Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
anya-hichu committed Oct 23, 2024
1 parent dc41f68 commit 13e5225
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 40 deletions.
52 changes: 37 additions & 15 deletions ChatContext/ChatEnricher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<Payload>())
{
if (payload is TextPayload rawPayload)
{
return rawPayload.Text!;
}
}

return string.Empty;
}
}
7 changes: 4 additions & 3 deletions ChatContext/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<XivChatType> Types { get; set; } = new() {
public HashSet<XivChatType> Types { get; set; } = [
XivChatType.Alliance,
XivChatType.Yell,
XivChatType.Party,
XivChatType.Say,
XivChatType.Shout
};
XivChatType.Shout
];

public string Format { get; set; } = "[ {0}]";

Expand Down
1 change: 0 additions & 1 deletion ChatContext/NearbyPlayers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class NearbyPlayers : IDisposable
private Configuration Configuration { get; init; }
public ImmutableDictionary<string, string> TargetNameByName { get; private set; } = ImmutableDictionary.Create<string, string>();


public NearbyPlayers(IClientState clientState, IObjectTable objectTable, IFramework framework, Configuration configuration)
{
ClientState = clientState;
Expand Down
24 changes: 19 additions & 5 deletions ChatContext/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dalamud.Interface.Windowing;
using Dalamud.Plugin.Services;
using ChatContext.Windows;
using Dalamud.Interface;

namespace ChatContext;

Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -55,9 +71,7 @@ public void Dispose()
{
NearbyPlayers.Dispose();
ChatEnricher.Dispose();

WindowSystem.RemoveAllWindows();
ConfigWindow.Dispose();
CommandManager.RemoveHandler(CommandName);
}

Expand Down
7 changes: 1 addition & 6 deletions ChatContext/Windows/ConfigWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
15 changes: 5 additions & 10 deletions ChatContext/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
Expand All @@ -7,9 +6,9 @@

namespace ChatContext.Windows;

public class MainWindow : Window, IDisposable
public class MainWindow : Window
{
public class TableComparer(ImGuiTableColumnSortSpecsPtr specs) : IComparer<KeyValuePair<string, string>>
private class TableComparer(ImGuiTableColumnSortSpecsPtr specs) : IComparer<KeyValuePair<string, string>>
{
private ImGuiTableColumnSortSpecsPtr Specs { get; init; } = specs;

Expand All @@ -25,27 +24,23 @@ public int Compare(KeyValuePair<string, string> lhs, KeyValuePair<string, string
private Plugin Plugin { get; init; }
private NearbyPlayers NearbyPlayers { get; init; }

public MainWindow(Plugin plugin, NearbyPlayers nearbyPlayers) : base("Chat Context##mainWindow")
public MainWindow(Plugin plugin, NearbyPlayers nearbyPlayers) : base("Chat Context Nearby Players##mainWindow")
{
SizeConstraints = new WindowSizeConstraints
{
MinimumSize = new Vector2(300, 380),
MinimumSize = new Vector2(300, 180),
MaximumSize = new Vector2(float.MaxValue, float.MaxValue)
};

Plugin = plugin;
NearbyPlayers = nearbyPlayers;
}

public void Dispose() { }


public override void Draw()
{
if (Plugin.Configuration.Enabled)
{
ImGui.Text("Nearby player targets:");
if (ImGui.BeginTable("nearbyPlayersTable", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY | ImGuiTableFlags.Sortable, new Vector2(ImGui.GetWindowWidth(), ImGui.GetWindowHeight() - ImGui.GetTextLineHeight() * 4)))
if (ImGui.BeginTable("nearbyPlayersTable", 2, ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY | ImGuiTableFlags.Sortable, new Vector2(ImGui.GetWindowWidth(), ImGui.GetWindowHeight() - ImGui.GetTextLineHeight() * 3)))
{
ImGui.TableSetupColumn("Name", ImGuiTableColumnFlags.DefaultSort | ImGuiTableColumnFlags.PreferSortAscending);
ImGui.TableSetupColumn("Target name");
Expand Down

0 comments on commit 13e5225

Please sign in to comment.