Disable Pandora's GCVendorDefault feature during turn-in

This commit is contained in:
Liza 2023-10-08 15:39:29 +02:00
parent a6005bdaba
commit 3e68dd6e79
Signed by: liza
GPG Key ID: 7199F8D727D55F67
4 changed files with 145 additions and 35 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<Version>2.2</Version>
<Version>2.3</Version>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

View File

@ -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<PurchaseItemRequest> _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);
}
}

View File

@ -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();
}
}

58
Deliveroo/External/PandoraIpc.cs vendored Normal file
View File

@ -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<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()
{
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<object>());
return true;
}
return false;
}
public void Enable()
{
object? feature = GetFeature(GcTabFeature);
if (feature == null)
return;
feature.GetType().GetMethod("Enable")!.Invoke(feature, Array.Empty<object>());
}
}