From 6052dde4b5c6453696d00453acf8a672020d4f59 Mon Sep 17 00:00:00 2001 From: Jukkales Date: Mon, 22 Apr 2024 21:04:11 +0200 Subject: [PATCH] cleanup and a little performance --- HoardFarm/HoardFarm.cs | 6 +- HoardFarm/HoardFarm.csproj | 2 +- HoardFarm/IPC/NavmeshIPC.cs | 4 +- HoardFarm/Model/Configuration.cs | 22 ++--- HoardFarm/Service/AchievementService.cs | 5 +- HoardFarm/Service/RetainerService.cs | 8 +- HoardFarm/Utils/DataIDs.cs | 3 - HoardFarm/Utils/Utils.cs | 103 +++++++++++------------- HoardFarm/Windows/ConfigWindow.cs | 12 +-- HoardFarm/Windows/MainWindow.cs | 55 ++++--------- HoardFarm/packages.lock.json | 4 +- 11 files changed, 96 insertions(+), 128 deletions(-) diff --git a/HoardFarm/HoardFarm.cs b/HoardFarm/HoardFarm.cs index ab337dc..92d8ee2 100644 --- a/HoardFarm/HoardFarm.cs +++ b/HoardFarm/HoardFarm.cs @@ -1,4 +1,5 @@ -using System.Globalization; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; using AutoRetainerAPI; using Dalamud; using Dalamud.Interface.Windowing; @@ -14,6 +15,7 @@ namespace HoardFarm; +[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] public sealed class HoardFarm : IDalamudPlugin { private readonly HoardFarmService hoardFarmService; @@ -23,7 +25,7 @@ public sealed class HoardFarm : IDalamudPlugin private readonly DeepDungeonMenuOverlay deepDungeonMenuOverlay; private readonly AutoRetainerApi autoRetainerApi; private readonly RetainerService retainerService; - public WindowSystem WindowSystem = new("HoardFarm"); + public readonly WindowSystem WindowSystem = new("HoardFarm"); public HoardFarm(DalamudPluginInterface? pluginInterface) { diff --git a/HoardFarm/HoardFarm.csproj b/HoardFarm/HoardFarm.csproj index 5add1b8..a58b29b 100644 --- a/HoardFarm/HoardFarm.csproj +++ b/HoardFarm/HoardFarm.csproj @@ -2,7 +2,7 @@ Jukkales - 1.5.2.1 + 1.5.2.2 HoardFarm Dalamud Plugin https://github.com/Jukkales/HoardFarm diff --git a/HoardFarm/IPC/NavmeshIPC.cs b/HoardFarm/IPC/NavmeshIPC.cs index edc37ec..17b0c8f 100644 --- a/HoardFarm/IPC/NavmeshIPC.cs +++ b/HoardFarm/IPC/NavmeshIPC.cs @@ -2,12 +2,14 @@ using ECommons; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Threading.Tasks; namespace HoardFarm.IPC; -public class NavmeshIPC +[SuppressMessage("ReSharper", "InconsistentNaming")] +public static class NavmeshIPC { internal static readonly string Name = "vnavmesh"; private static ICallGateSubscriber? _navIsReady; diff --git a/HoardFarm/Model/Configuration.cs b/HoardFarm/Model/Configuration.cs index df45225..1ecc40c 100644 --- a/HoardFarm/Model/Configuration.cs +++ b/HoardFarm/Model/Configuration.cs @@ -8,18 +8,18 @@ public class Configuration : IPluginConfiguration { public int Version { get; set; } = 1; - public int HoardModeSave { get; set; } - public int HoardFarmMode { get; set; } - public int StopAfter { get; set; } = 50; - public int StopAfterMode { get; set; } = 1; - public int OverallRuns { get; set; } - public int OverallFoundHoards { get; set; } - public int OverallTime { get; set; } - public bool ShowOverlay { get; set; } = true; - public bool ParanoidMode { get; set; } + public int HoardModeSave; + public int HoardFarmMode; + public int StopAfter = 50; + public int StopAfterMode = 1; + public int OverallRuns; + public int OverallFoundHoards; + public int OverallTime; + public bool ShowOverlay = true; + public bool ParanoidMode; - public bool DoRetainers { get; set; } - public int RetainerMode { get; set; } = 1; + public bool DoRetainers; + public int RetainerMode = 1; public void Save() { diff --git a/HoardFarm/Service/AchievementService.cs b/HoardFarm/Service/AchievementService.cs index 5315eb7..7cbabf2 100644 --- a/HoardFarm/Service/AchievementService.cs +++ b/HoardFarm/Service/AchievementService.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using ECommons.EzHookManager; using FFXIVClientStructs.FFXIV.Client.Game.UI; @@ -6,12 +7,11 @@ namespace HoardFarm.Service; public unsafe class AchievementService { - public delegate void ReceiveAchievementProgressDelegate(Achievement* achievement, uint id, uint current, uint max); [EzHook("C7 81 ?? ?? ?? ?? ?? ?? ?? ?? 89 91 ?? ?? ?? ?? 44 89 81")] public EzHook ReceiveAchievementProgressHook = null!; - public int Progress { get; set; } + public int Progress; public AchievementService() { @@ -24,6 +24,7 @@ public void UpdateProgress() Achievement.Instance()->RequestAchievementProgress(ForTheHoardAchievementId); } + [SuppressMessage("ReSharper", "UnusedMember.Local")] private void ReceiveAchievementProgressDetour(Achievement* achievement, uint id, uint current, uint max) { if (id == ForTheHoardAchievementId) diff --git a/HoardFarm/Service/RetainerService.cs b/HoardFarm/Service/RetainerService.cs index a72d5fe..1769500 100644 --- a/HoardFarm/Service/RetainerService.cs +++ b/HoardFarm/Service/RetainerService.cs @@ -20,7 +20,7 @@ public class RetainerService : IDisposable private bool autoRetainerEnabled; private readonly AutoRetainerIPC autoRetainerIcp = new(); - private DateTime? autoRetainerRunningTreshold; + private DateTime? autoRetainerRunningThreshold; public RetainerService() { @@ -182,11 +182,11 @@ private static bool CheckIsDone(long time) private bool AutoRetainerRunning() { if (autoRetainerIcp.IsBusy()) - autoRetainerRunningTreshold = null; + autoRetainerRunningThreshold = null; else - autoRetainerRunningTreshold ??= DateTime.Now.AddSeconds(10); + autoRetainerRunningThreshold ??= DateTime.Now.AddSeconds(10); - return autoRetainerRunningTreshold == null || autoRetainerRunningTreshold > DateTime.Now; + return autoRetainerRunningThreshold == null || autoRetainerRunningThreshold > DateTime.Now; } public bool CanRunRetainer() diff --git a/HoardFarm/Utils/DataIDs.cs b/HoardFarm/Utils/DataIDs.cs index 332b20b..6c13377 100644 --- a/HoardFarm/Utils/DataIDs.cs +++ b/HoardFarm/Utils/DataIDs.cs @@ -17,14 +17,11 @@ public static class DataIDs public const uint LimsaAetherytId = 8; public const uint RetainerBellDataId = 2000401; public static readonly Vector3 RetainerBellLocation = new(-124.1676f, 18f, 19.9041f); - public const ushort HoHMapId1 = 770; public const ushort HoHMapId11 = 771; public const ushort HoHMapId21 = 772; - - public const uint AbandonDutyMessageId = 2545; public const uint ConfirmPartyKoMessageId = 10483; diff --git a/HoardFarm/Utils/Utils.cs b/HoardFarm/Utils/Utils.cs index be5f7c2..727e456 100644 --- a/HoardFarm/Utils/Utils.cs +++ b/HoardFarm/Utils/Utils.cs @@ -12,57 +12,68 @@ using FFXIVClientStructs.FFXIV.Client.UI.Agent; using FFXIVClientStructs.FFXIV.Component.GUI; using HoardFarm.Model; +using Lumina.Excel.GeneratedSheets; namespace HoardFarm.Utils; public static class Utils { + public static readonly Version MinAutoRetainerVersion = new(4, 2, 6, 3); public static float SprintCD => Player.Status.FirstOrDefault(s => s.StatusId == 50)?.RemainingTime ?? 0; - public static bool Concealment => (Player.Status.FirstOrDefault(s => s.StatusId == VanishStatusId)?.RemainingTime ?? 0) > 0; + + public static bool Concealment => + (Player.Status.FirstOrDefault(s => s.StatusId == VanishStatusId)?.RemainingTime ?? 0) > 0; + public static bool InHoH => Player.Territory == HoHMapId11 || Player.Territory == HoHMapId21; public static bool InRubySea => Player.Territory == RubySeaMapId; - public static unsafe bool IsMoving() => AgentMap.Instance()->IsPlayerMoving == 1; - public static float Distance(this Vector3 v, Vector3 v2) => new Vector2(v.X - v2.X, v.Z - v2.Z).Length(); - public static bool PluginInstalled(string name) => DalamudReflector.TryGetDalamudPlugin(name, out _, false, true); - - public static unsafe bool NotBusy() + + public static unsafe bool IsMoving() + { + return AgentMap.Instance()->IsPlayerMoving == 1; + } + + public static float Distance(this Vector3 v, Vector3 v2) + { + return new Vector2(v.X - v2.X, v.Z - v2.Z).Length(); + } + + public static bool PluginInstalled(string name) { + return DalamudReflector.TryGetDalamudPlugin(name, out _, false, true); + } + public static unsafe bool NotBusy() + { var occupied = IsOccupied(); var target = TargetSystem.Instance()->Target; - - if (occupied && Svc.Condition[ConditionFlag.OccupiedInQuestEvent] && target != null && target->DataID == KyuseiDataId) - { - occupied = false; - } - + + if (occupied && Svc.Condition[ConditionFlag.OccupiedInQuestEvent] && target != null && + target->DataID == KyuseiDataId) occupied = false; + return Player.Available - && Player.Object.CastActionId == 0 + && Player.Object.CastActionId == 0 && !occupied - && !Svc.Condition[ConditionFlag.Jumping] + && !Svc.Condition[ConditionFlag.Jumping] && Player.Object.IsTargetable; } - - public static unsafe AtkResNode* GetNodeByIDChain(AtkResNode* node, params uint[] ids) { - if(node == null || ids.Length <= 0) { - return null; - } - if(node->NodeID == ids[0]) { - if(ids.Length == 1) { - return node; - } + public static unsafe AtkResNode* GetNodeByIDChain(AtkResNode* node, params uint[] ids) + { + if (node == null || ids.Length <= 0) return null; + + if (node->NodeID == ids[0]) + { + if (ids.Length == 1) return node; var newList = new List(ids); newList.RemoveAt(0); var childNode = node->ChildNode; - if(childNode != null) { - return GetNodeByIDChain(childNode, newList.ToArray()); - } + if (childNode != null) return GetNodeByIDChain(childNode, newList.ToArray()); - if((int)node->Type >= 1000) { + if ((int)node->Type >= 1000) + { var componentNode = node->GetAsAtkComponentNode(); var component = componentNode->Component; var uldManager = component->UldManager; @@ -80,20 +91,16 @@ public static unsafe bool NotBusy() public static unsafe AtkUnitBase* FindSelectYesNo(uint rowId) { - var s = Svc.Data.GetExcelSheet()!.GetRow(rowId)!.Text - .ToDalamudString().ExtractText(); + var s = Svc.Data.GetExcelSheet()!.GetRow(rowId)!.Text + .ToDalamudString().ExtractText(); for (var i = 1; i < 100; i++) - { try { if (TryGetAddonByName("SelectYesno", out var addon) && IsAddonReady(addon)) { var textNode = addon->UldManager.NodeList[15]->GetAsAtkTextNode(); var text = MemoryHelper.ReadSeString(&textNode->NodeText).ExtractText().Replace(" ", ""); - if (text.EqualsAny(s) || text.ContainsAny(s)) - { - return addon; - } + if (text.EqualsAny(s) || text.ContainsAny(s)) return addon; { return addon; } @@ -104,25 +111,18 @@ public static unsafe bool NotBusy() PluginLog.Debug(e.ToString()); return null; } - } + return null; } - - + + public static unsafe bool KyuseiInteractable() { if (ObjectTable.TryGetFirst(e => e.DataId == KyuseiDataId, out var npc)) - { return npc.Position.Distance(Player.GameObject->Position) < 7f; - } return false; } - - public static bool WaitTillOnokoro() - { - return InRubySea && Player.Interactable && NotBusy(); - } - + public static unsafe bool CanUsePomander(Pomander pomander) { if (TryGetAddonByName("DeepDungeonStatus", out var addon) && IsAddonReady(addon)) @@ -139,25 +139,18 @@ public static unsafe bool CanUsePomander(Pomander pomander) return false; } - + public static unsafe bool CanUseMagicite() { if (TryGetAddonByName("DeepDungeonStatus", out var addon) && IsAddonReady(addon)) - { return GetNodeByIDChain(addon->GetRootNode(), MagiciteChain)->IsVisible; - } 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; + return Svc.PluginInterface.InstalledPlugins.FirstOrDefault(x => x.IsLoaded && x.InternalName == "AutoRetainer") + ?.Version >= MinAutoRetainerVersion; } } diff --git a/HoardFarm/Windows/ConfigWindow.cs b/HoardFarm/Windows/ConfigWindow.cs index 05fb9e8..5981ef8 100644 --- a/HoardFarm/Windows/ConfigWindow.cs +++ b/HoardFarm/Windows/ConfigWindow.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using Dalamud.Interface; using Dalamud.Interface.Windowing; using ImGuiNET; @@ -8,7 +7,6 @@ namespace HoardFarm.Windows; public class ConfigWindow() : Window("Hoard Farm Config", ImGuiWindowFlags.AlwaysAutoResize) { - public override void Draw() { ImGui.Text("Actually no really much to configure here."); @@ -35,16 +33,12 @@ public override void Draw() Config.Save(); } - var showOverlay = Config.ShowOverlay; - if (ImGui.Checkbox("Show \"Open Hoardfarm\"-Overlay", ref showOverlay)) + if (ImGui.Checkbox("Show \"Open Hoardfarm\"-Overlay", ref Config.ShowOverlay)) { - Config.ShowOverlay = showOverlay; Config.Save(); } - var paranoid = Config.ParanoidMode; - if (ImGui.Checkbox("Paranoid mode", ref paranoid)) + if (ImGui.Checkbox("Paranoid mode", ref Config.ParanoidMode)) { - Config.ParanoidMode = paranoid; Config.Save(); } ImGui.SameLine(); diff --git a/HoardFarm/Windows/MainWindow.cs b/HoardFarm/Windows/MainWindow.cs index 474262d..42454ab 100644 --- a/HoardFarm/Windows/MainWindow.cs +++ b/HoardFarm/Windows/MainWindow.cs @@ -6,7 +6,6 @@ using Dalamud.Interface.Windowing; using HoardFarm.IPC; using HoardFarm.Model; -using HoardFarm.Service; using ImGuiNET; using static HoardFarm.ImGuiEx.ImGuiEx; @@ -28,51 +27,38 @@ public override void Draw() } if (!PluginInstalled(NavmeshIPC.Name)) + { if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip($"This features requires {NavmeshIPC.Name} to be installed."); + } ImGui.SameLine(230); ImGui.Text(HoardService.HoardModeStatus); ImGui.Text("Stop After:"); ImGui.SameLine(); ImGui.SetNextItemWidth(100); - var stopAfter = Config.StopAfter; - if (ImGui.InputInt("##stopAfter", ref stopAfter)) - { - Config.StopAfter = stopAfter; + if (ImGui.InputInt("##stopAfter", ref Config.StopAfter)) Config.Save(); - } ImGui.SameLine(); ImGui.SetNextItemWidth(120); - var stopAfterMode = Config.StopAfterMode; - if (ImGui.Combo("##stopAfterMode", ref stopAfterMode, ["Runs", "Found Hoards", "Minutes"], 3)) - { - Config.StopAfterMode = stopAfterMode; + if (ImGui.Combo("##stopAfterMode", ref Config.StopAfterMode, ["Runs", "Found Hoards", "Minutes"], 3)) Config.Save(); - } DrawRetainerSettings(); - ImGui.Separator(); ImGui.BeginGroup(); ImGui.Text("Savegame:"); ImGui.Indent(15); - var save = conf.HoardModeSave; - if (ImGui.RadioButton("Savegame 1", ref save, 0)) - { - conf.HoardModeSave = save; + + if (ImGui.RadioButton("Savegame 1", ref conf.HoardModeSave, 0)) Config.Save(); - } - if (ImGui.RadioButton("Savegame 2", ref save, 1)) - { - conf.HoardModeSave = save; + if (ImGui.RadioButton("Savegame 2", ref conf.HoardModeSave, 1)) Config.Save(); - } ImGui.Unindent(15); ImGui.EndGroup(); @@ -82,12 +68,8 @@ public override void Draw() ImGui.BeginGroup(); ImGui.Text("Farm Mode:"); ImGui.Indent(15); - var farmMode = conf.HoardFarmMode; - if (ImGui.RadioButton("Efficiency", ref farmMode, 0)) - { - conf.HoardFarmMode = farmMode; + if (ImGui.RadioButton("Efficiency", ref conf.HoardFarmMode, 0)) Config.Save(); - } ImGui.SameLine(); ImGui.PushFont(UiBuilder.IconFont); @@ -101,11 +83,9 @@ public override void Draw() "This mode is still recommended."); } - if (ImGui.RadioButton("Safety", ref farmMode, 1)) - { - conf.HoardFarmMode = farmMode; + if (ImGui.RadioButton("Safety", ref conf.HoardFarmMode, 1)) Config.Save(); - } + ImGui.SameLine(); ImGui.PushFont(UiBuilder.IconFont); @@ -190,29 +170,28 @@ private void DrawRetainerSettings() var autoRetainer = RetainerApi.Ready && AutoRetainerVersionHighEnough(); using (_ = ImRaii.Disabled(!autoRetainer)) { - var enabled = Config.DoRetainers; - if (ImGui.Checkbox("Do retainers:", ref enabled)) Config.DoRetainers = enabled; + if (ImGui.Checkbox("Do retainers:", ref Config.DoRetainers)) + Config.Save(); } var hoverText = "Ports to Limsa Lominsa and runs retainers between runs if done."; - if (!autoRetainer) hoverText = "This features requires AutoRetainer 4.2.6.3 or higher to be installed and configured."; + if (!autoRetainer) + hoverText = "This features requires AutoRetainer 4.2.6.3 or higher to be installed and configured."; if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) ImGui.SetTooltip(hoverText); ImGui.SameLine(); ImGui.SetNextItemWidth(170); - var retainerMode = Config.RetainerMode; - if (ImGui.Combo("##retainerMode", ref retainerMode, ["If ANY Retainer is done", "If ALL Retainer are done"], 2)) - { - Config.RetainerMode = retainerMode; + if (ImGui.Combo("##retainerMode", ref Config.RetainerMode, + ["If ANY Retainer is done", "If ALL Retainer are done"], 2)) Config.Save(); - } } private static void HeaderIcons() { if (AddHeaderIcon("OpenConfig", FontAwesomeIcon.Cog, new HeaderIconOptions { Tooltip = "Open Config" })) P.ShowConfigWindow(); + if (AddHeaderIcon("OpenHelp", FontAwesomeIcon.QuestionCircle, new HeaderIconOptions { Tooltip = "Open Help" })) { Process.Start(new ProcessStartInfo diff --git a/HoardFarm/packages.lock.json b/HoardFarm/packages.lock.json index e84fa63..ad8a9a6 100644 --- a/HoardFarm/packages.lock.json +++ b/HoardFarm/packages.lock.json @@ -11,7 +11,7 @@ "autoretainerapi": { "type": "Project", "dependencies": { - "ECommons": "[2.1.0.7, )" + "ECommons": "[2.2.0.1, )" } }, "ecommons": { @@ -19,4 +19,4 @@ } } } -} \ No newline at end of file +}