feat: add better IPC

This commit is contained in:
goat 2020-08-04 17:30:15 +02:00
parent 02ea2abf4c
commit ef3687f8d2
2 changed files with 57 additions and 0 deletions

View file

@ -190,14 +190,29 @@ namespace Dalamud.Interface
Log.Debug(msg.Expand); 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")) i1.Unsubscribe("DalamudTestPub");
if (ImGui.Button("Remove test sub any")) i1.UnsubscribeAny();
if (ImGui.Button("Send test message")) { if (ImGui.Button("Send test message")) {
dynamic testMsg = new ExpandoObject(); dynamic testMsg = new ExpandoObject();
testMsg.Expand = "dong"; testMsg.Expand = "dong";
i2.SendMessage(testMsg); 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) { foreach (var sub in this.dalamud.PluginManager.IpcSubscriptions) {
ImGui.Text($"Source:{sub.SourcePluginName} Sub:{sub.SubPluginName}"); ImGui.Text($"Source:{sub.SourcePluginName} Sub:{sub.SubPluginName}");
} }

View file

@ -131,6 +131,20 @@ namespace Dalamud.Plugin
#region IPC #region IPC
internal Action<string, ExpandoObject> anyPluginIpcAction;
/// <summary>
/// Subscribe to an IPC message by any plugin.
/// </summary>
/// <param name="action">The action to take when a message was received.</param>
public void SubscribeAny(Action<string, ExpandoObject> action)
{
if (this.anyPluginIpcAction != null)
throw new InvalidOperationException("Can't subscribe multiple times.");
this.anyPluginIpcAction = action;
}
/// <summary> /// <summary>
/// Subscribe to an IPC message by a plugin. /// Subscribe to an IPC message by a plugin.
/// </summary> /// </summary>
@ -143,6 +157,17 @@ namespace Dalamud.Plugin
this.dalamud.PluginManager.IpcSubscriptions.Add((this.pluginName, pluginName, action)); this.dalamud.PluginManager.IpcSubscriptions.Add((this.pluginName, pluginName, action));
} }
/// <summary>
/// Unsubscribe from messages from any plugin.
/// </summary>
public void UnsubscribeAny()
{
if (this.anyPluginIpcAction == null)
throw new InvalidOperationException("Wasn't subscribed to this plugin.");
this.anyPluginIpcAction = null;
}
/// <summary> /// <summary>
/// Unsubscribe from messages from a plugin. /// Unsubscribe from messages from a plugin.
/// </summary> /// </summary>
@ -166,6 +191,23 @@ namespace Dalamud.Plugin
} }
} }
/// <summary>
/// Send a message to a specific plugin.
/// </summary>
/// <param name="pluginName">The InternalName of the plugin to send the message to.</param>
/// <param name="message">The message to send.</param>
/// <returns>True if the corresponding plugin was present and received the message.</returns>
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 #endregion
#region Logging #region Logging