Skip to content

Commit

Permalink
better autoretainer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jukkales committed Apr 18, 2024
1 parent a8c1daa commit 2bdd0e8
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions HoardFarm/IPC/AutoRetainerIPC.cs
Original file line number Diff line number Diff line change
@@ -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<bool> IsBusy;
[EzIPC] public readonly Func<int> GetInventoryFreeSlotCount;
}
10 changes: 8 additions & 2 deletions HoardFarm/ImGuiEx/ImGuiEx.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion HoardFarm/Service/HoardFarmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions HoardFarm/Service/PluginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand Down
62 changes: 35 additions & 27 deletions HoardFarm/Service/RetainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -61,7 +68,6 @@ public unsafe void FinishProcess()
}
}


private unsafe void OnTimerUpdate(object? sender, ElapsedEventArgs e)
{
if (DateTime.Now.Subtract(startedAt).TotalMinutes > 5)
Expand All @@ -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)
{
Expand All @@ -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();
}
Expand Down Expand Up @@ -159,31 +167,31 @@ 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)
{
var data = RetainerApi.GetOfflineCharacterData(ClientState.LocalContentId).RetainerData;
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;
}

}
11 changes: 11 additions & 0 deletions HoardFarm/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions HoardFarm/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 2bdd0e8

Please sign in to comment.