1
0
forked from liza/Deliveroo

Use Pandora IPC

This commit is contained in:
Liza 2023-10-10 23:16:42 +02:00
parent 591a0806d1
commit 66b248cac3
Signed by: liza
GPG Key ID: 7199F8D727D55F67
2 changed files with 33 additions and 39 deletions

View File

@ -18,7 +18,7 @@ internal sealed class ExternalPluginHandler
var dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog); var dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog);
_yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector); _yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector);
_pandoraIpc = new PandoraIpc(dalamudReflector); _pandoraIpc = new PandoraIpc(pluginInterface, pluginLog);
} }
public bool Saved { get; private set; } public bool Saved { get; private set; }

View File

@ -1,58 +1,52 @@
using System; using Dalamud.Plugin;
using System.Collections; using Dalamud.Plugin.Ipc;
using System.Collections.Generic; using Dalamud.Plugin.Ipc.Exceptions;
using System.Linq; using Dalamud.Plugin.Services;
namespace Deliveroo.External; namespace Deliveroo.External;
internal sealed class PandoraIpc internal sealed class PandoraIpc
{ {
private const string GcTabFeature = "GCVendorDefault"; private const string GcTabFeature = "Default Grand Company Shop Menu";
private readonly DalamudReflector _dalamudReflector; private readonly IPluginLog _pluginLog;
private readonly ICallGateSubscriber<string, bool?> _getEnabled;
private readonly ICallGateSubscriber<string, bool, object?> _setEnabled;
public PandoraIpc(DalamudReflector dalamudReflector) public PandoraIpc(DalamudPluginInterface pluginInterface, IPluginLog pluginLog)
{ {
_dalamudReflector = dalamudReflector; _pluginLog = pluginLog;
} _getEnabled = pluginInterface.GetIpcSubscriber<string, bool?>("PandorasBox.GetFeatureEnabled");
_setEnabled = pluginInterface.GetIpcSubscriber<string, bool, object?>("PandorasBox.SetFeatureEnabled");
private IEnumerable<object>? GetFeatures()
{
if (_dalamudReflector.TryGetDalamudPlugin("Pandora's Box", out var plugin))
{
return ((IEnumerable)plugin!.GetType().GetProperty("Features")!.GetValue(plugin)!).Cast<object>();
}
return null;
}
private object? GetFeature(string name)
{
IEnumerable<object> features = GetFeatures() ?? Array.Empty<object>();
return features.FirstOrDefault(x => x.GetType().Name == name);
} }
public bool? DisableIfNecessary() public bool? DisableIfNecessary()
{ {
object? feature = GetFeature(GcTabFeature); try
if (feature == null)
return null;
if ((bool)feature.GetType().GetProperty("Enabled")!.GetValue(feature)!)
{ {
feature.GetType().GetMethod("Disable")!.Invoke(feature, Array.Empty<object>()); bool? enabled = _getEnabled.InvokeFunc(GcTabFeature);
return true; _pluginLog.Information($"Pandora's {GcTabFeature} is {enabled?.ToString() ?? "null"}");
} if (enabled == true)
_setEnabled.InvokeAction(GcTabFeature, false);
return false; return enabled;
}
catch (IpcNotReadyError e)
{
_pluginLog.Information(e, "Unable to read pandora state");
return null;
}
} }
public void Enable() public void Enable()
{ {
object? feature = GetFeature(GcTabFeature); try
if (feature == null) {
return; _setEnabled.InvokeAction(GcTabFeature, true);
}
feature.GetType().GetMethod("Enable")!.Invoke(feature, Array.Empty<object>()); catch (IpcNotReadyError e)
{
_pluginLog.Error(e, "Unable to restore pandora state");
}
} }
} }