Fix memory leak and rework zoom

This commit is contained in:
D1GQ committed 2025-12-11 01:24:55 -06:00
1 parent 218f9be485
commit e2ab725fd8
6 files changed
+117 -112

No files matched your search

+3 -1
View File
@@ -111,7 +111,9 @@ internal class NetworkManager
MessageReader messageReader = parentReader.ReadMessageAsNewBuffer();
int currentMessageNumber = InnerNetClient.msgNum++;
InnerNetClient.StartCoroutine(HandleGameDataInner(messageReader, currentMessageNumber));
RPCHandler.HandleRPC(parentReader.Tag, null, MessageReader.Get(parentReader), HandlerFlag.HandleGameDataTag);
var oldReader = MessageReader.Get(messageReader);
RPCHandler.HandleRPC(parentReader.Tag, null, oldReader, HandlerFlag.HandleGameDataTag);
oldReader.Recycle();
}
}
finally
+24 -10
View File
@@ -113,23 +113,23 @@ class BetterAntiCheat
// Handle RPC before anti cheat detection
internal static void HandleCheatRPCBeforeCheck(PlayerControl player, byte callId, MessageReader oldReader)
{
MessageReader reader = MessageReader.Get(oldReader);
if (!IsEnabled) return;
MessageReader reader = MessageReader.Get(oldReader);
RPCHandler.HandleRPC(callId, player, reader, HandlerFlag.CheatRpcCheck);
reader.Recycle();
}
// Check and notify for invalid rpcs
internal static void CheckRPC(PlayerControl player, byte callId, MessageReader oldReader)
{
MessageReader reader = MessageReader.Get(oldReader);
if (player == null || player?.Data == null || reader == null) return;
if (player == null || player?.Data == null) return;
if (!IsEnabled || !BAUPlugin.AntiCheat.Value || (GameState.IsBetterHostLobby && !GameState.IsHost) || !BetterGameSettings.DetectInvalidRPCs.GetBool()) return;
if (player.IsLocalPlayer() && player.IsHost()) return;
MessageReader reader = MessageReader.Get(oldReader);
RPCHandler.HandleRPC(callId, player, reader, HandlerFlag.AntiCheat);
reader.Recycle();
}
// Check notify and cancel out request for invalid rpcs
@@ -137,20 +137,22 @@ class BetterAntiCheat
{
try
{
MessageReader reader = MessageReader.Get(oldReader);
if (player == null || player?.Data == null || reader == null) return true;
if (player == null || player?.Data == null) return true;
if (!IsEnabled || !BAUPlugin.AntiCheat.Value || (GameState.IsBetterHostLobby && !GameState.IsHost) || !BetterGameSettings.DetectInvalidRPCs.GetBool()) return true;
if (player.IsLocalPlayer() && player.IsHost()) return true;
MessageReader reader = MessageReader.Get(oldReader);
if (TrustedRPCs(callId) != true && !player.IsHost())
{
BetterNotificationManager.NotifyCheat(player, $"Unregistered RPC received: {callId}");
reader.Recycle();
return false;
}
if (RPCHandler.HandleRPC(callId, player, reader, HandlerFlag.AntiCheatCancel) == false)
{
reader.Recycle();
return false;
}
@@ -164,6 +166,8 @@ class BetterAntiCheat
{
Logger.LogCheat($"{player.BetterData().RealName} {Enum.GetName((RpcCalls)callId)}: {!player.IsHost()}");
}
reader.Recycle();
return false;
}
}
@@ -181,6 +185,8 @@ class BetterAntiCheat
{
Logger.LogCheat($"{player.BetterData().RealName} {Enum.GetName((RpcCalls)callId)}: {GameState.IsInGamePlay}");
}
reader.Recycle();
return false;
}
}
@@ -219,10 +225,14 @@ class BetterAntiCheat
{
Logger.LogCheat($"{player.BetterData().RealName} {Enum.GetName((RpcCalls)callId)}: {GameState.IsInGame} && {GameState.IsLobby}");
}
reader.Recycle();
return false;
}
}
reader.Recycle();
return true;
}
catch (Exception ex)
@@ -238,8 +248,8 @@ class BetterAntiCheat
if (player == null || player?.Data == null || player.IsLocalPlayer()) return;
MessageReader reader = MessageReader.Get(oldReader);
RPCHandler.HandleRPC(callId, player, reader, HandlerFlag.Handle);
reader.Recycle();
}
// Check game states when sabotaging
@@ -256,8 +266,12 @@ class BetterAntiCheat
bool notCanceled = RPCHandler.HandleRPC((byte)RpcCalls.UpdateSystem, player, reader, HandlerFlag.AntiCheatCancel);
if (!notCanceled)
{
Logger.LogCheat($"RPC canceled by Anti-Cheat: {Enum.GetName(typeof(SystemTypes), (int)systemType)} - {MessageReader.Get(reader).ReadByte()}");
var tempReader = MessageReader.Get(reader);
Logger.LogCheat($"RPC canceled by Anti-Cheat: {Enum.GetName(typeof(SystemTypes), (int)systemType)} - {tempReader.ReadByte()}");
tempReader.Recycle();
}
reader.Recycle();
return notCanceled;
}
+6 -6
View File
@@ -48,11 +48,11 @@ internal abstract class RPCHandler
{
try
{
if (handlerFlag == HandlerFlag.Handle) handler.Handle(sender, MessageReader.Get(reader));
else if (handlerFlag == HandlerFlag.AntiCheatCancel) cancel = !handler.HandleAntiCheatCancel(sender, MessageReader.Get(reader));
else if (handlerFlag == HandlerFlag.AntiCheat) handler.HandleAntiCheat(sender, MessageReader.Get(reader));
else if (handlerFlag == HandlerFlag.CheatRpcCheck) handler.HandleCheatRpcCheck(sender, MessageReader.Get(reader));
else if (handlerFlag == HandlerFlag.BetterHost) cancel = !handler.BetterHandle(sender, MessageReader.Get(reader));
if (handlerFlag == HandlerFlag.Handle) handler.Handle(sender, reader);
else if (handlerFlag == HandlerFlag.AntiCheatCancel) cancel = !handler.HandleAntiCheatCancel(sender, reader);
else if (handlerFlag == HandlerFlag.AntiCheat) handler.HandleAntiCheat(sender, reader);
else if (handlerFlag == HandlerFlag.CheatRpcCheck) handler.HandleCheatRpcCheck(sender, reader);
else if (handlerFlag == HandlerFlag.BetterHost) cancel = !handler.BetterHandle(sender, reader);
if (!(cancel)) break;
}
catch
@@ -63,7 +63,7 @@ internal abstract class RPCHandler
{
try
{
handler.HandleGameData(MessageReader.Get(reader));
handler.HandleGameData(reader);
}
catch (Exception ex)
{
@@ -62,10 +62,13 @@ internal sealed class UpdateSystemHandler : RPCHandler
if (systemHandlers.TryGetValue(systemKey, out var handler))
{
oldReader.Recycle();
return handler.Invoke(sender, system, oldReader, count);
}
}
oldReader.Recycle();
return true;
}
-95
View File
@@ -1,95 +0,0 @@
using AmongUs.GameOptions;
using BetterAmongUs.Helpers;
using HarmonyLib;
using UnityEngine;
namespace BetterAmongUs.Modules;
[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))]
internal static class Zoom
{
private static bool resetButtons = false;
internal static void Postfix()
{
if (GameState.IsCanMove && !PlayerControl.LocalPlayer.Is(RoleTypes.GuardianAngel) && (!GameState.IsInGamePlay || !PlayerControl.LocalPlayer.IsAlive()))
{
if (Camera.main.orthographicSize > 3.0f)
resetButtons = true;
if (Input.mouseScrollDelta.y > 0 && Camera.main.orthographicSize > 3.0f)
{
SetZoomSize(times: false);
}
else if (Input.mouseScrollDelta.y < 0 && (GameState.IsDead || GameState.IsFreePlay || GameState.IsLobby) &&
Camera.main.orthographicSize < 18.0f)
{
SetZoomSize(times: true);
}
Flag.NewFlag("Zoom");
}
else
{
Flag.Run(() => SetZoomSize(reset: true), "Zoom");
}
}
private static void SetZoomSize(bool times = false, bool reset = false)
{
float size = times ? 1.5f : 1 / 1.5f;
if (reset)
{
Camera.main.orthographicSize = 3.0f;
HudManager.Instance.UICamera.orthographicSize = 3.0f;
HudManager.Instance.Chat.transform.localScale = Vector3.one;
if (GameState.IsMeeting)
MeetingHud.Instance.transform.localScale = Vector3.one;
}
else
{
Camera.main.orthographicSize *= size;
HudManager.Instance.UICamera.orthographicSize *= size;
}
HudManager.Instance?.ShadowQuad?.gameObject?.SetActive((reset || Camera.main.orthographicSize == 3.0f) && PlayerControl.LocalPlayer.IsAlive());
if (resetButtons)
{
ResolutionManager.ResolutionChanged.Invoke((float)Screen.width / Screen.height, Screen.width, Screen.height, Screen.fullScreen);
resetButtons = false;
}
}
internal static void OnFixedUpdate() => HudManager.Instance?.ShadowQuad?.gameObject?.SetActive(Camera.main.orthographicSize == 3.0f && PlayerControl.LocalPlayer.IsAlive());
}
internal static class Flag
{
private static readonly List<string> oneTimeList = [];
private static readonly List<string> firstRunList = [];
internal static void Run(Action action, string type, bool firstrun = false)
{
if (oneTimeList.Contains(type) || firstrun && !firstRunList.Contains(type))
{
if (!firstRunList.Contains(type))
firstRunList.Add(type);
oneTimeList.Remove(type);
action();
}
}
internal static void NewFlag(string type)
{
if (!oneTimeList.Contains(type))
oneTimeList.Add(type);
}
internal static void DeleteFlag(string type)
{
oneTimeList.Remove(type);
}
}
+81
View File
@@ -0,0 +1,81 @@
using AmongUs.GameOptions;
using BetterAmongUs.Helpers;
using BetterAmongUs.Modules;
using HarmonyLib;
using UnityEngine;
namespace BetterAmongUs.Patches.Gameplay;
[HarmonyPatch]
internal class ZoomPatch
{
private static bool _wasZooming = false;
private static bool _shouldResetButtons = false;
[HarmonyPatch(typeof(HudManager), nameof(HudManager.Update))]
[HarmonyPostfix]
private static void Update_Postfix()
{
bool canZoom = GameState.IsCanMove &&
!PlayerControl.LocalPlayer.Is(RoleTypes.GuardianAngel) &&
(!GameState.IsInGamePlay || !PlayerControl.LocalPlayer.IsAlive());
bool shouldReset = !canZoom && _wasZooming;
if (shouldReset)
{
SetZoomSize(reset: true);
_wasZooming = false;
}
else if (canZoom)
{
_wasZooming = true;
if (Camera.main.orthographicSize > 3.0f)
_shouldResetButtons = true;
if (Input.mouseScrollDelta.y > 0 && Camera.main.orthographicSize > 3.0f)
{
SetZoomSize(zoomIn: true);
}
else if (Input.mouseScrollDelta.y < 0 &&
(GameState.IsDead || GameState.IsFreePlay || GameState.IsLobby) &&
Camera.main.orthographicSize < 18.0f)
{
SetZoomSize(zoomOut: true);
}
}
}
private static void SetZoomSize(bool zoomIn = false, bool zoomOut = false, bool reset = false)
{
if (reset)
{
Camera.main.orthographicSize = 3.0f;
HudManager.Instance.UICamera.orthographicSize = 3.0f;
HudManager.Instance.Chat.transform.localScale = Vector3.one;
if (GameState.IsMeeting)
MeetingHud.Instance.transform.localScale = Vector3.one;
}
else if (zoomIn || zoomOut)
{
float size = zoomIn ? 1 / 1.5f : 1.5f;
Camera.main.orthographicSize *= size;
HudManager.Instance.UICamera.orthographicSize *= size;
}
HudManager.Instance?.ShadowQuad?.gameObject?.SetActive(
(reset || Camera.main.orthographicSize == 3.0f) &&
PlayerControl.LocalPlayer.IsAlive());
if (_shouldResetButtons)
{
ResolutionManager.ResolutionChanged.Invoke(
(float)Screen.width / Screen.height,
Screen.width, Screen.height,
Screen.fullScreen);
_shouldResetButtons = false;
}
}
}