From c93071e290a6abbb233e5114ae2c1651d49e8ceb Mon Sep 17 00:00:00 2001 From: Gurge44 Date: Sat, 14 Jun 2025 14:04:36 +0200 Subject: [PATCH] fix crash & more optimization --- Modules/AFKDetector.cs | 2 +- Modules/Debugger.cs | 4 ++-- Modules/ExtendedPlayerControl.cs | 13 ++++++++++- Modules/FixedUpdateCaller.cs | 29 +++++++++++++++++++++--- Modules/OptionShower.cs | 2 +- Modules/Utils.cs | 5 +++-- Modules/Zoom.cs | 3 +-- Patches/CredentialsPatch.cs | 30 ++++++++++++++++--------- Patches/ExilePatch.cs | 2 +- Patches/HudPatch.cs | 38 +++++++++++++++----------------- Patches/HudSpritePatch.cs | 5 ++--- Patches/LobbyPatch.cs | 2 +- Resources/Lang/en_US.json | 2 +- 13 files changed, 89 insertions(+), 48 deletions(-) diff --git a/Modules/AFKDetector.cs b/Modules/AFKDetector.cs index 1c268965e..0026473e3 100644 --- a/Modules/AFKDetector.cs +++ b/Modules/AFKDetector.cs @@ -44,7 +44,7 @@ public static class AFKDetector { if (!EnableDetector.GetBool() || !GameStates.IsInTask || pc == null || ExemptedPlayers.Contains(pc.PlayerId)) return; - float waitingTime = 15f; + var waitingTime = 10f; if (MeetingStates.FirstMeeting) waitingTime += 5f; if (!pc.IsAlive()) waitingTime += 5f; if (pc.Is(CustomRoles.Truant) && !MeetingStates.FirstMeeting) waitingTime += Options.TruantWaitingTime.GetFloat(); diff --git a/Modules/Debugger.cs b/Modules/Debugger.cs index 963665cbb..612706f8c 100644 --- a/Modules/Debugger.cs +++ b/Modules/Debugger.cs @@ -179,7 +179,7 @@ public class CustomLogger private CustomLogger() { if (!File.Exists(LOGFilePath)) File.WriteAllText(LOGFilePath, HtmlHeader); - else if (new FileInfo(LOGFilePath).Length > 4 * 1024 * 1024) // 4 MB + else if (Options.IsLoaded && new FileInfo(LOGFilePath).Length > 4 * 1024 * 1024) // 4 MB { ClearLog(false); PrivateInstance ??= new(); @@ -197,7 +197,7 @@ public class CustomLogger if (!check || (File.Exists(LOGFilePath) && new FileInfo(LOGFilePath).Length > 0)) { PrivateInstance?.Finish(); - Utils.DumpLog(false); + Utils.DumpLog(false, false); } File.WriteAllText(LOGFilePath, HtmlHeader); diff --git a/Modules/ExtendedPlayerControl.cs b/Modules/ExtendedPlayerControl.cs index 027ffe13a..8b3cead14 100644 --- a/Modules/ExtendedPlayerControl.cs +++ b/Modules/ExtendedPlayerControl.cs @@ -921,7 +921,16 @@ internal static class ExtendedPlayerControl public static string GetNameWithRole(this PlayerControl player, bool forUser = false) { - return $"{player?.Data?.PlayerName}" + (GameStates.IsInGame && Options.CurrentGameMode is not CustomGameMode.FFA and not CustomGameMode.MoveAndStop and not CustomGameMode.HotPotato and not CustomGameMode.Speedrun and not CustomGameMode.CaptureTheFlag and not CustomGameMode.NaturalDisasters and not CustomGameMode.RoomRush and not CustomGameMode.Quiz and not CustomGameMode.TheMindGame ? $" ({player?.GetAllRoleName(forUser).RemoveHtmlTags().Replace('\n', ' ')})" : string.Empty); + try + { + bool addRoleName = GameStates.IsInGame && Options.CurrentGameMode is not CustomGameMode.FFA and not CustomGameMode.MoveAndStop and not CustomGameMode.HotPotato and not CustomGameMode.Speedrun and not CustomGameMode.CaptureTheFlag and not CustomGameMode.NaturalDisasters and not CustomGameMode.RoomRush and not CustomGameMode.Quiz and not CustomGameMode.TheMindGame; + return $"{player?.Data?.PlayerName}" + (addRoleName ? $" ({player?.GetAllRoleName(forUser).RemoveHtmlTags().Replace('\n', ' ')})" : string.Empty); + } + catch (Exception e) + { + ThrowException(e); + return player == null || player.Data == null ? "Unknown Player" : player.Data.PlayerName; + } } public static string GetRoleColorCode(this PlayerControl player) @@ -1607,11 +1616,13 @@ internal static class ExtendedPlayerControl public static bool IsLocalPlayer(this PlayerControl pc) { + if (pc == null) return false; return pc.PlayerId == PlayerControl.LocalPlayer.PlayerId; } public static bool IsLocalPlayer(this NetworkedPlayerInfo npi) { + if (npi == null) return false; return npi.PlayerId == PlayerControl.LocalPlayer.PlayerId; } diff --git a/Modules/FixedUpdateCaller.cs b/Modules/FixedUpdateCaller.cs index 90b59c9cc..66b518831 100644 --- a/Modules/FixedUpdateCaller.cs +++ b/Modules/FixedUpdateCaller.cs @@ -1,6 +1,8 @@ using System; +using EHR.Patches; using HarmonyLib; using InnerNet; +using UnityEngine; namespace EHR.Modules; @@ -14,16 +16,37 @@ public static class FixedUpdateCaller { try { + PingTrackerUpdatePatch.LastFPS.Add(1.0f / Time.deltaTime); + if (PingTrackerUpdatePatch.LastFPS.Count > 10) PingTrackerUpdatePatch.LastFPS.RemoveAt(0); + InnerNetClientFixedUpdatePatch.Postfix(); - if (ShipStatus.Instance) + var shipStatus = ShipStatus.Instance; + + if (shipStatus) { ShipFixedUpdatePatch.Postfix(); - ShipStatusFixedUpdatePatch.Postfix(ShipStatus.Instance); + ShipStatusFixedUpdatePatch.Postfix(shipStatus); } - if (LobbyBehaviour.Instance) + var lobbyBehaviour = LobbyBehaviour.Instance; + + if (lobbyBehaviour) + { LobbyFixedUpdatePatch.Postfix(); + LobbyBehaviourUpdatePatch.Postfix(lobbyBehaviour); + } + + HudManager hudManager = FastDestroyableSingleton.Instance; + + if (hudManager) + { + HudManagerPatch.Postfix(hudManager); + Zoom.Postfix(); + HudSpritePatch.Postfix(hudManager); + } + + if (!PlayerControl.LocalPlayer) return; bool lobby = GameStates.IsLobby; diff --git a/Modules/OptionShower.cs b/Modules/OptionShower.cs index 230b7cab6..8f780394f 100644 --- a/Modules/OptionShower.cs +++ b/Modules/OptionShower.cs @@ -56,7 +56,7 @@ public static class OptionShower { if (kvp.Value.GameMode is CustomGameMode.Standard or CustomGameMode.All && kvp.Value.GetBool()) { - sb.Append($"{Utils.ColorString(Utils.GetRoleColor(kvp.Key), Utils.GetRoleName(kvp.Key))}: {kvp.Value.GetString()} ×{kvp.Key.GetCount()}\n"); + sb.Append($"{Utils.ColorString(Utils.GetRoleColor(kvp.Key), Utils.GetRoleName(kvp.Key))}: {kvp.Value.GetString()} x{kvp.Key.GetCount()}\n"); count++; if (count > MaxLinesPerPage) diff --git a/Modules/Utils.cs b/Modules/Utils.cs index bfb5cf33c..231572fda 100644 --- a/Modules/Utils.cs +++ b/Modules/Utils.cs @@ -3474,9 +3474,9 @@ public static class Utils return t.PadRight(Mathf.Max(num - (bc - t.Length), 0)); } - public static void DumpLog(bool open = true) + public static void DumpLog(bool open = true, bool finish = true) { - CustomLogger.Instance.Finish(); + if (finish) CustomLogger.Instance.Finish(); var t = DateTime.Now.ToString("yyyy-MM-dd_HH.mm.ss"); var f = $"{Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)}/EHR_Logs/{t}"; @@ -3707,6 +3707,7 @@ public static class Utils public static string RemoveHtmlTags(this string str) { + if (string.IsNullOrEmpty(str)) return string.Empty; return Regex.Replace(str, "<[^>]*?>", string.Empty); } diff --git a/Modules/Zoom.cs b/Modules/Zoom.cs index 0215347f2..4b64873ff 100644 --- a/Modules/Zoom.cs +++ b/Modules/Zoom.cs @@ -1,13 +1,12 @@ using System; using System.Collections.Generic; -using HarmonyLib; using UnityEngine; namespace EHR; // Credit: https://github.com/tugaru1975/TownOfPlus/TOPmods/Zoom.cs // Credit: https://github.com/Yumenopai/TownOfHost_Y -[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] +//[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] public static class Zoom { private static bool ResetButtons; diff --git a/Patches/CredentialsPatch.cs b/Patches/CredentialsPatch.cs index 24284ca50..171961db7 100644 --- a/Patches/CredentialsPatch.cs +++ b/Patches/CredentialsPatch.cs @@ -17,21 +17,31 @@ internal static class PingTrackerUpdatePatch public static PingTracker Instance; private static readonly StringBuilder Sb = new(); private static long LastUpdate; - private static readonly List LastFPS = []; - private static int Delay => GameStates.IsInTask ? 8 : 1; + public static readonly List LastFPS = []; - private static void Postfix(PingTracker __instance) + public static bool Prefix(PingTracker __instance) { - Instance = __instance; + PingTracker instance = Instance == null ? __instance : Instance; - Instance.text.alignment = TextAlignmentOptions.Center; - Instance.text.text = Sb.ToString(); + if (AmongUsClient.Instance == null) return false; - LastFPS.Add(1.0f / Time.deltaTime); - if (LastFPS.Count > 10) LastFPS.RemoveAt(0); + if (AmongUsClient.Instance.NetworkMode == NetworkModes.FreePlay) + { + instance.gameObject.SetActive(false); + return false; + } + + if (instance.name != "EHR_SettingsText") + { + instance.aspectPosition.DistanceFromEdge = !AmongUsClient.Instance.IsGameStarted ? instance.lobbyPos : instance.gamePos; + instance.text.alignment = TextAlignmentOptions.Center; + instance.text.text = Sb.ToString(); + } + + if (Instance == null) Instance = __instance; long now = Utils.TimeStamp; - if (now + Delay <= LastUpdate) return; // Only update every 2 seconds + if (now == LastUpdate) return false; LastUpdate = now; Sb.Clear(); @@ -72,7 +82,7 @@ internal static class PingTrackerUpdatePatch } if (GameStates.InGame) Sb.Append("\r\n."); - return; + return false; void AppendSeparator() => Sb.Append(GameStates.InGame ? " - " : " - "); } diff --git a/Patches/ExilePatch.cs b/Patches/ExilePatch.cs index e9f674988..0117220dd 100644 --- a/Patches/ExilePatch.cs +++ b/Patches/ExilePatch.cs @@ -122,7 +122,7 @@ internal static class ExileControllerWrapUpPatch { if (GameStates.IsEnded) return; AntiBlackout.RevertToActualRoleTypes(); - }, Math.Max(1f, Utils.CalculatePingDelay() * 2f), "Revert AntiBlackout Measures"); + }, Math.Max(1f, Utils.CalculatePingDelay()), "Revert AntiBlackout Measures"); } GameStates.AlreadyDied |= !Utils.IsAllAlive; diff --git a/Patches/HudPatch.cs b/Patches/HudPatch.cs index f1abd104e..51af6e7ca 100644 --- a/Patches/HudPatch.cs +++ b/Patches/HudPatch.cs @@ -14,7 +14,7 @@ using static EHR.Translator; namespace EHR.Patches; -[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] +//[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] internal static class HudManagerPatch { private static TextMeshPro LowerInfoText; @@ -32,18 +32,6 @@ internal static class HudManagerPatch LowerInfoText.text = string.Empty; } - public static bool Prefix(HudManager __instance) - { - if (PlayerControl.LocalPlayer != null) return true; - - __instance.taskDirtyTimer += Time.deltaTime; - if (__instance.taskDirtyTimer <= 0.25) return false; - - __instance.taskDirtyTimer = 0.0f; - __instance.TaskPanel?.SetTaskText(string.Empty); - return false; - } - public static void Postfix(HudManager __instance) { try @@ -69,15 +57,21 @@ internal static class HudManagerPatch if (GameStates.IsLobby) { - if (PingTrackerUpdatePatch.Instance != null) + if (PingTrackerUpdatePatch.Instance != null && SettingsText == null) { - if (SettingsText != null) Object.Destroy(SettingsText.gameObject); - - SettingsText = Object.Instantiate(PingTrackerUpdatePatch.Instance.text, __instance.transform, true); + SettingsText = Object.Instantiate(__instance.KillButton.cooldownTimerText, __instance.transform, true); + SettingsText.name = "EHR_SettingsText"; SettingsText.alignment = TextAlignmentOptions.TopLeft; SettingsText.verticalAlignment = VerticalAlignmentOptions.Top; - SettingsText.transform.position = AspectPosition.ComputeWorldPosition(Camera.main, AspectPosition.EdgeAlignments.LeftTop, new(0.38f, 0f, 0f)); - SettingsText.fontSize = SettingsText.fontSizeMin = SettingsText.fontSizeMax = 1.5f; + SettingsText.transform.position = AspectPosition.ComputeWorldPosition(Camera.main, AspectPosition.EdgeAlignments.LeftTop, new(0.38f, 0.3f, 0f)); + SettingsText.fontSize = SettingsText.fontSizeMin = SettingsText.fontSizeMax = 1.2f; + SettingsText.overflowMode = TextOverflowModes.Overflow; + SettingsText.enableWordWrapping = false; + } + else if (PingTrackerUpdatePatch.Instance == null && SettingsText != null) + { + Object.Destroy(SettingsText.gameObject); + SettingsText = null; } if (SettingsText != null) @@ -86,7 +80,11 @@ internal static class HudManagerPatch SettingsText.enabled = SettingsText.text != string.Empty; } } - else if (SettingsText != null) Object.Destroy(SettingsText.gameObject); + else if (SettingsText != null) + { + Object.Destroy(SettingsText.gameObject); + SettingsText = null; + } if (AmongUsClient.Instance.AmHost) { diff --git a/Patches/HudSpritePatch.cs b/Patches/HudSpritePatch.cs index 31da702d1..148ccd0b2 100644 --- a/Patches/HudSpritePatch.cs +++ b/Patches/HudSpritePatch.cs @@ -3,7 +3,6 @@ using EHR.Crewmate; using EHR.Impostor; using EHR.Neutral; using EHR.Patches; -using HarmonyLib; using UnityEngine; namespace EHR; @@ -16,8 +15,8 @@ public static class CustomButton } } -[HarmonyPriority(520)] -[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] +//[HarmonyPriority(520)] +//[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))] public static class HudSpritePatch { private static Sprite Kill; diff --git a/Patches/LobbyPatch.cs b/Patches/LobbyPatch.cs index c9e3df88b..a26e57623 100644 --- a/Patches/LobbyPatch.cs +++ b/Patches/LobbyPatch.cs @@ -74,7 +74,7 @@ public static class LobbyPatch } // https://github.com/SuperNewRoles/SuperNewRoles/blob/master/SuperNewRoles/Patches/LobbyBehaviourPatch.cs -[HarmonyPatch(typeof(LobbyBehaviour), nameof(LobbyBehaviour.Update))] +//[HarmonyPatch(typeof(LobbyBehaviour), nameof(LobbyBehaviour.Update))] internal static class LobbyBehaviourUpdatePatch { public static void Postfix(LobbyBehaviour __instance) diff --git a/Resources/Lang/en_US.json b/Resources/Lang/en_US.json index 8c2809959..ae0e8ee35 100644 --- a/Resources/Lang/en_US.json +++ b/Resources/Lang/en_US.json @@ -1868,7 +1868,7 @@ "KickCommandKickedRole": "Their role was", "ModeratorTag": "<#8000ff>Moderator", "VIPTag": "<#ffff00>VIP", - "EnableAFKDetector": "Enable AFK Detector", + "EnableAFKDetector": "Enable AFK Detector (Also Used To Detect And Fix Black Screens)", "AFKWarning": "<#ffff00>Please move to avoid being marked as AFK <#ffffff>({0})", "AFKSuffix": "<#ffa500>(AFK)", "AFKConsequence": "Consequence for AFK players",