From 7dc744a27d220890469f3ff581ff60edf50c4116 Mon Sep 17 00:00:00 2001 From: evrydayjunglist Date: Thu, 12 Nov 2026 19:18:47 -0800 Subject: [PATCH] fix(waypoint-tools): leave loaded.json to the Gateway that runs the game Waypoint's Give item answered that the mod is not loaded whenever Waypoint was started while the game already ran, until the Gateway loaded the mod again. A Gateway asked for its `commands` or `values` loads the mods too, for their commands, and Waypoint's Console asks when Waypoint starts. The mod wrote loaded.json on every load, so that short-lived Gateway wrote its own process over the one that runs the game and ended: the file named an ended process, which Waypoint reads as "left by a Gateway that stopped". loaded.json now belongs to the first Gateway that loads the mod in the folder, for as long as it runs. Another Gateway there leaves the file and the requests alone. When the named process has ended (a stop does not unload the mods) or is another program by now, the next scan takes the file, a quarter of a second later at most. The file gains `started`, the named process's start time, so a process number used again is not taken for the Gateway; a file without it (1.0.2 and before) is matched by the program's name. Unloading removes the file only when it is this Gateway's. 1.0.3. Checked on a LocalExile 0.5.1-beta release with the 3.29.3d client: with the game running, a `commands` run leaves the file naming the game's Gateway; a file naming an ended process, or another running program, is taken back within a second; asked first and started after, the Gateway takes the file; and a Waypoint started while the game ran gave an Iron Ring ("Added Iron Ring"), where with 1.0.2 it answered that the mod is not loaded. --- mods.json | 2 +- mods/waypoint-tools/README.md | 11 +++- mods/waypoint-tools/WaypointToolsMod.cs | 84 ++++++++++++++++++++++--- mods/waypoint-tools/mod.json | 2 +- 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/mods.json b/mods.json index 543b2f3..504331c 100644 --- a/mods.json +++ b/mods.json @@ -10,7 +10,7 @@ { "id": "liveeconomy", "name": "Live Economy", "description": "Faustus's Currency Exchange as a live market: the official Path of Exile leagues' prices, a spread, depth, and orders that fill with time.", "author": "JetStream", "version": "1.1.3", "compatibleClientVersions": ["0.4.3-beta", "0.5.0-beta", "0.5.1-beta"], "kind": "feature", "type": "project", "path": "mods/liveeconomy", "depends": [], "settings": true }, { "id": "petloot", "name": "Pet Loot", "description": "Worn pets collect selected currency with per-character filters and an immediate pause switch.", "author": "zj1022", "version": "1.0.5", "compatibleClientVersions": ["0.5.1-beta"], "kind": "feature", "type": "script", "path": "mods/petloot", "depends": ["Game", "Cosmetics"], "settings": true }, { "id": "pob", "name": "POB", "description": "Preview and import a character build through a configured external PoB converter.", "author": "nikolos89", "version": "1.0.2", "compatibleClientVersions": ["0.4.3-beta", "0.5.0-beta", "0.5.1-beta"], "kind": "feature", "type": "project", "path": "mods/pob", "depends": [], "settings": true }, - { "id": "waypoint-tools", "name": "Waypoint Tools", "description": "Lets Waypoint's tools reach the game without the client control bridge: the item crafter gives its items through it.", "author": "Waypoint", "version": "1.0.2", "compatibleClientVersions": ["0.4.3-beta", "0.5.0-beta", "0.5.1-beta"], "kind": "feature", "type": "project", "path": "mods/waypoint-tools", "depends": ["Game"], "settings": false }, + { "id": "waypoint-tools", "name": "Waypoint Tools", "description": "Lets Waypoint's tools reach the game without the client control bridge: the item crafter gives its items through it.", "author": "Waypoint", "version": "1.0.3", "compatibleClientVersions": ["0.4.3-beta", "0.5.0-beta", "0.5.1-beta"], "kind": "feature", "type": "project", "path": "mods/waypoint-tools", "depends": ["Game"], "settings": false }, { "id": "zoom", "name": "Zoom", "description": "A wider view in normal play: the camera stands further back, and /zoom sets how far.", "author": "evrydayjunglist", "version": "1.0.0", "compatibleClientVersions": ["0.5.1-beta"], "kind": "feature", "type": "project", "path": "mods/zoom", "depends": ["Game"], "settings": true } ] } diff --git a/mods/waypoint-tools/README.md b/mods/waypoint-tools/README.md index d7c45e5..ab3fe84 100644 --- a/mods/waypoint-tools/README.md +++ b/mods/waypoint-tools/README.md @@ -12,8 +12,12 @@ this server, and says so in that character's chat (`Waypoint: Added Dire Abyss I Through files in the LocalExile folder (the extracted release, or a checkout); nothing listens on the network. - `data/waypoint-tools/loaded.json`: written while the mod is loaded, removed when it unloads. - `{"protocol": 1, "version": "1.0.0", "pid": , "actions": ["item_give"]}`. A file whose - `pid` is not a running Gateway was left by one that stopped. + `{"protocol": 1, "version": "1.0.0", "pid": , "started": "", + "actions": ["item_give"]}`. A file whose `pid` is not a running Gateway was left by one that stopped. + It names the Gateway that runs the game: the first to load the mod in the folder, for as long as it runs. A Gateway + that is only asked a question (`GameServer.Gateway commands` or `values`, as Waypoint's Console does when it starts) + loads the mods too; it leaves the file and the requests alone while the named one runs. When the named Gateway ended + without unloading, another takes the file within a quarter of a second. - `data/waypoint-tools/requests/.json`: a request, one JSON object. Write it as `.tmp` and rename it, so the mod never reads half of it. `` is the caller's (a GUID). `{"action": "item_give", "character": "", "item": }`. Without `character` (or with `""`) it goes to @@ -36,6 +40,7 @@ None. ## Works with -1.0.2: LocalExile v0.4.3-beta, v0.5.0-beta and v0.5.1-beta (the same code as 1.0.1); 1.0.0: v0.4.2-beta. It needs no other mod: it uses the server and LocalExile's Game module directly, as a module +1.0.3: LocalExile v0.4.3-beta, v0.5.0-beta and v0.5.1-beta; it fixes Give item answering that the mod is not loaded +after Waypoint was started while the game ran. 1.0.2: the same versions (the same code as 1.0.1); 1.0.0: v0.4.2-beta. It needs no other mod: it uses the server and LocalExile's Game module directly, as a module does, and lists `Game` (that module, not a mod) in `depends` so it reloads with it. So it needs v0.4.0-beta or later (the first where mods reach what modules reach). diff --git a/mods/waypoint-tools/WaypointToolsMod.cs b/mods/waypoint-tools/WaypointToolsMod.cs index 357477b..6562543 100644 --- a/mods/waypoint-tools/WaypointToolsMod.cs +++ b/mods/waypoint-tools/WaypointToolsMod.cs @@ -1,4 +1,6 @@ using System.Collections.Concurrent; +using System.Diagnostics; +using System.Globalization; using System.Text.Json; using System.Text.Json.Nodes; using GameServer.Core; @@ -57,13 +59,8 @@ public void Load(IModContext mod) _mod = mod; Directory.CreateDirectory(Requests); Directory.CreateDirectory(Replies); - WriteAtomically(Loaded, new JsonObject - { - ["protocol"] = Protocol, - ["version"] = mod.Manifest.Version, - ["pid"] = Environment.ProcessId, - ["actions"] = new JsonArray("item_give"), - }); + try { Serving(); } + catch (Exception e) when (e is IOException or UnauthorizedAccessException) { mod.Log($"writing {Loaded} failed: {e.Message}"); } GameEvents.Tick.Add(mod.Manifest.Id, (conn, _) => Tick(conn)); _scan = new Timer(_ => Scan(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(250)); } @@ -74,8 +71,75 @@ public void Dispose() lock (_scanning) _disposed = true; using (var stopped = new ManualResetEvent(false)) if (_scan?.Dispose(stopped) == true) stopped.WaitOne(TimeSpan.FromSeconds(2)); - // What was not answered stays in requests/: the next load reads it again, with its own age. - try { File.Delete(Loaded); } catch (IOException) { } catch (UnauthorizedAccessException) { } + // What was not answered stays in requests/: the next load reads it again, with its own age. The file goes only + // when it is this Gateway's: another one's stays. + try { if (Named() is var (pid, _) && pid == Environment.ProcessId) File.Delete(Loaded); } + catch (IOException) { } + catch (UnauthorizedAccessException) { } + } + + /// + /// Whether this Gateway is the one loaded.json names, the one Waypoint's requests are for: the first to load + /// the mod in this folder, for as long as it runs. It takes the file when no running Gateway has it. + /// + /// A second Gateway in the same folder is one that was only asked a question: its commands and values + /// modes load the mods too, for their commands, and Waypoint's Console asks when it starts. Such a Gateway serves no + /// player and ends in a moment, so it must leave the file and the requests to the one that runs the game; written + /// by it, the file named a process that had ended, and Waypoint took the mod for not loaded. Should the named + /// Gateway end without unloading, the next scan of another takes its place. + /// + bool Serving() + { + if (Named() is var (pid, started)) + { + if (pid == Environment.ProcessId) return true; + if (Runs(pid, started)) return false; + } + using var me = Process.GetCurrentProcess(); + WriteAtomically(Loaded, new JsonObject + { + ["protocol"] = Protocol, + ["version"] = _mod.Manifest.Version, + ["pid"] = Environment.ProcessId, + // With the pid, which process: a number of an ended Gateway may be another process's by now. + ["started"] = me.StartTime.ToUniversalTime().ToString("O", CultureInfo.InvariantCulture), + ["actions"] = new JsonArray("item_give"), + }); + return true; + } + + /// The process loaded.json names and when it started (null from a version that did not write it), or + /// null without a readable file. + static (int Pid, DateTime? Started)? Named() + { + try + { + if (!File.Exists(Loaded) || JsonNode.Parse(File.ReadAllText(Loaded)) is not JsonObject json || (int?)json["pid"] is not int pid) return null; + return (pid, DateTime.TryParse((string?)json["started"], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var started) + ? started.ToUniversalTime() : null); + } + catch (Exception e) when (e is IOException or UnauthorizedAccessException or JsonException or InvalidOperationException or FormatException) + { + return null; + } + } + + /// Whether that process still runs and is the one that wrote the file: started when the file says, or, + /// without a start time, a program of this Gateway's name. + static bool Runs(int pid, DateTime? started) + { + try + { + using var process = Process.GetProcessById(pid); + if (process.HasExited) return false; + if (started is { } at) return Math.Abs((process.StartTime.ToUniversalTime() - at).TotalSeconds) < 2; + using var me = Process.GetCurrentProcess(); + return string.Equals(process.ProcessName, me.ProcessName, StringComparison.OrdinalIgnoreCase); + } + catch (Exception e) when (e is ArgumentException or InvalidOperationException or System.ComponentModel.Win32Exception or NotSupportedException) + { + return false; + } } /// Reads the new requests, refuses those it cannot read, and answers those that waited too long. @@ -86,6 +150,8 @@ void Scan() if (_disposed) return; try { + // The requests are for the Gateway the file names. + if (!Serving()) return; foreach (string file in Directory.EnumerateFiles(Requests, "*.json")) { string id = Path.GetFileNameWithoutExtension(file); diff --git a/mods/waypoint-tools/mod.json b/mods/waypoint-tools/mod.json index be7dbd7..e75487a 100644 --- a/mods/waypoint-tools/mod.json +++ b/mods/waypoint-tools/mod.json @@ -3,7 +3,7 @@ "name": "Waypoint Tools", "description": "Lets Waypoint's tools reach the game without the client control bridge: the item crafter gives its items through it.", "author": "Waypoint", - "version": "1.0.2", + "version": "1.0.3", "compatibleClientVersions": ["0.4.3-beta", "0.5.0-beta", "0.5.1-beta"], "kind": "feature", "depends": ["Game"]