|
| 1 | +using System.Collections.Generic; |
| 2 | +using HarmonyLib; |
| 3 | +using Multiplayer.Client.Util; |
| 4 | +using UnityEngine; |
| 5 | +using Verse; |
| 6 | +using Verse.Sound; |
| 7 | + |
| 8 | +namespace Multiplayer.Client.Patches |
| 9 | +{ |
| 10 | + // Sustainers with PerTick/PerTickRare maintenance compare an ambient tick |
| 11 | + // against lastMaintainTick, but under per-map clocks the stamp and the |
| 12 | + // check can come from different clocks: Maintain() runs under whatever |
| 13 | + // context the maintainer had (often a map's tick loop), while |
| 14 | + // SustainerUpdate runs under the sound bracket's clock (owner map, or the |
| 15 | + // world clock for map-less sustainers). A stamp clock behind the check |
| 16 | + // clock reads as "unmaintained" and ends the sustainer even though its |
| 17 | + // owner maintained it microseconds ago - and owners respawn ended |
| 18 | + // sustainers, so the sound restarts every frame. |
| 19 | + // |
| 20 | + // Fix: before the vanilla staleness check runs, if the sustainer LOOKS |
| 21 | + // tick-stale but was really maintained within the grace window (measured |
| 22 | + // in realtime, which no clock swap can touch), re-stamp it under the |
| 23 | + // clock the check will use. Truly abandoned sustainers still die, at most |
| 24 | + // GraceSeconds late - audio-only, imperceptible. PerFrame maintenance is |
| 25 | + // untouched (frame counts are global). Vanilla paused behavior is |
| 26 | + // untouched (a frozen clock never reads as stale). |
| 27 | + public static class SustainerRealtimeMaintenance |
| 28 | + { |
| 29 | + public static readonly Dictionary<Sustainer, float> lastMaintainRealTime = new(); |
| 30 | + |
| 31 | + public const float GraceSeconds = 1f; |
| 32 | + |
| 33 | + private static float lastRescueReport; |
| 34 | + private static int rescueCount; |
| 35 | + |
| 36 | + public static void NoteRescue(Sustainer sustainer, int ambientTicks, int staleStamp) |
| 37 | + { |
| 38 | + rescueCount++; |
| 39 | + var now = Time.realtimeSinceStartup; |
| 40 | + if (now - lastRescueReport < 60f) return; |
| 41 | + lastRescueReport = now; |
| 42 | + MpLog.Debug($"Sustainer cross-clock rescue: {sustainer.def} ambient={ambientTicks} stamp={staleStamp} ({rescueCount} rescues since last report)"); |
| 43 | + rescueCount = 0; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [HarmonyPatch(typeof(Sustainer), MethodType.Constructor, typeof(SoundDef), typeof(SoundInfo))] |
| 48 | + static class SustainerCtorRealtimeStamp |
| 49 | + { |
| 50 | + static void Postfix(Sustainer __instance) |
| 51 | + { |
| 52 | + if (Multiplayer.Client == null) return; |
| 53 | + SustainerRealtimeMaintenance.lastMaintainRealTime[__instance] = Time.realtimeSinceStartup; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [HarmonyPatch(typeof(Sustainer), nameof(Sustainer.Maintain))] |
| 58 | + static class SustainerMaintainRealtimeStamp |
| 59 | + { |
| 60 | + static void Postfix(Sustainer __instance) |
| 61 | + { |
| 62 | + if (Multiplayer.Client == null) return; |
| 63 | + SustainerRealtimeMaintenance.lastMaintainRealTime[__instance] = Time.realtimeSinceStartup; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [HarmonyPatch(typeof(Sustainer), nameof(Sustainer.End))] |
| 68 | + static class SustainerEndCleanup |
| 69 | + { |
| 70 | + static void Postfix(Sustainer __instance) |
| 71 | + => SustainerRealtimeMaintenance.lastMaintainRealTime.Remove(__instance); |
| 72 | + } |
| 73 | + |
| 74 | + [HarmonyPatch(typeof(Sustainer), nameof(Sustainer.SustainerUpdate))] |
| 75 | + static class SustainerTolerantEndCheck |
| 76 | + { |
| 77 | + // Runs after SustainerUpdateMapTime's prefix so the re-stamp uses the |
| 78 | + // same clock the vanilla staleness check is about to read |
| 79 | + [HarmonyPriority(Priority.Low)] |
| 80 | + static void Prefix(Sustainer __instance) |
| 81 | + { |
| 82 | + if (Multiplayer.Client == null || __instance.Ended) return; |
| 83 | + |
| 84 | + var maintenance = __instance.info.Maintenance; |
| 85 | + int staleAfter; |
| 86 | + if (maintenance == MaintenanceType.PerTick) |
| 87 | + staleAfter = 1; |
| 88 | + else if (maintenance == MaintenanceType.PerTickRare) |
| 89 | + staleAfter = 250; |
| 90 | + else |
| 91 | + return; |
| 92 | + |
| 93 | + var ambientTicks = Find.TickManager.TicksGame; |
| 94 | + if (ambientTicks <= __instance.lastMaintainTick + staleAfter) |
| 95 | + return; // not stale, vanilla keeps it |
| 96 | + |
| 97 | + if (!SustainerRealtimeMaintenance.lastMaintainRealTime.TryGetValue(__instance, out var maintainedAt) || |
| 98 | + Time.realtimeSinceStartup - maintainedAt > SustainerRealtimeMaintenance.GraceSeconds) |
| 99 | + return; // truly abandoned, let vanilla end it |
| 100 | + |
| 101 | + SustainerRealtimeMaintenance.NoteRescue(__instance, ambientTicks, __instance.lastMaintainTick); |
| 102 | + __instance.lastMaintainTick = ambientTicks; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments