mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-01-03 14:23:40 +01:00
feat: add better IPC
This commit is contained in:
parent
02ea2abf4c
commit
ef3687f8d2
2 changed files with 57 additions and 0 deletions
|
|
@ -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}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,6 +131,20 @@ namespace Dalamud.Plugin
|
|||
|
||||
#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>
|
||||
/// Subscribe to an IPC message by a plugin.
|
||||
/// </summary>
|
||||
|
|
@ -143,6 +157,17 @@ namespace Dalamud.Plugin
|
|||
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>
|
||||
/// Unsubscribe from messages from a plugin.
|
||||
/// </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
|
||||
|
||||
#region Logging
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue