diff --git a/Modules/Utils.cs b/Modules/Utils.cs index 287988f58..3c8e84212 100644 --- a/Modules/Utils.cs +++ b/Modules/Utils.cs @@ -3846,43 +3846,72 @@ public static class Utils return true; } - public static IEnumerator SendGameData() + public static IEnumerator SendGameDataContinuously() { - int messages = 0; - - MessageWriter writer = MessageWriter.Get(SendOption.Reliable); - writer.StartMessage(5); - writer.Write(AmongUsClient.Instance.GameId); - - foreach (NetworkedPlayerInfo playerinfo in GameData.Instance.AllPlayers) + Stopwatch stopwatch = Stopwatch.StartNew(); + + while (GameStates.InGame && !GameStates.IsEnded && ShipStatus.Instance) { - if (writer.Length > 500 || messages >= AmongUsClient.Instance.GetMaxMessagePackingLimit()) + if (ReportDeadBodyPatch.MeetingStarted || GameStates.IsMeeting || ExileController.Instance || AntiBlackout.SkipTasks) { - messages = 0; - writer.EndMessage(); - var qa = DataFlagRateLimiter.Enqueue(() => AmongUsClient.Instance.SendOrDisconnect(writer)); - yield return qa.Wait(); - if (qa.Dropped) + stopwatch.Reset(); + yield return new WaitForSecondsRealtime(10f); + stopwatch.Start(); + continue; + } + + int messages = 0; + + MessageWriter writer = MessageWriter.Get(); + writer.StartMessage(5); + writer.Write(AmongUsClient.Instance.GameId); + + for (var index = 0; index < GameData.Instance.AllPlayers.Count; index++) + { + NetworkedPlayerInfo playerinfo = GameData.Instance.AllPlayers[index]; + + if (writer.Length > 500 || messages >= AmongUsClient.Instance.GetMaxMessagePackingLimit()) { - writer.Recycle(); - yield break; + messages = 0; + writer.EndMessage(); + var qa = DataFlagRateLimiter.Enqueue(() => AmongUsClient.Instance.SendOrDisconnect(writer), channel: SendOption.None); + yield return qa.Wait(); + yield return new WaitForSecondsRealtime(1f); + + if (qa.Dropped || !GameStates.InGame || GameStates.IsEnded || !ShipStatus.Instance) + { + writer.Recycle(); + Logger.Msg("Coroutine finished", nameof(SendGameDataContinuously)); + yield break; + } + + writer.Clear(SendOption.None); + writer.StartMessage(5); + writer.Write(AmongUsClient.Instance.GameId); } - writer.Clear(SendOption.Reliable); - writer.StartMessage(5); - writer.Write(AmongUsClient.Instance.GameId); + + if (!playerinfo || (SoulCollector.On && Main.PlayerStates.Values.Any(x => x.Role is SoulCollector sc && sc.ToExile.Contains(playerinfo.PlayerId)))) continue; + + playerinfo.IsDead = !playerinfo.Object.IsAlive(); + + writer.StartMessage(1); + writer.WritePacked(playerinfo.NetId); + playerinfo.Serialize(writer, false); + writer.EndMessage(); + + messages++; } - writer.StartMessage(1); - writer.WritePacked(playerinfo.NetId); - playerinfo.Serialize(writer, false); writer.EndMessage(); + if (messages > 0) yield return DataFlagRateLimiter.Enqueue(() => AmongUsClient.Instance.SendOrDisconnect(writer), channel: SendOption.None).Wait(); + writer.Recycle(); - messages++; + stopwatch.Reset(); + yield return new WaitForSecondsRealtime(5f); + stopwatch.Start(); } - - writer.EndMessage(); - yield return DataFlagRateLimiter.Enqueue(() => AmongUsClient.Instance.SendOrDisconnect(writer)).Wait(); - writer.Recycle(); + + Logger.Msg("Coroutine finished", nameof(SendGameDataContinuously)); } public static void SendGameDataTo(int targetClientId) diff --git a/Patches/ExilePatch.cs b/Patches/ExilePatch.cs index 8ca0f49b0..0c6d762c8 100644 --- a/Patches/ExilePatch.cs +++ b/Patches/ExilePatch.cs @@ -232,11 +232,6 @@ internal static class ExileControllerWrapUpPatch if (GameStates.IsEnded) yield break; yield return Utils.NotifyEveryoneAsync(); - - foreach (NetworkedPlayerInfo playerinfo in GameData.Instance.AllPlayers) - playerinfo.IsDead = !playerinfo.Object.IsAlive(); - - yield return Utils.SendGameData(); } } diff --git a/Patches/IntroPatch.cs b/Patches/IntroPatch.cs index 6f50b8ed6..82236fbf4 100644 --- a/Patches/IntroPatch.cs +++ b/Patches/IntroPatch.cs @@ -1369,6 +1369,7 @@ internal static class IntroCutsceneDestroyPatch LateTask.New(() => Main.Instance.StartCoroutine(Utils.NotifyEveryoneAsync()), 3f, "NotifyEveryoneAsync On Game Start"); LateTask.New(Utils.MarkEveryoneDirtySettings, 0.5f, "SyncAllSettings On Game Start"); LateTask.New(() => Main.Instance.StartCoroutine(ShipStatusFixedUpdatePatch.Postfix()), 5f, "ShipStatusFixedUpdatePatch Postfix Start"); + LateTask.New(() => Main.Instance.StartCoroutine(Utils.SendGameDataContinuously()), 10f, "SendGameDataContinuously Start"); Utils.CheckAndSetVentInteractions(); } diff --git a/Patches/ShipStatusPatch.cs b/Patches/ShipStatusPatch.cs index bf57cc986..00d22211e 100644 --- a/Patches/ShipStatusPatch.cs +++ b/Patches/ShipStatusPatch.cs @@ -773,7 +773,7 @@ internal static class ShipStatusFixedUpdatePatch { Stopwatch = Stopwatch.StartNew(); - while (ShipStatus.Instance) + while (GameStates.InGame && !GameStates.IsEnded && ShipStatus.Instance) { if (ReportDeadBodyPatch.MeetingStarted || GameStates.IsMeeting || ExileController.Instance || AntiBlackout.SkipTasks) { @@ -831,7 +831,6 @@ internal static class ShipStatusFixedUpdatePatch Stopwatch.Start(); } - if (ShipStatus.Instance) - Main.Instance.StartCoroutine(Postfix()); + Logger.Msg("Coroutine finished", nameof(ShipStatusFixedUpdatePatch)); } } \ No newline at end of file diff --git a/Roles/Standard/Neutral/Killing/SoulCollector.cs b/Roles/Standard/Neutral/Killing/SoulCollector.cs index f30150431..d6d820bfe 100644 --- a/Roles/Standard/Neutral/Killing/SoulCollector.cs +++ b/Roles/Standard/Neutral/Killing/SoulCollector.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using AmongUs.GameOptions; using Hazel;