feat: add plugin ipc

This commit is contained in:
goat 2020-05-14 21:50:04 +02:00
parent 054315dfc0
commit 09295d4e61
3 changed files with 73 additions and 2 deletions

View file

@ -1,11 +1,17 @@
using System;
using System.Dynamic;
using System.Linq;
using System.Net.Mime;
using System.Numerics;
using Dalamud.Game.Chat;
using Dalamud.Game.ClientState.Actors.Types;
using Dalamud.Game.ClientState.Actors.Types.NonPlayer;
using Dalamud.Plugin;
using ImGuiNET;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Serilog;
using SharpDX.Direct3D11;
namespace Dalamud.Interface
{
@ -51,8 +57,8 @@ namespace Dalamud.Interface
ImGui.SameLine();
var copy = ImGui.Button("Copy all");
ImGui.SameLine();
ImGui.Combo("Data kind", ref this.currentKind, new[] {"ServerOpCode", "ContentFinderCondition", "Actor Table", "Font Test", "Party List"},
5);
ImGui.Combo("Data kind", ref this.currentKind, new[] {"ServerOpCode", "ContentFinderCondition", "Actor Table", "Font Test", "Party List", "Plugin IPC"},
6);
ImGui.BeginChild("scrolling", new Vector2(0, 0), false, ImGuiWindowFlags.HorizontalScrollbar);
@ -165,6 +171,27 @@ namespace Dalamud.Interface
ImGui.TextUnformatted(partyString);
}
break;
case 5:
var i1 = new DalamudPluginInterface(this.dalamud, "DalamudTestSub", null);
var i2 = new DalamudPluginInterface(this.dalamud, "DalamudTestPub", null);
if (ImGui.Button("Add test sub")) i1.Subscribe("DalamudTestPub", o => {
dynamic msg = o;
Log.Debug(msg.Expand);
});
if (ImGui.Button("Remove test sub")) i1.Unsubscribe("DalamudTestPub");
if (ImGui.Button("Send test message")) {
dynamic testMsg = new ExpandoObject();
testMsg.Expand = "dong";
i2.SendMessage(testMsg);
}
foreach (var sub in this.dalamud.PluginManager.IpcSubscriptions) {
ImGui.Text($"Source:{sub.SourcePluginName} Sub:{sub.SubPluginName}");
}
break;
}
else

View file

@ -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>

View file

@ -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;