mirror of
https://github.com/D1GQ/BetterAmongUs.git
synced 2026-10-20 01:46:19 +00:00
329 lines
13 KiB
C#
329 lines
13 KiB
C#
using AmongUs.GameOptions;
|
|
using BetterAmongUs.Data;
|
|
using BetterAmongUs.Helpers;
|
|
using BetterAmongUs.Modules;
|
|
using HarmonyLib;
|
|
using System.Text;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace BetterAmongUs.Patches;
|
|
|
|
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]
|
|
internal static void Toggle_Postfix(/*ChatController __instance*/)
|
|
{
|
|
SetChatTheme();
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.Update))]
|
|
[HarmonyPrefix]
|
|
[HarmonyPriority(Priority.First)]
|
|
internal static void Update_Prefix(ChatController __instance)
|
|
{
|
|
if (Main.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]
|
|
internal static void AddChat_Postfix(ChatController __instance, [HarmonyArgument(0)] PlayerControl sourcePlayer, [HarmonyArgument(1)] string chatText)
|
|
{
|
|
ChatBubble? chatBubble = SetChatPoolTheme();
|
|
if (chatBubble == null) return;
|
|
|
|
StringBuilder sbTag = new StringBuilder();
|
|
StringBuilder sbInfo = new StringBuilder();
|
|
|
|
string hashPuid = Utils.GetHashPuid(sourcePlayer);
|
|
string friendCode = sourcePlayer.Data.FriendCode;
|
|
string playerName = sourcePlayer.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}";
|
|
}
|
|
|
|
chatBubble.NameText.text = playerName;
|
|
if (sourcePlayer.IsAlive() || !PlayerControl.LocalPlayer.IsAlive())
|
|
{
|
|
Logger.Log($"{sourcePlayer.Data.PlayerName} -> {chatText}", "ChatLog");
|
|
}
|
|
else
|
|
{
|
|
Logger.LogPrivate($"{sourcePlayer.Data.PlayerName} -> {chatText}", "ChatLog");
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.AddChatNote))]
|
|
[HarmonyPostfix]
|
|
internal static void AddChatNote_Postfix(ChatController __instance)
|
|
{
|
|
SetChatPoolTheme();
|
|
}
|
|
|
|
[HarmonyPatch(nameof(ChatController.AddChatWarning))]
|
|
[HarmonyPostfix]
|
|
internal static void AddChatWarning_Postfix(ChatController __instance)
|
|
{
|
|
SetChatPoolTheme();
|
|
}
|
|
|
|
internal static void SetChatTheme()
|
|
{
|
|
var chat = HudManager.Instance.Chat;
|
|
|
|
if (Main.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.ToArray().Select(c => c.GetComponent<ChatBubble>()))
|
|
{
|
|
SetChatPoolTheme(item);
|
|
}
|
|
}
|
|
|
|
// Set chat theme
|
|
internal static ChatBubble? SetChatPoolTheme(ChatBubble? asChatBubble = null)
|
|
{
|
|
ChatBubble Get() => HudManager.Instance.Chat.chatBubblePool.activeChildren.ToArray()
|
|
.Select(c => c.GetComponent<ChatBubble>())
|
|
.Last();
|
|
|
|
ChatBubble chatBubble = asChatBubble ??= Get();
|
|
|
|
if (chatBubble == null) return chatBubble;
|
|
|
|
if (Main.ChatDarkMode.Value)
|
|
{
|
|
chatBubble.transform.Find("ChatText (TMP)").GetComponent<TextMeshPro>().color = new Color(1f, 1f, 1f, 1f);
|
|
chatBubble.transform.Find("Background").GetComponent<SpriteRenderer>().color = new Color(0.15f, 0.15f, 0.15f, 1f);
|
|
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark") != null)
|
|
{
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark").GetComponent<SpriteRenderer>().enabled == true)
|
|
{
|
|
chatBubble.transform.Find("Background").GetComponent<SpriteRenderer>().color = new Color(0.15f, 0.15f, 0.15f, 0.5f);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
chatBubble.transform.Find("ChatText (TMP)").GetComponent<TextMeshPro>().color = new Color(0f, 0f, 0f, 1f);
|
|
chatBubble.transform.Find("Background").GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 1f);
|
|
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark") != null)
|
|
{
|
|
if (chatBubble.transform.Find("PoolablePlayer/xMark").GetComponent<SpriteRenderer>().enabled == true)
|
|
{
|
|
chatBubble.transform.Find("Background").GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.5f);
|
|
}
|
|
}
|
|
}
|
|
|
|
return chatBubble;
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(FreeChatInputField))]
|
|
class FreeChatInputFieldPatch
|
|
{
|
|
[HarmonyPatch(nameof(FreeChatInputField.Awake))]
|
|
[HarmonyPostfix]
|
|
internal 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]
|
|
internal 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)
|
|
{
|
|
|
|
switch (length)
|
|
{
|
|
case int n when n > 117:
|
|
if (ColorUtility.TryParseHtmlString("#ff0000", out Color newColor1))
|
|
color = newColor1;
|
|
break;
|
|
case int n when n > 74:
|
|
if (ColorUtility.TryParseHtmlString("#ffff00", out Color newColor3))
|
|
color = newColor3;
|
|
break;
|
|
default:
|
|
if (ColorUtility.TryParseHtmlString("#00f04c", out Color newColor4))
|
|
color = newColor4;
|
|
break;
|
|
}
|
|
|
|
return color;
|
|
}
|
|
}
|