diff --git a/Deliveroo/Deliveroo.csproj b/Deliveroo/Deliveroo.csproj index 24fdcd1..8d7561d 100644 --- a/Deliveroo/Deliveroo.csproj +++ b/Deliveroo/Deliveroo.csproj @@ -1,7 +1,7 @@ net7.0-windows - 2.2 + 2.3 11.0 enable true diff --git a/Deliveroo/DeliverooPlugin.cs b/Deliveroo/DeliverooPlugin.cs index 86236e3..0d74a36 100644 --- a/Deliveroo/DeliverooPlugin.cs +++ b/Deliveroo/DeliverooPlugin.cs @@ -35,7 +35,7 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable private readonly Configuration _configuration; - private readonly YesAlreadyIpc _yesAlreadyIpc; + private readonly ExternalPluginHandler _externalPluginHandler; // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable private readonly GcRewardsCache _gcRewardsCache; @@ -47,7 +47,6 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin private Stage _currentStageInternal = Stage.Stopped; private DateTime _continueAt = DateTime.MinValue; private List _itemsToPurchaseNow = new(); - private (bool Saved, bool? PreviousState) _yesAlreadyState = (false, null); public DeliverooPlugin(DalamudPluginInterface pluginInterface, IChatGui chatGui, IGameGui gameGui, IFramework framework, IClientState clientState, IObjectTable objectTable, ITargetManager targetManager, @@ -64,8 +63,7 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin _commandManager = commandManager; _pluginLog = pluginLog; - var dalamudReflector = new DalamudReflector(_pluginInterface, _framework, _pluginLog); - _yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector); + _externalPluginHandler = new ExternalPluginHandler(_pluginInterface, _framework, _pluginLog); _configuration = (Configuration?)_pluginInterface.GetPluginConfig() ?? new Configuration(); _gcRewardsCache = new GcRewardsCache(dataManager); _configWindow = new ConfigWindow(_pluginInterface, this, _configuration, _gcRewardsCache, _clientState, _pluginLog); @@ -161,7 +159,7 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin _turnInWindow.State = false; if (CurrentStage != Stage.Stopped) { - RestoreYesAlready(); + _externalPluginHandler.Restore(); CurrentStage = Stage.Stopped; } } @@ -174,7 +172,7 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin { if (CurrentStage != Stage.Stopped) { - RestoreYesAlready(); + _externalPluginHandler.Restore(); CurrentStage = Stage.Stopped; } @@ -207,8 +205,8 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin CurrentStage = Stage.SelectRewardTier; } - if (CurrentStage != Stage.Stopped && CurrentStage != Stage.RequestStop && !_yesAlreadyState.Saved) - SaveYesAlready(); + if (CurrentStage != Stage.Stopped && CurrentStage != Stage.RequestStop && !_externalPluginHandler.Saved) + _externalPluginHandler.Save(); switch (CurrentStage) { @@ -277,7 +275,7 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin break; case Stage.RequestStop: - RestoreYesAlready(); + _externalPluginHandler.Restore(); CurrentStage = Stage.Stopped; break; @@ -301,32 +299,8 @@ public sealed partial class DeliverooPlugin : IDalamudPlugin _pluginInterface.UiBuilder.Draw -= _windowSystem.Draw; _framework.Update -= FrameworkUpdate; - RestoreYesAlready(); + _externalPluginHandler.Restore(); } private void ProcessCommand(string command, string arguments) => _configWindow.Toggle(); - - private void SaveYesAlready() - { - if (_yesAlreadyState.Saved) - { - _pluginLog.Information("Not overwriting yesalready state"); - return; - } - - _yesAlreadyState = (true, _yesAlreadyIpc.DisableIfNecessary()); - _pluginLog.Information($"Previous yesalready state: {_yesAlreadyState.PreviousState}"); - } - - private void RestoreYesAlready() - { - if (_yesAlreadyState.Saved) - { - _pluginLog.Information($"Restoring previous yesalready state: {_yesAlreadyState.PreviousState}"); - if (_yesAlreadyState.PreviousState == true) - _yesAlreadyIpc.Enable(); - } - - _yesAlreadyState = (false, null); - } } diff --git a/Deliveroo/External/ExternalPluginHandler.cs b/Deliveroo/External/ExternalPluginHandler.cs new file mode 100644 index 0000000..3ee7ca8 --- /dev/null +++ b/Deliveroo/External/ExternalPluginHandler.cs @@ -0,0 +1,78 @@ +using Dalamud.Plugin; +using Dalamud.Plugin.Services; + +namespace Deliveroo.External; + +internal sealed class ExternalPluginHandler +{ + private readonly IPluginLog _pluginLog; + private readonly YesAlreadyIpc _yesAlreadyIpc; + private readonly PandoraIpc _pandoraIpc; + + private bool? _yesAlreadyState; + private bool? _pandoraState; + + public ExternalPluginHandler(DalamudPluginInterface pluginInterface, IFramework framework, IPluginLog pluginLog) + { + _pluginLog = pluginLog; + + var dalamudReflector = new DalamudReflector(pluginInterface, framework, pluginLog); + _yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector); + _pandoraIpc = new PandoraIpc(dalamudReflector); + } + + public bool Saved { get; private set; } + + public void Save() + { + if (Saved) + { + _pluginLog.Information("Not overwriting external plugin state"); + return; + } + + _pluginLog.Information("Saving external plugin state..."); + SaveYesAlreadyState(); + SavePandoraState(); + Saved = true; + } + + private void SaveYesAlreadyState() + { + _yesAlreadyState = _yesAlreadyIpc.DisableIfNecessary(); + _pluginLog.Information($"Previous yesalready state: {_yesAlreadyState}"); + } + + private void SavePandoraState() + { + _pandoraState = _pandoraIpc.DisableIfNecessary(); + _pluginLog.Info($"Previous pandora feature state: {_pandoraState}"); + } + + public void Restore() + { + if (Saved) + { + RestoreYesAlready(); + RestorePandora(); + } + + Saved = false; + _yesAlreadyState = null; + _pandoraState = null; + } + + private void RestoreYesAlready() + { + _pluginLog.Information($"Restoring previous yesalready state: {_yesAlreadyState}"); + if (_yesAlreadyState == true) + _yesAlreadyIpc.Enable(); + } + + private void RestorePandora() + { + _pluginLog.Information($"Restoring previous pandora state: {_pandoraState}"); + if (_pandoraState == true) + _pandoraIpc.Enable(); + } +} diff --git a/Deliveroo/External/PandoraIpc.cs b/Deliveroo/External/PandoraIpc.cs new file mode 100644 index 0000000..23ad466 --- /dev/null +++ b/Deliveroo/External/PandoraIpc.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace Deliveroo.External; + +internal sealed class PandoraIpc +{ + private const string GcTabFeature = "GCVendorDefault"; + + private readonly DalamudReflector _dalamudReflector; + + public PandoraIpc(DalamudReflector dalamudReflector) + { + _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); + } + + public bool? DisableIfNecessary() + { + object? feature = GetFeature(GcTabFeature); + if (feature == null) + return null; + + if ((bool)feature.GetType().GetProperty("Enabled")!.GetValue(feature)!) + { + feature.GetType().GetMethod("Disable")!.Invoke(feature, Array.Empty()); + return true; + } + + return false; + } + + public void Enable() + { + object? feature = GetFeature(GcTabFeature); + if (feature == null) + return; + + feature.GetType().GetMethod("Enable")!.Invoke(feature, Array.Empty()); + } +}