Decompiled source of ControllerTerminalKeyboard v1.0.0

TerminalControllerKeyboard.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TerminalControllerKeyboard")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Adds an on-screen keyboard for controller users")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TerminalControllerKeyboard")]
[assembly: AssemblyTitle("TerminalControllerKeyboard")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ControllerTerminalKeyboard
{
	[BepInPlugin("com.toxcgang.terminalkeyboard", "Controller Terminal Keyboard", "1.0.0")]
	public class TerminalKeyboardPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private sealed class <ProcessKeyRoutine>d__17 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public string key;

			public Key mappedKey;

			private ScrollRect <scrollRect>5__1;

			private Scrollbar <scroll>5__2;

			object IEnumerator<object>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <ProcessKeyRoutine>d__17(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<scrollRect>5__1 = null;
				<scroll>5__2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_028d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0054: Unknown result type (might be due to invalid IL or missing references)
				//IL_005a: Expected I4, but got Unknown
				//IL_005a: Unknown result type (might be due to invalid IL or missing references)
				//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
				//IL_02b3: Unknown result type (might be due to invalid IL or missing references)
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					if ((int)mappedKey != 0 && Keyboard.current != null)
					{
						InputSystem.QueueStateEvent<KeyboardState>((InputDevice)(object)Keyboard.current, new KeyboardState((Key[])(object)new Key[1] { (Key)(int)mappedKey }), -1.0);
					}
					<>2__current = null;
					<>1__state = 1;
					return true;
				case 1:
					<>1__state = -1;
					if ((Object)(object)terminalInstance != (Object)null && (Object)(object)terminalInstance.screenText != (Object)null)
					{
						if (key == "ENT")
						{
							terminalInstance.OnSubmit();
						}
						else if (key == "ESC")
						{
							terminalInstance.QuitTerminal(true);
						}
						else if (key == "BK")
						{
							if (terminalInstance.screenText.text.Length > terminalInstance.textAdded)
							{
								terminalInstance.screenText.text = terminalInstance.screenText.text.Substring(0, terminalInstance.screenText.text.Length - 1);
							}
						}
						else if (key == "SPC")
						{
							TMP_InputField screenText = terminalInstance.screenText;
							screenText.text += " ";
						}
						else
						{
							TMP_InputField screenText2 = terminalInstance.screenText;
							screenText2.text += key.ToLower();
						}
						terminalInstance.screenText.caretPosition = terminalInstance.screenText.text.Length;
						<scrollRect>5__1 = ((Component)terminalInstance).GetComponentInChildren<ScrollRect>();
						if ((Object)(object)<scrollRect>5__1 != (Object)null)
						{
							<scrollRect>5__1.verticalNormalizedPosition = 0f;
						}
						<scroll>5__2 = ((Component)terminalInstance).GetComponentInChildren<Scrollbar>();
						if ((Object)(object)<scroll>5__2 != (Object)null)
						{
							<scroll>5__2.value = 0f;
						}
						<scrollRect>5__1 = null;
						<scroll>5__2 = null;
					}
					<>2__current = null;
					<>1__state = 2;
					return true;
				case 2:
					<>1__state = -1;
					if ((int)mappedKey != 0 && Keyboard.current != null)
					{
						InputSystem.QueueStateEvent<KeyboardState>((InputDevice)(object)Keyboard.current, default(KeyboardState), -1.0);
					}
					return false;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}
		}

		private static TerminalKeyboardPlugin Instance;

		private static GameObject keyboardCanvas;

		private static Terminal terminalInstance;

		private static int selectedIndex = 10;

		private static Image[] keyImages;

		private static bool stickMoved;

		private static float keyCooldown = 0f;

		private static readonly string[] keys = new string[40]
		{
			"1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
			"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",
			"A", "S", "D", "F", "G", "H", "J", "K", "L", "BK",
			"Z", "X", "C", "V", "B", "N", "M", "SPC", "ENT", "ESC"
		};

		private void Awake()
		{
			Instance = this;
			Harmony.CreateAndPatchAll(typeof(TerminalKeyboardPlugin), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Controller Terminal Keyboard 1.1.1 Loaded!");
		}

		[HarmonyPatch(typeof(Terminal), "BeginUsingTerminal")]
		[HarmonyPostfix]
		private static void ShowKeyboard(Terminal __instance)
		{
			terminalInstance = __instance;
			if ((Object)(object)StartOfRound.Instance != (Object)null && StartOfRound.Instance.localPlayerUsingController)
			{
				CreateKeyboardUI();
			}
		}

		[HarmonyPatch(typeof(Terminal), "QuitTerminal")]
		[HarmonyPostfix]
		private static void HideKeyboard()
		{
			if ((Object)(object)keyboardCanvas != (Object)null)
			{
				Object.Destroy((Object)(object)keyboardCanvas);
			}
		}

		private static void CreateKeyboardUI()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0171: Expected O, but got Unknown
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e9: Expected O, but got Unknown
			//IL_0271: Unknown result type (might be due to invalid IL or missing references)
			keyboardCanvas = new GameObject("TerminalKeyboardCanvas");
			Canvas val = keyboardCanvas.AddComponent<Canvas>();
			val.renderMode = (RenderMode)0;
			val.sortingOrder = 100;
			keyboardCanvas.AddComponent<CanvasScaler>().uiScaleMode = (ScaleMode)1;
			keyboardCanvas.AddComponent<GraphicRaycaster>();
			GameObject val2 = new GameObject("KeyboardPanel");
			val2.transform.SetParent(keyboardCanvas.transform, false);
			RectTransform val3 = val2.AddComponent<RectTransform>();
			val3.sizeDelta = new Vector2(600f, 240f);
			val3.anchoredPosition = new Vector2(0f, -100f);
			((Graphic)val2.AddComponent<Image>()).color = new Color(0f, 0f, 0f, 0.6f);
			GameObject val4 = new GameObject("Grid");
			val4.transform.SetParent(val2.transform, false);
			RectTransform val5 = val4.AddComponent<RectTransform>();
			val5.sizeDelta = new Vector2(580f, 220f);
			GridLayoutGroup val6 = val4.AddComponent<GridLayoutGroup>();
			val6.cellSize = new Vector2(52f, 42f);
			val6.spacing = new Vector2(5f, 5f);
			((LayoutGroup)val6).childAlignment = (TextAnchor)4;
			keyImages = (Image[])(object)new Image[keys.Length];
			for (int i = 0; i < keys.Length; i++)
			{
				GameObject val7 = new GameObject("Key_" + keys[i]);
				val7.transform.SetParent(val4.transform, false);
				Image val8 = val7.AddComponent<Image>();
				((Graphic)val8).color = ((i == selectedIndex) ? new Color(0f, 1f, 0f, 0.6f) : new Color(0.2f, 0.2f, 0.2f, 0.4f));
				keyImages[i] = val8;
				GameObject val9 = new GameObject("Text");
				val9.transform.SetParent(val7.transform, false);
				TextMeshProUGUI val10 = val9.AddComponent<TextMeshProUGUI>();
				TMP_InputField screenText = terminalInstance.screenText;
				if ((Object)(object)((screenText != null) ? screenText.textComponent : null) != (Object)null)
				{
					((TMP_Text)val10).font = terminalInstance.screenText.textComponent.font;
				}
				((TMP_Text)val10).text = keys[i];
				((TMP_Text)val10).fontSize = 16f;
				((TMP_Text)val10).alignment = (TextAlignmentOptions)514;
				((Graphic)val10).color = Color.white;
			}
		}

		private void Update()
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)keyboardCanvas == (Object)null || (Object)(object)terminalInstance == (Object)null || !terminalInstance.terminalInUse)
			{
				return;
			}
			Gamepad current = Gamepad.current;
			if (current == null)
			{
				return;
			}
			if (current.dpad.up.isPressed)
			{
				ScrollTerminal(1f);
			}
			else if (current.dpad.down.isPressed)
			{
				ScrollTerminal(-1f);
			}
			Vector2 val = ((InputControl<Vector2>)(object)current.leftStick).ReadValue();
			if (!stickMoved)
			{
				if (val.y > 0.5f)
				{
					MoveSelection(-10);
					stickMoved = true;
				}
				else if (val.y < -0.5f)
				{
					MoveSelection(10);
					stickMoved = true;
				}
				else if (val.x < -0.5f)
				{
					MoveSelection(-1);
					stickMoved = true;
				}
				else if (val.x > 0.5f)
				{
					MoveSelection(1);
					stickMoved = true;
				}
			}
			else if (((Vector2)(ref val)).magnitude < 0.2f)
			{
				stickMoved = false;
			}
			if (keyCooldown > 0f)
			{
				keyCooldown -= Time.deltaTime;
			}
			if (current.buttonSouth.wasPressedThisFrame && keyCooldown <= 0f)
			{
				keyCooldown = 0.15f;
				OnKeyPress(keys[selectedIndex]);
			}
		}

		private static void ScrollTerminal(float direction)
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)terminalInstance == (Object)null)
			{
				return;
			}
			bool flag = false;
			if ((Object)(object)terminalInstance.screenText != (Object)null && (Object)(object)EventSystem.current != (Object)null)
			{
				PointerEventData val = new PointerEventData(EventSystem.current);
				val.scrollDelta = new Vector2(0f, direction * 1500f * Time.deltaTime);
				GameObject val2 = ExecuteEvents.ExecuteHierarchy<IScrollHandler>(((Component)terminalInstance.screenText).gameObject, (BaseEventData)(object)val, ExecuteEvents.scrollHandler);
				flag = (Object)(object)val2 != (Object)null;
			}
			if (flag)
			{
				return;
			}
			float num = 6f;
			ScrollRect componentInChildren = ((Component)terminalInstance).GetComponentInChildren<ScrollRect>();
			if ((Object)(object)componentInChildren != (Object)null)
			{
				componentInChildren.verticalNormalizedPosition += direction * num * Time.deltaTime;
				componentInChildren.verticalNormalizedPosition = Mathf.Clamp(componentInChildren.verticalNormalizedPosition, 0f, 1f);
				return;
			}
			Scrollbar componentInChildren2 = ((Component)terminalInstance).GetComponentInChildren<Scrollbar>();
			if ((Object)(object)componentInChildren2 != (Object)null)
			{
				componentInChildren2.value += direction * num * Time.deltaTime;
				componentInChildren2.value = Mathf.Clamp(componentInChildren2.value, 0f, 1f);
			}
		}

		private static void MoveSelection(int delta)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			int num = selectedIndex + delta;
			if (num >= 0 && num < keys.Length)
			{
				((Graphic)keyImages[selectedIndex]).color = new Color(0.2f, 0.2f, 0.2f, 0.4f);
				selectedIndex = num;
				((Graphic)keyImages[selectedIndex]).color = new Color(0f, 1f, 0f, 0.6f);
			}
		}

		private static Key GetKeyMapping(string key)
		{
			//IL_06ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_068e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0689: Unknown result type (might be due to invalid IL or missing references)
			//IL_06a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0657: Unknown result type (might be due to invalid IL or missing references)
			//IL_0647: Unknown result type (might be due to invalid IL or missing references)
			//IL_067f: Unknown result type (might be due to invalid IL or missing references)
			//IL_066f: Unknown result type (might be due to invalid IL or missing references)
			//IL_060f: Unknown result type (might be due to invalid IL or missing references)
			//IL_063f: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_06de: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_06bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_0701: Unknown result type (might be due to invalid IL or missing references)
			//IL_06fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_06b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_06b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0684: Unknown result type (might be due to invalid IL or missing references)
			//IL_06a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_069d: Unknown result type (might be due to invalid IL or missing references)
			//IL_065f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0677: Unknown result type (might be due to invalid IL or missing references)
			//IL_0667: Unknown result type (might be due to invalid IL or missing references)
			//IL_061f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0637: Unknown result type (might be due to invalid IL or missing references)
			//IL_062f: Unknown result type (might be due to invalid IL or missing references)
			//IL_06fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_06c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_06e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_06cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0693: Unknown result type (might be due to invalid IL or missing references)
			//IL_0698: Unknown result type (might be due to invalid IL or missing references)
			//IL_064f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0617: Unknown result type (might be due to invalid IL or missing references)
			//IL_0627: Unknown result type (might be due to invalid IL or missing references)
			//IL_06e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_06d4: Unknown result type (might be due to invalid IL or missing references)
			return (Key)(key switch
			{
				"A" => 15, 
				"B" => 16, 
				"C" => 17, 
				"D" => 18, 
				"E" => 19, 
				"F" => 20, 
				"G" => 21, 
				"H" => 22, 
				"I" => 23, 
				"J" => 24, 
				"K" => 25, 
				"L" => 26, 
				"M" => 27, 
				"N" => 28, 
				"O" => 29, 
				"P" => 30, 
				"Q" => 31, 
				"R" => 32, 
				"S" => 33, 
				"T" => 34, 
				"U" => 35, 
				"V" => 36, 
				"W" => 37, 
				"X" => 38, 
				"Y" => 39, 
				"Z" => 40, 
				"1" => 41, 
				"2" => 42, 
				"3" => 43, 
				"4" => 44, 
				"5" => 45, 
				"6" => 46, 
				"7" => 47, 
				"8" => 48, 
				"9" => 49, 
				"0" => 50, 
				"ENT" => 2, 
				"ESC" => 60, 
				"BK" => 65, 
				"SPC" => 1, 
				_ => 0, 
			});
		}

		private static void OnKeyPress(string key)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)terminalInstance == (Object)null) && !((Object)(object)Instance == (Object)null))
			{
				Key keyMapping = GetKeyMapping(key);
				((MonoBehaviour)Instance).StartCoroutine(ProcessKeyRoutine(key, keyMapping));
			}
		}

		[IteratorStateMachine(typeof(<ProcessKeyRoutine>d__17))]
		private static IEnumerator ProcessKeyRoutine(string key, Key mappedKey)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <ProcessKeyRoutine>d__17(0)
			{
				key = key,
				mappedKey = mappedKey
			};
		}
	}
}