From 6ffeecf1ba8931f5dfb0bb8cf410b058716ad485 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Sun, 13 Feb 2022 14:08:59 -0500 Subject: [PATCH] feat: begin adding ipc --- ChatTwo/IpcManager.cs | 47 +++++++++++++++++++++++++++++++++++++++ ChatTwo/PayloadHandler.cs | 25 +++++++++++++++++++++ ChatTwo/Plugin.cs | 3 +++ 3 files changed, 75 insertions(+) create mode 100755 ChatTwo/IpcManager.cs diff --git a/ChatTwo/IpcManager.cs b/ChatTwo/IpcManager.cs new file mode 100755 index 0000000..e7f6088 --- /dev/null +++ b/ChatTwo/IpcManager.cs @@ -0,0 +1,47 @@ +using Dalamud.Game.Text.SeStringHandling; +using Dalamud.Game.Text.SeStringHandling.Payloads; +using Dalamud.Plugin; +using Dalamud.Plugin.Ipc; + +namespace ChatTwo; + +internal sealed class IpcManager : IDisposable { + private DalamudPluginInterface Interface { get; } + private ICallGateProvider RegisterGate { get; } + private ICallGateProvider UnregisterGate { get; } + private ICallGateProvider InvokeGate { get; } + + internal List Registered { get; } = new(); + + public IpcManager(DalamudPluginInterface pluginInterface) { + this.Interface = pluginInterface; + + this.RegisterGate = this.Interface.GetIpcProvider("ChatTwo.Register"); + this.RegisterGate.RegisterFunc(this.Register); + + this.UnregisterGate = this.Interface.GetIpcProvider("ChatTwo.Unregister"); + this.UnregisterGate.RegisterAction(this.Unregister); + + this.InvokeGate = this.Interface.GetIpcProvider("ChatTwo.Invoke"); + } + + internal void Invoke(string id, PlayerPayload? sender, ulong contentId, Payload? payload) { + this.InvokeGate.SendMessage(id, sender, contentId, payload); + } + + private string Register() { + var id = Guid.NewGuid().ToString(); + this.Registered.Add(id); + return id; + } + + private void Unregister(string id) { + this.Registered.Remove(id); + } + + public void Dispose() { + this.UnregisterGate.UnregisterFunc(); + this.RegisterGate.UnregisterFunc(); + this.Registered.Clear(); + } +} diff --git a/ChatTwo/PayloadHandler.cs b/ChatTwo/PayloadHandler.cs index 461893c..dd8f900 100755 --- a/ChatTwo/PayloadHandler.cs +++ b/ChatTwo/PayloadHandler.cs @@ -74,11 +74,36 @@ internal sealed class PayloadHandler { } this.ContextFooter(drawn, chunk); + this.Integrations(chunk, payload); ImGui.PopID(); ImGui.EndPopup(); } + private void Integrations(Chunk chunk, Payload? payload) { + var registered = this.Ui.Plugin.Ipc.Registered; + if (registered.Count == 0) { + return; + } + + var contentId = chunk.Message?.ContentId ?? 0; + var sender = chunk.Message?.Sender + .Select(chunk => chunk.Link) + .FirstOrDefault(chunk => chunk is PlayerPayload) as PlayerPayload; + + if (ImGui.BeginMenu("Integrations")) { + foreach (var id in registered) { + try { + this.Ui.Plugin.Ipc.Invoke(id, sender, contentId, payload); + } catch (Exception ex) { + PluginLog.Error(ex, "Error executing integration"); + } + } + + ImGui.EndMenu(); + } + } + private void ContextFooter(bool separator, Chunk chunk) { if (separator) { ImGui.Separator(); diff --git a/ChatTwo/Plugin.cs b/ChatTwo/Plugin.cs index 1765526..cde31fc 100755 --- a/ChatTwo/Plugin.cs +++ b/ChatTwo/Plugin.cs @@ -64,6 +64,7 @@ public sealed class Plugin : IDalamudPlugin { internal TextureCache TextureCache { get; } internal GameFunctions.GameFunctions Functions { get; } internal Store Store { get; } + internal IpcManager Ipc { get; } internal PluginUi Ui { get; } internal int DeferredSaveFrames = -1; @@ -84,6 +85,7 @@ public sealed class Plugin : IDalamudPlugin { this.TextureCache = new TextureCache(this.DataManager!); this.Functions = new GameFunctions.GameFunctions(this); this.Store = new Store(this); + this.Ipc = new IpcManager(this.Interface); this.Ui = new PluginUi(this); // let all the other components register, then initialise commands @@ -104,6 +106,7 @@ public sealed class Plugin : IDalamudPlugin { GameFunctions.GameFunctions.SetChatInteractable(true); this.Ui.Dispose(); + this.Ipc.Dispose(); this.Store.Dispose(); this.Functions.Dispose(); this.TextureCache.Dispose();