Files
BetterAmongUs/Patches/PrivateLobbyPatch.cs
T
2025-03-25 20:34:50 -05:00

110 lines
3.6 KiB
C#

using BetterAmongUs.Helpers;
using BetterAmongUs.Modules;
using HarmonyLib;
using TMPro;
using UnityEngine;
namespace BetterAmongUs.Patches;
[HarmonyPatch]
internal class PrivateLobbyPatch
{
private static GameObject? toggle;
private static List<PassiveButton>? buttons = [];
private static TextMeshPro? toggleText;
[HarmonyPatch(typeof(CreateGameOptions))]
[HarmonyPatch(nameof(CreateGameOptions.Show))]
[HarmonyPostfix]
internal static void CreateGameOptionsShow_Postfix(CreateGameOptions __instance)
{
if (toggle != null) return;
buttons.Clear();
toggle = UnityEngine.Object.Instantiate(__instance.AprilFoolsToggle, __instance.contentObjects.First().transform.parent);
if (toggle != null)
{
toggle.name = "PrivateOnlyLobby";
buttons.Add(toggle.transform.Find("AprilOn").GetComponent<PassiveButton>());
buttons.Add(toggle.transform.Find("AprilOff").GetComponent<PassiveButton>());
if (buttons.Count < 2) return;
toggle.gameObject.SetActive(true);
var onButton = buttons[0];
if (onButton != null)
{
onButton.gameObject.SetActive(false);
onButton.OnClick = new();
onButton.OnClick.AddListener((Action)(() => TogglePrivateOnlyLobby(true)));
}
var offButton = buttons[1];
if (offButton != null)
{
offButton.gameObject.SetActive(false);
offButton.OnClick = new();
offButton.OnClick.AddListener((Action)(() => TogglePrivateOnlyLobby(false)));
}
var aspect = toggle.gameObject.AddComponent<AspectPosition>();
aspect.Alignment = AspectPosition.EdgeAlignments.Top;
aspect.DistanceFromEdge = new UnityEngine.Vector3(0.4f, 1.62f, 0);
aspect.AdjustPosition();
var text = toggle.transform.Find("BlackSquare/ModeText")?.GetComponent<TextMeshPro>();
if (text != null)
{
text.DestroyTextTranslator();
text.text = "Private Only Lobby";
toggleText = text;
}
}
TogglePrivateOnlyLobby(Main.PrivateOnlyLobby.Value);
}
private static void TogglePrivateOnlyLobby(bool modeOn)
{
if (buttons.Count < 2) return;
buttons[0].gameObject.SetActive(true);
buttons[1].gameObject.SetActive(true);
buttons[0].SelectButton(false);
buttons[1].SelectButton(false);
if (modeOn)
{
buttons[0].SelectButton(true);
}
else
{
buttons[1].SelectButton(true);
}
Main.PrivateOnlyLobby.Value = modeOn;
}
[HarmonyPatch(typeof(LobbyInfoPane))]
[HarmonyPatch(nameof(LobbyInfoPane.Update))]
[HarmonyPostfix]
internal static void LobbyInfoPaneUpdate_Postfix(LobbyInfoPane __instance)
{
if (Main.PrivateOnlyLobby.Value && !GameState.IsLocalGame && GameState.IsHost)
{
if (AmongUsClient.Instance.IsGamePublic)
{
AmongUsClient.Instance.ChangeGamePublic(false);
}
var button = __instance.HostPrivateButton.GetComponent<PassiveButton>();
if (button != null)
{
button.DestroyMono();
var sprite = __instance.HostPrivateButton.transform.Find("Inactive").GetComponent<SpriteRenderer>();
if (sprite != null)
{
sprite.color = new(0.35f, 1, 1, 1);
}
}
}
}
}