Fallback for when FC window times out

This commit is contained in:
Liza 2024-03-06 01:17:16 +01:00
parent ef1769c4f7
commit bd28ba259c
Signed by: liza
GPG Key ID: 7199F8D727D55F67

View File

@ -13,7 +13,6 @@ using FFXIVClientStructs.FFXIV.Client.System.Framework;
using FFXIVClientStructs.FFXIV.Client.UI.Info; using FFXIVClientStructs.FFXIV.Client.UI.Info;
using FFXIVClientStructs.FFXIV.Component.GUI; using FFXIVClientStructs.FFXIV.Component.GUI;
using Newtonsoft.Json; using Newtonsoft.Json;
using Task = System.Threading.Tasks.Task;
namespace Influx.LocalStatistics; namespace Influx.LocalStatistics;
@ -30,7 +29,7 @@ public class FcStatsCalculator : IDisposable
private readonly Dictionary<ulong, FcStats> _cache = new(); private readonly Dictionary<ulong, FcStats> _cache = new();
private bool closeFcWindow = false; private Status? _status = null;
public FcStatsCalculator( public FcStatsCalculator(
IDalamudPlugin plugin, IDalamudPlugin plugin,
@ -54,7 +53,9 @@ public class FcStatsCalculator : IDisposable
_autoRetainerApi = new(); _autoRetainerApi = new();
_autoRetainerApi.OnCharacterPostprocessStep += CheckCharacterPostProcess; _autoRetainerApi.OnCharacterPostprocessStep += CheckCharacterPostProcess;
_autoRetainerApi.OnCharacterReadyToPostProcess += DoCharacterPostProcess; _autoRetainerApi.OnCharacterReadyToPostProcess += DoCharacterPostProcess;
_addonLifecycle.RegisterListener(AddonEvent.PostReceiveEvent, "FreeCompany", CloseFcWindow); _addonLifecycle.RegisterListener(AddonEvent.PostReceiveEvent, "FreeCompany", FcPostReceiveEvent);
_framework.Update += FrameworkUpdate;
_clientState.Logout += Logout;
foreach (var file in _pluginInterface.ConfigDirectory.GetFiles("f.*.json")) foreach (var file in _pluginInterface.ConfigDirectory.GetFiles("f.*.json"))
{ {
@ -99,45 +100,51 @@ public class FcStatsCalculator : IDisposable
private void DoCharacterPostProcess() private void DoCharacterPostProcess()
{ {
closeFcWindow = true; _status = new();
unsafe unsafe
{ {
AtkUnitBase* addon = (AtkUnitBase*)_gameGui.GetAddonByName("FreeCompany"); AtkUnitBase* addon = (AtkUnitBase*)_gameGui.GetAddonByName("FreeCompany");
if (addon != null && addon->IsVisible) if (addon != null && addon->IsVisible)
CloseFcWindow(AddonEvent.PostReceiveEvent); FcPostReceiveEvent(AddonEvent.PostReceiveEvent);
else else
Chat.Instance.SendMessage("/freecompanycmd"); Chat.Instance.SendMessage("/freecompanycmd");
} }
} }
private void CloseFcWindow(AddonEvent type, AddonArgs? args = null) private void FcPostReceiveEvent(AddonEvent type, AddonArgs? args = null)
{ {
_framework.RunOnTick(() => UpdateOnTick(0), TimeSpan.FromMilliseconds(100)); if (_status != null)
{
_pluginLog.Verbose("FC window received event...");
_status.WindowOpened = true;
}
else
_pluginLog.Verbose("Not tracking status for FC window");
} }
private void UpdateOnTick(int counter) private void FrameworkUpdate(IFramework framework)
{ {
bool finalAttempt = ++counter >= 10; if (_status == null)
if (UpdateFcCredits() || finalAttempt)
{
if (closeFcWindow)
{
unsafe
{
AtkUnitBase* addon = (AtkUnitBase*)_gameGui.GetAddonByName("FreeCompany");
if (addon != null && addon->IsVisible)
addon->FireCallbackInt(-1);
}
closeFcWindow = false;
_autoRetainerApi.FinishCharacterPostProcess();
}
return; return;
}
_framework.RunOnTick(() => UpdateOnTick(counter + 1), TimeSpan.FromMilliseconds(100)); if (_status.FallbackFinishPostProcessing < DateTime.Now)
{
_status = null;
_autoRetainerApi.FinishCharacterPostProcess();
}
else if (_status.WindowOpened && UpdateFcCredits())
{
_status = null;
_autoRetainerApi.FinishCharacterPostProcess();
}
}
private void Logout()
{
if (_status != null)
_autoRetainerApi.FinishCharacterPostProcess();
_status = null;
} }
// ideally we'd hook the update to the number array, but #effort // ideally we'd hook the update to the number array, but #effort
@ -210,10 +217,18 @@ public class FcStatsCalculator : IDisposable
public void Dispose() public void Dispose()
{ {
_addonLifecycle.UnregisterListener(AddonEvent.PostReceiveEvent, "FreeCompany", CloseFcWindow); _clientState.Logout -= Logout;
_framework.Update -= FrameworkUpdate;
_addonLifecycle.UnregisterListener(AddonEvent.PostReceiveEvent, "FreeCompany", FcPostReceiveEvent);
_autoRetainerApi.OnCharacterPostprocessStep -= CheckCharacterPostProcess; _autoRetainerApi.OnCharacterPostprocessStep -= CheckCharacterPostProcess;
_autoRetainerApi.OnCharacterReadyToPostProcess -= DoCharacterPostProcess; _autoRetainerApi.OnCharacterReadyToPostProcess -= DoCharacterPostProcess;
_autoRetainerApi.Dispose(); _autoRetainerApi.Dispose();
ECommonsMain.Dispose(); ECommonsMain.Dispose();
} }
private sealed class Status
{
public bool WindowOpened { get; set; }
public DateTime FallbackFinishPostProcessing { get; set; } = DateTime.Now.AddSeconds(10);
}
} }