// ============================================================================ // RynthSuite - Plugins/Shared/MoveAudit.cs // Movement calls with a caller tag: host.SetAutoRunBy("Nav", true) and friends. // Linked into the plugins that move the character, like UseAudit.cs. // // Why (2026-10-05, Drakkon's stuck casts on DreamWeave): a movement packet sent // while a cast winds up can cancel the cast, or leave the server holding it open. // The engine logged only one movement call in eight and never said who sent it, // so "which subsystem sent SetAutoRun during Buffing" could not be answered. // Every call is now one log line: // // [Move] RynthAi/Nav SetAutoRun(False) // // Identical calls from the same subsystem in a row (the nav servo turns every // tick; any angle counts as identical for turns) are counted instead of written // one by one, and the count goes out with the next line that is written (the // next different call, or the next repeat after RepeatWindowMs): // // [Move] RynthAi/Arbiter StopCompletely() (+14 more TurnToHeading, last 230.5 before this, over 480 ms) // // The engine logs the same calls (with the plugin's name) behind its own move // trace switch; this line adds the subsystem, which only the plugin knows. // ============================================================================ using System; using RynthCore.PluginSdk; namespace RynthCore.Plugin.Shared; internal static class MoveAudit { /// Off = the calls go through without a line. On by default. public static volatile bool Enabled = true; /// The plugin's name in every line (UseAudit.PluginName is set at Init too). public static string PluginName => UseAudit.PluginName; /// Clock (ms); tests replace it. public static Func NowMs = () => Environment.TickCount64; // Identical calls closer than this to the last WRITTEN line are only counted. private const long RepeatWindowMs = 1000; private static readonly object Gate = new(); private static string _lastKey = ""; private static long _lastWrittenMs = long.MinValue; private static int _repeats; private static string _repeatLastArg = ""; private static long _repeatFirstMs; public static bool SetAutoRunBy(this RynthCoreHost host, string who, bool on) { Note(host, who, "SetAutoRun", on ? "True" : "False", collapseByKindOnly: false); return host.SetAutoRun(on); } public static bool StopCompletelyBy(this RynthCoreHost host, string who) { Note(host, who, "StopCompletely", "", collapseByKindOnly: false); return host.StopCompletely(); } public static bool SetMotionBy(this RynthCoreHost host, string who, uint motion, bool on) { Note(host, who, "SetMotion", $"0x{motion:X8}, {(on ? "True" : "False")}", collapseByKindOnly: false); return host.SetMotion(motion, on); } public static bool DoMovementBy(this RynthCoreHost host, string who, uint motion, float speed, int holdKey) { Note(host, who, "DoMovement", $"0x{motion:X8}, {speed:0.##}, {holdKey}", collapseByKindOnly: false); return host.DoMovement(motion, speed, holdKey); } public static bool StopMovementBy(this RynthCoreHost host, string who, uint motion, int holdKey) { Note(host, who, "StopMovement", $"0x{motion:X8}, {holdKey}", collapseByKindOnly: false); return host.StopMovement(motion, holdKey); } /// A heading snap. Successive turns from one subsystem collapse whatever the angle. public static bool TurnToHeadingBy(this RynthCoreHost host, string who, float headingDegrees) { Note(host, who, "TurnToHeading", headingDegrees.ToString("0.0", System.Globalization.CultureInfo.InvariantCulture), collapseByKindOnly: true); return host.TurnToHeading(headingDegrees); } /// Writes (or counts) one call. Internal for the tests. internal static string? Note(RynthCoreHost host, string who, string call, string arg, bool collapseByKindOnly) { if (!Enabled) return null; string? line = null; lock (Gate) { long now = NowMs(); string key = collapseByKindOnly ? $"{who}|{call}" : $"{who}|{call}({arg})"; if (key == _lastKey && now - _lastWrittenMs < RepeatWindowMs) { if (_repeats == 0) _repeatFirstMs = now; _repeats++; _repeatLastArg = arg; return null; } string more = _repeats > 0 ? $" (+{_repeats} more {RepeatName()} before this, over {now - _repeatFirstMs} ms)" : ""; line = $"[Move] {PluginName}/{who} {call}({arg}){more}"; _lastKey = key; _lastWrittenMs = now; _repeats = 0; _repeatLastArg = ""; } try { host.Log(line); } catch { } return line; } private static string RepeatName() { int bar = _lastKey.IndexOf('|'); string last = bar >= 0 ? _lastKey.Substring(bar + 1) : _lastKey; return _lastKey.EndsWith(")", StringComparison.Ordinal) ? last : $"{last}, last {_repeatLastArg}"; } /// Back to a fresh load (Init, tests). public static void Reset() { lock (Gate) { _lastKey = ""; _lastWrittenMs = long.MinValue; _repeats = 0; _repeatLastArg = ""; } } }