1
0
forked from liza/Deliveroo

YesAlready integration

This commit is contained in:
Liza 2023-09-22 21:44:56 +02:00
parent 7a5dbe095d
commit c663c03b10
Signed by: liza
GPG Key ID: 7199F8D727D55F67
3 changed files with 230 additions and 12 deletions

View File

@ -14,6 +14,7 @@ using Dalamud.Interface.Windowing;
using Dalamud.Logging; using Dalamud.Logging;
using Dalamud.Memory; using Dalamud.Memory;
using Dalamud.Plugin; using Dalamud.Plugin;
using Deliveroo.External;
using Deliveroo.GameData; using Deliveroo.GameData;
using Deliveroo.Windows; using Deliveroo.Windows;
using FFXIVClientStructs.FFXIV.Client.Game; using FFXIVClientStructs.FFXIV.Client.Game;
@ -44,15 +45,20 @@ public sealed class DeliverooPlugin : IDalamudPlugin
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly Configuration _configuration; private readonly Configuration _configuration;
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly YesAlreadyIpc _yesAlreadyIpc;
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly GcRewardsCache _gcRewardsCache; private readonly GcRewardsCache _gcRewardsCache;
private readonly ConfigWindow _configWindow; private readonly ConfigWindow _configWindow;
private readonly TurnInWindow _turnInWindow; private readonly TurnInWindow _turnInWindow;
private readonly IReadOnlyDictionary<uint, uint> _sealCaps; private readonly IReadOnlyDictionary<uint, uint> _sealCaps;
private Stage _currentStageInternal = Stage.Stop; private Stage _currentStageInternal = Stage.Stopped;
private DateTime _continueAt = DateTime.MinValue; private DateTime _continueAt = DateTime.MinValue;
private GcRewardItem _selectedRewardItem = GcRewardItem.None; private GcRewardItem _selectedRewardItem = GcRewardItem.None;
private (bool Saved, bool? PreviousState) _yesAlreadyState = (false, null);
public DeliverooPlugin(DalamudPluginInterface pluginInterface, ChatGui chatGui, GameGui gameGui, public DeliverooPlugin(DalamudPluginInterface pluginInterface, ChatGui chatGui, GameGui gameGui,
Framework framework, ClientState clientState, ObjectTable objectTable, TargetManager targetManager, Framework framework, ClientState clientState, ObjectTable objectTable, TargetManager targetManager,
@ -66,6 +72,8 @@ public sealed class DeliverooPlugin : IDalamudPlugin
_objectTable = objectTable; _objectTable = objectTable;
_targetManager = targetManager; _targetManager = targetManager;
var dalamudReflector = new DalamudReflector(_pluginInterface, _framework);
_yesAlreadyIpc = new YesAlreadyIpc(dalamudReflector);
_configuration = (Configuration?)_pluginInterface.GetPluginConfig() ?? new Configuration(); _configuration = (Configuration?)_pluginInterface.GetPluginConfig() ?? new Configuration();
_gcRewardsCache = new GcRewardsCache(dataManager); _gcRewardsCache = new GcRewardsCache(dataManager);
_configWindow = new ConfigWindow(_pluginInterface, this, _configuration, _gcRewardsCache); _configWindow = new ConfigWindow(_pluginInterface, this, _configuration, _gcRewardsCache);
@ -104,7 +112,11 @@ public sealed class DeliverooPlugin : IDalamudPlugin
{ {
_turnInWindow.IsOpen = false; _turnInWindow.IsOpen = false;
_turnInWindow.State = false; _turnInWindow.State = false;
CurrentStage = Stage.Stop; if (CurrentStage != Stage.Stopped)
{
RestoreYesAlready();
CurrentStage = Stage.Stopped;
}
} }
else if (DateTime.Now > _continueAt) else if (DateTime.Now > _continueAt)
{ {
@ -113,11 +125,15 @@ public sealed class DeliverooPlugin : IDalamudPlugin
if (!_turnInWindow.State) if (!_turnInWindow.State)
{ {
CurrentStage = Stage.Stop; if (CurrentStage != Stage.Stopped)
{
RestoreYesAlready();
CurrentStage = Stage.Stopped;
}
return; return;
} }
else if (_turnInWindow.State && CurrentStage == Stage.Stopped)
if (_turnInWindow.State && CurrentStage == Stage.Stop)
{ {
CurrentStage = Stage.TargetPersonnelOfficer; CurrentStage = Stage.TargetPersonnelOfficer;
_selectedRewardItem = _turnInWindow.SelectedItem; _selectedRewardItem = _turnInWindow.SelectedItem;
@ -136,6 +152,9 @@ public sealed class DeliverooPlugin : IDalamudPlugin
CurrentStage = Stage.CloseGcExchange; CurrentStage = Stage.CloseGcExchange;
} }
if (CurrentStage != Stage.Stopped && CurrentStage != Stage.RequestStop && !_yesAlreadyState.Saved)
SaveYesAlready();
switch (CurrentStage) switch (CurrentStage)
{ {
case Stage.TargetPersonnelOfficer: case Stage.TargetPersonnelOfficer:
@ -238,7 +257,7 @@ public sealed class DeliverooPlugin : IDalamudPlugin
if (!_selectedRewardItem.IsValid()) if (!_selectedRewardItem.IsValid())
{ {
_turnInWindow.State = false; _turnInWindow.State = false;
CurrentStage = Stage.Stop; CurrentStage = Stage.RequestStop;
} }
else else
{ {
@ -256,13 +275,13 @@ public sealed class DeliverooPlugin : IDalamudPlugin
if (!_selectedRewardItem.IsValid()) if (!_selectedRewardItem.IsValid())
{ {
_turnInWindow.State = false; _turnInWindow.State = false;
CurrentStage = Stage.Stop; CurrentStage = Stage.RequestStop;
} }
else if (GetCurrentSealCount() <= else if (GetCurrentSealCount() <=
_configuration.ReservedSealCount + _selectedRewardItem.SealCost) _configuration.ReservedSealCount + _selectedRewardItem.SealCost)
{ {
_turnInWindow.State = false; _turnInWindow.State = false;
CurrentStage = Stage.Stop; CurrentStage = Stage.RequestStop;
} }
else else
{ {
@ -276,7 +295,7 @@ public sealed class DeliverooPlugin : IDalamudPlugin
case Stage.TargetQuartermaster: case Stage.TargetQuartermaster:
if (GetCurrentSealCount() < _configuration.ReservedSealCount) if (GetCurrentSealCount() < _configuration.ReservedSealCount)
{ {
CurrentStage = Stage.Stop; CurrentStage = Stage.RequestStop;
break; break;
} }
@ -381,7 +400,12 @@ public sealed class DeliverooPlugin : IDalamudPlugin
break; break;
} }
case Stage.Stop: case Stage.RequestStop:
RestoreYesAlready();
CurrentStage = Stage.Stopped;
break;
case Stage.Stopped:
break; break;
default: default:
PluginLog.Warning($"Unknown stage {CurrentStage}"); PluginLog.Warning($"Unknown stage {CurrentStage}");
@ -418,6 +442,8 @@ public sealed class DeliverooPlugin : IDalamudPlugin
_pluginInterface.UiBuilder.OpenConfigUi -= _configWindow.Toggle; _pluginInterface.UiBuilder.OpenConfigUi -= _configWindow.Toggle;
_pluginInterface.UiBuilder.Draw -= _windowSystem.Draw; _pluginInterface.UiBuilder.Draw -= _windowSystem.Draw;
_framework.Update -= FrameworkUpdate; _framework.Update -= FrameworkUpdate;
RestoreYesAlready();
} }
private unsafe List<TurnInItem> BuildTurnInList(AgentGrandCompanySupply* agent) private unsafe List<TurnInItem> BuildTurnInList(AgentGrandCompanySupply* agent)
@ -514,7 +540,7 @@ public sealed class DeliverooPlugin : IDalamudPlugin
if (toBuy == 0) if (toBuy == 0)
{ {
_turnInWindow.State = false; _turnInWindow.State = false;
CurrentStage = Stage.Stop; CurrentStage = Stage.RequestStop;
break; break;
} }
@ -645,6 +671,30 @@ public sealed class DeliverooPlugin : IDalamudPlugin
(FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)obj.Address, false); (FFXIVClientStructs.FFXIV.Client.Game.Object.GameObject*)obj.Address, false);
} }
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);
}
internal enum Stage internal enum Stage
{ {
TargetPersonnelOfficer, TargetPersonnelOfficer,
@ -662,6 +712,7 @@ public sealed class DeliverooPlugin : IDalamudPlugin
ConfirmReward, ConfirmReward,
CloseGcExchange, CloseGcExchange,
Stop, RequestStop,
Stopped,
} }
} }

114
Deliveroo/External/DalamudReflector.cs vendored Normal file
View File

@ -0,0 +1,114 @@
using Dalamud.Game;
using Dalamud.Logging;
using Dalamud.Plugin;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Deliveroo.External;
/// <summary>
/// Originally part of ECommons by NightmareXIV.
///
/// https://github.com/NightmareXIV/ECommons/blob/master/ECommons/Reflection/DalamudReflector.cs
/// </summary>
internal sealed class DalamudReflector : IDisposable
{
private readonly DalamudPluginInterface _pluginInterface;
private readonly Framework _framework;
private readonly Dictionary<string, IDalamudPlugin> _pluginCache = new();
private bool _pluginsChanged = false;
public DalamudReflector(DalamudPluginInterface pluginInterface, Framework framework)
{
_pluginInterface = pluginInterface;
_framework = framework;
var pm = GetPluginManager();
pm.GetType().GetEvent("OnInstalledPluginsChanged")!.AddEventHandler(pm, OnInstalledPluginsChanged);
_framework.Update += FrameworkUpdate;
}
public void Dispose()
{
_framework.Update -= FrameworkUpdate;
var pm = GetPluginManager();
pm.GetType().GetEvent("OnInstalledPluginsChanged")!.RemoveEventHandler(pm, OnInstalledPluginsChanged);
}
private void FrameworkUpdate(Framework framework)
{
if (_pluginsChanged)
{
_pluginsChanged = false;
_pluginCache.Clear();
}
}
private object GetPluginManager()
{
return _pluginInterface.GetType().Assembly.GetType("Dalamud.Service`1", true)!
.MakeGenericType(
_pluginInterface.GetType().Assembly.GetType("Dalamud.Plugin.Internal.PluginManager", true)!)
.GetMethod("Get")!.Invoke(null, BindingFlags.Default, null, Array.Empty<object>(), null)!;
}
public bool TryGetDalamudPlugin(string internalName, out IDalamudPlugin? instance, bool suppressErrors = false,
bool ignoreCache = false)
{
if (!ignoreCache && _pluginCache.TryGetValue(internalName, out instance))
{
return true;
}
try
{
var pluginManager = GetPluginManager();
var installedPlugins =
(System.Collections.IList)pluginManager.GetType().GetProperty("InstalledPlugins")!.GetValue(
pluginManager)!;
foreach (var t in installedPlugins)
{
if ((string?)t.GetType().GetProperty("Name")!.GetValue(t) == internalName)
{
var type = t.GetType().Name == "LocalDevPlugin" ? t.GetType().BaseType : t.GetType();
var plugin = (IDalamudPlugin?)type!
.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(t);
if (plugin == null)
{
PluginLog.Warning($"[DalamudReflector] Found requested plugin {internalName} but it was null");
}
else
{
instance = plugin;
_pluginCache[internalName] = plugin;
return true;
}
}
}
instance = null;
return false;
}
catch (Exception e)
{
if (!suppressErrors)
{
PluginLog.Error(e, $"Can't find {internalName} plugin: {e.Message}");
}
instance = null;
return false;
}
}
private void OnInstalledPluginsChanged()
{
PluginLog.Verbose("Installed plugins changed event fired");
_pluginsChanged = true;
}
}

53
Deliveroo/External/YesAlreadyIpc.cs vendored Normal file
View File

@ -0,0 +1,53 @@
using System.Reflection;
using Dalamud.Logging;
namespace Deliveroo.External;
internal sealed class YesAlreadyIpc
{
private readonly DalamudReflector _dalamudReflector;
public YesAlreadyIpc(DalamudReflector dalamudReflector)
{
_dalamudReflector = dalamudReflector;
}
private object? GetConfiguration()
{
if (_dalamudReflector.TryGetDalamudPlugin("Yes Already", out var plugin))
{
var pluginService = plugin!.GetType().Assembly.GetType("YesAlready.Service");
return pluginService!.GetProperty("Configuration", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null);
}
return null;
}
public bool? DisableIfNecessary()
{
object? configuration = GetConfiguration();
if (configuration == null)
return null;
var property = configuration.GetType().GetProperty("Enabled")!;
bool enabled = (bool)property.GetValue(configuration)!;
if (enabled)
{
property.SetValue(configuration, false);
return true;
}
return false;
}
public void Enable()
{
object? configuration = GetConfiguration();
if (configuration == null)
return;
var property = configuration.GetType().GetProperty("Enabled")!;
property.SetValue(configuration, true);
}
}