From 2c854192a919710afb46508bc01656fbc8804b89 Mon Sep 17 00:00:00 2001 From: Gurge44 Date: Fri, 17 Apr 2026 09:21:00 +0200 Subject: [PATCH] Add code to make sure to not exceed new rate limits --- Gamemodes/CaptureTheFlag.cs | 13 ++++++---- Gamemodes/KingOfTheZones.cs | 4 +-- Modules/ExtendedPlayerControl.cs | 7 ++++++ Modules/Utils.cs | 22 ++++++++++++++--- Patches/OnGameStartedPatch.cs | 8 +++--- Patches/PlayerControlPatch.cs | 42 ++++++++++++++++++-------------- 6 files changed, 64 insertions(+), 32 deletions(-) diff --git a/Gamemodes/CaptureTheFlag.cs b/Gamemodes/CaptureTheFlag.cs index d8560da53..5e85dd6ae 100644 --- a/Gamemodes/CaptureTheFlag.cs +++ b/Gamemodes/CaptureTheFlag.cs @@ -319,13 +319,16 @@ public static class CaptureTheFlag void ResetSkins() { - foreach ((byte key, NetworkedPlayerInfo.PlayerOutfit outfit) in DefaultOutfits) + Utils.CombineSendTimeLowering(() => { - PlayerControl pc = key.GetPlayer(); + foreach ((byte key, NetworkedPlayerInfo.PlayerOutfit outfit) in DefaultOutfits) + { + PlayerControl pc = key.GetPlayer(); - if (pc && outfit != null) - Utils.RpcChangeSkin(pc, outfit); - } + if (pc && outfit != null) + Utils.RpcChangeSkin(pc, outfit); + } + }); } } diff --git a/Gamemodes/KingOfTheZones.cs b/Gamemodes/KingOfTheZones.cs index 7c69de84c..7055fb393 100644 --- a/Gamemodes/KingOfTheZones.cs +++ b/Gamemodes/KingOfTheZones.cs @@ -645,7 +645,7 @@ public static class KingOfTheZones } } - void ResetSkins() => DefaultOutfits.Select(x => (pc: x.Key.GetPlayer(), outfit: x.Value)).DoIf(x => x.pc && x.outfit != null, x => Utils.RpcChangeSkin(x.pc, x.outfit)); + void ResetSkins() => Utils.CombineSendTimeLowering(() => DefaultOutfits.Select(x => (pc: x.Key.GetPlayer(), outfit: x.Value)).DoIf(x => x.pc && x.outfit != null, x => Utils.RpcChangeSkin(x.pc, x.outfit))); } public static bool IsNotInLocalPlayersTeam(PlayerControl pc) @@ -949,7 +949,7 @@ public static class KingOfTheZones { if (tie) return; GameGoing = false; - DefaultOutfits.Select(x => (pc: x.Key.GetPlayer(), outfit: x.Value)).DoIf(x => x.pc && x.outfit != null, x => Utils.RpcChangeSkin(x.pc, x.outfit)); + Utils.CombineSendTimeLowering(() => DefaultOutfits.Select(x => (pc: x.Key.GetPlayer(), outfit: x.Value)).DoIf(x => x.pc && x.outfit != null, x => Utils.RpcChangeSkin(x.pc, x.outfit))); KOTZTeam winner = Points.GetKeyByValue(highestPoints); CustomWinnerHolder.WinnerIds = PlayerTeams.Where(x => x.Value == winner).Select(x => x.Key).ToHashSet(); Color color = winner.GetColor(); diff --git a/Modules/ExtendedPlayerControl.cs b/Modules/ExtendedPlayerControl.cs index 1110b1140..11187b10c 100644 --- a/Modules/ExtendedPlayerControl.cs +++ b/Modules/ExtendedPlayerControl.cs @@ -24,6 +24,7 @@ internal static class ExtendedPlayerControl public static readonly HashSet CancelBlackScreenFix = []; private static readonly List ResultBuffer = []; public static readonly HashSet TempExiled = []; + public static bool DontLowerSendTimer; extension(PlayerControl player) { @@ -250,6 +251,9 @@ internal static class ExtendedPlayerControl writer.EndMessage(); AmongUsClient.Instance.SendOrDisconnect(writer); writer.Recycle(); + + if (DontLowerSendTimer) return; + AmongUsClient.Instance.timer -= AmongUsClient.Instance.MinSendInterval; } public ClientData GetClient() @@ -2443,6 +2447,9 @@ internal static class ExtendedPlayerControl writer.EndMessage(); AmongUsClient.Instance.SendOrDisconnect(writer); writer.Recycle(); + + if (DontLowerSendTimer) return; + AmongUsClient.Instance.timer -= AmongUsClient.Instance.MinSendInterval; } } diff --git a/Modules/Utils.cs b/Modules/Utils.cs index 29d691e59..12e473e31 100644 --- a/Modules/Utils.cs +++ b/Modules/Utils.cs @@ -3486,8 +3486,7 @@ public static class Utils return true; } - -/* + public static void SendGameData() { int messages = 0; @@ -3520,8 +3519,9 @@ public static class Utils writer.EndMessage(); AmongUsClient.Instance.SendOrDisconnect(writer); writer.Recycle(); + + AmongUsClient.Instance.timer -= AmongUsClient.Instance.MinSendInterval; } -*/ public static void SendGameDataTo(int targetClientId) { @@ -4508,7 +4508,21 @@ public static class Utils } } - aapc.Do(x => x.SetChatVisible(true)); + CombineSendTimeLowering(() => aapc.Do(x => x.SetChatVisible(true))); + } + + /// + /// Wraps code to run while is set to true, then lowers the sending timer once. + /// + public static void CombineSendTimeLowering([Annotations.InstantHandle] Action action) + { + ExtendedPlayerControl.DontLowerSendTimer = true; + + try { action(); } + catch (Exception e) { ThrowException(e); } + + ExtendedPlayerControl.DontLowerSendTimer = false; + AmongUsClient.Instance.timer -= AmongUsClient.Instance.MinSendInterval; } public static bool TryCast(this Il2CppObjectBase obj, out T casted) where T : Il2CppObjectBase diff --git a/Patches/OnGameStartedPatch.cs b/Patches/OnGameStartedPatch.cs index a2148809e..cdd760f2d 100644 --- a/Patches/OnGameStartedPatch.cs +++ b/Patches/OnGameStartedPatch.cs @@ -1142,8 +1142,9 @@ internal static class StartGameHostPatch yield return null; pc.Data.Disconnected = true; - pc.Data.SendGameData(); } + + Utils.SendGameData(); Logger.Info("Successfully set everyone's data as Disconnected", "StartGameHost"); @@ -1159,12 +1160,13 @@ internal static class StartGameHostPatch foreach (PlayerControl pc in PlayerControl.AllPlayerControls) { - if (pc == null || pc.Data == null) continue; + if (!pc || !pc.Data) continue; bool disconnected = Main.PlayerStates.TryGetValue(pc.PlayerId, out var state) && state.IsDead && state.deathReason == PlayerState.DeathReason.Disconnected; pc.Data.Disconnected = disconnected; - if (!disconnected) pc.Data.SendGameData(); } + + Utils.SendGameData(); } private static bool IsBasisChangingPlayer(byte id, CustomRoles role) diff --git a/Patches/PlayerControlPatch.cs b/Patches/PlayerControlPatch.cs index 447488035..669ca6f37 100644 --- a/Patches/PlayerControlPatch.cs +++ b/Patches/PlayerControlPatch.cs @@ -1472,33 +1472,39 @@ internal static class ReportDeadBodyPatch { Main.ProcessShapeshifts = false; - foreach (PlayerControl pc in Main.EnumeratePlayerControls()) + void Action() { - try + foreach (PlayerControl pc in Main.EnumeratePlayerControls()) { - if (pc.IsAlive()) + try { - if (Camouflage.IsCamouflage && !Magistrate.CallCourtNextMeeting && !Doppelganger.DoppelVictim.ContainsKey(pc.PlayerId)) - Camouflage.RpcSetSkin(pc, revertToDefault: true, forceRevert: true); - - if (Magistrate.CallCourtNextMeeting) + if (pc.IsAlive()) { - string name = pc.GetRealName(); - RpcChangeSkin(pc, new NetworkedPlayerInfo.PlayerOutfit().Set(name, 15, "", "", "", "", "")); - } + if (Camouflage.IsCamouflage && !Magistrate.CallCourtNextMeeting && !Doppelganger.DoppelVictim.ContainsKey(pc.PlayerId)) + Camouflage.RpcSetSkin(pc, revertToDefault: true, forceRevert: true); - if (Main.CheckShapeshift.ContainsKey(pc.PlayerId)) - pc.RpcShapeshift(pc, false); - - if (pc.Is(CustomRoles.Truant)) - Main.AllPlayerSpeed[pc.PlayerId] = Main.MinSpeed; + if (Magistrate.CallCourtNextMeeting) + { + string name = pc.GetRealName(); + RpcChangeSkin(pc, new NetworkedPlayerInfo.PlayerOutfit().Set(name, 15, "", "", "", "", "")); + } + + if (Main.CheckShapeshift.ContainsKey(pc.PlayerId)) + pc.RpcShapeshift(pc, false); + + if (pc.Is(CustomRoles.Truant)) + Main.AllPlayerSpeed[pc.PlayerId] = Main.MinSpeed; + } + else if (!pc.Data.IsDead && (!pc.AmOwner || !TempReviveHostRunning)) + pc.RpcExileV2(); } - else if (!pc.Data.IsDead && (!pc.AmOwner || !TempReviveHostRunning)) - pc.RpcExileV2(); + catch (Exception e) { ThrowException(e); } } - catch (Exception e) { ThrowException(e); } } + if (!Camouflage.IsCamouflage && !Magistrate.CallCourtNextMeeting) Action(); + else CombineSendTimeLowering(Action); + RPCHandlerPatch.RemoveExpiredWhiteList(); Camouflage.CamoTimesThisRound = 0;