diff --git a/Tools/Butlerv4/Butlerv4.cs b/Tools/Butlerv4/Butlerv4.cs index 136a004d7..1eb9bf170 100644 --- a/Tools/Butlerv4/Butlerv4.cs +++ b/Tools/Butlerv4/Butlerv4.cs @@ -13,6 +13,7 @@ using System.Text; using System.Threading.Tasks; using Skua.Core.Interfaces; +using Skua.Core.Models; using Skua.Core.Models.Quests; using Skua.Core.Options; @@ -29,10 +30,13 @@ private static CoreAdvanced Adv private static CoreAdvanced _Adv; public bool DontPreconfigure = true; - public string OptionsStorage = "Butler1"; + // Per account (Butler1-), so one account's leader never changes another's. + public string OptionsStorage = AccountStorage(); + private const string SharedStorage = "Butler1"; public List Options = new() { + new Option("Leader", "Leader", "This account's leader. When set it wins over the Leader 1-4 lists. Stored for this account only, so other accounts keep their own leader.", ""), new Option("Leader1Name", "Leader 1 Name", "Name of leader 1.", ""), new Option("Leader1Butlers", "Butlers For Leader 1", "Comma-separated butler account names. Example: acc1,acc2,acc3", ""), new Option("Leader2Name", "Leader 2 Name", "Name of leader 2.", ""), @@ -75,6 +79,9 @@ private static CoreAdvanced Adv private bool _leaderPortLookupFailedLogged = false; private bool _pickupAllDrops = false; private bool _attackFirst = false; + private DateTime _optionsWriteTime = DateTime.MinValue; + private DateTime _lastOptionsCheck = DateTime.MinValue; + private const int OptionsCheckIntervalMs = 2000; // ── Quest state ────────────────────────────────────────────────── private int[] _questIds = Array.Empty(); @@ -89,24 +96,15 @@ public void ScriptMain(IScriptInterface bot) Bot.Events.ExtensionPacketReceived += ChatListener; - string myUsername = Bot.Player.Username ?? ""; - - for (int i = 1; i <= 4; i++) - { - string butlerList = Bot.Config!.Get($"Leader{i}Butlers") ?? ""; - var butlers = butlerList.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - if (butlers.Any(b => string.Equals(b, myUsername, StringComparison.OrdinalIgnoreCase))) - { - playerName = Bot.Config!.Get($"Leader{i}Name") ?? ""; - break; - } - } + _optionsWriteTime = OptionsWriteTime(); + playerName = ResolveLeader(); if (string.IsNullOrEmpty(playerName)) { - Core.Logger($"This account is not assigned to any leader's butler list.", messageBox: true, stopBot: true); + Core.Logger($"This account has no Leader set and is not assigned to any leader's butler list.", messageBox: true, stopBot: true); return; } + Core.Logger($"[Butler] Following {playerName} (options: {Bot.Config!.Storage})."); _attackFirst = Bot.Config!.Get("AttackFirst"); if (_attackFirst) @@ -140,6 +138,7 @@ private void FollowLeader() while (!Bot.ShouldExit && Bot.Player?.Alive != true) Core.Sleep(250); + CheckLeaderChanged(); PollTcpData(); if (_pickupAllDrops && Bot.Drops.CurrentDrops.Any()) @@ -230,6 +229,121 @@ private void FollowLeader() } } + // ================================================================ + // LEADER + // ================================================================ + + /// + /// The options storage of the logged-in account, Butler1-<account>. Skua's + /// options window and 'skua script start --option' both write to it, so setting + /// one account's leader (or lists, drops, quests) never rewrites another + /// account's. The first time, it starts as a copy of the shared Butler1 so an + /// existing setup carries over. With nobody logged in it is the shared Butler1. + /// + private static string AccountStorage() + { + string account; + try + { + account = Bot.Player.Username?.Trim().ToLowerInvariant() ?? ""; + } + catch + { + return SharedStorage; + } + if (account.Length == 0 || account.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) + return SharedStorage; + + string storage = $"{SharedStorage}-{account}"; + try + { + string shared = Path.Combine(ClientFileSources.SkuaOptionsDIR, $"{SharedStorage}.cfg"); + string own = Path.Combine(ClientFileSources.SkuaOptionsDIR, $"{storage}.cfg"); + if (!File.Exists(own) && File.Exists(shared)) + File.Copy(shared, own); + } + catch { } + return storage; + } + + /// + /// This account's leader: the Leader option when set, otherwise the leader of + /// the first Leader 1-4 list that names this account. "" when neither does. + /// + private string ResolveLeader() + { + string leader = (Bot.Config!.Get("Leader") ?? "").Trim(); + if (leader.Length > 0) + return leader; + + string myUsername = Bot.Player.Username ?? ""; + for (int i = 1; i <= 4; i++) + { + string butlerList = Bot.Config!.Get($"Leader{i}Butlers") ?? ""; + var butlers = butlerList.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + if (butlers.Any(b => string.Equals(b, myUsername, StringComparison.OrdinalIgnoreCase))) + return (Bot.Config!.Get($"Leader{i}Name") ?? "").Trim(); + } + return ""; + } + + /// + /// Every couple of seconds, re-reads this account's options if their file + /// changed, and switches to the new leader if the Leader option or the + /// lists now point somewhere else. + /// + private void CheckLeaderChanged() + { + var now = DateTime.UtcNow; + if ((now - _lastOptionsCheck).TotalMilliseconds < OptionsCheckIntervalMs) + return; + _lastOptionsCheck = now; + + DateTime written = OptionsWriteTime(); + if (written == _optionsWriteTime) + return; + _optionsWriteTime = written; + + try + { + Bot.Config!.Load(); + } + catch (Exception ex) + { + // Load sets the values before it notifies, so a failed notify still leaves them read. + Core.Logger($"[Butler] Re-reading options: {ex.Message}"); + } + + string leader = ResolveLeader(); + if (string.IsNullOrEmpty(leader)) + { + Core.Logger($"[Butler] Options changed and no longer name a leader for this account; still following {playerName}."); + return; + } + if (string.Equals(leader, playerName, StringComparison.OrdinalIgnoreCase)) + return; + + Core.Logger($"[Butler] Leader changed: {playerName} -> {leader}."); + Disconnect(); + playerName = leader; + _tcpMap = _tcpRoom = _tcpCell = _tcpPad = ""; + _tcpInCombat = _tcpHasTarget = false; + _leaderPortLookupFailedLogged = false; + } + + private DateTime OptionsWriteTime() + { + try + { + string file = Bot.Config!.OptionsFile; + return File.Exists(file) ? File.GetLastWriteTimeUtc(file) : DateTime.MinValue; + } + catch + { + return DateTime.MinValue; + } + } + private void TryGotoLeader() { if (_gotoPending) diff --git a/scripts.json b/scripts.json index 14ae3bee0..5721c2fc1 100644 --- a/scripts.json +++ b/scripts.json @@ -23585,8 +23585,8 @@ "tcp" ], "path": "Tools/Butlerv4/Butlerv4.cs", - "size": 26209, - "sha256": "ef7996786bc10e3f01da1e92210f64a7cb58dada24ce818034e013e3eaa48dd2", + "size": 30562, + "sha256": "e26a5da6577479c4605e0cb2c3dd02eedb79c15c4182d3c21be2e443eb2ff14b", "fileName": "Butlerv4.cs", "downloadUrl": "https://raw.githubusercontent.com/auqw/Scripts/Skua/Tools/Butlerv4/Butlerv4.cs" },