Add IChatGui

This commit is contained in:
MidoriKami 2023-06-24 22:18:41 -07:00
parent 3e613cffd0
commit 6eba1415f5
2 changed files with 142 additions and 89 deletions

View file

@ -11,6 +11,7 @@ using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Hooking; using Dalamud.Hooking;
using Dalamud.IoC; using Dalamud.IoC;
using Dalamud.IoC.Internal; using Dalamud.IoC.Internal;
using Dalamud.Plugin.Services;
using Dalamud.Utility; using Dalamud.Utility;
using Serilog; using Serilog;
@ -22,7 +23,10 @@ namespace Dalamud.Game.Gui;
[PluginInterface] [PluginInterface]
[InterfaceVersion("1.0")] [InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService] [ServiceManager.BlockingEarlyLoadedService]
public sealed class ChatGui : IDisposable, IServiceType #pragma warning disable SA1015
[ResolveVia<IChatGui>]
#pragma warning restore SA1015
public sealed class ChatGui : IDisposable, IServiceType, IChatGui
{ {
private readonly ChatGuiAddressResolver address; private readonly ChatGuiAddressResolver address;
@ -51,45 +55,7 @@ public sealed class ChatGui : IDisposable, IServiceType
this.populateItemLinkHook = Hook<PopulateItemLinkDelegate>.FromAddress(this.address.PopulateItemLinkObject, this.HandlePopulateItemLinkDetour); this.populateItemLinkHook = Hook<PopulateItemLinkDelegate>.FromAddress(this.address.PopulateItemLinkObject, this.HandlePopulateItemLinkDetour);
this.interactableLinkClickedHook = Hook<InteractableLinkClickedDelegate>.FromAddress(this.address.InteractableLinkClicked, this.InteractableLinkClickedDetour); this.interactableLinkClickedHook = Hook<InteractableLinkClickedDelegate>.FromAddress(this.address.InteractableLinkClicked, this.InteractableLinkClickedDetour);
} }
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessage"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
/// <param name="isHandled">A value indicating whether the message was handled or should be propagated.</param>
public delegate void OnMessageDelegate(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.CheckMessageHandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
/// <param name="isHandled">A value indicating whether the message was handled or should be propagated.</param>
public delegate void OnCheckMessageHandledDelegate(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessageHandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
public delegate void OnMessageHandledDelegate(XivChatType type, uint senderId, SeString sender, SeString message);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessageUnhandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
public delegate void OnMessageUnhandledDelegate(XivChatType type, uint senderId, SeString sender, SeString message);
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr PrintMessageDelegate(IntPtr manager, XivChatType chatType, IntPtr senderName, IntPtr message, uint senderId, IntPtr parameter); private delegate IntPtr PrintMessageDelegate(IntPtr manager, XivChatType chatType, IntPtr senderName, IntPtr message, uint senderId, IntPtr parameter);
@ -99,34 +65,22 @@ public sealed class ChatGui : IDisposable, IServiceType
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate void InteractableLinkClickedDelegate(IntPtr managerPtr, IntPtr messagePtr); private delegate void InteractableLinkClickedDelegate(IntPtr managerPtr, IntPtr messagePtr);
/// <summary> /// <inheritdoc/>
/// Event that will be fired when a chat message is sent to chat by the game. public event IChatGui.OnMessageDelegate ChatMessage;
/// </summary>
public event OnMessageDelegate ChatMessage;
/// <summary> /// <inheritdoc/>
/// Event that allows you to stop messages from appearing in chat by setting the isHandled parameter to true. public event IChatGui.OnCheckMessageHandledDelegate CheckMessageHandled;
/// </summary>
public event OnCheckMessageHandledDelegate CheckMessageHandled;
/// <summary> /// <inheritdoc/>
/// Event that will be fired when a chat message is handled by Dalamud or a Plugin. public event IChatGui.OnMessageHandledDelegate ChatMessageHandled;
/// </summary>
public event OnMessageHandledDelegate ChatMessageHandled;
/// <summary> /// <inheritdoc/>
/// Event that will be fired when a chat message is not handled by Dalamud or a Plugin. public event IChatGui.OnMessageUnhandledDelegate ChatMessageUnhandled;
/// </summary>
public event OnMessageUnhandledDelegate ChatMessageUnhandled;
/// <summary> /// <inheritdoc/>
/// Gets the ID of the last linked item.
/// </summary>
public int LastLinkedItemId { get; private set; } public int LastLinkedItemId { get; private set; }
/// <summary> /// <inheritdoc/>
/// Gets the flags of the last linked item.
/// </summary>
public byte LastLinkedItemFlags { get; private set; } public byte LastLinkedItemFlags { get; private set; }
/// <summary> /// <summary>
@ -139,21 +93,13 @@ public sealed class ChatGui : IDisposable, IServiceType
this.interactableLinkClickedHook.Dispose(); this.interactableLinkClickedHook.Dispose();
} }
/// <summary> /// <inheritdoc/>
/// Queue a chat message. While method is named as PrintChat, it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="chat">A message to send.</param>
public void PrintChat(XivChatEntry chat) public void PrintChat(XivChatEntry chat)
{ {
this.chatQueue.Enqueue(chat); this.chatQueue.Enqueue(chat);
} }
/// <summary> /// <inheritdoc/>
/// Queue a chat message. While method is named as PrintChat (it calls it internally), it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void Print(string message) public void Print(string message)
{ {
// Log.Verbose("[CHATGUI PRINT REGULAR]{0}", message); // Log.Verbose("[CHATGUI PRINT REGULAR]{0}", message);
@ -164,11 +110,7 @@ public sealed class ChatGui : IDisposable, IServiceType
}); });
} }
/// <summary> /// <inheritdoc/>
/// Queue a chat message. While method is named as PrintChat (it calls it internally), it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void Print(SeString message) public void Print(SeString message)
{ {
// Log.Verbose("[CHATGUI PRINT SESTRING]{0}", message.TextValue); // Log.Verbose("[CHATGUI PRINT SESTRING]{0}", message.TextValue);
@ -179,11 +121,7 @@ public sealed class ChatGui : IDisposable, IServiceType
}); });
} }
/// <summary> /// <inheritdoc/>
/// Queue an error chat message. While method is named as PrintChat (it calls it internally), it only add a entry to
/// the queue, later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void PrintError(string message) public void PrintError(string message)
{ {
// Log.Verbose("[CHATGUI PRINT REGULAR ERROR]{0}", message); // Log.Verbose("[CHATGUI PRINT REGULAR ERROR]{0}", message);
@ -194,11 +132,7 @@ public sealed class ChatGui : IDisposable, IServiceType
}); });
} }
/// <summary> /// <inheritdoc/>
/// Queue an error chat message. While method is named as PrintChat (it calls it internally), it only add a entry to
/// the queue, later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void PrintError(SeString message) public void PrintError(SeString message)
{ {
// Log.Verbose("[CHATGUI PRINT SESTRING ERROR]{0}", message.TextValue); // Log.Verbose("[CHATGUI PRINT SESTRING ERROR]{0}", message.TextValue);
@ -330,7 +264,7 @@ public sealed class ChatGui : IDisposable, IServiceType
{ {
try try
{ {
var messageHandledDelegate = @delegate as OnCheckMessageHandledDelegate; var messageHandledDelegate = @delegate as IChatGui.OnCheckMessageHandledDelegate;
messageHandledDelegate!.Invoke(chattype, senderid, ref parsedSender, ref parsedMessage, ref isHandled); messageHandledDelegate!.Invoke(chattype, senderid, ref parsedSender, ref parsedMessage, ref isHandled);
} }
catch (Exception e) catch (Exception e)
@ -346,7 +280,7 @@ public sealed class ChatGui : IDisposable, IServiceType
{ {
try try
{ {
var messageHandledDelegate = @delegate as OnMessageDelegate; var messageHandledDelegate = @delegate as IChatGui.OnMessageDelegate;
messageHandledDelegate!.Invoke(chattype, senderid, ref parsedSender, ref parsedMessage, ref isHandled); messageHandledDelegate!.Invoke(chattype, senderid, ref parsedSender, ref parsedMessage, ref isHandled);
} }
catch (Exception e) catch (Exception e)

View file

@ -0,0 +1,119 @@
using Dalamud.Game.Gui;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
namespace Dalamud.Plugin.Services;
/// <summary>
/// This class handles interacting with the native chat UI.
/// </summary>
public interface IChatGui
{
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessage"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
/// <param name="isHandled">A value indicating whether the message was handled or should be propagated.</param>
public delegate void OnMessageDelegate(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.CheckMessageHandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
/// <param name="isHandled">A value indicating whether the message was handled or should be propagated.</param>
public delegate void OnCheckMessageHandledDelegate(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessageHandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
public delegate void OnMessageHandledDelegate(XivChatType type, uint senderId, SeString sender, SeString message);
/// <summary>
/// A delegate type used with the <see cref="ChatGui.ChatMessageUnhandled"/> event.
/// </summary>
/// <param name="type">The type of chat.</param>
/// <param name="senderId">The sender ID.</param>
/// <param name="sender">The sender name.</param>
/// <param name="message">The message sent.</param>
public delegate void OnMessageUnhandledDelegate(XivChatType type, uint senderId, SeString sender, SeString message);
/// <summary>
/// Event that will be fired when a chat message is sent to chat by the game.
/// </summary>
public event OnMessageDelegate ChatMessage;
/// <summary>
/// Event that allows you to stop messages from appearing in chat by setting the isHandled parameter to true.
/// </summary>
public event OnCheckMessageHandledDelegate CheckMessageHandled;
/// <summary>
/// Event that will be fired when a chat message is handled by Dalamud or a Plugin.
/// </summary>
public event OnMessageHandledDelegate ChatMessageHandled;
/// <summary>
/// Event that will be fired when a chat message is not handled by Dalamud or a Plugin.
/// </summary>
public event OnMessageUnhandledDelegate ChatMessageUnhandled;
/// <summary>
/// Gets the ID of the last linked item.
/// </summary>
public int LastLinkedItemId { get; }
/// <summary>
/// Gets the flags of the last linked item.
/// </summary>
public byte LastLinkedItemFlags { get; }
/// <summary>
/// Queue a chat message. While method is named as PrintChat, it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="chat">A message to send.</param>
public void PrintChat(XivChatEntry chat);
/// <summary>
/// Queue a chat message. While method is named as PrintChat (it calls it internally), it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void Print(string message);
/// <summary>
/// Queue a chat message. While method is named as PrintChat (it calls it internally), it only add a entry to the queue,
/// later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void Print(SeString message);
/// <summary>
/// Queue an error chat message. While method is named as PrintChat (it calls it internally), it only add a entry to
/// the queue, later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void PrintError(string message);
/// <summary>
/// Queue an error chat message. While method is named as PrintChat (it calls it internally), it only add a entry to
/// the queue, later to be processed when UpdateQueue() is called.
/// </summary>
/// <param name="message">A message to send.</param>
public void PrintError(SeString message);
/// <summary>
/// Process a chat queue.
/// </summary>
public void UpdateQueue();
}