Decompiled source of BetterTextChat v0.1.1

BetterTextChat.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using CellMenu;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
using Player;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("BetterTextChat")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BetterTextChat")]
[assembly: AssemblyTitle("BetterTextChat")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace BetterTextChat
{
	internal static class PlayerChatManager_Patches
	{
		private static string unwrappedCurrentValue = "";

		[HarmonyPatch(typeof(PlayerChatManager), "UpdateTextChatInput")]
		[HarmonyPrefix]
		private static void UpdateTextChatInput(PlayerChatManager __instance, ref bool __runOriginal)
		{
			__runOriginal = false;
			if (!PlayerChatManager.TextChatInputEnabled)
			{
				return;
			}
			if ((Object)(object)PlayerChatManager.Current != (Object)null && !PlayerChatManager.InChatMode && InputMapper.GetButtonDownKeyMouseGamepad((InputAction)56, (eFocusState)0))
			{
				PlayerChatManager.Log("Open chat window");
				__instance.EnterChatMode();
			}
			else
			{
				if (!((Object)(object)PlayerChatManager.Current != (Object)null) || !PlayerChatManager.InChatMode)
				{
					return;
				}
				if (InputMapper.GetButtonDownKeyMouseGamepad((InputAction)57, (eFocusState)0))
				{
					unwrappedCurrentValue = "";
					__instance.m_currentValue = "";
					__instance.m_currentImeCompositionString = "";
					__instance.UpdateMessage();
					__instance.ExitChatMode();
					return;
				}
				string inputString = Input.inputString;
				for (int i = 0; i < inputString.Length; i++)
				{
					char c = inputString[i];
					if (!ConfigManager.allow_forbidden_chars.Value && ((Il2CppArrayBase<int>)(object)__instance.m_forbiddenChars).Contains((int)c))
					{
						continue;
					}
					switch (c)
					{
					case '\b':
						if (unwrappedCurrentValue.Length > 0)
						{
							unwrappedCurrentValue = unwrappedCurrentValue.Substring(0, unwrappedCurrentValue.Length - 1);
						}
						break;
					default:
						if (c != '\r')
						{
							unwrappedCurrentValue += c;
							break;
						}
						goto case '\n';
					case '\n':
						if (Input.GetKey((KeyCode)304) || Input.GetKey((KeyCode)303))
						{
							unwrappedCurrentValue += "\n";
						}
						else
						{
							__instance.PostMessage();
						}
						break;
					}
				}
				if ((Input.GetKey((KeyCode)306) || Input.GetKey((KeyCode)305)) && Input.GetKeyDown((KeyCode)118))
				{
					unwrappedCurrentValue += GUIUtility.systemCopyBuffer.ReplaceLineEndings("\n");
				}
				__instance.m_currentValue = Utils.WordWrap(unwrappedCurrentValue, __instance.m_maxLen);
				__instance.m_currentImeCompositionString = Input.compositionString;
				__instance.UpdateMessage();
			}
		}

		[HarmonyPatch(typeof(PlayerChatManager), "PostMessage")]
		[HarmonyPrefix]
		private static void PostMessage_Prefix(ref bool __runOriginal)
		{
			__runOriginal = false;
		}

		[HarmonyPatch(typeof(PlayerChatManager), "PostMessage")]
		[HarmonyPostfix]
		private static void PostMessage_Postfix(PlayerChatManager __instance)
		{
			if (unwrappedCurrentValue.Trim('\n').Length > 0 && __instance.m_currentValue.Length > 0)
			{
				string[] array = __instance.m_currentValue.Split('\n');
				foreach (string text in array)
				{
					PlayerChatManager.WantToSentTextMessage(PlayerManager.GetLocalPlayerAgent(), text, (PlayerAgent)null);
				}
			}
			unwrappedCurrentValue = "";
			__instance.m_currentValue = "";
			__instance.ExitChatMode();
		}
	}
	internal static class PUI_GameEventLog_Patches
	{
		[HarmonyPatch(typeof(PUI_GameEventLog), "Update")]
		[HarmonyPostfix]
		private static void Update_Postfix(PUI_GameEventLog __instance)
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			((TMP_Text)__instance.m_txtInput).enableWordWrapping = false;
			((TMP_Text)__instance.m_txtInput).overflowMode = (TextOverflowModes)0;
			__instance.m_blinkSuffix = (__instance.m_blink ? "_" : " ");
			((TMP_Text)__instance.m_txtInput).text = __instance.m_textInputString + __instance.m_blinkSuffix;
			((TMP_Text)__instance.m_txtInput).alignment = (TextAlignmentOptions)1025;
			float num = ((TMP_Text)__instance.m_txtInput).preferredHeight + 5.1f;
			Transform val = __instance.m_chatActiveRoot.transform.FindChild("Input Background Active");
			SpriteRenderer component = ((Component)val).GetComponent<SpriteRenderer>();
			component.size = new Vector2(component.size.x, num);
			RectTransform component2 = ((Component)val).GetComponent<RectTransform>();
			((Transform)component2).localPosition = new Vector3(0f, num / 2f - 1f, 0f);
			float num2 = (((TMP_Text)__instance.m_txtInput).text.Contains('\n') ? (num - __instance.m_logItemHeight) : 0f);
			for (int i = 0; i < __instance.m_logItems.Count; i++)
			{
				((Component)__instance.m_logItems[i]).transform.localPosition = new Vector3(0f, num2 + (float)(__instance.m_logItems.Count - i - 1) * __instance.m_logItemHeight);
			}
		}
	}
	internal static class Utils
	{
		private static ManualLogSource logger = Logger.CreateLogSource("BetterTextChat");

		public static void LogInfo(string text)
		{
			logger.LogInfo((object)text);
		}

		public static void LogError(string text)
		{
			logger.LogError((object)text);
		}

		public static void SendChatMessage(string text)
		{
			LogInfo("SendChatMessage: " + text);
			string text2 = "<color=yellow>";
			int num = 50 - text2.Length;
			while (text.Length > num)
			{
				PlayerChatManager.WantToSentTextMessage(PlayerManager.GetLocalPlayerAgent(), text2 + text.Substring(0, num).Trim(), (PlayerAgent)null);
				text = text.Substring(num).Trim();
			}
			PlayerChatManager.WantToSentTextMessage(PlayerManager.GetLocalPlayerAgent(), text2 + text, (PlayerAgent)null);
		}

		public static void SendLocalMessage(string text)
		{
			LogInfo("SendLocalMessage: " + text);
			GuiManager.PlayerLayer.m_gameEventLog.AddLogItem("<color=yellow>" + text, (eGameEventChatLogType)2);
			CM_PageLoadout.Current.m_gameEventLog.AddLogItem("<color=yellow>" + text, (eGameEventChatLogType)2);
		}

		public static string WordWrap(string original_text, int width)
		{
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			StringBuilder stringBuilder3 = new StringBuilder();
			foreach (char c in original_text)
			{
				switch (c)
				{
				case '\n':
					if (stringBuilder2.Length + stringBuilder3.Length > width)
					{
						stringBuilder.Append(stringBuilder2);
						stringBuilder.Append('\n');
						stringBuilder2.Clear();
					}
					stringBuilder2.Append(stringBuilder3);
					stringBuilder3.Clear();
					stringBuilder.Append(stringBuilder2);
					stringBuilder.Append('\n');
					stringBuilder2.Clear();
					break;
				case ' ':
					if (stringBuilder2.Length + stringBuilder3.Length > width)
					{
						stringBuilder.Append(stringBuilder2);
						stringBuilder.Append('\n');
						stringBuilder2.Clear();
					}
					stringBuilder2.Append(stringBuilder3);
					stringBuilder2.Append(' ');
					stringBuilder3.Clear();
					break;
				default:
					stringBuilder3.Append(c);
					if (stringBuilder3.Length > width)
					{
						int num = width - stringBuilder2.Length;
						stringBuilder2.Append(stringBuilder3.ToString(0, num));
						stringBuilder3.Remove(0, num);
						stringBuilder.Append(stringBuilder2);
						stringBuilder.Append('\n');
						stringBuilder2.Clear();
					}
					break;
				}
			}
			if (stringBuilder2.Length + stringBuilder3.Length > width)
			{
				stringBuilder.Append(stringBuilder2);
				stringBuilder.Append('\n');
				stringBuilder2.Clear();
			}
			stringBuilder2.Append(stringBuilder3);
			stringBuilder3.Clear();
			stringBuilder.Append(stringBuilder2);
			stringBuilder2.Clear();
			if (stringBuilder.Length > 0 && stringBuilder[stringBuilder.Length - 1] == '\n' && original_text[original_text.Length - 1] != '\n')
			{
				stringBuilder.Remove(stringBuilder.Length - 1, 1);
			}
			return stringBuilder.ToString();
		}
	}
	[BepInPlugin("BetterTextChat", "BetterTextChat", "0.1.1")]
	internal class Plugin : BasePlugin
	{
		public override void Load()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Expected O, but got Unknown
			Utils.LogInfo("BetterTextChat is loading...");
			PUI_GameEventLog.s_logItemsMax = ConfigManager.chat_history_length.Value;
			Harmony val = new Harmony("BetterTextChat");
			val.PatchAll(typeof(PlayerChatManager_Patches));
			val.PatchAll(typeof(PUI_GameEventLog_Patches));
			Utils.LogInfo("BetterTextChat is loaded");
		}
	}
	internal static class ConfigManager
	{
		public static readonly ConfigEntry<bool> allow_forbidden_chars;

		public static readonly ConfigEntry<int> chat_history_length;

		static ConfigManager()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			ConfigFile val = new ConfigFile(Path.Combine(Paths.ConfigPath, "BetterTextChat.cfg"), true);
			allow_forbidden_chars = val.Bind<bool>("General", "Allow special characters", true, "Makes it possible to type <, >, and = in chat. (Beware: Things can get weird if you use formatting tags, especially across multiple lines.)");
			chat_history_length = val.Bind<int>("General", "Chat history length", 15, "Number of messages to show in chat history");
		}
	}
}