From 211ca83ddf4d148c6c8cb5a60cd29b609088afbc Mon Sep 17 00:00:00 2001 From: Mooshua <43320783+Mooshua@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:12:54 -0800 Subject: [PATCH] Prevent some crashes when touching disconnected clients (#22) * Make global state eviction slightly more verbose * All format operations will check for a valid client before continuing --- mod/Jailbreak.Teams/Queue/QueueBehavior.cs | 14 ++++++- .../Extensions/ViewExtensions.cs | 37 +++++++++++++------ .../Behaviors/GlobalStateTracker.cs | 12 ++++++ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs index 9133a528..e9c42416 100644 --- a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs +++ b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs @@ -46,6 +46,9 @@ public void Start(BasePlugin parent) public bool TryEnterQueue(CCSPlayerController player) { + if (!player.IsReal()) + return false; + if (player.GetTeam() == CsTeam.CounterTerrorist) return false; @@ -59,6 +62,9 @@ public bool TryEnterQueue(CCSPlayerController player) public bool TryExitQueue(CCSPlayerController player) { + if (!player.IsReal()) + return false; + var state = _state.Get(player); state.InQueue = false; state.IsGuard = false; @@ -68,7 +74,9 @@ public bool TryExitQueue(CCSPlayerController player) public bool TryPop(int count) { - var queue = Queue.ToList(); + var queue = Queue + .Where(player => player.IsReal()) + .ToList(); if (queue.Count <= count) { @@ -91,9 +99,11 @@ public bool TryPop(int count) public bool TryPush(int count) { var players = Utilities.GetPlayers() + .Where(player => player.IsReal()) .Where(player => player.GetTeam() == CsTeam.CounterTerrorist) .Shuffle(Random.Shared) .ToList(); + _logger.LogInformation("[Queue] Push requested {@Count} out of {@GuardCount}", count, players.Count); for (int i = 0; i < Math.Min(count, players.Count); i++) @@ -176,7 +186,7 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn ev, GameEventInfo info) var player = ev.Userid; if (!player.IsReal()) return HookResult.Continue; - + var state = _state.Get(ev.Userid); if (player.GetTeam() == CsTeam.CounterTerrorist && !state.IsGuard) diff --git a/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs b/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs index 981f483a..36e3997c 100644 --- a/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs +++ b/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs @@ -3,6 +3,7 @@ using Jailbreak.Formatting.Base; using Jailbreak.Formatting.Core; +using Jailbreak.Public.Extensions; namespace Jailbreak.Formatting.Extensions; @@ -22,40 +23,52 @@ public static FormatWriter ToWriter(this IView view) public static IView ToPlayerConsole(this IView view, CCSPlayerController player) { - var writer = view.ToWriter(); + if (player.IsReal()) + { + var writer = view.ToWriter(); - foreach (string writerLine in writer.Plain) - player.PrintToConsole(writerLine); + foreach (string writerLine in writer.Plain) + player.PrintToConsole(writerLine); + } return view; } public static IView ToPlayerChat(this IView view, CCSPlayerController player) { - var writer = view.ToWriter(); + if (player.IsReal()) + { + var writer = view.ToWriter(); - foreach (string writerLine in writer.Chat) - player.PrintToChat(writerLine); + foreach (string writerLine in writer.Chat) + player.PrintToChat(writerLine); + } return view; } public static IView ToPlayerCenter(this IView view, CCSPlayerController player) { - var writer = view.ToWriter(); - var merged = string.Join('\n', writer.Plain); + if (player.IsReal()) + { + var writer = view.ToWriter(); + var merged = string.Join('\n', writer.Plain); - player.PrintToCenter(merged); + player.PrintToCenter(merged); + } return view; } public static IView ToPlayerCenterHtml(this IView view, CCSPlayerController player) { - var writer = view.ToWriter(); - var merged = string.Join('\n', writer.Panorama); + if (player.IsReal()) + { + var writer = view.ToWriter(); + var merged = string.Join('\n', writer.Panorama); - player.PrintToCenterHtml(merged); + player.PrintToCenterHtml(merged); + } return view; } diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs index 5eeeb7cb..c5c25789 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs @@ -23,4 +23,16 @@ public HookResult OnDisconnect(EventPlayerDisconnect ev, GameEventInfo info) Reset(ev.Userid); return HookResult.Continue; } + + /// + /// Reset all global states when a new game starts + /// + /// + /// + /// + public HookResult OnGameEnd(EventGameEnd ev, GameEventInfo info) + { + ResetAll(); + return HookResult.Continue; + } }