From ae3b3e0c2aa7da9bd265edbe86edca40a77c8a1c Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:41:46 +0100 Subject: [PATCH 1/3] fix: add bounded race activity grace period for telemetry dropouts --- .../State/RaceLoadingGracePeriod.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/AzimuthRPC.Core/State/RaceLoadingGracePeriod.cs diff --git a/src/AzimuthRPC.Core/State/RaceLoadingGracePeriod.cs b/src/AzimuthRPC.Core/State/RaceLoadingGracePeriod.cs new file mode 100644 index 0000000..24eda70 --- /dev/null +++ b/src/AzimuthRPC.Core/State/RaceLoadingGracePeriod.cs @@ -0,0 +1,45 @@ +namespace AzimuthRPC; + +/// +/// Avoid reporting "Loading Race" during a brief loss of the telemetry-active bit +/// while an event context is still confirmed. A real loading UI always wins. +/// This is a bounded display-only grace period, not a new game-state detector. +/// +internal sealed class RaceLoadingGracePeriod +{ + private static readonly TimeSpan MaxTelemetryGap = TimeSpan.FromSeconds(2); + private DateTime? _lastConfirmedRaceUtc; + + internal GameSnapshot Apply(GameSnapshot snapshot, DateTime nowUtc) + { + // Do not carry a previous race into a different activity, game process, + // missing hook, or an explicitly detected loading screen. + if (!snapshot.Attached || !snapshot.Hooks.Installed || + snapshot.Hooks.EventContext != 1 || snapshot.UiMode == UiMode.Loading) + { + Reset(); + return snapshot; + } + + if (snapshot.Activity == GameActivity.Event && snapshot.Hooks.Active) + { + _lastConfirmedRaceUtc = nowUtc; + return snapshot; + } + + // UiMode.Unknown is not proof of a loading screen. A single inactive + // hook sample should not replace a recently observed racing presence. + if (snapshot.UiMode == UiMode.Unknown && + snapshot.Activity == GameActivity.RaceLoading && + !snapshot.Hooks.Active && + _lastConfirmedRaceUtc is DateTime last && + nowUtc >= last && nowUtc - last <= MaxTelemetryGap) + { + return snapshot with { Activity = GameActivity.Event }; + } + + return snapshot; + } + + internal void Reset() => _lastConfirmedRaceUtc = null; +} From cd3b745b090310630582655bf86f88e174f9c2b7 Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:42:04 +0100 Subject: [PATCH 2/3] fix: stabilize Discord race presence while telemetry briefly drops --- src/AzimuthRPC.Debug/DebugRuntime.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/AzimuthRPC.Debug/DebugRuntime.cs b/src/AzimuthRPC.Debug/DebugRuntime.cs index a9e8ac8..320a24f 100644 --- a/src/AzimuthRPC.Debug/DebugRuntime.cs +++ b/src/AzimuthRPC.Debug/DebugRuntime.cs @@ -11,6 +11,7 @@ internal sealed class DebugRuntime : IAsyncDisposable private static readonly TimeSpan PresenceMinInterval = TimeSpan.FromSeconds(4); private AppConfig _options; private readonly CancellationTokenSource _stop = new(); + private readonly RaceLoadingGracePeriod _raceLoadingGrace = new(); private Task? _loop; private Process? _process; private ExternalProcessMemoryReader? _memory; @@ -46,7 +47,7 @@ private async Task RunAsync() { try { await TickAsync(_stop.Token); } catch (OperationCanceledException) when (_stop.IsCancellationRequested) { break; } - catch (Exception e) { _memorySnapshot = GameSnapshot.Unavailable(e.Message); Log?.Invoke(e.Message); Publish(_memorySnapshot); } + catch (Exception e) { _raceLoadingGrace.Reset(); _memorySnapshot = GameSnapshot.Unavailable(e.Message); Log?.Invoke(e.Message); Publish(_memorySnapshot); } } while (await timer.WaitForNextTickAsync(_stop.Token)); } catch (OperationCanceledException) when (_stop.IsCancellationRequested) { } @@ -54,7 +55,7 @@ private async Task RunAsync() private async Task TickAsync(CancellationToken token) { var options = Volatile.Read(ref _options); var now = DateTime.UtcNow; - if (options.Offline) { Publish(GameSnapshot.Unavailable("Offline debug mode; memory, hooks and Discord disabled")); return; } + if (options.Offline) { _raceLoadingGrace.Reset(); Publish(GameSnapshot.Unavailable("Offline debug mode; memory, hooks and Discord disabled")); return; } if (_process is not null && Exited(_process)) await DetachAsync(); if (_process is null && now >= _nextAttach) { @@ -85,7 +86,7 @@ private async Task TickAsync(CancellationToken token) else { candidate.Dispose(); Log?.Invoke(installed.Status); } } var telemetry = _hooks?.Read() ?? Fh2TelemetrySample.Unavailable("Telemetry hooks unavailable"); - var snapshot = GameStateInterpreter.Apply(_memorySnapshot, telemetry, _hooks?.Installed == true); + var snapshot = _raceLoadingGrace.Apply(GameStateInterpreter.Apply(_memorySnapshot, telemetry, _hooks?.Installed == true), now); var presence = DiscordPresenceBuilder.Build(snapshot, options, _started == default ? now : _started); if (_discord is not null && (!_discord.IsConnected || _discordApplicationId != options.ApplicationId)) await DisconnectAsync(); if (_process is not null && !string.IsNullOrWhiteSpace(options.ApplicationId) && _discord is null && now >= _nextDiscord) @@ -115,7 +116,7 @@ private void Publish(GameSnapshot snapshot, DiscordPresence? presence = null) var next = new DebugStatus(snapshot, _process is null ? "Waiting for Azimuth" : $"azimuth ยท PID {_process.Id}", snapshot.Attached ? $"0x{snapshot.GameBase:X} ({(_pe.PreferredImageBase == snapshot.GameBase ? "preferred" : "relocated")})" : snapshot.Error ?? "Not attached", _discordStatus, presence ?? DiscordPresenceBuilder.Build(snapshot, Volatile.Read(ref _options), _started), _process?.Id, _started); Volatile.Write(ref _status, next); Changed?.Invoke(next); } - private void ResetHooks() { _hooks?.Dispose(); _hooks = null; _nextHook = DateTime.MinValue; } + private void ResetHooks() { _raceLoadingGrace.Reset(); _hooks?.Dispose(); _hooks = null; _nextHook = DateTime.MinValue; } private void ResetMapping() { ResetHooks(); _base = 0; _reader = _memory is null ? null : new(_memory, Fh2MainlandLocationResolver.Instance); _memorySnapshot = GameSnapshot.Unavailable("Mapping invalidated"); } private async Task DetachAsync() { From b3ba278aadfb96974f0bee21dbe1cd76c5e55275 Mon Sep 17 00:00:00 2001 From: Refil <53364591+Refilewski@users.noreply.github.com> Date: Sun, 25 Oct 2026 15:42:34 +0100 Subject: [PATCH 3/3] test: cover transient race hook loss, genuine loading and resets --- tests/AzimuthRPC.Core.Tests/Program.cs | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/AzimuthRPC.Core.Tests/Program.cs b/tests/AzimuthRPC.Core.Tests/Program.cs index 06dc892..a011c70 100644 --- a/tests/AzimuthRPC.Core.Tests/Program.cs +++ b/tests/AzimuthRPC.Core.Tests/Program.cs @@ -76,6 +76,37 @@ var s when s.StartsWith("worldmap_") => UiMode.WorldMap, Check(game.Activity == expected); Check(game.Hooks.NormalizedRev == .13f); Check(game.WorldPresence == WorldPresence.InWorld); } }); +Test("Brief inactive telemetry does not flicker into Loading Race", () => +{ + var grace = new RaceLoadingGracePeriod(); + var time = DateTime.UnixEpoch; + GameSnapshot Observed(int eventContext, bool active, UiMode ui = UiMode.Unknown) => + GameStateInterpreter.Apply( + new GameSnapshot { Attached = true, UiMode = ui }, + new Fh2TelemetrySample(true, active, null, null, false, false, eventContext, true, "Synthetic"), + true); + + Check(grace.Apply(Observed(1, true), time).Activity == GameActivity.Event); + var brieflyInactive = grace.Apply(Observed(1, false), time.AddMilliseconds(700)); + Check(brieflyInactive.Activity == GameActivity.Event, "Brief telemetry gap should retain racing"); + Check(DiscordPresenceBuilder.Build(brieflyInactive, new PresenceOptions(), time).Details == "Racing"); + Check(grace.Apply(Observed(1, false), time.AddSeconds(3)).Activity == GameActivity.RaceLoading, + "Grace period must expire; do not invent permanent racing"); + Check(grace.Apply(Observed(1, true), time.AddSeconds(4)).Activity == GameActivity.Event); + Check(grace.Apply(Observed(1, false, UiMode.Loading), time.AddSeconds(4.1)).Activity == GameActivity.RaceLoading, + "An explicit loading UI must never be masked"); + Check(grace.Apply(Observed(1, false), time.AddSeconds(4.2)).Activity == GameActivity.RaceLoading, + "Confirmed loading must reset the grace timer"); + + Check(grace.Apply(Observed(1, true), time.AddSeconds(5)).Activity == GameActivity.Event); + Check(grace.Apply(Observed(2, false), time.AddSeconds(5.1)).Activity == GameActivity.FreeRoam); + Check(grace.Apply(Observed(1, false), time.AddSeconds(5.2)).Activity == GameActivity.RaceLoading, + "Free Roam must cancel previous race activity"); + Check(grace.Apply(Observed(1, true), time.AddSeconds(6)).Activity == GameActivity.Event); + Check(!grace.Apply(GameSnapshot.Unavailable("Game closed"), time.AddSeconds(6.1)).Attached); + Check(grace.Apply(Observed(1, false), time.AddSeconds(6.2)).Activity == GameActivity.RaceLoading, + "Detach must not leak previous game's race"); +}); Test("Unavailable memory clears live telemetry and activity", () => { var game = GameStateInterpreter.Apply(GameSnapshot.Unavailable("Read failed"), new(true, true, .5f, .2f, true, true, 1, true, "Old"), true);