plugins\Chat.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Steamworks;
using Steamworks.Data;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Chat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Bopl Battle text chat. Online lobby, or dead players during a match. T to type, Enter to send.")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("Chat")]
[assembly: AssemblyTitle("Chat")]
[assembly: AssemblyVersion("1.0.2.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Chat
{
	internal static class ChatLog
	{
		private const int MaxLines = 4;

		private static readonly List<string> lines = new List<string>();

		public static int Count => lines.Count;

		public static void Push(string text)
		{
			lines.Add(text);
			if (lines.Count > 4)
			{
				lines.RemoveAt(0);
			}
		}

		public static string Text(int index)
		{
			return lines[index];
		}
	}
	[HarmonyPatch(typeof(SteamManager), "OnChatMessageCallback")]
	public static class ChatReceivePatch
	{
		public static void Postfix(Friend __1, string __2)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			Plugin.Receive(__1, __2);
		}
	}
	[BepInPlugin("com.solsticio.boplbattle.chat", "Chat", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.solsticio.boplbattle.chat";

		public const string PluginName = "Chat";

		public const string PluginVersion = "1.0.2";

		internal static ManualLogSource Log;

		internal static bool Open;

		internal static string Draft = "";

		private const int MaxChars = 80;

		private const string Prefix = "\u001e";

		private Keyboard hooked;

		private bool ignoreOpenChar;

		private bool enterWasDown;

		private bool numpadWasDown;

		private bool escapeWasDown;

		private bool backspaceWasDown;

		private GUIStyle lineStyle;

		private GUIStyle hintStyle;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			new Harmony("com.solsticio.boplbattle.chat").PatchAll();
			InputSystem.onEvent += (Action<InputEventPtr, InputDevice>)OnInputEvent;
			Log.LogInfo((object)"Chat: loaded.");
		}

		private void OnDestroy()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			InputSystem.onEvent -= (Action<InputEventPtr, InputDevice>)OnInputEvent;
			if (hooked != null)
			{
				hooked.onTextInput -= OnText;
			}
		}

		private void Update()
		{
			Keyboard current = Keyboard.current;
			if (current != hooked)
			{
				if (hooked != null)
				{
					hooked.onTextInput -= OnText;
				}
				hooked = current;
				if (hooked != null)
				{
					hooked.onTextInput += OnText;
				}
			}
			if (!CanType())
			{
				Open = false;
				Draft = "";
			}
			else if (current != null && !Open && ((ButtonControl)current.tKey).wasPressedThisFrame)
			{
				Open = true;
				Draft = "";
				ignoreOpenChar = true;
				enterWasDown = false;
				numpadWasDown = false;
				escapeWasDown = false;
				backspaceWasDown = false;
			}
		}

		private void OnInputEvent(InputEventPtr ptr, InputDevice device)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (!Open || ((InputEventPtr)(ref ptr)).type == new FourCC('T', 'E', 'X', 'T') || (!(device is Keyboard) && !(device is Mouse) && !(device is Gamepad) && !(device is Joystick)))
			{
				return;
			}
			Keyboard val = (Keyboard)(object)((device is Keyboard) ? device : null);
			if (val != null)
			{
				bool num = Pressed((ButtonControl)(object)val.escapeKey, ptr, ref escapeWasDown);
				bool flag = Pressed((ButtonControl)(object)val.enterKey, ptr, ref enterWasDown);
				bool flag2 = Pressed((ButtonControl)(object)val.numpadEnterKey, ptr, ref numpadWasDown);
				bool flag3 = Pressed((ButtonControl)(object)val.backspaceKey, ptr, ref backspaceWasDown);
				if (num)
				{
					Open = false;
					Draft = "";
				}
				else if (flag || flag2)
				{
					string text = Draft.Trim();
					Open = false;
					Draft = "";
					if (text.Length > 0)
					{
						Send(text);
					}
				}
				else if (flag3 && Draft.Length > 0)
				{
					Draft = Draft.Substring(0, Draft.Length - 1);
				}
			}
			((InputEventPtr)(ref ptr)).handled = true;
		}

		private static bool Pressed(ButtonControl key, InputEventPtr ptr, ref bool wasDown)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			float num = default(float);
			if (!InputControlExtensions.ReadValueFromEvent<float>((InputControl<float>)(object)key, ptr, ref num))
			{
				return false;
			}
			bool flag = num >= 0.5f;
			bool result = flag && !wasDown;
			wasDown = flag;
			return result;
		}

		private void OnText(char c)
		{
			if (!Open)
			{
				return;
			}
			if (ignoreOpenChar)
			{
				ignoreOpenChar = false;
				if (c == 't' || c == 'T')
				{
					return;
				}
			}
			if (c >= ' ' && c != '\u007f' && Draft.Length < 80)
			{
				Draft += c;
			}
		}

		private static void Send(string text)
		{
			SteamManager instance = SteamManager.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				((Lobby)(ref instance.currentLobby)).SendChatString("\u001e" + text);
			}
		}

		internal static void Receive(Friend friend, string message)
		{
			if (message == null || !message.StartsWith("\u001e"))
			{
				return;
			}
			string text = message.Substring("\u001e".Length).Trim();
			if (text.Length != 0)
			{
				string text2 = ((Friend)(ref friend)).Name;
				if (string.IsNullOrEmpty(text2))
				{
					text2 = "Player";
				}
				ChatLog.Push(text2 + ": " + text);
			}
		}

		internal static bool CanSee()
		{
			if (GameLobby.isPlayingAReplay)
			{
				return false;
			}
			if (InOnlineLobby() || OnScoreScreen())
			{
				return true;
			}
			if (GameLobby.isOnlineGame)
			{
				return !GameSession.inMenus;
			}
			return false;
		}

		internal static bool CanType()
		{
			if (!CanSee())
			{
				return false;
			}
			if (InOnlineLobby() || OnScoreScreen())
			{
				return true;
			}
			return LocalPlayerIsDead();
		}

		private static bool InOnlineLobby()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			Scene activeScene = SceneManager.GetActiveScene();
			if (((Scene)(ref activeScene)).name == "ChSelect_online")
			{
				return true;
			}
			return (Object)(object)Object.FindObjectOfType<CharacterSelectHandler_online>() != (Object)null;
		}

		private static bool OnScoreScreen()
		{
			if (GameLobby.isOnlineGame)
			{
				return (Object)(object)Object.FindObjectOfType<CharacterStatsList>() != (Object)null;
			}
			return false;
		}

		private static bool LocalPlayerIsDead()
		{
			PlayerHandler val = PlayerHandler.Get();
			if (val == null)
			{
				return false;
			}
			List<Player> list = val.PlayerList();
			if (list == null)
			{
				return false;
			}
			for (int i = 0; i < list.Count; i++)
			{
				Player val2 = list[i];
				if (val2 != null && val2.IsLocalPlayer)
				{
					return !val2.IsAlive;
				}
			}
			return false;
		}

		private void OnGUI()
		{
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Unknown result type (might be due to invalid IL or missing references)
			if (CanSee())
			{
				EnsureStyles();
				float num = Mathf.Max(1f, (float)Screen.height / 1080f);
				float num2 = 560f * num;
				float num3 = 28f * num;
				int count = ChatLog.Count;
				int num4 = (Open ? 2 : (CanType() ? 1 : 0));
				float num5 = num3 * (float)(count + num4) + 16f * num;
				float num6 = 18f * num;
				float num7 = (float)Screen.height - num5 - 18f * num;
				float num8 = num6 + 12f * num;
				float num9 = num7 + 8f * num;
				for (int i = ChatLog.Count - count; i < ChatLog.Count; i++)
				{
					DrawLine(new Rect(num8, num9, num2 - 24f * num, num3), ChatLog.Text(i), num);
					num9 += num3;
				}
				if (Open)
				{
					DrawLine(new Rect(num8, num9, num2 - 24f * num, num3), "> " + Draft + "_", num);
				}
				else if (CanType())
				{
					GUI.Label(new Rect(num8, num9, num2 - 24f * num, num3), "T", hintStyle);
				}
			}
		}

		private void DrawLine(Rect rect, string text, float scale)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			lineStyle.normal.textColor = new Color(0f, 0f, 0f, 0.9f);
			Rect val = rect;
			((Rect)(ref val)).x = ((Rect)(ref val)).x + 2f * scale;
			((Rect)(ref val)).y = ((Rect)(ref val)).y + 2f * scale;
			GUI.Label(val, text, lineStyle);
			lineStyle.normal.textColor = Color.white;
			GUI.Label(rect, text, lineStyle);
		}

		private void EnsureStyles()
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			if (lineStyle == null)
			{
				int fontSize = Mathf.Max(16, Screen.height / 54);
				GUIStyle val = new GUIStyle(GUI.skin.label)
				{
					fontSize = fontSize,
					richText = false,
					wordWrap = false
				};
				val.normal.textColor = Color.white;
				lineStyle = val;
				GUIStyle val2 = new GUIStyle(lineStyle);
				val2.normal.textColor = new Color(1f, 1f, 1f, 0.45f);
				hintStyle = val2;
			}
		}
	}
}