diff --git a/Dalamud/Interface/DalamudDataWindow.cs b/Dalamud/Interface/DalamudDataWindow.cs index c0cded6e1..0636d5ad2 100644 --- a/Dalamud/Interface/DalamudDataWindow.cs +++ b/Dalamud/Interface/DalamudDataWindow.cs @@ -190,14 +190,29 @@ namespace Dalamud.Interface Log.Debug(msg.Expand); }); + if (ImGui.Button("Add test sub any")) i1.SubscribeAny((o, a) => { + dynamic msg = a; + Log.Debug($"From {o}: {msg.Expand}"); + }); + if (ImGui.Button("Remove test sub")) i1.Unsubscribe("DalamudTestPub"); + if (ImGui.Button("Remove test sub any")) i1.UnsubscribeAny(); + if (ImGui.Button("Send test message")) { dynamic testMsg = new ExpandoObject(); testMsg.Expand = "dong"; i2.SendMessage(testMsg); } + // This doesn't actually work, so don't mind it - impl relies on plugins being registered in PluginManager + if (ImGui.Button("Send test message any")) + { + dynamic testMsg = new ExpandoObject(); + testMsg.Expand = "dong"; + i2.SendMessage("DalamudTestSub", testMsg); + } + foreach (var sub in this.dalamud.PluginManager.IpcSubscriptions) { ImGui.Text($"Source:{sub.SourcePluginName} Sub:{sub.SubPluginName}"); } diff --git a/Dalamud/Plugin/DalamudPluginInterface.cs b/Dalamud/Plugin/DalamudPluginInterface.cs index bda21c643..fd7230c0a 100644 --- a/Dalamud/Plugin/DalamudPluginInterface.cs +++ b/Dalamud/Plugin/DalamudPluginInterface.cs @@ -131,6 +131,20 @@ namespace Dalamud.Plugin #region IPC + internal Action anyPluginIpcAction; + + /// + /// Subscribe to an IPC message by any plugin. + /// + /// The action to take when a message was received. + public void SubscribeAny(Action action) + { + if (this.anyPluginIpcAction != null) + throw new InvalidOperationException("Can't subscribe multiple times."); + + this.anyPluginIpcAction = action; + } + /// /// Subscribe to an IPC message by a plugin. /// @@ -143,6 +157,17 @@ namespace Dalamud.Plugin this.dalamud.PluginManager.IpcSubscriptions.Add((this.pluginName, pluginName, action)); } + /// + /// Unsubscribe from messages from any plugin. + /// + public void UnsubscribeAny() + { + if (this.anyPluginIpcAction == null) + throw new InvalidOperationException("Wasn't subscribed to this plugin."); + + this.anyPluginIpcAction = null; + } + /// /// Unsubscribe from messages from a plugin. /// @@ -166,6 +191,23 @@ namespace Dalamud.Plugin } } + /// + /// Send a message to a specific plugin. + /// + /// The InternalName of the plugin to send the message to. + /// The message to send. + /// True if the corresponding plugin was present and received the message. + public bool SendMessage(string pluginName, ExpandoObject message) + { + var (_, _, pluginInterface) = this.dalamud.PluginManager.Plugins.FirstOrDefault(x => x.Definition.InternalName == this.pluginName); + + if (pluginInterface?.anyPluginIpcAction == null) + return false; + + pluginInterface.anyPluginIpcAction.Invoke(this.pluginName, message); + return true; + } + #endregion #region Logging