From 63e7cb25b5bc271aeccb1ac7c11800b31f07ddca Mon Sep 17 00:00:00 2001 From: Haselnussbomber Date: Mon, 4 Aug 2025 03:07:21 +0200 Subject: [PATCH] [Api13] Update ChatLinkHandler functions (#2322) * Update ChatLinkHandler functions - Move functions to IChatGui - Switch CommandId to type Guid and generate them automatically * Remove unused field --- Dalamud/Game/ChatHandlers.cs | 2 - Dalamud/Game/Gui/ChatGui.cs | 56 ++++++++++++++++--- .../Payloads/DalamudLinkPayload.cs | 12 ++-- Dalamud/Plugin/DalamudPluginInterface.cs | 35 ------------ Dalamud/Plugin/IDalamudPluginInterface.cs | 21 +------ .../Internal/AutoUpdate/AutoUpdateManager.cs | 2 - Dalamud/Plugin/Internal/PluginManager.cs | 2 - Dalamud/Plugin/Services/IChatGui.cs | 21 ++++++- 8 files changed, 74 insertions(+), 77 deletions(-) diff --git a/Dalamud/Game/ChatHandlers.cs b/Dalamud/Game/ChatHandlers.cs index 61cd88a05..c57dd70b8 100644 --- a/Dalamud/Game/ChatHandlers.cs +++ b/Dalamud/Game/ChatHandlers.cs @@ -119,8 +119,6 @@ internal partial class ChatHandlers : IServiceType if (string.IsNullOrEmpty(this.configuration.LastVersion) || !Util.AssemblyVersion.StartsWith(this.configuration.LastVersion)) { var linkPayload = chatGui.AddChatLinkHandler( - "dalamud", - 8459324, (_, _) => dalamudInterface.OpenPluginInstallerTo(PluginInstallerOpenKind.Changelogs)); var updateMessage = new SeStringBuilder() diff --git a/Dalamud/Game/Gui/ChatGui.cs b/Dalamud/Game/Gui/ChatGui.cs index 022968c7e..6de4e3c3f 100644 --- a/Dalamud/Game/Gui/ChatGui.cs +++ b/Dalamud/Game/Gui/ChatGui.cs @@ -12,6 +12,7 @@ using Dalamud.IoC; using Dalamud.IoC.Internal; using Dalamud.Logging.Internal; using Dalamud.Memory; +using Dalamud.Plugin.Internal.Types; using Dalamud.Plugin.Services; using Dalamud.Utility; @@ -40,7 +41,7 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui private static readonly ModuleLog Log = new("ChatGui"); private readonly Queue chatQueue = new(); - private readonly Dictionary<(string PluginName, uint CommandId), Action> dalamudLinkHandlers = new(); + private readonly Dictionary<(string PluginName, Guid CommandId), Action> dalamudLinkHandlers = new(); private readonly Hook printMessageHook; private readonly Hook inventoryItemCopyHook; @@ -49,7 +50,7 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui [ServiceManager.ServiceDependency] private readonly DalamudConfiguration configuration = Service.Get(); - private ImmutableDictionary<(string PluginName, uint CommandId), Action>? dalamudLinkHandlersCopy; + private ImmutableDictionary<(string PluginName, Guid CommandId), Action>? dalamudLinkHandlersCopy; [ServiceManager.ServiceConstructor] private ChatGui() @@ -85,7 +86,7 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui public byte LastLinkedItemFlags { get; private set; } /// - public IReadOnlyDictionary<(string PluginName, uint CommandId), Action> RegisteredLinkHandlers + public IReadOnlyDictionary<(string PluginName, Guid CommandId), Action> RegisteredLinkHandlers { get { @@ -161,6 +162,28 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui #endregion + #region Chat Links + + /// + public DalamudLinkPayload AddChatLinkHandler(Action commandAction) + { + return this.AddChatLinkHandler("Dalamud", commandAction); + } + + /// + public void RemoveChatLinkHandler(Guid commandId) + { + this.RemoveChatLinkHandler("Dalamud", commandId); + } + + /// + public void RemoveChatLinkHandler() + { + this.RemoveChatLinkHandler("Dalamud"); + } + + #endregion + /// /// Process a chat queue. /// @@ -217,12 +240,11 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui /// Create a link handler. /// /// The name of the plugin handling the link. - /// The ID of the command to run. /// The command action itself. /// A payload for handling. - [Api13ToDo("Plugins should not specify their own command IDs here. We should assign them ourselves.")] - internal DalamudLinkPayload AddChatLinkHandler(string pluginName, uint commandId, Action commandAction) + internal DalamudLinkPayload AddChatLinkHandler(string pluginName, Action commandAction) { + var commandId = Guid.CreateVersion7(); var payload = new DalamudLinkPayload { Plugin = pluginName, CommandId = commandId }; lock (this.dalamudLinkHandlers) { @@ -255,7 +277,7 @@ internal sealed unsafe class ChatGui : IInternalDisposableService, IChatGui /// /// The name of the plugin handling the link. /// The ID of the command to be removed. - internal void RemoveChatLinkHandler(string pluginName, uint commandId) + internal void RemoveChatLinkHandler(string pluginName, Guid commandId) { lock (this.dalamudLinkHandlers) { @@ -478,11 +500,15 @@ internal class ChatGuiPluginScoped : IInternalDisposableService, IChatGui [ServiceManager.ServiceDependency] private readonly ChatGui chatGuiService = Service.Get(); + private readonly LocalPlugin plugin; + /// /// Initializes a new instance of the class. /// - internal ChatGuiPluginScoped() + /// The plugin. + internal ChatGuiPluginScoped(LocalPlugin plugin) { + this.plugin = plugin; this.chatGuiService.ChatMessage += this.OnMessageForward; this.chatGuiService.CheckMessageHandled += this.OnCheckMessageForward; this.chatGuiService.ChatMessageHandled += this.OnMessageHandledForward; @@ -508,7 +534,7 @@ internal class ChatGuiPluginScoped : IInternalDisposableService, IChatGui public byte LastLinkedItemFlags => this.chatGuiService.LastLinkedItemFlags; /// - public IReadOnlyDictionary<(string PluginName, uint CommandId), Action> RegisteredLinkHandlers => this.chatGuiService.RegisteredLinkHandlers; + public IReadOnlyDictionary<(string PluginName, Guid CommandId), Action> RegisteredLinkHandlers => this.chatGuiService.RegisteredLinkHandlers; /// void IInternalDisposableService.DisposeService() @@ -524,6 +550,18 @@ internal class ChatGuiPluginScoped : IInternalDisposableService, IChatGui this.ChatMessageUnhandled = null; } + /// + public DalamudLinkPayload AddChatLinkHandler(Action commandAction) + => this.chatGuiService.AddChatLinkHandler(this.plugin.InternalName, commandAction); + + /// + public void RemoveChatLinkHandler(Guid commandId) + => this.chatGuiService.RemoveChatLinkHandler(this.plugin.InternalName, commandId); + + /// + public void RemoveChatLinkHandler() + => this.chatGuiService.RemoveChatLinkHandler(this.plugin.InternalName); + /// public void Print(XivChatEntry chat) => this.chatGuiService.Print(chat); diff --git a/Dalamud/Game/Text/SeStringHandling/Payloads/DalamudLinkPayload.cs b/Dalamud/Game/Text/SeStringHandling/Payloads/DalamudLinkPayload.cs index 8b020b111..ee06172b4 100644 --- a/Dalamud/Game/Text/SeStringHandling/Payloads/DalamudLinkPayload.cs +++ b/Dalamud/Game/Text/SeStringHandling/Payloads/DalamudLinkPayload.cs @@ -16,7 +16,7 @@ public class DalamudLinkPayload : Payload public override PayloadType Type => PayloadType.DalamudLink; /// Gets the plugin command ID to be linked. - public uint CommandId { get; internal set; } + public Guid CommandId { get; internal set; } /// Gets an optional extra integer value 1. public int Extra1 { get; internal set; } @@ -40,7 +40,7 @@ public class DalamudLinkPayload : Payload var ssb = Lumina.Text.SeStringBuilder.SharedPool.Get(); var res = ssb.BeginMacro(MacroCode.Link) .AppendIntExpression((int)EmbeddedInfoType.DalamudLink - 1) - .AppendUIntExpression(this.CommandId) + .AppendStringExpression(this.CommandId.ToString()) .AppendIntExpression(this.Extra1) .AppendIntExpression(this.Extra2) .BeginStringExpression() @@ -72,15 +72,15 @@ public class DalamudLinkPayload : Payload if (!pluginExpression.TryGetString(out var pluginString)) return; - if (!commandIdExpression.TryGetUInt(out var commandId)) + if (!commandIdExpression.TryGetString(out var commandId)) return; this.Plugin = pluginString.ExtractText(); - this.CommandId = commandId; + this.CommandId = Guid.Parse(commandId.ExtractText()); } else { - if (!commandIdExpression.TryGetUInt(out var commandId)) + if (!commandIdExpression.TryGetString(out var commandId)) return; if (!extra1Expression.TryGetInt(out var extra1)) @@ -102,7 +102,7 @@ public class DalamudLinkPayload : Payload return; } - this.CommandId = commandId; + this.CommandId = Guid.Parse(commandId.ExtractText()); this.Extra1 = extra1; this.Extra2 = extra2; this.Plugin = extraData[0]; diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index e1c6b7830..b857fe438 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -13,8 +13,6 @@ using Dalamud.Data; using Dalamud.Game.Gui; using Dalamud.Game.Text; using Dalamud.Game.Text.Sanitizer; -using Dalamud.Game.Text.SeStringHandling; -using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Interface; using Dalamud.Interface.Internal; using Dalamud.Interface.Internal.Windows.PluginInstaller; @@ -427,39 +425,6 @@ internal sealed class DalamudPluginInterface : IDalamudPluginInterface, IDisposa #endregion - #region Chat Links - - // TODO API9: Move to chatgui, don't allow passing own commandId - - /// - /// Register a chat link handler. - /// - /// The ID of the command. - /// The action to be executed. - /// Returns an SeString payload for the link. - public DalamudLinkPayload AddChatLinkHandler(uint commandId, Action commandAction) - { - return Service.Get().AddChatLinkHandler(this.plugin.InternalName, commandId, commandAction); - } - - /// - /// Remove a chat link handler. - /// - /// The ID of the command. - public void RemoveChatLinkHandler(uint commandId) - { - Service.Get().RemoveChatLinkHandler(this.plugin.InternalName, commandId); - } - - /// - /// Removes all chat link handlers registered by the plugin. - /// - public void RemoveChatLinkHandler() - { - Service.Get().RemoveChatLinkHandler(this.plugin.InternalName); - } - #endregion - #region Dependency Injection /// diff --git a/Dalamud/Plugin/IDalamudPluginInterface.cs b/Dalamud/Plugin/IDalamudPluginInterface.cs index 5b7c3836e..4f1192df2 100644 --- a/Dalamud/Plugin/IDalamudPluginInterface.cs +++ b/Dalamud/Plugin/IDalamudPluginInterface.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Threading.Tasks; @@ -281,25 +281,6 @@ public interface IDalamudPluginInterface /// directory with path of AppData/XIVLauncher/pluginConfig/PluginInternalName/loc. string GetPluginLocDirectory(); - /// - /// Register a chat link handler. - /// - /// The ID of the command. - /// The action to be executed. - /// Returns an SeString payload for the link. - DalamudLinkPayload AddChatLinkHandler(uint commandId, Action commandAction); - - /// - /// Remove a chat link handler. - /// - /// The ID of the command. - void RemoveChatLinkHandler(uint commandId); - - /// - /// Removes all chat link handlers registered by the plugin. - /// - void RemoveChatLinkHandler(); - /// /// Create a new object of the provided type using its default constructor, then inject objects and properties. /// diff --git a/Dalamud/Plugin/Internal/AutoUpdate/AutoUpdateManager.cs b/Dalamud/Plugin/Internal/AutoUpdate/AutoUpdateManager.cs index 1a57790f8..558cde790 100644 --- a/Dalamud/Plugin/Internal/AutoUpdate/AutoUpdateManager.cs +++ b/Dalamud/Plugin/Internal/AutoUpdate/AutoUpdateManager.cs @@ -102,8 +102,6 @@ internal class AutoUpdateManager : IServiceType this.openInstallerWindowLinkTask = Service.GetAsync().ContinueWith( chatGuiTask => chatGuiTask.Result.AddChatLinkHandler( - "Dalamud", - 1001, (_, _) => { Service.GetNullable()?.OpenPluginInstallerTo(PluginInstallerOpenKind.InstalledPlugins); diff --git a/Dalamud/Plugin/Internal/PluginManager.cs b/Dalamud/Plugin/Internal/PluginManager.cs index bfb1b3430..1d1908144 100644 --- a/Dalamud/Plugin/Internal/PluginManager.cs +++ b/Dalamud/Plugin/Internal/PluginManager.cs @@ -125,8 +125,6 @@ internal class PluginManager : IInternalDisposableService this.openInstallerWindowPluginChangelogsLink = Service.GetAsync().ContinueWith( chatGuiTask => chatGuiTask.Result.AddChatLinkHandler( - "Dalamud", - 1003, (_, _) => { Service.GetNullable()?.OpenPluginInstallerTo( diff --git a/Dalamud/Plugin/Services/IChatGui.cs b/Dalamud/Plugin/Services/IChatGui.cs index 3f221b3bb..ab595dc3f 100644 --- a/Dalamud/Plugin/Services/IChatGui.cs +++ b/Dalamud/Plugin/Services/IChatGui.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Dalamud.Game.Gui; using Dalamud.Game.Text; using Dalamud.Game.Text.SeStringHandling; +using Dalamud.Game.Text.SeStringHandling.Payloads; namespace Dalamud.Plugin.Services; @@ -82,7 +83,25 @@ public interface IChatGui /// /// Gets the dictionary of Dalamud Link Handlers. /// - public IReadOnlyDictionary<(string PluginName, uint CommandId), Action> RegisteredLinkHandlers { get; } + public IReadOnlyDictionary<(string PluginName, Guid CommandId), Action> RegisteredLinkHandlers { get; } + + /// + /// Register a chat link handler. + /// + /// The action to be executed. + /// Returns an SeString payload for the link. + public DalamudLinkPayload AddChatLinkHandler(Action commandAction); + + /// + /// Remove a chat link handler. + /// + /// The ID of the command. + public void RemoveChatLinkHandler(Guid commandId); + + /// + /// Removes all chat link handlers registered by the plugin. + /// + public void RemoveChatLinkHandler(); /// /// Queue a chat message. Dalamud will send queued messages on the next framework event.