From 50cb486cdad64b660dc3feb1d93d371e9d586f08 Mon Sep 17 00:00:00 2001 From: Infi Date: Fri, 3 May 2024 06:06:24 +0200 Subject: [PATCH] Don't process chunks in a thread --- ChatTwo/ChatTwo.csproj | 2 +- ChatTwo/GameFunctions/Chat.cs | 2 +- ChatTwo/MessageManager.cs | 77 +++++++++++++---------------------- 3 files changed, 30 insertions(+), 51 deletions(-) diff --git a/ChatTwo/ChatTwo.csproj b/ChatTwo/ChatTwo.csproj index 4f5b547..2afe5a1 100755 --- a/ChatTwo/ChatTwo.csproj +++ b/ChatTwo/ChatTwo.csproj @@ -1,6 +1,6 @@ - 1.23.3 + 1.23.4 net8.0-windows enable enable diff --git a/ChatTwo/GameFunctions/Chat.cs b/ChatTwo/GameFunctions/Chat.cs index 27ad31c..cda932a 100755 --- a/ChatTwo/GameFunctions/Chat.cs +++ b/ChatTwo/GameFunctions/Chat.cs @@ -626,7 +626,7 @@ internal sealed unsafe class Chat : IDisposable EurekaContextMenuTellHook!.Original(param1, playerName, worldName, world, id, param6); } - internal ulong? GetContentIdForEntry(uint index) + internal ulong GetContentIdForEntry(uint index) { return Framework.Instance()->GetUiModule()->GetRaptureLogModule()->GetContentIdForLogMessage((int) index); } diff --git a/ChatTwo/MessageManager.cs b/ChatTwo/MessageManager.cs index ba672a7..c0a15c0 100644 --- a/ChatTwo/MessageManager.cs +++ b/ChatTwo/MessageManager.cs @@ -21,7 +21,7 @@ internal class MessageManager : IAsyncDisposable private Dictionary Formats { get; } = new(); private ulong LastContentId { get; set; } - private ConcurrentQueue Pending { get; } = new(); + private ConcurrentQueue Pending { get; } = new(); private ulong LastMessageIndex { get; set; } private readonly Thread PendingMessageThread; @@ -147,15 +147,29 @@ internal class MessageManager : IAsyncDisposable private void ChatMessage(XivChatType type, uint senderId, SeString sender, SeString message) { LastMessage = (sender, message); - var pendingMessage = new PendingMessage + + var chatCode = new ChatCode((ushort)type); + + NameFormatting? formatting = null; + if (sender.Payloads.Count > 0) + formatting = FormatFor(chatCode.Type); + + var senderChunks = new List(); + if (formatting is { IsPresent: true }) { - ReceiverId = CurrentContentId, - ContentId = 0, - Type = type, - SenderId = senderId, - Sender = sender, - Content = message, - }; + senderChunks.Add(new TextChunk(ChunkSource.None, null, formatting.Before) + { + FallbackColour = chatCode.Type, + }); + senderChunks.AddRange(ChunkUtil.ToChunks(sender, ChunkSource.Sender, chatCode.Type)); + senderChunks.Add(new TextChunk(ChunkSource.None, null, formatting.After) + { + FallbackColour = chatCode.Type, + }); + } + + var contentChunks = ChunkUtil.ToChunks(message, ChunkSource.Content, chatCode.Type).ToList(); + var msg = new Message(CurrentContentId, 0, chatCode, senderChunks, contentChunks, sender, message); // If the message was rendered in the vanilla chat log window it has an // index, and we can use that to get the sender's content ID. The @@ -169,47 +183,22 @@ internal class MessageManager : IAsyncDisposable // that you received the message, or you just get null. Plugin.Framework.RunOnTick(() => { - var contentId = Plugin.Functions.Chat.GetContentIdForEntry(idx - 1); - pendingMessage.ContentId = contentId ?? 0; - Pending.Enqueue(pendingMessage); + msg.ContentId = Plugin.Functions.Chat.GetContentIdForEntry(idx - 1); + Pending.Enqueue(msg); }); + return; } - Pending.Enqueue(pendingMessage); + Pending.Enqueue(msg); } - private void ProcessMessage(PendingMessage pendingMessage) + private void ProcessMessage(Message msg) { - var chatCode = new ChatCode((ushort)pendingMessage.Type); - - NameFormatting? formatting = null; - if (pendingMessage.Sender.Payloads.Count > 0) - formatting = FormatFor(chatCode.Type); - - var senderChunks = new List(); - if (formatting is { IsPresent: true }) - { - senderChunks.Add(new TextChunk(ChunkSource.None, null, formatting.Before) - { - FallbackColour = chatCode.Type, - }); - senderChunks.AddRange(ChunkUtil.ToChunks(pendingMessage.Sender, ChunkSource.Sender, chatCode.Type)); - senderChunks.Add(new TextChunk(ChunkSource.None, null, formatting.After) - { - FallbackColour = chatCode.Type, - }); - } - - var contentChunks = ChunkUtil.ToChunks(pendingMessage.Content, ChunkSource.Content, chatCode.Type).ToList(); - - var msg = new Message(pendingMessage.ReceiverId, pendingMessage.ContentId, chatCode, senderChunks, contentChunks, pendingMessage.Sender, pendingMessage.Content); - if (Plugin.Config.DatabaseBattleMessages || !msg.Code.IsBattle()) Store.UpsertMessage(msg); var currentMatches = Plugin.ChatLogWindow.CurrentTab?.Matches(msg) ?? false; - foreach (var tab in Plugin.Config.Tabs) { var unread = !(tab.UnreadMode == UnreadMode.Unseen && Plugin.ChatLogWindow.CurrentTab != tab && currentMatches); @@ -279,14 +268,4 @@ internal class MessageManager : IAsyncDisposable return nameFormatting; } - - private class PendingMessage - { - internal ulong ReceiverId { get; set; } - internal ulong ContentId { get; set; } // 0 if unknown - internal XivChatType Type { get; set; } - internal uint SenderId { get; set; } - internal SeString Sender { get; set; } - internal SeString Content { get; set; } - } }