mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-21 07:17:45 +01:00
feat: add plugin ipc
This commit is contained in:
parent
054315dfc0
commit
09295d4e61
3 changed files with 73 additions and 2 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
|
@ -115,6 +116,45 @@ namespace Dalamud.Plugin
|
|||
return this.configs.Load(this.pluginName);
|
||||
}
|
||||
|
||||
#region IPC
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe to an IPC message by a plugin.
|
||||
/// </summary>
|
||||
/// <param name="pluginName">The InternalName of the plugin to subscribe to.</param>
|
||||
/// <param name="action">The action to take when a message was received.</param>
|
||||
public void Subscribe(string pluginName, Action<ExpandoObject> action) {
|
||||
if (this.dalamud.PluginManager.IpcSubscriptions.Any(x => x.SourcePluginName == this.pluginName && x.SubPluginName == pluginName))
|
||||
throw new InvalidOperationException("Can't add multiple subscriptions for the same plugin.");
|
||||
|
||||
this.dalamud.PluginManager.IpcSubscriptions.Add((this.pluginName, pluginName, action));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribe from messages from a plugin.
|
||||
/// </summary>
|
||||
/// <param name="pluginName">The InternalName of the plugin to unsubscribe from.</param>
|
||||
public void Unsubscribe(string pluginName) {
|
||||
var sub = this.dalamud.PluginManager.IpcSubscriptions.FirstOrDefault(x => x.SourcePluginName == this.pluginName && x.SubPluginName == pluginName);
|
||||
if (sub.SubAction == null)
|
||||
throw new InvalidOperationException("Wasn't subscribed to this plugin.");
|
||||
|
||||
this.dalamud.PluginManager.IpcSubscriptions.Remove(sub);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a message to all subscribed plugins.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to send.</param>
|
||||
public void SendMessage(ExpandoObject message) {
|
||||
var subs = this.dalamud.PluginManager.IpcSubscriptions.Where(x => x.SubPluginName == this.pluginName);
|
||||
foreach (var sub in subs.Select(x => x.SubAction)) {
|
||||
sub.Invoke(message);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Logging
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
|
@ -20,6 +22,8 @@ namespace Dalamud.Plugin
|
|||
|
||||
public readonly List<(IDalamudPlugin Plugin, PluginDefinition Definition, DalamudPluginInterface PluginInterface)> Plugins = new List<(IDalamudPlugin plugin, PluginDefinition def, DalamudPluginInterface PluginInterface)>();
|
||||
|
||||
public List<(string SourcePluginName, string SubPluginName, Action<ExpandoObject> SubAction)> IpcSubscriptions = new List<(string SourcePluginName, string SubPluginName, Action<ExpandoObject> SubAction)>();
|
||||
|
||||
public PluginManager(Dalamud dalamud, string pluginDirectory, string devPluginDirectory) {
|
||||
this.dalamud = dalamud;
|
||||
this.pluginDirectory = pluginDirectory;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue