mirror of
https://github.com/D1GQ/BetterAmongUs.git
synced 2026-10-20 01:46:19 +00:00
315 lines
13 KiB
C#
315 lines
13 KiB
C#
using AmongUs.GameOptions;
|
|
using BetterAmongUs.Data;
|
|
using BetterAmongUs.Helpers;
|
|
using BetterAmongUs.Modules;
|
|
using BetterAmongUs.Mono;
|
|
using HarmonyLib;
|
|
using System.Text;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace BetterAmongUs.Patches.Gameplay.UI.Chat;
|
|
|
|
internal static class ChatPatch
|
|
{
|
|
internal static List<string> ChatHistory = [];
|
|
internal static int CurrentHistorySelection = -1;
|
|
|
|
internal const string CommandPostfixName = "<size=0%>IsCommand</size>";
|
|
|
|
internal static void ClearChat()
|
|
{
|
|
if (!HudManager.InstanceExists) return;
|
|
|
|
HudManager.Instance.Chat.chatBubblePool.ReclaimAll();
|
|
}
|
|
|
|
internal static void ClearPlayerChats()
|
|
{
|
|
if (!HudManager.InstanceExists) return;
|
|
|
|
foreach (var obj in HudManager.Instance.Chat.chatBubblePool.activeChildren.ToArray())
|
|
{
|
|
var chatBubble = obj.GetComponent<ChatBubble>();
|
|
if (chatBubble != null)
|
|
{
|
|
if (chatBubble.NameText.text.EndsWith(CommandPostfixName)) continue;
|
|
HudManager.Instance.Chat.chatBubblePool.Reclaim(chatBubble);
|
|
}
|
|
}
|
|
HudManager.Instance.Chat.AlignAllBubbles();
|
|
}
|
|
|
|
internal static void ClearCommands()
|
|
{
|
|
if (!HudManager.InstanceExists) return;
|
|
|
|
foreach (var obj in HudManager.Instance.Chat.chatBubblePool.activeChildren.ToArray())
|
|
{
|
|
var chatBubble = obj.GetComponent<ChatBubble>();
|
|
if (chatBubble != null)
|
|
{
|
|
if (!chatBubble.NameText.text.EndsWith(CommandPostfixName)) continue;
|
|
HudManager.Instance.Chat.chatBubblePool.Reclaim(chatBubble);
|
|
}
|
|
}
|
|
HudManager.Instance.Chat.AlignAllBubbles();
|
|
}
|
|
|
|
[HarmonyPatch(typeof(ChatController))]
|
|
internal class ChatControllerPatch
|
|
{
|
|
[HarmonyPatch(nameof(ChatController.Toggle))]
|
|
[HarmonyPostfix]
|
|
private static void Toggle_Postfix(/*ChatController __instance*/)
|
|
{
|
|
SetChatTheme();
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.Update))]
|
|
[HarmonyPrefix]
|
|
[HarmonyPriority(Priority.First)]
|
|
private static void Update_Prefix(ChatController __instance)
|
|
{
|
|
if (BAUPlugin.ChatDarkMode.Value)
|
|
{
|
|
// Free chat color
|
|
__instance.freeChatField.background.color = new Color32(40, 40, 40, byte.MaxValue);
|
|
__instance.freeChatField.textArea.compoText.Color(Color.white);
|
|
__instance.freeChatField.textArea.outputText.color = Color.white;
|
|
}
|
|
else
|
|
{
|
|
// Free chat color
|
|
__instance.freeChatField.background.color = new Color32(255, 255, 255, byte.MaxValue);
|
|
__instance.freeChatField.textArea.compoText.Color(Color.black);
|
|
__instance.freeChatField.textArea.outputText.color = Color.black;
|
|
}
|
|
|
|
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.X))
|
|
{
|
|
ClipboardHelper.PutClipboardString(__instance.freeChatField.textArea.text);
|
|
__instance.freeChatField.textArea.SetText("");
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.UpArrow) && ChatHistory.Any())
|
|
{
|
|
CurrentHistorySelection = Mathf.Clamp(--CurrentHistorySelection, 0, ChatHistory.Count - 1);
|
|
__instance.freeChatField.textArea.SetText(ChatHistory[CurrentHistorySelection]);
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.DownArrow) && ChatHistory.Any())
|
|
{
|
|
CurrentHistorySelection++;
|
|
if (CurrentHistorySelection < ChatHistory.Count)
|
|
__instance.freeChatField.textArea.SetText(ChatHistory[CurrentHistorySelection]);
|
|
else __instance.freeChatField.textArea.SetText("");
|
|
}
|
|
}
|
|
|
|
// Add extra information to chat bubble
|
|
[HarmonyPatch(nameof(ChatController.AddChat))]
|
|
[HarmonyPostfix]
|
|
private static void AddChat_Postfix(ChatController __instance, PlayerControl sourcePlayer, string chatText)
|
|
{
|
|
if (sourcePlayer.IsAlive() || !PlayerControl.LocalPlayer.IsAlive())
|
|
{
|
|
Logger.Log($"{sourcePlayer.Data.PlayerName} -> {chatText}", "ChatLog");
|
|
}
|
|
else
|
|
{
|
|
Logger.LogPrivate($"{sourcePlayer.Data.PlayerName} -> {chatText}", "ChatLog");
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.SetChatBubbleName))]
|
|
[HarmonyPostfix]
|
|
private static void SetChatBubbleName_Postfix(ChatController __instance, ChatBubble bubble, NetworkedPlayerInfo playerInfo, bool isDead, bool didVote)
|
|
{
|
|
if (didVote) return;
|
|
|
|
StringBuilder sbTag = new();
|
|
StringBuilder sbInfo = new();
|
|
|
|
var sourcePlayer = playerInfo.Object;
|
|
string hashPuid = Utils.GetHashPuid(sourcePlayer);
|
|
string friendCode = playerInfo.FriendCode;
|
|
string playerName = playerInfo.BetterData()?.RealName ?? "???";
|
|
string Role = $"<size=75%><color={sourcePlayer.GetTeamHexColor()}>{sourcePlayer.GetRoleName()}</color></size>+++";
|
|
|
|
if (GameState.IsLobby && !GameState.IsFreePlay)
|
|
{
|
|
Role = "";
|
|
|
|
if (sourcePlayer.IsDev())
|
|
sbTag.Append($"<color=#0088ff>Dev</color>+++");
|
|
|
|
if (sourcePlayer.IsLocalPlayer() || sourcePlayer.BetterData().IsBetterUser)
|
|
sbTag.AppendFormat("<color=#0dff00>{1}{0}</color>+++", Translator.GetString("Player.BetterUser"), sourcePlayer.BetterData().IsVerifiedBetterUser || sourcePlayer.IsLocalPlayer() ? "✓ " : "");
|
|
|
|
if (BetterDataManager.BetterDataFile.SickoData.Any(info => info.CheckPlayerData(sourcePlayer.Data)))
|
|
sbTag.Append($"<color=#00f583>{Translator.GetString("Player.SickoUser")}</color>+++");
|
|
else if (BetterDataManager.BetterDataFile.AUMData.Any(info => info.CheckPlayerData(sourcePlayer.Data)))
|
|
sbTag.Append($"<color=#4f0000>{Translator.GetString("Player.AUMUser")}</color>+++");
|
|
else if (BetterDataManager.BetterDataFile.KNData.Any(info => info.CheckPlayerData(sourcePlayer.Data)))
|
|
sbTag.Append($"<color=#8731e7>{Translator.GetString("Player.KNUser")}</color>+++");
|
|
else if (BetterDataManager.BetterDataFile.CheatData.Any(info => info.CheckPlayerData(sourcePlayer.Data)))
|
|
sbTag.Append($"<color=#fc0000>{Translator.GetString("Player.KnownCheater")}</color>+++");
|
|
}
|
|
|
|
if (!sourcePlayer.IsImpostorTeammate())
|
|
{
|
|
if (PlayerControl.LocalPlayer.IsAlive() && !sourcePlayer.IsLocalPlayer())
|
|
{
|
|
Role = "";
|
|
}
|
|
}
|
|
|
|
if (PlayerControl.LocalPlayer.Is(RoleTypes.GuardianAngel) && !sourcePlayer.IsAlive() || !PlayerControl.LocalPlayer.Is(RoleTypes.GuardianAngel))
|
|
{
|
|
sbTag.Append(Role);
|
|
}
|
|
|
|
sbInfo.Append("<size=75%>");
|
|
for (int i = 0; i < sbTag.ToString().Split("+++").Length; i++)
|
|
{
|
|
if (!string.IsNullOrEmpty(sbTag.ToString().Split("+++")[i]))
|
|
{
|
|
if (i < sbTag.ToString().Split("+++").Length)
|
|
{
|
|
sbInfo.Append(sbTag.ToString().Split("+++")[i]);
|
|
}
|
|
if (i != sbTag.ToString().Split("+++").Length - 2)
|
|
{
|
|
sbInfo.Append(" - ");
|
|
}
|
|
}
|
|
}
|
|
sbInfo.Append("</size>");
|
|
|
|
bool flag = sourcePlayer.IsLocalPlayer();
|
|
if (flag)
|
|
{
|
|
playerName = $"{sbInfo} " + playerName;
|
|
}
|
|
else
|
|
{
|
|
playerName += $" {sbInfo}";
|
|
}
|
|
|
|
bubble.NameText.SetText(playerName);
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.GetPooledBubble))]
|
|
[HarmonyPostfix]
|
|
private static void GetPooledBubble_Postfix(ChatController __instance, ChatBubble __result)
|
|
{
|
|
SetChatPoolTheme(__result);
|
|
}
|
|
|
|
internal static void SetChatTheme()
|
|
{
|
|
var chat = HudManager.Instance.Chat;
|
|
|
|
if (BAUPlugin.ChatDarkMode.Value)
|
|
{
|
|
// Quick chat color
|
|
chat.quickChatField.background.color = new Color32(40, 40, 40, byte.MaxValue);
|
|
chat.quickChatField.text.color = Color.white;
|
|
|
|
// Icons
|
|
chat.quickChatButton.transform.Find("QuickChatIcon").GetComponent<SpriteRenderer>().color = new Color(0.5f, 0.5f, 0.5f, 1f);
|
|
chat.openKeyboardButton.transform.Find("OpenKeyboardIcon").GetComponent<SpriteRenderer>().color = new Color(0.5f, 0.5f, 0.5f, 1f);
|
|
}
|
|
else
|
|
{
|
|
// Quick chat color
|
|
chat.quickChatField.background.color = new Color32(255, 255, 255, byte.MaxValue);
|
|
chat.quickChatField.text.color = Color.black;
|
|
|
|
// Icons
|
|
chat.quickChatButton.transform.Find("QuickChatIcon").GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
|
|
chat.openKeyboardButton.transform.Find("OpenKeyboardIcon").GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
|
|
}
|
|
|
|
foreach (var item in HudManager.Instance.Chat.chatBubblePool.activeChildren.SelectIl2Cpp(c => c.GetComponent<ChatBubble>()))
|
|
{
|
|
SetChatPoolTheme(item);
|
|
}
|
|
}
|
|
|
|
// Set chat theme
|
|
internal static ChatBubble SetChatPoolTheme(ChatBubble asChatBubble)
|
|
{
|
|
ChatBubble chatBubble = asChatBubble;
|
|
|
|
if (BAUPlugin.ChatDarkMode.Value)
|
|
{
|
|
chatBubble.transform.Find("ChatText (TMP)").GetComponentInChildren<TextMeshPro>(true).color = new Color(1f, 1f, 1f, 1f);
|
|
chatBubble.transform.Find("Background").GetComponentInChildren<SpriteRenderer>(true).color = new Color(0.15f, 0.15f, 0.15f, 1f);
|
|
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark") != null)
|
|
{
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark").GetComponentInChildren<SpriteRenderer>(true).enabled == true)
|
|
{
|
|
chatBubble.transform.Find("Background").GetComponentInChildren<SpriteRenderer>(true).color = new Color(0.15f, 0.15f, 0.15f, 0.5f);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
chatBubble.transform.Find("ChatText (TMP)").GetComponentInChildren<TextMeshPro>(true).color = new Color(0f, 0f, 0f, 1f);
|
|
chatBubble.transform.Find("Background").GetComponentInChildren<SpriteRenderer>(true).color = new Color(1f, 1f, 1f, 1f);
|
|
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark") != null)
|
|
{
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark").GetComponentInChildren<SpriteRenderer>(true).enabled == true)
|
|
{
|
|
chatBubble.transform.Find("Background").GetComponentInChildren<SpriteRenderer>(true).color = new Color(1f, 1f, 1f, 0.5f);
|
|
}
|
|
}
|
|
}
|
|
|
|
return chatBubble;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(FreeChatInputField))]
|
|
internal static class FreeChatInputFieldPatch
|
|
{
|
|
[HarmonyPatch(nameof(FreeChatInputField.Awake))]
|
|
[HarmonyPostfix]
|
|
private static void Awake_Postfix(FreeChatInputField __instance)
|
|
{
|
|
__instance.textArea.allowAllCharacters = true;
|
|
__instance.textArea.AllowSymbols = true;
|
|
__instance.textArea.AllowPaste = true;
|
|
__instance.textArea.AllowEmail = true;
|
|
__instance.textArea.characterLimit = 118;
|
|
__instance.charCountText.text = "0/118";
|
|
}
|
|
|
|
[HarmonyPatch(nameof(FreeChatInputField.UpdateCharCount))]
|
|
[HarmonyPostfix]
|
|
private static void UpdateCharCount_Postfix(FreeChatInputField __instance)
|
|
{
|
|
int length = __instance.textArea.text.Length;
|
|
__instance.charCountText.text = string.Format("{0}/118", length);
|
|
__instance.charCountText.color = GetCharColor(length, Color.white);
|
|
}
|
|
}
|
|
|
|
private static Color GetCharColor(int length, Color color)
|
|
{
|
|
Color[] colorGradient =
|
|
[
|
|
Color.green,
|
|
Color.yellow,
|
|
Color.red
|
|
];
|
|
|
|
(float min, float max) lerpRange = (0f, 117f);
|
|
return colorGradient.LerpColor(lerpRange, length);
|
|
}
|
|
}
|