From 66b248cac3c03badc06a95e6f390c563f34ede54 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Tue, 10 Oct 2023 23:16:42 +0200 Subject: [PATCH] Use Pandora IPC --- Deliveroo/External/ExternalPluginHandler.cs | 2 +- Deliveroo/External/PandoraIpc.cs | 70 ++++++++++----------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/Deliveroo/External/ExternalPluginHandler.cs b/Deliveroo/External/ExternalPluginHandler.cs index 3ee7ca8..3e8cd60 100644 --- a/Deliveroo/External/ExternalPluginHandler.cs +++ b/Deliveroo/External/ExternalPluginHandler.cs @@ -18,7 +18,7 @@ internal sealed class ExternalPluginHandler var dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog); _yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector); - _pandoraIpc = new PandoraIpc(dalamudReflector); + _pandoraIpc = new PandoraIpc(pluginInterface, pluginLog); } public bool Saved { get; private set; } diff --git a/Deliveroo/External/PandoraIpc.cs b/Deliveroo/External/PandoraIpc.cs index 23ad466..da188fc 100644 --- a/Deliveroo/External/PandoraIpc.cs +++ b/Deliveroo/External/PandoraIpc.cs @@ -1,58 +1,52 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; +using Dalamud.Plugin; +using Dalamud.Plugin.Ipc; +using Dalamud.Plugin.Ipc.Exceptions; +using Dalamud.Plugin.Services; namespace Deliveroo.External; 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 _getEnabled; + private readonly ICallGateSubscriber _setEnabled; - public PandoraIpc(DalamudReflector dalamudReflector) + public PandoraIpc(DalamudPluginInterface pluginInterface, IPluginLog pluginLog) { - _dalamudReflector = dalamudReflector; - } - - private IEnumerable? GetFeatures() - { - if (_dalamudReflector.TryGetDalamudPlugin("Pandora's Box", out var plugin)) - { - return ((IEnumerable)plugin!.GetType().GetProperty("Features")!.GetValue(plugin)!).Cast(); - } - - return null; - } - - private object? GetFeature(string name) - { - IEnumerable features = GetFeatures() ?? Array.Empty(); - return features.FirstOrDefault(x => x.GetType().Name == name); + _pluginLog = pluginLog; + _getEnabled = pluginInterface.GetIpcSubscriber("PandorasBox.GetFeatureEnabled"); + _setEnabled = pluginInterface.GetIpcSubscriber("PandorasBox.SetFeatureEnabled"); } public bool? DisableIfNecessary() { - object? feature = GetFeature(GcTabFeature); - if (feature == null) - return null; - - if ((bool)feature.GetType().GetProperty("Enabled")!.GetValue(feature)!) + try { - feature.GetType().GetMethod("Disable")!.Invoke(feature, Array.Empty()); - return true; - } + bool? enabled = _getEnabled.InvokeFunc(GcTabFeature); + _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() { - object? feature = GetFeature(GcTabFeature); - if (feature == null) - return; - - feature.GetType().GetMethod("Enable")!.Invoke(feature, Array.Empty()); + try + { + _setEnabled.InvokeAction(GcTabFeature, true); + } + catch (IpcNotReadyError e) + { + _pluginLog.Error(e, "Unable to restore pandora state"); + } } }