fix crash & more optimization

This commit is contained in:
Gurge44 committed 2025-06-14 14:04:36 +02:00
1 parent 4718a06780
commit c93071e290
13 files changed
+89 -48

No files matched your search

+1 -1
View File
@@ -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();
+2 -2
View File
@@ -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);
+12 -1
View File
@@ -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;
}
+26 -3
View File
@@ -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<HudManager>.Instance;
if (hudManager)
{
HudManagerPatch.Postfix(hudManager);
Zoom.Postfix();
HudSpritePatch.Postfix(hudManager);
}
if (!PlayerControl.LocalPlayer) return;
bool lobby = GameStates.IsLobby;
+1 -1
View File
@@ -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)
+3 -2
View File
@@ -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);
}
+1 -2
View File
@@ -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;
+20 -10
View File
@@ -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<float> LastFPS = [];
private static int Delay => GameStates.IsInTask ? 8 : 1;
public static readonly List<float> 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 ? " - " : " - ");
}
+1 -1
View File
@@ -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;
+18 -20
View File
@@ -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)
{
+2 -3
View File
@@ -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;
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -1868,7 +1868,7 @@
"KickCommandKickedRole": "Their role was",
"ModeratorTag": "<#8000ff>Moderator</color>",
"VIPTag": "<#ffff00>VIP</color>",
"EnableAFKDetector": "Enable AFK Detector",
"EnableAFKDetector": "Enable AFK Detector (Also Used To Detect And Fix Black Screens)",
"AFKWarning": "<#ffff00><size=90%>Please move to avoid being marked as AFK</color> <#ffffff>({0})</size></color>",
"AFKSuffix": "<#ffa500>(AFK)</color>",
"AFKConsequence": "Consequence for AFK players",