From 2bdd0e8b4fa3025d02359d7e63f0b45fcecbe6c1 Mon Sep 17 00:00:00 2001 From: Jukkales Date: Thu, 18 Apr 2024 19:36:11 +0200 Subject: [PATCH] better autoretainer handling --- CHANGELOG.md | 3 ++ ECommons | 2 +- HoardFarm/IPC/AutoRetainerIPC.cs | 19 ++++++++ HoardFarm/ImGuiEx/ImGuiEx.cs | 10 ++++- HoardFarm/Service/HoardFarmService.cs | 4 +- HoardFarm/Service/PluginService.cs | 2 + HoardFarm/Service/RetainerService.cs | 62 +++++++++++++++------------ HoardFarm/Utils/Utils.cs | 11 +++++ HoardFarm/Windows/MainWindow.cs | 4 +- 9 files changed, 84 insertions(+), 33 deletions(-) create mode 100644 HoardFarm/IPC/AutoRetainerIPC.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0586d89..6efee84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ I try to keep this changelog up to date with the latest changes in the project. +## [1.5.2.0] +- better AutoRetainer handling + ## [1.5.1.0] - timer bugfix diff --git a/ECommons b/ECommons index a2d8748..515433f 160000 --- a/ECommons +++ b/ECommons @@ -1 +1 @@ -Subproject commit a2d8748a68d8038a989b42f18c6100e015edb886 +Subproject commit 515433fba468f4706fb4eca92447ebc54b7164e3 diff --git a/HoardFarm/IPC/AutoRetainerIPC.cs b/HoardFarm/IPC/AutoRetainerIPC.cs new file mode 100644 index 0000000..6a13b93 --- /dev/null +++ b/HoardFarm/IPC/AutoRetainerIPC.cs @@ -0,0 +1,19 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using ECommons.EzIpcManager; +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. + +namespace HoardFarm.IPC; + +[SuppressMessage("ReSharper", "InconsistentNaming")] +[SuppressMessage("ReSharper", "UnassignedReadonlyField")] +public class AutoRetainerIPC +{ + public AutoRetainerIPC() + { + EzIPC.Init(this, "AutoRetainer.PluginState"); + } + + [EzIPC] public readonly Func IsBusy; + [EzIPC] public readonly Func GetInventoryFreeSlotCount; +} diff --git a/HoardFarm/ImGuiEx/ImGuiEx.cs b/HoardFarm/ImGuiEx/ImGuiEx.cs index 02bcbc5..52b56b5 100644 --- a/HoardFarm/ImGuiEx/ImGuiEx.cs +++ b/HoardFarm/ImGuiEx/ImGuiEx.cs @@ -1,6 +1,7 @@ using System.Numerics; using System.Runtime.InteropServices; using Dalamud.Interface; +using Dalamud.Interface.ImGuiNotification; using Dalamud.Interface.Internal.Notifications; using Dalamud.Interface.Utility; using ECommons.ImGuiMethods; @@ -32,7 +33,7 @@ public record HeaderIconOptions private static uint headerCurrentPos = 0; private static float headerImGuiButtonWidth = 0; - public static bool AddHeaderIcon(string id, FontAwesomeIcon icon, HeaderIconOptions options = null) + public static bool AddHeaderIcon(string id, FontAwesomeIcon icon, HeaderIconOptions? options = null) { if (ImGui.IsWindowCollapsed()) return false; @@ -72,7 +73,12 @@ public static bool AddHeaderIcon(string id, FontAwesomeIcon icon, HeaderIconOpti if (ImGui.IsMouseReleased(options.MouseButton)) pressed = true; if (options.ToastTooltipOnClick && ImGui.IsMouseReleased(options.ToastTooltipOnClickButton)) - PluginInterface.UiBuilder.AddNotification(options.Tooltip!, null, NotificationType.Info); + NotificationManager.AddNotification(new Notification + { + Type = NotificationType.Info, + Content = options.Tooltip, + Title = null, + }); } ImGui.SetCursorPos(buttonPos); diff --git a/HoardFarm/Service/HoardFarmService.cs b/HoardFarm/Service/HoardFarmService.cs index 440ff96..86a1e00 100644 --- a/HoardFarm/Service/HoardFarmService.cs +++ b/HoardFarm/Service/HoardFarmService.cs @@ -342,7 +342,9 @@ private void UpdateObjectPositions() private bool CheckRetainer() { - if (Config.DoRetainers && RetainerService.CheckRetainersDone(Config.RetainerMode == 1)) + if (Config.DoRetainers + && RetainerService.CheckRetainersDone(Config.RetainerMode == 1) + && RetainerScv.CanRunRetainer()) { RetainerScv.StartProcess(); return true; diff --git a/HoardFarm/Service/PluginService.cs b/HoardFarm/Service/PluginService.cs index ff7e404..200f753 100644 --- a/HoardFarm/Service/PluginService.cs +++ b/HoardFarm/Service/PluginService.cs @@ -39,6 +39,8 @@ public class PluginService [PluginService] public static ITargetManager TargetManager { get; private set; } = null!; [PluginService] + public static INotificationManager NotificationManager { get; private set; } = null!; + [PluginService] public static IPluginLog PluginLog { get; private set; } = null!; public static Configuration Config { get; set; } = null!; public static HoardFarmService HoardService { get; set; } = null!; diff --git a/HoardFarm/Service/RetainerService.cs b/HoardFarm/Service/RetainerService.cs index a2fe4fc..a72d5fe 100644 --- a/HoardFarm/Service/RetainerService.cs +++ b/HoardFarm/Service/RetainerService.cs @@ -15,32 +15,39 @@ namespace HoardFarm.Service; public class RetainerService : IDisposable { public bool Running {get; private set;} - private bool finished; private readonly Timer updateTimer; private DateTime startedAt; + private bool autoRetainerEnabled; + private readonly AutoRetainerIPC autoRetainerIcp = new(); + + private DateTime? autoRetainerRunningTreshold; public RetainerService() { - RetainerApi.OnRetainerPostprocessStep += CheckRetainerPostProcess; updateTimer = new Timer(); updateTimer.Elapsed += OnTimerUpdate; updateTimer.Interval = 1000; updateTimer.Enabled = false; } - public void Dispose() { - RetainerApi.OnRetainerPostprocessStep -= CheckRetainerPostProcess; updateTimer.Dispose(); } public void StartProcess() { - Running = true; - finished = false; - updateTimer.Enabled = true; - startedAt = DateTime.Now; + if (CanRunRetainer()) + { + Running = true; + autoRetainerEnabled = false; + updateTimer.Enabled = true; + startedAt = DateTime.Now; + } + else + { + PluginLog.Information("Not enough inventory space to run retainers."); + } } public unsafe void FinishProcess() @@ -61,7 +68,6 @@ public unsafe void FinishProcess() } } - private unsafe void OnTimerUpdate(object? sender, ElapsedEventArgs e) { if (DateTime.Now.Subtract(startedAt).TotalMinutes > 5) @@ -75,7 +81,7 @@ private unsafe void OnTimerUpdate(object? sender, ElapsedEventArgs e) return; } - if (!TaskManager.IsBusy && Running) + if (!TaskManager.IsBusy && Running && !AutoRetainerRunning()) { if (Player.Territory != LimsaMapId) { @@ -100,13 +106,15 @@ private unsafe void OnTimerUpdate(object? sender, ElapsedEventArgs e) { Enqueue(() => { EnableAutoRetainer(); + autoRetainerEnabled = true; return true; }); } return; } - if (CheckRetainerListOpen() && finished) + // We not reach here until autoretainer is done + if (autoRetainerEnabled && !AutoRetainerRunning()) { FinishProcess(); } @@ -159,16 +167,6 @@ private void DisableAutoRetainer() }); } } - - private void CheckRetainerPostProcess(string retainername) - { - PluginLog.Information($"Retainer {retainername} has finished processing."); - if (CheckAllRetainersOnVenture()) - { - PluginLog.Information("All retainers are processed."); - finished = true; - } - } public static bool CheckRetainersDone(bool all = true) { @@ -176,14 +174,24 @@ public static bool CheckRetainersDone(bool all = true) return all ? data.All(e => CheckIsDone(e.VentureEndsAt)) : data.Any(e => CheckIsDone(e.VentureEndsAt)); } - public static bool CheckAllRetainersOnVenture() - { - var data = RetainerApi.GetOfflineCharacterData(ClientState.LocalContentId).RetainerData; - return data.All(e => CheckIsDone(e.VentureEndsAt) == false); - } - private static bool CheckIsDone(long time) { return time+10 <= CSFramework.GetServerTime(); } + + private bool AutoRetainerRunning() + { + if (autoRetainerIcp.IsBusy()) + autoRetainerRunningTreshold = null; + else + autoRetainerRunningTreshold ??= DateTime.Now.AddSeconds(10); + + return autoRetainerRunningTreshold == null || autoRetainerRunningTreshold > DateTime.Now; + } + + public bool CanRunRetainer() + { + return autoRetainerIcp.GetInventoryFreeSlotCount() > 4; + } + } diff --git a/HoardFarm/Utils/Utils.cs b/HoardFarm/Utils/Utils.cs index db0fda5..be5f7c2 100644 --- a/HoardFarm/Utils/Utils.cs +++ b/HoardFarm/Utils/Utils.cs @@ -149,4 +149,15 @@ public static unsafe bool CanUseMagicite() return false; } + + public static bool AutoRetainerVersionHighEnough() + { + if (DalamudReflector.TryGetDalamudPlugin("AutoRetainer", out var plugin, false, true)) + { + var minimalVersion = new Version(4, 2, 6, 3); + return plugin.GetType().Assembly.GetName().Version >= minimalVersion; + } + + return false; + } } diff --git a/HoardFarm/Windows/MainWindow.cs b/HoardFarm/Windows/MainWindow.cs index b69b83b..a93ea81 100644 --- a/HoardFarm/Windows/MainWindow.cs +++ b/HoardFarm/Windows/MainWindow.cs @@ -187,14 +187,14 @@ public override void Draw() private void DrawRetainerSettings() { - using (_ = ImRaii.Disabled(!RetainerApi.Ready)) + using (_ = ImRaii.Disabled(!RetainerApi.Ready || !AutoRetainerVersionHighEnough())) { var enabled = Config.DoRetainers; if (ImGui.Checkbox("Do retainers:", ref enabled)) Config.DoRetainers = enabled; } var hoverText = "Ports to Limsa Lominsa and runs retainers between runs if done."; - if (!RetainerApi.Ready) hoverText = "This features requires AutoRetainer to be installed and configured."; + if (!RetainerApi.Ready) hoverText = "This features requires AutoRetainer 4.2.6.3 or higher to be installed and configured."; if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip(hoverText);