|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows.Media; |
| 7 | +using ff14bot; |
| 8 | +using ff14bot.Behavior; |
| 9 | +using ff14bot.Managers; |
| 10 | +using LlamaLibrary.Logging; |
| 11 | +using TreeSharp; |
| 12 | + |
| 13 | +namespace LlamaLibrary.Helpers; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Base decorator used to create and register orderbot hooks with the TreeHooks system. |
| 17 | +/// </summary> |
| 18 | +/// <remarks> |
| 19 | +/// Derive from this class and implement <see cref="HookName"/>, <see cref="HookDescription"/>, |
| 20 | +/// <see cref="ShouldRun(object)"/> and <see cref="Run"/> to provide hook behavior. The constructor |
| 21 | +/// creates a coroutine child that invokes the hook logic and attaches it as the decorator's child. |
| 22 | +/// </remarks> |
| 23 | +public abstract class OrderbotHook : Decorator |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Logger instance used by the hook for informational and diagnostic messages. |
| 27 | + /// </summary> |
| 28 | + protected readonly LLogger Log; |
| 29 | + |
| 30 | + private bool _added; |
| 31 | + private bool _includeBusyCheck; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="OrderbotHook"/> class. |
| 35 | + /// </summary> |
| 36 | + /// <param name="logLevel"> |
| 37 | + /// The log level used to construct the <see cref="LLogger"/> for this hook. |
| 38 | + /// Defaults to <see cref="LogLevel.Information"/>. |
| 39 | + /// </param> |
| 40 | + /// <param name="includeBusyCheck"> |
| 41 | + /// If <c>true</c>, the default busy check is applied in <see cref="CanRun(object)"/> to prevent running |
| 42 | + /// while moving, in combat, dead, in instance, or within a fate. Defaults to <c>true</c>. |
| 43 | + /// </param> |
| 44 | + /// <remarks> |
| 45 | + /// The constructor constructs the logger, creates an <see cref="ActionRunCoroutine"/> child that invokes the hook logic, |
| 46 | + /// assigns it as a child of this decorator, and stores the busy-check preference. |
| 47 | + /// </remarks> |
| 48 | + protected OrderbotHook(LogLevel logLevel = LogLevel.Information, bool includeBusyCheck = true) |
| 49 | + { |
| 50 | + Log = new LLogger(HookName, Colors.DarkOrange, logLevel); |
| 51 | + Composite func = new ActionRunCoroutine(async result => await HookRun()); |
| 52 | + func.Parent = this; |
| 53 | + Children = new List<Composite> { func }; |
| 54 | + _includeBusyCheck = includeBusyCheck; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Gets the hook's display name. |
| 59 | + /// </summary> |
| 60 | + /// <value>The name used for logging, diagnostics and identification of the hook.</value> |
| 61 | + protected abstract string HookName { get; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Gets a short description of the hook's purpose. |
| 65 | + /// </summary> |
| 66 | + /// <value>A human-readable description shown in logs and diagnostics.</value> |
| 67 | + protected abstract string HookDescription { get; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets the location key within <c>TreeHooks</c> where this hook will be registered. |
| 71 | + /// </summary> |
| 72 | + /// <value>The hook registration location; defaults to <c>TreeStart</c>.</value> |
| 73 | + /// <remarks> |
| 74 | + /// Override to change where the hook is attached (for example, <c>TreeStart</c>, <c>DeathReturnLogic</c> ,<c>DeathReviveLogic</c> ,<c>PoiAction</c> ,<c>Pull</c> ,<c>RoutineCombat</c> ,<c>HotspotPoi</c> ,<c>PoiAction2</c> ,<c>SetDeathPoi</c> ,<c>SetCombatPoi</c> ,<c>SetHotspotPoi</c> ,<c>SelectPoiType</c> ). |
| 75 | + /// </remarks> |
| 76 | + protected virtual string Location => "TreeStart"; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Determines whether this hook should run for the provided behavior-tree <paramref name="context"/>. |
| 80 | + /// </summary> |
| 81 | + /// <param name="context">The behavior-tree context passed into <see cref="CanRun(object)"/>.</param> |
| 82 | + /// <returns><c>true</c> if the hook should run for the given context; otherwise <c>false</c>.</returns> |
| 83 | + /// <remarks>Implementations should be quick and non-blocking. This is used by the decorator to decide execution.</remarks> |
| 84 | + protected abstract bool ShouldRun(object context); |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Executes the hook's logic asynchronously. |
| 88 | + /// </summary> |
| 89 | + /// <returns> |
| 90 | + /// A <see cref="Task"/> that completes with <c>true</c> if the hook completed successfully |
| 91 | + /// (and any downstream behavior should consider the run successful); otherwise <c>false</c>. |
| 92 | + /// </returns> |
| 93 | + /// <remarks>This method is invoked when <see cref="ShouldRun(object)"/> returns <c>true</c>. Implementations may perform async work.</remarks> |
| 94 | + protected abstract Task<bool> Run(); |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Determines whether the decorator can run in the given <paramref name="context"/>. |
| 98 | + /// </summary> |
| 99 | + /// <param name="context">The behavior-tree context.</param> |
| 100 | + /// <returns><c>false</c> if the default busy check disallows running; otherwise returns the result of <see cref="ShouldRun(object)"/>.</returns> |
| 101 | + protected override bool CanRun(object context) |
| 102 | + { |
| 103 | + if (_includeBusyCheck && DefaultBusyCheck()) |
| 104 | + { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + return ShouldRun(context); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Internal wrapper that times and logs execution of <see cref="Run"/>. |
| 113 | + /// </summary> |
| 114 | + /// <returns>The boolean result returned by <see cref="Run"/>.</returns> |
| 115 | + private async Task<bool> HookRun() |
| 116 | + { |
| 117 | + var timer = Stopwatch.StartNew(); |
| 118 | + Log.Information($"{HookName} started"); |
| 119 | + var result = await Run(); |
| 120 | + timer.Stop(); |
| 121 | + Log.Information($"{HookName} took {timer.ElapsedMilliseconds:N0}ms to complete"); |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Adds this hook to the <c>TreeHooks</c> collection at <see cref="Location"/>, if not already added. |
| 127 | + /// </summary> |
| 128 | + public virtual void AddHook() |
| 129 | + { |
| 130 | + if (_added || TreeHooks.Instance.Hooks.TryGetValue(Location, out var list) && list.Any(Equals)) |
| 131 | + { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + TreeHooks.Instance.AddHook(Location, this); |
| 136 | + Log.Information($"{Location} hook added ({Guid})"); |
| 137 | + TreeHooks.Instance.OnHooksCleared += OnHooksCleared; |
| 138 | + _added = true; |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Removes this hook from the <c>TreeHooks</c> collection at <see cref="Location"/> if it was added. |
| 143 | + /// </summary> |
| 144 | + public virtual void RemoveHook() |
| 145 | + { |
| 146 | + if (!_added) |
| 147 | + { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + TreeHooks.Instance.RemoveHook(Location, this); |
| 152 | + Log.Information($"{Location} hook Removed ({Guid})"); |
| 153 | + TreeHooks.Instance.OnHooksCleared -= OnHooksCleared; |
| 154 | + _added = false; |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Returns the hook display name and Guid. |
| 159 | + /// </summary> |
| 160 | + /// <returns>The value of <see cref="HookName"/>.</returns> |
| 161 | + public override string ToString() |
| 162 | + { |
| 163 | + return $"{HookName} {Guid}"; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Handler called when <see cref="TreeHooks.Instance"/> raises <c>OnHooksCleared</c>. |
| 168 | + /// The hook will attempt to re-add itself after hooks are cleared. |
| 169 | + /// </summary> |
| 170 | + private void OnHooksCleared(object sender, EventArgs args) |
| 171 | + { |
| 172 | + _added = false; |
| 173 | + Log.Information($"{Location} hook Removed ({Guid}) on HooksCleared"); |
| 174 | + AddHook(); |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Determines equality by comparing the <see cref="Composite.Guid"/> values of the objects. |
| 179 | + /// </summary> |
| 180 | + /// <param name="obj">The object to compare with the current instance.</param> |
| 181 | + /// <returns><c>true</c> if the objects have the same <c>Guid</c>; otherwise <c>false</c>.</returns> |
| 182 | + public override bool Equals(object? obj) |
| 183 | + { |
| 184 | + return obj switch |
| 185 | + { |
| 186 | + null => false, |
| 187 | + OrderbotHook other => Guid == other.Guid, |
| 188 | + Composite composite => Guid == composite.Guid, |
| 189 | + _ => false |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + /// <summary> |
| 194 | + /// Returns the hash code for this instance, derived from the underlying <c>Guid</c>. |
| 195 | + /// </summary> |
| 196 | + /// <returns>A 32-bit signed integer hash code.</returns> |
| 197 | + public override int GetHashCode() |
| 198 | + { |
| 199 | + return Guid.GetHashCode(); |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Performs the default busy check used to prevent hooks from running when the player is unavailable. |
| 204 | + /// </summary> |
| 205 | + /// <returns> |
| 206 | + /// <c>true</c> if the player is moving, occupied, in combat, dead, inside an instance, or within a fate; otherwise <c>false</c>. |
| 207 | + /// </returns> |
| 208 | + public static bool DefaultBusyCheck() |
| 209 | + { |
| 210 | + return MovementManager.IsMoving || MovementManager.IsOccupied || Core.Me.InCombat || !Core.Me.IsAlive || DutyManager.InInstance || FateManager.WithinFate; |
| 211 | + } |
| 212 | +} |
0 commit comments