Decompiled source of ControllerChords v1.3.0

ControllerChords.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GUIFramework;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace ControllerChords;

internal static class GamepadCapture
{
	private static readonly HashSet<string> Owners = new HashSet<string>();

	private static bool applied;

	private static int restoreNavigationFrame = -1;

	public static bool Active => Owners.Count > 0;

	public static void Set(string owner, bool on)
	{
		if (on ? Owners.Add(owner) : Owners.Remove(owner))
		{
			Apply();
		}
	}

	public static void Maintain()
	{
		if (!applied)
		{
			if (restoreNavigationFrame >= 0 && Time.frameCount >= restoreNavigationFrame)
			{
				restoreNavigationFrame = -1;
				if ((Object)(object)EventSystem.current != (Object)null)
				{
					EventSystem.current.sendNavigationEvents = true;
				}
			}
			return;
		}
		try
		{
			if (!ZInput.VirtualKeyboardOpen)
			{
				ZInput.VirtualKeyboardOpen = true;
			}
		}
		catch
		{
		}
		EventSystem current = EventSystem.current;
		if ((Object)(object)current != (Object)null && current.sendNavigationEvents)
		{
			current.sendNavigationEvents = false;
		}
	}

	private static void Apply()
	{
		bool flag = Owners.Count > 0;
		if (flag == applied)
		{
			return;
		}
		applied = flag;
		try
		{
			ZInput.VirtualKeyboardOpen = flag || GuiInputField.VirtualKeyboardOpen;
		}
		catch (Exception ex)
		{
			ControllerChordsPlugin.Log.LogWarning((object)("Could not switch Valheim gamepad input: " + ex.Message));
		}
		if (flag)
		{
			restoreNavigationFrame = -1;
			EventSystem current = EventSystem.current;
			if ((Object)(object)current != (Object)null)
			{
				current.sendNavigationEvents = false;
			}
			ComboEngine.ReleaseHeld(includeToggles: false);
		}
		else
		{
			restoreNavigationFrame = Time.frameCount + 2;
		}
	}

	public static void ReleaseAll()
	{
		Owners.Clear();
		Apply();
	}
}
internal static class ConfigManagerBridge
{
	private static object instance;

	private static PropertyInfo displayingWindow;

	private static float nextScan;

	private static int cachedFrame = -1;

	private static bool cachedOpen;

	public static bool Found
	{
		get
		{
			Scan();
			return instance != null;
		}
	}

	public static string Name { get; private set; }

	public static bool IsOpen
	{
		get
		{
			if (cachedFrame == Time.frameCount)
			{
				return cachedOpen;
			}
			cachedFrame = Time.frameCount;
			cachedOpen = false;
			Scan();
			if (instance == null)
			{
				return false;
			}
			try
			{
				cachedOpen = (bool)displayingWindow.GetValue(instance, null);
			}
			catch
			{
				cachedOpen = false;
			}
			return cachedOpen;
		}
	}

	public static void SetOpen(bool open)
	{
		Scan();
		if (instance == null)
		{
			return;
		}
		try
		{
			displayingWindow.SetValue(instance, open, null);
			cachedFrame = -1;
		}
		catch (Exception ex)
		{
			ControllerChordsPlugin.Log.LogWarning((object)("Could not toggle " + Name + ": " + ex.Message));
		}
	}

	private static void Scan()
	{
		if (instance != null || Time.unscaledTime < nextScan)
		{
			return;
		}
		nextScan = Time.unscaledTime + 5f;
		foreach (KeyValuePair<string, PluginInfo> pluginInfo in Chainloader.PluginInfos)
		{
			BaseUnityPlugin val = ((pluginInfo.Value != null) ? pluginInfo.Value.Instance : null);
			if (!((Object)(object)val == (Object)null))
			{
				PropertyInfo property = ((object)val).GetType().GetProperty("DisplayingWindow", BindingFlags.Instance | BindingFlags.Public);
				if (property != null && property.PropertyType == typeof(bool) && property.CanRead && property.CanWrite)
				{
					instance = val;
					displayingWindow = property;
					Name = pluginInfo.Value.Metadata.Name;
					break;
				}
			}
		}
	}
}
internal sealed class ConfigurationManagerAttributes
{
	public bool? ShowRangeAsPercent;

	public Action<ConfigEntryBase> CustomDrawer;

	public bool? Browsable;

	public string Category;

	public object DefaultValue;

	public bool? HideDefaultButton;

	public bool? HideSettingName;

	public string Description;

	public string DispName;

	public int? Order;

	public bool? ReadOnly;

	public bool? IsAdvanced;

	public Func<object, string> ObjToStr;

	public Func<string, object> StrToObj;
}
internal static class Recorder
{
	public static bool Active;

	public static object Owner;

	private static string prompt;

	private static int stage;

	private static float startTime;

	private static uint snapshotMask;

	private static PadButton snapshotTrigger;

	private static Action<string> onDone;

	private static readonly List<PadButton> PressOrder = new List<PadButton>();

	private const float Timeout = 10f;

	public static void Start(object owner, string title, Action<string> done)
	{
		if (Active)
		{
			Finish(null);
		}
		Active = true;
		Owner = owner;
		prompt = title;
		onDone = done;
		stage = 0;
		startTime = Time.unscaledTime;
		snapshotMask = 0u;
		snapshotTrigger = PadButton.None;
		PressOrder.Clear();
		ControllerCursor.ReleaseButtons();
		GamepadCapture.Set("recorder", on: true);
	}

	public static void Cancel()
	{
		Finish(null);
	}

	public static void Update()
	{
		if (!Active)
		{
			return;
		}
		float unscaledTime = Time.unscaledTime;
		if (unscaledTime - startTime > 10f)
		{
			Ui.Toast("Recording timed out");
			Finish(null);
			return;
		}
		uint held = Pad.Held;
		if (stage == 0)
		{
			if (held == 0 && unscaledTime - startTime > 0.1f)
			{
				stage = 1;
			}
			return;
		}
		if (stage == 1)
		{
			if (held == 0)
			{
				return;
			}
			stage = 2;
		}
		uint num = held & ~Pad.Previous;
		if (num != 0)
		{
			PadButton padButton = PadButton.None;
			int num2 = -1;
			for (int i = 1; i <= 16; i++)
			{
				PadButton padButton2 = (PadButton)i;
				if ((num & Pad.Bit(padButton2)) != 0)
				{
					PressOrder.Remove(padButton2);
					PressOrder.Add(padButton2);
					int num3 = Pad.TriggerPreference(padButton2);
					if (num3 > num2)
					{
						num2 = num3;
						padButton = padButton2;
					}
				}
			}
			snapshotMask = held;
			snapshotTrigger = padButton;
		}
		if (held != 0)
		{
			return;
		}
		List<PadButton> list = new List<PadButton>();
		for (int j = 0; j < PressOrder.Count; j++)
		{
			PadButton padButton3 = PressOrder[j];
			if (padButton3 != snapshotTrigger && (snapshotMask & Pad.Bit(padButton3)) != 0)
			{
				list.Add(padButton3);
			}
		}
		while (list.Count > 2)
		{
			list.RemoveAt(0);
		}
		list.Add(snapshotTrigger);
		if (list.Count == 1 && list[0] == PadButton.B)
		{
			Ui.Toast("Recording cancelled");
			Finish(null);
		}
		else
		{
			Finish(ButtonCombo.Format(list));
		}
	}

	private static void Finish(string combo)
	{
		Active = false;
		Owner = null;
		Action<string> action = onDone;
		onDone = null;
		GamepadCapture.Set("recorder", on: false);
		if (action != null)
		{
			try
			{
				action(combo);
			}
			catch (Exception ex)
			{
				ControllerChordsPlugin.Log.LogWarning((object)("Saving the recorded combo failed: " + ex.Message));
			}
		}
	}

	public static void Draw()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: 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)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0293: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0332: Unknown result type (might be due to invalid IL or missing references)
		//IL_0337: Unknown result type (might be due to invalid IL or missing references)
		if (!Active)
		{
			return;
		}
		Ui.Fill(new Rect(0f, 0f, Ui.VW, Ui.VH), new Color(0f, 0f, 0f, 0.5f));
		Rect rect = default(Rect);
		((Rect)(ref rect))..ctor((Ui.VW - 700f) * 0.5f, (Ui.VH - 330f) * 0.5f, 700f, 330f);
		Ui.Panel(rect);
		Ui.Text(new Rect(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 22f, ((Rect)(ref rect)).width - 60f, 40f), prompt, 28, Ui.Amber, (TextAnchor)3, bold: true);
		Ui.Text(new Rect(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 66f, ((Rect)(ref rect)).width - 60f, 30f), (stage == 0) ? "Let go of all buttons…" : "Hold the button(s), press the last one, then let go.", 22, Ui.TextLight, (TextAnchor)3);
		Ui.Text(new Rect(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 96f, ((Rect)(ref rect)).width - 60f, 28f), "Example: hold LB, press Y.  Up to 3 buttons.", 19, Ui.TextDim, (TextAnchor)3);
		List<PadButton> list = new List<PadButton>();
		for (int i = 0; i < PressOrder.Count; i++)
		{
			if (Pad.IsHeld(PressOrder[i]))
			{
				list.Add(PressOrder[i]);
			}
		}
		for (int j = 1; j <= 16; j++)
		{
			PadButton padButton = (PadButton)j;
			if (Pad.IsHeld(padButton) && !list.Contains(padButton))
			{
				list.Add(padButton);
			}
		}
		Rect rect2 = default(Rect);
		((Rect)(ref rect2))..ctor(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 140f, ((Rect)(ref rect)).width - 60f, 90f);
		Ui.Fill(rect2, new Color(0.02f, 0.02f, 0.02f, 1f), 12f);
		if (list.Count > 0 && stage > 0)
		{
			PadButton[] buttons = list.ToArray();
			float num = Ui.ComboWidth(buttons, 56f);
			Ui.Combo(((Rect)(ref rect2)).x + (((Rect)(ref rect2)).width - num) * 0.5f, ((Rect)(ref rect2)).y + 17f, 56f, buttons);
		}
		else
		{
			Ui.Text(rect2, (stage == 0) ? "…" : "Waiting for buttons", 24, Ui.TextDim, (TextAnchor)4);
		}
		float num2 = Mathf.Clamp01((Time.unscaledTime - startTime) / 10f);
		Rect rect3 = default(Rect);
		((Rect)(ref rect3))..ctor(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 250f, ((Rect)(ref rect)).width - 60f, 8f);
		Ui.Fill(rect3, new Color(1f, 1f, 1f, 0.1f), 4f);
		Ui.Fill(new Rect(((Rect)(ref rect3)).x, ((Rect)(ref rect3)).y, ((Rect)(ref rect3)).width * (1f - num2), ((Rect)(ref rect3)).height), Ui.Amber, 4f);
		Ui.Legend(((Rect)(ref rect)).x + 30f, ((Rect)(ref rect)).y + 276f, 30f, "on its own cancels", PadButton.B);
	}
}
internal static class ComboEditor
{
	private enum View
	{
		List,
		Pick,
		KeyGrid
	}

	private sealed class Row
	{
		public string Header;

		public ComboSlot Slot;

		public BuiltinCombo Builtin;

		public bool IsAdd;

		public bool Selectable => Header == null;
	}

	private sealed class PickItem
	{
		public string Header;

		public string Title;

		public string Detail;

		public string Target;

		public bool OpensKeyGrid;

		public bool Clears;
	}

	public static bool IsOpen;

	private static View view;

	private static bool closeAfterPick;

	private static uint ignoreMask;

	private static readonly Repeater NavRepeat = new Repeater();

	private static readonly List<Row> Rows = new List<Row>();

	private static int selected;

	private static int column = 1;

	private static float listScroll;

	private static ComboSlot deleteArmed;

	private static float deleteArmedUntil;

	private static ComboSlot newSlot;

	private static readonly List<PickItem> PickItems = new List<PickItem>();

	private static int pickSelected;

	private static float pickScroll;

	private static ComboSlot pickSlot;

	private static readonly KeyCode[][] Grid;

	private static int gridRow;

	private static int gridColumn;

	private static readonly List<KeyCode> GridModifiers;

	public static void Open()
	{
		if (!IsOpen)
		{
			IsOpen = true;
			view = View.List;
			closeAfterPick = false;
			newSlot = null;
			ignoreMask = Pad.Held;
			NavRepeat.Reset();
			RebuildRows();
			if (selected <= 0 || selected >= Rows.Count || !Rows[selected].Selectable)
			{
				selected = FirstSelectable();
			}
			ControllerCursor.ReleaseButtons();
			GamepadCapture.Set("editor", on: true);
		}
	}

	public static void OpenPicker(ComboSlot slot)
	{
		Open();
		BeginPick(slot);
		closeAfterPick = true;
	}

	public static void Close()
	{
		if (IsOpen)
		{
			IsOpen = false;
			GamepadCapture.Set("editor", on: false);
			ControllerChordsPlugin.Instance.RefreshConfigManagerVisibility();
		}
	}

	private static bool Pressed(PadButton button)
	{
		if (Pad.WasPressed(button))
		{
			return (ignoreMask & Pad.Bit(button)) == 0;
		}
		return false;
	}

	public static void Update()
	{
		if (IsOpen)
		{
			ignoreMask &= Pad.Held;
			int num = Pad.NavDirection(ignoreMask);
			bool flag = NavRepeat.Fire(num, 0.36f, 0.08f);
			switch (view)
			{
			case View.List:
				UpdateList(flag ? num : 0);
				break;
			case View.Pick:
				UpdatePick(flag ? num : 0);
				break;
			case View.KeyGrid:
				UpdateGrid(flag ? num : 0);
				break;
			}
		}
	}

	private static void RebuildRows()
	{
		Rows.Clear();
		Rows.Add(new Row
		{
			Header = "YOUR COMBOS"
		});
		for (int i = 0; i < ComboEngine.Slots.Count; i++)
		{
			ComboSlot comboSlot = ComboEngine.Slots[i];
			if (!comboSlot.IsEmpty || comboSlot == newSlot)
			{
				Rows.Add(new Row
				{
					Slot = comboSlot
				});
			}
		}
		Rows.Add(new Row
		{
			IsAdd = true
		});
		Rows.Add(new Row
		{
			Header = "SYSTEM SHORTCUTS"
		});
		for (int j = 0; j < ComboEngine.Builtins.Count; j++)
		{
			Rows.Add(new Row
			{
				Builtin = ComboEngine.Builtins[j]
			});
		}
		selected = Mathf.Clamp(selected, 0, Rows.Count - 1);
	}

	private static int FirstSelectable()
	{
		for (int i = 0; i < Rows.Count; i++)
		{
			if (Rows[i].Selectable)
			{
				return i;
			}
		}
		return 0;
	}

	private static void UpdateList(int direction)
	{
		if (Pressed(PadButton.B) || Pressed(PadButton.Start))
		{
			Close();
			return;
		}
		switch (direction)
		{
		case 1:
		case 2:
		{
			int num = ((direction != 1) ? 1 : (-1));
			int i;
			for (i = selected + num; i >= 0 && i < Rows.Count && !Rows[i].Selectable; i += num)
			{
			}
			if (i >= 0 && i < Rows.Count)
			{
				selected = i;
			}
			break;
		}
		case 3:
			column = Mathf.Max(0, column - 1);
			break;
		case 4:
			column = Mathf.Min(3, column + 1);
			break;
		}
		Row row = Rows[selected];
		if (Pressed(PadButton.A))
		{
			ActivateCell(row, (row.Slot == null) ? 1 : column);
		}
		else if (Pressed(PadButton.X))
		{
			DeleteRow(row);
		}
		else if (Pressed(PadButton.Y))
		{
			AddCombo();
		}
		else if (Pressed(PadButton.LB) && row.Slot != null)
		{
			CycleMode(row.Slot, -1);
		}
		else if (Pressed(PadButton.RB) && row.Slot != null)
		{
			CycleMode(row.Slot, 1);
		}
	}

	private static void ActivateCell(Row row, int cell)
	{
		if (row.IsAdd)
		{
			AddCombo();
			return;
		}
		if (row.Builtin != null)
		{
			BuiltinCombo builtin = row.Builtin;
			Recorder.Start(builtin, "Shortcut: " + builtin.Name, delegate(string combo)
			{
				if (combo != null)
				{
					string text = FindConflictExcept(combo, null, builtin);
					builtin.Buttons.Value = combo;
					Ui.Toast((text != null) ? ("Saved — also used by " + text) : (builtin.Name + ": " + combo));
				}
			});
			return;
		}
		ComboSlot slot = row.Slot;
		if (slot != null)
		{
			switch (cell)
			{
			case 0:
				slot.Enabled.Value = !slot.Enabled.Value;
				break;
			case 1:
				RecordFor(slot, thenPick: false);
				break;
			case 2:
				BeginPick(slot);
				break;
			default:
				CycleMode(slot, 1);
				break;
			}
		}
	}

	private static void CycleMode(ComboSlot slot, int step)
	{
		int length = Enum.GetValues(typeof(ComboMode)).Length;
		slot.Mode.Value = (ComboMode)((int)(slot.Mode.Value + step + length) % length);
		Ui.Toast(ModeHelp(slot.Mode.Value));
	}

	private static string ModeHelp(ComboMode mode)
	{
		return mode switch
		{
			ComboMode.Tap => "Tap: a quick key press, however long you hold the combo", 
			ComboMode.Toggle => "Toggle: press once to hold the key down, again to let go", 
			_ => "Hold: the key stays down while you hold the combo", 
		};
	}

	private static void RecordFor(ComboSlot slot, bool thenPick)
	{
		bool wasEmpty = slot.IsEmpty;
		Recorder.Start(slot, slot.Label + ": press the combo", delegate(string combo)
		{
			if (combo == null)
			{
				if (wasEmpty && string.IsNullOrEmpty(slot.Action.Value))
				{
					slot.Buttons.Value = string.Empty;
					if (newSlot == slot)
					{
						newSlot = null;
					}
					RebuildRows();
					selected = Mathf.Clamp(selected, 0, Rows.Count - 1);
					if (!Rows[selected].Selectable)
					{
						selected = FirstSelectable();
					}
				}
			}
			else
			{
				string text = FindConflictExcept(combo, slot, null);
				slot.Buttons.Value = combo;
				if (newSlot == slot)
				{
					newSlot = null;
				}
				ButtonCombo buttonCombo = ButtonCombo.Parse(combo);
				if (text != null)
				{
					Ui.Toast(combo + " is also used by " + text);
				}
				else if (buttonCombo.ModCount == 0)
				{
					Ui.Toast("Single button: it will stop doing its normal game action");
				}
				if (thenPick && IsOpen)
				{
					BeginPick(slot);
				}
			}
		});
	}

	private static string FindConflictExcept(string combo, ComboSlot slotSelf, BuiltinCombo builtinSelf)
	{
		ButtonCombo buttonCombo = ButtonCombo.Parse(combo);
		for (int i = 0; i < ComboEngine.Slots.Count; i++)
		{
			ComboSlot comboSlot = ComboEngine.Slots[i];
			if (comboSlot != slotSelf && !comboSlot.IsEmpty && comboSlot.Combo.IsValid && comboSlot.Combo.Trigger == buttonCombo.Trigger && comboSlot.Combo.ModMask == buttonCombo.ModMask)
			{
				return comboSlot.Label;
			}
		}
		for (int j = 0; j < ComboEngine.Builtins.Count; j++)
		{
			BuiltinCombo builtinCombo = ComboEngine.Builtins[j];
			if (builtinCombo != builtinSelf && builtinCombo.Combo.IsValid && builtinCombo.Combo.Trigger == buttonCombo.Trigger && builtinCombo.Combo.ModMask == buttonCombo.ModMask)
			{
				return "\"" + builtinCombo.Name + "\"";
			}
		}
		return null;
	}

	private static void AddCombo()
	{
		ComboSlot comboSlot = null;
		for (int i = 0; i < ComboEngine.Slots.Count; i++)
		{
			if (ComboEngine.Slots[i].IsEmpty)
			{
				comboSlot = ComboEngine.Slots[i];
				break;
			}
		}
		if (comboSlot == null)
		{
			Ui.Toast("All " + ComboEngine.Slots.Count + " combo slots are in use — delete one first");
			return;
		}
		comboSlot.Enabled.Value = true;
		comboSlot.Mode.Value = ComboMode.Hold;
		column = 1;
		newSlot = comboSlot;
		RebuildRowsKeeping(comboSlot);
		RecordFor(comboSlot, thenPick: true);
	}

	private static void RebuildRowsKeeping(ComboSlot slot)
	{
		RebuildRows();
		for (int i = 0; i < Rows.Count; i++)
		{
			if (Rows[i].Slot == slot)
			{
				selected = i;
				break;
			}
		}
	}

	private static void DeleteRow(Row row)
	{
		if (row.Builtin != null)
		{
			row.Builtin.Buttons.Value = string.Empty;
			Ui.Toast(row.Builtin.Name + ": no controller shortcut");
			return;
		}
		ComboSlot slot = row.Slot;
		if (slot == null)
		{
			return;
		}
		float unscaledTime = Time.unscaledTime;
		if (deleteArmed == slot && unscaledTime < deleteArmedUntil)
		{
			ComboEngine.Deactivate(slot);
			slot.Buttons.Value = string.Empty;
			slot.Action.Value = string.Empty;
			slot.Mode.Value = ComboMode.Hold;
			slot.Enabled.Value = true;
			deleteArmed = null;
			RebuildRows();
			if (!Rows[selected].Selectable)
			{
				selected = FirstSelectable();
			}
			Ui.Toast(slot.Label + " deleted");
		}
		else
		{
			deleteArmed = slot;
			deleteArmedUntil = unscaledTime + 2.5f;
			Ui.Toast("Press X again to delete " + slot.Label);
		}
	}

	private static void BeginPick(ComboSlot slot)
	{
		pickSlot = slot;
		view = View.Pick;
		PickItems.Clear();
		List<ModHotkey> list = ComboTargets.ScanModHotkeys();
		PickItems.Add(new PickItem
		{
			Header = ((list.Count > 0) ? "KEYS YOUR MODS USE" : "NO MOD HOTKEYS FOUND")
		});
		for (int i = 0; i < list.Count; i++)
		{
			ModHotkey modHotkey = list[i];
			PickItems.Add(new PickItem
			{
				Title = modHotkey.ModName + " › " + modHotkey.Key,
				Detail = modHotkey.Section + "  ·  currently " + modHotkey.CurrentValue,
				Target = modHotkey.Target
			});
		}
		PickItems.Add(new PickItem
		{
			Header = "OTHER"
		});
		PickItems.Add(new PickItem
		{
			Title = "Choose a keyboard key…",
			Detail = "Any key, with Shift / Ctrl / Alt if you like",
			OpensKeyGrid = true
		});
		PickItems.Add(new PickItem
		{
			Title = "Nothing",
			Detail = "Clear what this combo presses",
			Clears = true
		});
		pickSelected = 1;
		for (int j = 0; j < PickItems.Count; j++)
		{
			if (PickItems[j].Header == null && PickItems[j].Target != null && PickItems[j].Target == slot.Action.Value)
			{
				pickSelected = j;
				break;
			}
		}
		if (PickItems[pickSelected].Header != null)
		{
			pickSelected = NextPick(pickSelected, 1);
		}
		pickScroll = 0f;
	}

	private static int NextPick(int from, int step)
	{
		int i;
		for (i = from + step; i >= 0 && i < PickItems.Count && PickItems[i].Header != null; i += step)
		{
		}
		if (i < 0 || i >= PickItems.Count)
		{
			return from;
		}
		return i;
	}

	private static void UpdatePick(int direction)
	{
		if (Pressed(PadButton.B))
		{
			EndPick();
			return;
		}
		switch (direction)
		{
		case 1:
			pickSelected = NextPick(pickSelected, -1);
			break;
		case 2:
			pickSelected = NextPick(pickSelected, 1);
			break;
		}
		if (Pressed(PadButton.LB))
		{
			for (int i = 0; i < 8; i++)
			{
				pickSelected = NextPick(pickSelected, -1);
			}
		}
		if (Pressed(PadButton.RB))
		{
			for (int j = 0; j < 8; j++)
			{
				pickSelected = NextPick(pickSelected, 1);
			}
		}
		if (Pressed(PadButton.A))
		{
			Choose(PickItems[pickSelected]);
		}
	}

	private static void Choose(PickItem item)
	{
		if (item.Header != null)
		{
			return;
		}
		if (item.OpensKeyGrid)
		{
			view = View.KeyGrid;
			GridModifiers.Clear();
			gridRow = 3;
			gridColumn = 1;
			return;
		}
		pickSlot.Action.Value = (item.Clears ? string.Empty : item.Target);
		if (!item.Clears)
		{
			Ui.Toast(pickSlot.Label + " now presses " + item.Title);
		}
		EndPick();
	}

	private static void EndPick()
	{
		if (closeAfterPick)
		{
			Close();
			return;
		}
		view = View.List;
		RebuildRowsKeeping(pickSlot);
	}

	private static void UpdateGrid(int direction)
	{
		if (Pressed(PadButton.B))
		{
			view = View.Pick;
			return;
		}
		switch (direction)
		{
		case 1:
		case 2:
		{
			int num = gridRow + ((direction != 1) ? 1 : (-1));
			if (num >= 0 && num < Grid.Length)
			{
				float num2 = ((float)gridColumn + 0.5f) / (float)Grid[gridRow].Length;
				gridRow = num;
				gridColumn = Mathf.Clamp(Mathf.FloorToInt(num2 * (float)Grid[gridRow].Length), 0, Grid[gridRow].Length - 1);
			}
			break;
		}
		case 3:
			gridColumn = (gridColumn - 1 + Grid[gridRow].Length) % Grid[gridRow].Length;
			break;
		case 4:
			gridColumn = (gridColumn + 1) % Grid[gridRow].Length;
			break;
		}
		if (Pressed(PadButton.A))
		{
			PickGridKey(Grid[gridRow][gridColumn]);
		}
		else if (Pressed(PadButton.Start) && GridModifiers.Count > 0)
		{
			FinishGrid((KeyCode)0);
		}
	}

	private static void PickGridKey(KeyCode key)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: 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)
		if (ComboTargets.IsModifierKey(key))
		{
			if (!GridModifiers.Remove(key))
			{
				GridModifiers.Add(key);
			}
		}
		else
		{
			FinishGrid(key);
		}
	}

	private static void FinishGrid(KeyCode main)
	{
		//IL_000b: 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)
		List<KeyCode> list = new List<KeyCode>(GridModifiers);
		if ((int)main != 0)
		{
			list.Add(main);
		}
		if (list.Count != 0)
		{
			pickSlot.Action.Value = ComboTargets.KeysTarget(list);
			Ui.Toast(pickSlot.Label + " now presses " + ComboTargets.Describe(pickSlot.Action.Value));
			EndPick();
		}
	}

	public static void Draw()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		if (IsOpen)
		{
			Ui.Fill(new Rect(0f, 0f, Ui.VW, Ui.VH), new Color(0f, 0f, 0f, 0.55f));
			float num = Mathf.Min(Ui.VW - 80f, 1320f);
			float num2 = Mathf.Min(Ui.VH - 80f, 860f);
			Rect val = default(Rect);
			((Rect)(ref val))..ctor((Ui.VW - num) * 0.5f, (Ui.VH - num2) * 0.5f, num, num2);
			Ui.Panel(val);
			switch (view)
			{
			case View.List:
				DrawList(val);
				break;
			case View.Pick:
				DrawPick(val);
				break;
			case View.KeyGrid:
				DrawGrid(val);
				break;
			}
			Event current = Event.current;
			if (current != null && current.isMouse && !Recorder.Active)
			{
				current.Use();
			}
		}
	}

	private static void DrawTitle(Rect panel, string title, string subtitle)
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		Ui.Text(new Rect(((Rect)(ref panel)).x + 36f, ((Rect)(ref panel)).y + 24f, ((Rect)(ref panel)).width - 72f, 44f), title, 34, Ui.Amber, (TextAnchor)3, bold: true);
		Ui.Text(new Rect(((Rect)(ref panel)).x + 36f, ((Rect)(ref panel)).y + 70f, ((Rect)(ref panel)).width - 72f, 28f), subtitle, 20, Ui.TextDim, (TextAnchor)3);
	}

	private static void DrawList(Rect panel)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: 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_0221: Unknown result type (might be due to invalid IL or missing references)
		//IL_031d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0336: Unknown result type (might be due to invalid IL or missing references)
		//IL_0419: Unknown result type (might be due to invalid IL or missing references)
		//IL_0422: Unknown result type (might be due to invalid IL or missing references)
		//IL_028c: Unknown result type (might be due to invalid IL or missing references)
		DrawTitle(panel, "Controller Chords", "Hold the first button(s) and press the last one: your combo presses a keyboard key for your mods.");
		float num = ((Rect)(ref panel)).x + 30f;
		float num2 = ((Rect)(ref panel)).xMax - 30f;
		float num3 = num + 12f;
		float num4 = num + 110f;
		float num5 = num + 430f;
		float num6 = num2 - 180f;
		float num7 = ((Rect)(ref panel)).y + 112f;
		Ui.Text(new Rect(num3, num7, 90f, 26f), "On", 18, Ui.TextDim, (TextAnchor)3);
		Ui.Text(new Rect(num4, num7, 300f, 26f), "Combo", 18, Ui.TextDim, (TextAnchor)3);
		Ui.Text(new Rect(num5, num7, num6 - num5, 26f), "Presses", 18, Ui.TextDim, (TextAnchor)3);
		Ui.Text(new Rect(num6, num7, 170f, 26f), "Mode", 18, Ui.TextDim, (TextAnchor)3);
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(num, num7 + 34f, num2 - num, ((Rect)(ref panel)).yMax - (num7 + 34f) - 120f);
		float num8 = 0f;
		float num9 = 0f;
		float num10 = 0f;
		for (int i = 0; i < Rows.Count; i++)
		{
			float num11 = ((Rows[i].Header != null) ? 46f : 60f);
			if (i == selected)
			{
				num8 = num10;
				num9 = num10 + num11;
			}
			num10 += num11;
		}
		float num12 = num10;
		if (num8 - listScroll < 0f)
		{
			listScroll = Mathf.Max(0f, num8 - ((selected > 0 && Rows[selected - 1].Header != null) ? 46f : 0f));
		}
		else if (num9 - listScroll > ((Rect)(ref val)).height)
		{
			listScroll = num9 - ((Rect)(ref val)).height;
		}
		listScroll = Mathf.Clamp(listScroll, 0f, Mathf.Max(0f, num12 - ((Rect)(ref val)).height));
		GUI.BeginGroup(val);
		num10 = 0f - listScroll;
		for (int j = 0; j < Rows.Count; j++)
		{
			Row row = Rows[j];
			float num13 = ((row.Header != null) ? 46f : 60f);
			if (num10 + num13 > 0f && num10 < ((Rect)(ref val)).height)
			{
				DrawRow(row, j == selected, new Rect(0f, num10, ((Rect)(ref val)).width, num13), num3 - num, num4 - num, num5 - num, num6 - num, j);
			}
			num10 += num13;
		}
		GUI.EndGroup();
		if (num12 > ((Rect)(ref val)).height)
		{
			float num14 = ((Rect)(ref val)).height * ((Rect)(ref val)).height / num12;
			float num15 = ((Rect)(ref val)).y + (((Rect)(ref val)).height - num14) * (listScroll / (num12 - ((Rect)(ref val)).height));
			Ui.Fill(new Rect(num2 + 8f, num15, 5f, num14), new Color(1f, 1f, 1f, 0.25f), 2.5f);
		}
		Row row2 = Rows[selected];
		Ui.Text(text: row2.IsAdd ? "Add a combo: record the buttons, then pick the key it should press." : ((row2.Builtin != null) ? ("Press A to record a new controller shortcut for \"" + row2.Builtin.Name + "\". X removes it.") : (column switch
		{
			0 => "Turn this combo on or off without deleting it.", 
			1 => "Press A, then hold your buttons and let go to record the combo.", 
			2 => ComboTargets.Describe(row2.Slot.Action.Value), 
			_ => ModeHelp(row2.Slot.Mode.Value) + ".  LB / RB also change it.", 
		})), rect: new Rect(num + 6f, ((Rect)(ref panel)).yMax - 110f, num2 - num, 30f), size: 20, color: Ui.TextLight, anchor: (TextAnchor)3);
		float y = ((Rect)(ref panel)).yMax - 62f;
		Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(num + 6f, y, 32f, "Edit", PadButton.A), y, 32f, "Delete", PadButton.X), y, 32f, "Add combo", PadButton.Y), y, 32f, "Mode", PadButton.LB, PadButton.RB), y, 32f, "Column", PadButton.DPadLeft, PadButton.DPadRight);
		Ui.Legend(num2 - Ui.LegendWidth(32f, "Close", PadButton.B) + 16f, y, 32f, "Close", PadButton.B);
	}

	private static void DrawRow(Row row, bool isSelected, Rect rect, float onX, float comboX, float actionX, float modeX, int index)
	{
		//IL_0031: 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_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_016c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_028c: Unknown result type (might be due to invalid IL or missing references)
		//IL_028e: Unknown result type (might be due to invalid IL or missing references)
		//IL_021f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_0377: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_039b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0318: Unknown result type (might be due to invalid IL or missing references)
		//IL_032a: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0426: Unknown result type (might be due to invalid IL or missing references)
		//IL_042b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0477: Unknown result type (might be due to invalid IL or missing references)
		//IL_0479: Unknown result type (might be due to invalid IL or missing references)
		//IL_0521: Unknown result type (might be due to invalid IL or missing references)
		//IL_04f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_04fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0544: Unknown result type (might be due to invalid IL or missing references)
		//IL_0588: Unknown result type (might be due to invalid IL or missing references)
		//IL_058a: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0620: Unknown result type (might be due to invalid IL or missing references)
		//IL_0654: Unknown result type (might be due to invalid IL or missing references)
		//IL_0656: Unknown result type (might be due to invalid IL or missing references)
		//IL_0665: 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_0619: Unknown result type (might be due to invalid IL or missing references)
		//IL_0612: Unknown result type (might be due to invalid IL or missing references)
		//IL_06c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_06cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_071b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0720: Unknown result type (might be due to invalid IL or missing references)
		if (row.Header != null)
		{
			Ui.Text(new Rect(((Rect)(ref rect)).x + 8f, ((Rect)(ref rect)).y + 12f, ((Rect)(ref rect)).width, 30f), row.Header, 18, Ui.Amber, (TextAnchor)3, bold: true);
			Ui.Fill(new Rect(((Rect)(ref rect)).x + 8f, ((Rect)(ref rect)).yMax - 4f, ((Rect)(ref rect)).width - 16f, 1.5f), new Color(Ui.Amber.r, Ui.Amber.g, Ui.Amber.b, 0.3f));
			return;
		}
		Rect rect2 = default(Rect);
		((Rect)(ref rect2))..ctor(((Rect)(ref rect)).x, ((Rect)(ref rect)).y + 4f, ((Rect)(ref rect)).width, ((Rect)(ref rect)).height - 8f);
		bool flag = row.Slot != null && row.Slot == deleteArmed && Time.unscaledTime < deleteArmedUntil;
		if (isSelected || flag)
		{
			Ui.Fill(rect2, flag ? new Color(0.6f, 0.15f, 0.12f, 0.35f) : new Color(Ui.Amber.r, Ui.Amber.g, Ui.Amber.b, 0.1f), 10f);
		}
		if (Ui.Clicked(rect2))
		{
			selected = index;
			float x = Event.current.mousePosition.x;
			int num = (column = ((!(x < comboX - 10f)) ? ((x < actionX - 10f) ? 1 : ((x < modeX - 10f) ? 2 : 3)) : 0));
			ActivateCell(row, (row.Slot == null) ? 1 : num);
		}
		if (row.IsAdd)
		{
			Rect rect3 = default(Rect);
			((Rect)(ref rect3))..ctor(comboX - 6f, ((Rect)(ref rect2)).y + 6f, 330f, ((Rect)(ref rect2)).height - 12f);
			if (isSelected)
			{
				Ui.Fill(rect3, Ui.Amber, 8f);
			}
			Ui.Text(rect3, "+  Add a combo", 22, isSelected ? Ui.TextDark : Ui.TextLight, (TextAnchor)4, bold: true);
			return;
		}
		float num2 = 36f;
		float y = ((Rect)(ref rect2)).y + (((Rect)(ref rect2)).height - num2) * 0.5f;
		if (row.Builtin != null)
		{
			Rect rect4 = default(Rect);
			((Rect)(ref rect4))..ctor(comboX - 8f, ((Rect)(ref rect2)).y + 3f, actionX - comboX - 12f, ((Rect)(ref rect2)).height - 6f);
			if (isSelected)
			{
				Ui.Outline(rect4, Ui.Amber, 2.5f, 8f);
			}
			if (row.Builtin.Combo.IsValid)
			{
				Ui.Combo(comboX, y, num2, row.Builtin.Combo.Buttons);
			}
			else
			{
				Ui.Text(new Rect(comboX, ((Rect)(ref rect2)).y, 280f, ((Rect)(ref rect2)).height), "(none)", 21, Ui.TextDim, (TextAnchor)3);
			}
			Ui.Text(new Rect(actionX, ((Rect)(ref rect2)).y, modeX - actionX + 170f, ((Rect)(ref rect2)).height), row.Builtin.Name, 22, Ui.TextLight, (TextAnchor)3);
			return;
		}
		ComboSlot slot = row.Slot;
		int num3 = (isSelected ? column : (-1));
		Rect rect5 = default(Rect);
		((Rect)(ref rect5))..ctor(onX - 4f, ((Rect)(ref rect2)).y + 9f, 74f, ((Rect)(ref rect2)).height - 18f);
		Ui.Fill(rect5, slot.Enabled.Value ? new Color(0.2f, 0.5f, 0.22f, 1f) : new Color(0.25f, 0.24f, 0.23f, 1f), 8f);
		Ui.Text(rect5, slot.Enabled.Value ? "ON" : "OFF", 19, Ui.TextLight, (TextAnchor)4, bold: true);
		if (num3 == 0)
		{
			Ui.Outline(new Rect(((Rect)(ref rect5)).x - 3f, ((Rect)(ref rect5)).y - 3f, ((Rect)(ref rect5)).width + 6f, ((Rect)(ref rect5)).height + 6f), Ui.Amber, 2.5f, 10f);
		}
		Rect rect6 = default(Rect);
		((Rect)(ref rect6))..ctor(comboX - 8f, ((Rect)(ref rect2)).y + 3f, actionX - comboX - 12f, ((Rect)(ref rect2)).height - 6f);
		if (num3 == 1)
		{
			Ui.Outline(rect6, Ui.Amber, 2.5f, 8f);
		}
		if (slot.Combo.IsValid)
		{
			float num4 = Ui.Combo(comboX, y, num2, slot.Combo.Buttons);
			if (FindConflictExcept(slot.Buttons.Value, slot, null) != null)
			{
				Ui.Text(new Rect(comboX + num4 + 10f, ((Rect)(ref rect2)).y, actionX - comboX - num4 - 20f, ((Rect)(ref rect2)).height), "clash", 17, Ui.Warn, (TextAnchor)3);
			}
		}
		else
		{
			Ui.Text(new Rect(comboX, ((Rect)(ref rect2)).y, 300f, ((Rect)(ref rect2)).height), (Recorder.Active && Recorder.Owner == slot) ? "recording…" : "(press A to record)", 20, Ui.TextDim, (TextAnchor)3);
		}
		Rect rect7 = default(Rect);
		((Rect)(ref rect7))..ctor(actionX - 8f, ((Rect)(ref rect2)).y + 3f, modeX - actionX - 8f, ((Rect)(ref rect2)).height - 6f);
		if (num3 == 2)
		{
			Ui.Outline(rect7, Ui.Amber, 2.5f, 8f);
		}
		Ui.Text(text: string.IsNullOrEmpty(slot.Action.Value) ? "(press A to choose)" : ComboTargets.Describe(slot.Action.Value), rect: new Rect(actionX, ((Rect)(ref rect2)).y, ((Rect)(ref rect7)).width - 12f, ((Rect)(ref rect2)).height), size: 21, color: string.IsNullOrEmpty(slot.Action.Value) ? Ui.TextDim : (slot.Enabled.Value ? Ui.TextLight : Ui.TextDim), anchor: (TextAnchor)3);
		Rect rect8 = default(Rect);
		((Rect)(ref rect8))..ctor(modeX, ((Rect)(ref rect2)).y + 9f, 150f, ((Rect)(ref rect2)).height - 18f);
		Ui.Fill(rect8, Ui.KeySpecial, 8f);
		Ui.Text(rect8, slot.Mode.Value.ToString(), 20, Ui.TextLight, (TextAnchor)4, bold: true);
		if (num3 == 3)
		{
			Ui.Outline(new Rect(((Rect)(ref rect8)).x - 3f, ((Rect)(ref rect8)).y - 3f, ((Rect)(ref rect8)).width + 6f, ((Rect)(ref rect8)).height + 6f), Ui.Amber, 2.5f, 10f);
		}
		if (slot.Active)
		{
			Ui.Fill(new Rect(((Rect)(ref rect2)).xMax - 12f, ((Rect)(ref rect2)).y + ((Rect)(ref rect2)).height * 0.5f - 5f, 10f, 10f), Ui.Good, 5f);
		}
	}

	private static void DrawPick(Rect panel)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0281: Unknown result type (might be due to invalid IL or missing references)
		//IL_027a: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0316: Unknown result type (might be due to invalid IL or missing references)
		//IL_034c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0331: Unknown result type (might be due to invalid IL or missing references)
		//IL_0358: Unknown result type (might be due to invalid IL or missing references)
		DrawTitle(panel, "What should " + ((pickSlot != null) ? pickSlot.Label : "this combo") + " press?", "Picking a mod's setting follows it — if you change that mod's key later, the combo follows.");
		float num = ((Rect)(ref panel)).x + 30f;
		float num2 = ((Rect)(ref panel)).xMax - 30f;
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(num, ((Rect)(ref panel)).y + 116f, num2 - num, ((Rect)(ref panel)).height - 116f - 90f);
		float num3 = 0f;
		float num4 = 0f;
		float num5 = 0f;
		for (int i = 0; i < PickItems.Count; i++)
		{
			float num6 = ((PickItems[i].Header != null) ? 46f : 64f);
			if (i == pickSelected)
			{
				num3 = num5;
				num4 = num5 + num6;
			}
			num5 += num6;
		}
		float num7 = num5;
		if (num3 - pickScroll < 0f)
		{
			pickScroll = Mathf.Max(0f, num3 - 46f);
		}
		else if (num4 - pickScroll > ((Rect)(ref val)).height)
		{
			pickScroll = num4 - ((Rect)(ref val)).height;
		}
		pickScroll = Mathf.Clamp(pickScroll, 0f, Mathf.Max(0f, num7 - ((Rect)(ref val)).height));
		GUI.BeginGroup(val);
		num5 = 0f - pickScroll;
		Rect rect = default(Rect);
		for (int j = 0; j < PickItems.Count; j++)
		{
			PickItem pickItem = PickItems[j];
			float num8 = ((pickItem.Header != null) ? 46f : 64f);
			if (num5 + num8 > 0f && num5 < ((Rect)(ref val)).height)
			{
				new Rect(0f, num5, ((Rect)(ref val)).width, num8);
				if (pickItem.Header != null)
				{
					Ui.Text(new Rect(8f, num5 + 12f, ((Rect)(ref val)).width, 30f), pickItem.Header, 18, Ui.Amber, (TextAnchor)3, bold: true);
				}
				else
				{
					((Rect)(ref rect))..ctor(0f, num5 + 3f, ((Rect)(ref val)).width, num8 - 6f);
					bool flag = j == pickSelected;
					bool flag2 = pickSlot != null && pickItem.Target != null && pickItem.Target == pickSlot.Action.Value;
					Ui.Fill(rect, (Color)(flag ? Ui.Amber : new Color(1f, 1f, 1f, 0.04f)), 10f);
					Color color = (flag ? Ui.TextDark : Ui.TextLight);
					Ui.Text(new Rect(18f, ((Rect)(ref rect)).y + 4f, ((Rect)(ref rect)).width - 36f, 30f), pickItem.Title + (flag2 ? "   ✓ current" : string.Empty), 23, color, (TextAnchor)3, bold: true);
					Ui.Text(new Rect(18f, ((Rect)(ref rect)).y + 32f, ((Rect)(ref rect)).width - 36f, 24f), pickItem.Detail ?? string.Empty, 18, (Color)(flag ? new Color(0.2f, 0.14f, 0.05f, 1f) : Ui.TextDim), (TextAnchor)3);
					if (Ui.Clicked(rect))
					{
						pickSelected = j;
						Choose(pickItem);
					}
				}
			}
			num5 += num8;
		}
		GUI.EndGroup();
		float y = ((Rect)(ref panel)).yMax - 62f;
		Ui.Legend(Ui.Legend(num + 6f, y, 32f, "Choose", PadButton.A), y, 32f, "Page", PadButton.LB, PadButton.RB);
		Ui.Legend(num2 - Ui.LegendWidth(32f, "Back", PadButton.B) + 16f, y, 32f, "Back", PadButton.B);
	}

	private static void DrawGrid(Rect panel)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_0197: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		DrawTitle(panel, "Choose a key", "Shift / Ctrl / Alt toggle on and off; picking any other key finishes. Menu uses just the modifiers.");
		float num = ((Rect)(ref panel)).x + 30f;
		float num2 = ((Rect)(ref panel)).xMax - 30f;
		List<KeyCode> list = new List<KeyCode>(GridModifiers);
		KeyCode val = Grid[gridRow][gridColumn];
		if (!ComboTargets.IsModifierKey(val))
		{
			list.Add(val);
		}
		string text = string.Join(" + ", list.ConvertAll(ComboTargets.KeyName).ToArray());
		Ui.Text(new Rect(num + 6f, ((Rect)(ref panel)).y + 108f, num2 - num, 34f), "Will press:  " + text, 24, Ui.TextLight, (TextAnchor)3, bold: true);
		float num3 = ((Rect)(ref panel)).y + 158f;
		float num4 = Mathf.Min(70f, (((Rect)(ref panel)).yMax - 100f - num3) / (float)Grid.Length);
		for (int i = 0; i < Grid.Length; i++)
		{
			KeyCode[] array = Grid[i];
			float num5 = (num2 - num) / (float)array.Length;
			for (int j = 0; j < array.Length; j++)
			{
				Rect rect = new Rect(num + (float)j * num5 + 3f, num3 + (float)i * num4 + 3f, num5 - 6f, num4 - 6f);
				bool flag = i == gridRow && j == gridColumn;
				bool flag2 = GridModifiers.Contains(array[j]);
				Color color = (flag ? Ui.Amber : (flag2 ? Ui.AmberDark : (ComboTargets.IsModifierKey(array[j]) ? Ui.KeySpecial : Ui.KeyFill)));
				Ui.Fill(rect, color, 8f);
				string text2 = ComboTargets.KeyName(array[j]).Replace("Keypad", "Num ").Replace("Arrow", "");
				Ui.Text(rect, text2, (text2.Length > 4) ? 17 : 21, flag ? Ui.TextDark : Ui.TextLight, (TextAnchor)4, bold: true);
				if (Ui.Clicked(rect))
				{
					gridRow = i;
					gridColumn = j;
					PickGridKey(array[j]);
					if (view != View.KeyGrid)
					{
						return;
					}
				}
			}
		}
		float y = ((Rect)(ref panel)).yMax - 62f;
		Ui.Legend(Ui.Legend(num + 6f, y, 32f, "Pick", PadButton.A), y, 32f, "Use modifiers only", PadButton.Start);
		Ui.Legend(num2 - Ui.LegendWidth(32f, "Back", PadButton.B) + 16f, y, 32f, "Back", PadButton.B);
	}

	static ComboEditor()
	{
		KeyCode[][] array = new KeyCode[8][];
		KeyCode[] array2 = new KeyCode[13];
		RuntimeHelpers.InitializeArray(array2, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[0] = (KeyCode[])(object)array2;
		KeyCode[] array3 = new KeyCode[14];
		RuntimeHelpers.InitializeArray(array3, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[1] = (KeyCode[])(object)array3;
		KeyCode[] array4 = new KeyCode[14];
		RuntimeHelpers.InitializeArray(array4, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[2] = (KeyCode[])(object)array4;
		KeyCode[] array5 = new KeyCode[13];
		RuntimeHelpers.InitializeArray(array5, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[3] = (KeyCode[])(object)array5;
		KeyCode[] array6 = new KeyCode[12];
		RuntimeHelpers.InitializeArray(array6, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[4] = (KeyCode[])(object)array6;
		KeyCode[] array7 = new KeyCode[11];
		RuntimeHelpers.InitializeArray(array7, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[5] = (KeyCode[])(object)array7;
		KeyCode[] array8 = new KeyCode[14];
		RuntimeHelpers.InitializeArray(array8, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[6] = (KeyCode[])(object)array8;
		KeyCode[] array9 = new KeyCode[11];
		RuntimeHelpers.InitializeArray(array9, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
		array[7] = (KeyCode[])(object)array9;
		Grid = array;
		gridRow = 3;
		gridColumn = 1;
		GridModifiers = new List<KeyCode>();
	}
}
internal static class ConfigDrawers
{
	public static void ButtonsDrawer(ConfigEntryBase entry)
	{
		bool flag = Recorder.Active && Recorder.Owner == entry;
		string text = entry.BoxedValue as string;
		GUILayout.Label(flag ? "Hold the buttons on your controller, then let go…" : (string.IsNullOrEmpty(text) ? "(none)" : text), (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
		if (GUILayout.Button(flag ? "Cancel" : "Record", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(false) }))
		{
			if (flag)
			{
				Recorder.Cancel();
			}
			else
			{
				Recorder.Start(entry, "Record " + entry.Definition.Section + " " + entry.Definition.Key, delegate(string combo)
				{
					if (combo != null)
					{
						entry.BoxedValue = combo;
					}
				});
			}
		}
		if (GUILayout.Button("Clear", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(false) }))
		{
			entry.BoxedValue = string.Empty;
		}
	}

	public static void ActionDrawer(ConfigEntryBase entry)
	{
		GUILayout.Label(ComboTargets.Describe(entry.BoxedValue as string), (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
		if (!GUILayout.Button("Choose…", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(false) }))
		{
			return;
		}
		for (int i = 0; i < ComboEngine.Slots.Count; i++)
		{
			if ((object)ComboEngine.Slots[i].Action == entry)
			{
				ComboEditor.OpenPicker(ComboEngine.Slots[i]);
				break;
			}
		}
	}

	public static void EditorButtonDrawer(ConfigEntryBase entry)
	{
		if (GUILayout.Button("Open the controller combo editor", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) }))
		{
			ComboEditor.Open();
		}
	}
}
public enum ComboMode
{
	Hold,
	Tap,
	Toggle
}
internal struct ButtonCombo
{
	public uint ModMask;

	public int ModCount;

	public PadButton Trigger;

	public PadButton[] Buttons;

	public bool IsValid => Trigger != PadButton.None;

	public uint AllMask => ModMask | Pad.Bit(Trigger);

	public static ButtonCombo Parse(string text)
	{
		ButtonCombo result = default(ButtonCombo);
		if (string.IsNullOrEmpty(text))
		{
			return result;
		}
		List<PadButton> list = new List<PadButton>();
		string[] array = text.Split(new char[1] { '+' });
		for (int i = 0; i < array.Length; i++)
		{
			PadButton padButton = Pad.Parse(array[i]);
			if (padButton != PadButton.None && !list.Contains(padButton))
			{
				list.Add(padButton);
			}
		}
		if (list.Count == 0)
		{
			return result;
		}
		result.Buttons = list.ToArray();
		result.Trigger = list[list.Count - 1];
		for (int j = 0; j < list.Count - 1; j++)
		{
			result.ModMask |= Pad.Bit(list[j]);
			result.ModCount++;
		}
		return result;
	}

	public static string Format(IList<PadButton> buttons)
	{
		if (buttons == null || buttons.Count == 0)
		{
			return string.Empty;
		}
		return string.Join(" + ", buttons.Select(Pad.Name).ToArray());
	}
}
internal sealed class ComboSlot
{
	public int Index;

	public ConfigEntry<bool> Enabled;

	public ConfigEntry<string> Buttons;

	public ConfigEntry<string> Action;

	public ConfigEntry<ComboMode> Mode;

	public ConfigurationManagerAttributes[] Attributes;

	public ButtonCombo Combo;

	public bool Active;

	public KeyCode[] ActiveKeys;

	public bool IsEmpty
	{
		get
		{
			if (string.IsNullOrEmpty(Buttons.Value))
			{
				return string.IsNullOrEmpty(Action.Value);
			}
			return false;
		}
	}

	public bool IsLive
	{
		get
		{
			if (Enabled.Value && Combo.IsValid)
			{
				return !string.IsNullOrEmpty(Action.Value);
			}
			return false;
		}
	}

	public string Label => "Combo " + Index.ToString("00");

	public void Reparse()
	{
		Combo = ButtonCombo.Parse(Buttons.Value);
	}
}
internal sealed class BuiltinCombo
{
	public string Name;

	public ConfigEntry<string> Buttons;

	public ButtonCombo Combo;

	public Action Run;

	public void Reparse()
	{
		Combo = ButtonCombo.Parse(Buttons.Value);
	}
}
internal sealed class ModHotkey
{
	public string Guid;

	public string ModName;

	public string Section;

	public string Key;

	public ConfigEntryBase Entry;

	public string Target => "mod:" + Guid + "|" + Section + "|" + Key;

	public string CurrentValue => ComboTargets.EntryValueText(Entry);
}
internal static class ComboTargets
{
	public const string ModPrefix = "mod:";

	public const string KeysPrefix = "keys:";

	public static bool Resolve(string target, List<KeyCode> keys)
	{
		//IL_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		keys.Clear();
		if (string.IsNullOrEmpty(target))
		{
			return false;
		}
		if (target.StartsWith("mod:", StringComparison.OrdinalIgnoreCase))
		{
			ConfigEntryBase val = FindModEntry(target);
			if (val == null)
			{
				return false;
			}
			AddEntryKeys(val, keys);
			return keys.Count > 0;
		}
		string[] array = (target.StartsWith("keys:", StringComparison.OrdinalIgnoreCase) ? target.Substring("keys:".Length) : target).Split(new char[1] { '+' });
		for (int i = 0; i < array.Length; i++)
		{
			if (TryParseKey(array[i], out var key) && (int)key != 0 && !keys.Contains(key))
			{
				keys.Add(key);
			}
		}
		return keys.Count > 0;
	}

	public unsafe static string KeysTarget(IList<KeyCode> keys)
	{
		return "keys:" + string.Join("+", keys.Select((KeyCode k) => ((object)(*(KeyCode*)(&k))/*cast due to .constrained prefix*/).ToString()).ToArray());
	}

	public static string Describe(string target)
	{
		if (string.IsNullOrEmpty(target))
		{
			return "(nothing chosen)";
		}
		if (target.StartsWith("mod:", StringComparison.OrdinalIgnoreCase))
		{
			if (!SplitModTarget(target, out var guid, out var _, out var key))
			{
				return target;
			}
			if (!Chainloader.PluginInfos.TryGetValue(guid, out var value) || value == null)
			{
				return key + "  (mod not installed: " + guid + ")";
			}
			ConfigEntryBase val = FindModEntry(target);
			string text = ((val != null) ? EntryValueText(val) : "setting missing");
			return value.Metadata.Name + " › " + key + "  [" + text + "]";
		}
		List<KeyCode> list = new List<KeyCode>();
		Resolve(target, list);
		if (list.Count != 0)
		{
			return "Key " + string.Join(" + ", list.Select(KeyName).ToArray());
		}
		return target;
	}

	public static string EntryValueText(ConfigEntryBase entry)
	{
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		if (entry == null)
		{
			return string.Empty;
		}
		object boxedValue = entry.BoxedValue;
		if (boxedValue is KeyCode)
		{
			return KeyName((KeyCode)boxedValue);
		}
		if (boxedValue is KeyboardShortcut val)
		{
			if ((int)((KeyboardShortcut)(ref val)).MainKey == 0)
			{
				return "none";
			}
			List<string> list = ((KeyboardShortcut)(ref val)).Modifiers.Select(KeyName).ToList();
			list.Add(KeyName(((KeyboardShortcut)(ref val)).MainKey));
			return string.Join(" + ", list.ToArray());
		}
		return Convert.ToString(boxedValue, CultureInfo.InvariantCulture);
	}

	public unsafe static string KeyName(KeyCode key)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Invalid comparison between Unknown and I4
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Invalid comparison between Unknown and I4
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Invalid comparison between Unknown and I4
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Expected I4, but got Unknown
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Invalid comparison between Unknown and I4
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected I4, but got Unknown
		if ((int)key <= 27)
		{
			if ((int)key == 13)
			{
				return "Enter";
			}
			if ((int)key == 27)
			{
				return "Esc";
			}
		}
		else
		{
			if ((int)key == 96)
			{
				return "`";
			}
			switch (key - 303)
			{
			case 1:
				return "LShift";
			case 0:
				return "RShift";
			case 3:
				return "LCtrl";
			case 2:
				return "RCtrl";
			case 5:
				return "LAlt";
			case 4:
				return "RAlt";
			}
			switch (key - 323)
			{
			case 0:
				return "Left click";
			case 1:
				return "Right click";
			case 2:
				return "Middle click";
			}
		}
		string text = ((object)(*(KeyCode*)(&key))/*cast due to .constrained prefix*/).ToString();
		if (text.StartsWith("Alpha", StringComparison.Ordinal) && text.Length == 6)
		{
			return text.Substring(5);
		}
		return text;
	}

	public static bool TryParseKey(string text, out KeyCode key)
	{
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Expected I4, but got Unknown
		key = (KeyCode)0;
		if (string.IsNullOrEmpty(text))
		{
			return false;
		}
		string text2 = text.Trim();
		if (text2.Length == 1 && char.IsDigit(text2[0]))
		{
			text2 = "Alpha" + text2;
		}
		try
		{
			key = (KeyCode)(int)(KeyCode)Enum.Parse(typeof(KeyCode), text2, ignoreCase: true);
			return true;
		}
		catch
		{
			return false;
		}
	}

	public static bool IsModifierKey(KeyCode key)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Invalid comparison between Unknown and I4
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Invalid comparison between Unknown and I4
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Invalid comparison between Unknown and I4
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Invalid comparison between Unknown and I4
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Invalid comparison between Unknown and I4
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Invalid comparison between Unknown and I4
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Invalid comparison between Unknown and I4
		if ((int)key != 304 && (int)key != 303 && (int)key != 306 && (int)key != 305 && (int)key != 308 && (int)key != 307)
		{
			return (int)key == 313;
		}
		return true;
	}

	public static ConfigEntryBase FindModEntry(string target)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Expected O, but got Unknown
		if (!SplitModTarget(target, out var guid, out var section, out var key))
		{
			return null;
		}
		if (!Chainloader.PluginInfos.TryGetValue(guid, out var value) || value == null || (Object)(object)value.Instance == (Object)null)
		{
			return null;
		}
		if (!((IDictionary<ConfigDefinition, ConfigEntryBase>)value.Instance.Config).TryGetValue(new ConfigDefinition(section, key), out ConfigEntryBase value2))
		{
			return null;
		}
		return value2;
	}

	private static bool SplitModTarget(string target, out string guid, out string section, out string key)
	{
		guid = (section = (key = null));
		string[] array = target.Substring("mod:".Length).Split(new char[1] { '|' });
		if (array.Length < 3)
		{
			return false;
		}
		guid = array[0];
		section = array[1];
		key = string.Join("|", array, 2, array.Length - 2);
		return true;
	}

	private static void AddEntryKeys(ConfigEntryBase entry, List<KeyCode> keys)
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: 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_002a: 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)
		//IL_0032: 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_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		object boxedValue = entry.BoxedValue;
		if (boxedValue is KeyCode val)
		{
			if ((int)val != 0)
			{
				keys.Add(val);
			}
		}
		else
		{
			if (!(boxedValue is KeyboardShortcut val2) || (int)((KeyboardShortcut)(ref val2)).MainKey == 0)
			{
				return;
			}
			foreach (KeyCode modifier in ((KeyboardShortcut)(ref val2)).Modifiers)
			{
				if ((int)modifier != 0 && !keys.Contains(modifier))
				{
					keys.Add(modifier);
				}
			}
			keys.Add(((KeyboardShortcut)(ref val2)).MainKey);
		}
	}

	public static List<ModHotkey> ScanModHotkeys()
	{
		List<ModHotkey> list = new List<ModHotkey>();
		foreach (KeyValuePair<string, PluginInfo> pluginInfo in Chainloader.PluginInfos)
		{
			PluginInfo value = pluginInfo.Value;
			if (value == null || (Object)(object)value.Instance == (Object)null || pluginInfo.Key == "marc.valheim.controllerchords")
			{
				continue;
			}
			ConfigFile config;
			try
			{
				config = value.Instance.Config;
			}
			catch
			{
				continue;
			}
			if (config == null)
			{
				continue;
			}
			foreach (ConfigDefinition item in config.Keys.ToList())
			{
				ConfigEntryBase val = config[item];
				if (val != null && (!(val.SettingType != typeof(KeyCode)) || !(val.SettingType != typeof(KeyboardShortcut))))
				{
					list.Add(new ModHotkey
					{
						Guid = pluginInfo.Key,
						ModName = value.Metadata.Name,
						Section = item.Section,
						Key = item.Key,
						Entry = val
					});
				}
			}
		}
		list.Sort(delegate(ModHotkey a, ModHotkey b)
		{
			int num = string.Compare(a.ModName, b.ModName, StringComparison.OrdinalIgnoreCase);
			if (num != 0)
			{
				return num;
			}
			int num2 = string.Compare(a.Section, b.Section, StringComparison.OrdinalIgnoreCase);
			return (num2 == 0) ? string.Compare(a.Key, b.Key, StringComparison.OrdinalIgnoreCase) : num2;
		});
		return list;
	}
}
internal static class ComboEngine
{
	public static readonly List<ComboSlot> Slots = new List<ComboSlot>();

	public static readonly List<BuiltinCombo> Builtins = new List<BuiltinCombo>();

	public static bool CombosLive;

	public static bool BuiltinsLive;

	private static uint latched;

	private static readonly List<KeyCode> KeyScratch = new List<KeyCode>();

	private static int maskFrame = -1;

	private static bool maskFixed;

	private static uint maskCache;

	private static readonly Dictionary<string, uint> ActionMasks = new Dictionary<string, uint>(StringComparer.Ordinal);

	private static float actionMasksBuiltAt = -100f;

	private static readonly HashSet<string> WarnedTargets = new HashSet<string>();

	public static void Update()
	{
		uint held = Pad.Held;
		uint pressedMask = Pad.PressedMask;
		latched &= held;
		for (int i = 0; i < Slots.Count; i++)
		{
			ComboSlot comboSlot = Slots[i];
			if (!comboSlot.Active)
			{
				continue;
			}
			bool flag = comboSlot.IsLive && CombosLive;
			if (comboSlot.Mode.Value == ComboMode.Hold)
			{
				uint allMask = comboSlot.Combo.AllMask;
				if (!flag || (held & allMask) != allMask)
				{
					Deactivate(comboSlot);
				}
			}
			else if (comboSlot.Mode.Value == ComboMode.Toggle && !comboSlot.IsLive)
			{
				Deactivate(comboSlot);
			}
		}
		if (pressedMask == 0)
		{
			return;
		}
		for (int j = 1; j <= 16; j++)
		{
			PadButton padButton = (PadButton)j;
			if ((pressedMask & Pad.Bit(padButton)) == 0)
			{
				continue;
			}
			BuiltinCombo builtinCombo = (BuiltinsLive ? BestBuiltin(padButton, held) : null);
			ComboSlot comboSlot2 = (CombosLive ? BestSlot(padButton, held) : null);
			if (builtinCombo != null && (comboSlot2 == null || builtinCombo.Combo.ModCount >= comboSlot2.Combo.ModCount))
			{
				latched |= builtinCombo.Combo.AllMask;
				try
				{
					builtinCombo.Run();
				}
				catch (Exception ex)
				{
					ControllerChordsPlugin.Log.LogWarning((object)("Shortcut '" + builtinCombo.Name + "' failed: " + ex.Message));
				}
			}
			else if (comboSlot2 != null)
			{
				latched |= Pad.Bit(padButton);
				if (Settings.BlockHeldButtonsAfterCombo.Value)
				{
					latched |= comboSlot2.Combo.ModMask;
				}
				Fire(comboSlot2);
			}
		}
	}

	private static ComboSlot BestSlot(PadButton trigger, uint held)
	{
		ComboSlot comboSlot = null;
		for (int i = 0; i < Slots.Count; i++)
		{
			ComboSlot comboSlot2 = Slots[i];
			if (comboSlot2.IsLive && comboSlot2.Combo.Trigger == trigger && (held & comboSlot2.Combo.ModMask) == comboSlot2.Combo.ModMask && (comboSlot == null || comboSlot2.Combo.ModCount > comboSlot.Combo.ModCount))
			{
				comboSlot = comboSlot2;
			}
		}
		return comboSlot;
	}

	private static BuiltinCombo BestBuiltin(PadButton trigger, uint held)
	{
		BuiltinCombo builtinCombo = null;
		for (int i = 0; i < Builtins.Count; i++)
		{
			BuiltinCombo builtinCombo2 = Builtins[i];
			if (builtinCombo2.Combo.IsValid && builtinCombo2.Combo.Trigger == trigger && (held & builtinCombo2.Combo.ModMask) == builtinCombo2.Combo.ModMask && (builtinCombo == null || builtinCombo2.Combo.ModCount > builtinCombo.Combo.ModCount))
			{
				builtinCombo = builtinCombo2;
			}
		}
		return builtinCombo;
	}

	private unsafe static void Fire(ComboSlot slot)
	{
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		if (!ComboTargets.Resolve(slot.Action.Value, KeyScratch))
		{
			if (WarnedTargets.Add(slot.Action.Value ?? string.Empty))
			{
				ControllerChordsPlugin.Log.LogWarning((object)(slot.Label + ": nothing to press for '" + slot.Action.Value + "' (mod missing or key set to None)."));
			}
			Ui.Toast(slot.Buttons.Value + ": " + ComboTargets.Describe(slot.Action.Value));
			return;
		}
		switch (slot.Mode.Value)
		{
		case ComboMode.Tap:
		{
			for (int i = 0; i < KeyScratch.Count; i++)
			{
				VirtualKeys.Tap(KeyScratch[i], Settings.TapSeconds.Value);
			}
			break;
		}
		case ComboMode.Toggle:
			if (slot.Active)
			{
				Deactivate(slot);
				if (Settings.ShowToasts.Value)
				{
					Ui.Toast(ShortName(slot) + ": released");
				}
			}
			else
			{
				Activate(slot);
				if (Settings.ShowToasts.Value)
				{
					Ui.Toast(ShortName(slot) + ": held");
				}
			}
			break;
		default:
			if (!slot.Active)
			{
				Activate(slot);
			}
			break;
		}
		if (Settings.LogEvents.Value)
		{
			ControllerChordsPlugin.Log.LogInfo((object)(slot.Label + " " + slot.Buttons.Value + " -> " + string.Join("+", KeyScratch.Select((KeyCode k) => ((object)(*(KeyCode*)(&k))/*cast due to .constrained prefix*/).ToString()).ToArray()) + " (" + slot.Mode.Value.ToString() + ")"));
		}
	}

	private static string ShortName(ComboSlot slot)
	{
		ConfigEntryBase val = ComboTargets.FindModEntry(slot.Action.Value);
		if (val == null)
		{
			return ComboTargets.Describe(slot.Action.Value);
		}
		return val.Definition.Key;
	}

	private static void Activate(ComboSlot slot)
	{
		slot.ActiveKeys = KeyScratch.ToArray();
		for (int i = 0; i < slot.ActiveKeys.Length; i++)
		{
			VirtualKeys.Press(slot.ActiveKeys[i]);
		}
		slot.Active = true;
	}

	public static void Deactivate(ComboSlot slot)
	{
		if (slot.ActiveKeys != null)
		{
			for (int i = 0; i < slot.ActiveKeys.Length; i++)
			{
				VirtualKeys.Release(slot.ActiveKeys[i]);
			}
		}
		slot.ActiveKeys = null;
		slot.Active = false;
	}

	public static void ReleaseHeld(bool includeToggles)
	{
		for (int i = 0; i < Slots.Count; i++)
		{
			ComboSlot comboSlot = Slots[i];
			if (comboSlot.Active && (includeToggles || comboSlot.Mode.Value != ComboMode.Toggle))
			{
				Deactivate(comboSlot);
			}
		}
	}

	public static uint SuppressMask()
	{
		if (!Settings.BlockGameButtons.Value || (!CombosLive && !BuiltinsLive))
		{
			return 0u;
		}
		int frameCount = Time.frameCount;
		bool inFixedTimeStep = Time.inFixedTimeStep;
		if (frameCount == maskFrame && inFixedTimeStep == maskFixed)
		{
			return maskCache;
		}
		maskFrame = frameCount;
		maskFixed = inFixedTimeStep;
		uint num = Pad.ReadLive();
		uint num2 = latched & num;
		if (CombosLive)
		{
			for (int i = 0; i < Slots.Count; i++)
			{
				ComboSlot comboSlot = Slots[i];
				if (comboSlot.IsLive && (num & comboSlot.Combo.ModMask) == comboSlot.Combo.ModMask)
				{
					num2 |= Pad.Bit(comboSlot.Combo.Trigger);
				}
			}
		}
		if (BuiltinsLive)
		{
			for (int j = 0; j < Builtins.Count; j++)
			{
				BuiltinCombo builtinCombo = Builtins[j];
				if (builtinCombo.Combo.IsValid && builtinCombo.Combo.ModCount > 0 && (num & builtinCombo.Combo.ModMask) == builtinCombo.Combo.ModMask)
				{
					num2 |= Pad.Bit(builtinCombo.Combo.Trigger);
				}
			}
		}
		maskCache = num2;
		return num2;
	}

	public static uint ActionMask(string actionName)
	{
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Unknown result type (might be due to invalid IL or missing references)
		if (string.IsNullOrEmpty(actionName))
		{
			return 0u;
		}
		float unscaledTime = Time.unscaledTime;
		if (unscaledTime - actionMasksBuiltAt > 10f)
		{
			ActionMasks.Clear();
			actionMasksBuiltAt = unscaledTime;
		}
		if (ActionMasks.TryGetValue(actionName, out var value))
		{
			return value;
		}
		value = 0u;
		try
		{
			ZInput instance = ZInput.instance;
			ButtonDef val = ((instance != null) ? instance.GetButtonDef(actionName) : null);
			if (val != null && val.ButtonAction != null)
			{
				Enumerator<InputBinding> enumerator = val.ButtonAction.bindings.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						InputBinding current = enumerator.Current;
						value |= PathToMask(((InputBinding)(ref current)).effectivePath);
					}
				}
				finally
				{
					((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
				}
			}
		}
		catch
		{
			value = 0u;
		}
		ActionMasks[actionName] = value;
		return value;
	}

	public static void InvalidateActionMasks()
	{
		ActionMasks.Clear();
	}

	private static uint PathToMask(string path)
	{
		if (string.IsNullOrEmpty(path) || path.IndexOf("<Gamepad>", StringComparison.OrdinalIgnoreCase) < 0)
		{
			return 0u;
		}
		return path.Substring(path.IndexOf('/') + 1).ToLowerInvariant() switch
		{
			"buttonsouth" => Pad.Bit(PadButton.A), 
			"buttoneast" => Pad.Bit(PadButton.B), 
			"buttonwest" => Pad.Bit(PadButton.X), 
			"buttonnorth" => Pad.Bit(PadButton.Y), 
			"leftshoulder" => Pad.Bit(PadButton.LB), 
			"rightshoulder" => Pad.Bit(PadButton.RB), 
			"lefttrigger" => Pad.Bit(PadButton.LT), 
			"righttrigger" => Pad.Bit(PadButton.RT), 
			"select" => Pad.Bit(PadButton.Back), 
			"start" => Pad.Bit(PadButton.Start), 
			"leftstickpress" => Pad.Bit(PadButton.LeftStick), 
			"rightstickpress" => Pad.Bit(PadButton.RightStick), 
			"dpad/up" => Pad.Bit(PadButton.DPadUp), 
			"dpad/down" => Pad.Bit(PadButton.DPadDown), 
			"dpad/left" => Pad.Bit(PadButton.DPadLeft), 
			"dpad/right" => Pad.Bit(PadButton.DPadRight), 
			_ => 0u, 
		};
	}

	public static string FindConflict(ComboSlot self, string buttons)
	{
		ButtonCombo buttonCombo = ButtonCombo.Parse(buttons);
		if (!buttonCombo.IsValid)
		{
			return null;
		}
		for (int i = 0; i < Slots.Count; i++)
		{
			ComboSlot comboSlot = Slots[i];
			if (comboSlot != self && !comboSlot.IsEmpty && comboSlot.Combo.IsValid && comboSlot.Combo.Trigger == buttonCombo.Trigger && comboSlot.Combo.ModMask == buttonCombo.ModMask)
			{
				return comboSlot.Label;
			}
		}
		for (int j = 0; j < Builtins.Count; j++)
		{
			BuiltinCombo builtinCombo = Builtins[j];
			if (builtinCombo.Combo.IsValid && builtinCombo.Combo.Trigger == buttonCombo.Trigger && builtinCombo.Combo.ModMask == buttonCombo.ModMask)
			{
				return builtinCombo.Name;
			}
		}
		return null;
	}
}
internal static class ControllerCursor
{
	private const uint LeftDown = 2u;

	private const uint LeftUp = 4u;

	private const uint RightDown = 8u;

	private const uint RightUp = 16u;

	private const uint Wheel = 2048u;

	public static bool Active;

	public static bool ManualMode;

	private static Vector2 position;

	private static Vector2 lastWarp;

	private static bool positionValid;

	private static bool leftDown;

	private static bool rightDown;

	private static float wheelRemainder;

	private static uint ignoreMask;

	private static readonly Repeater ScrollRepeat = new Repeater();

	public static bool Wanted
	{
		get
		{
			if (!Settings.Enabled.Value || !Pad.Connected || !Application.isFocused)
			{
				return false;
			}
			if (!ManualMode)
			{
				if (Settings.CursorForConfigManager.Value)
				{
					return ConfigManagerBridge.IsOpen;
				}
				return false;
			}
			return true;
		}
	}

	[DllImport("user32.dll")]
	private static extern void mouse_event(uint flags, uint dx, uint dy, int data, UIntPtr extraInfo);

	public static void Update()
	{
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: 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_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0130: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		bool wanted = Wanted;
		if (wanted != Active)
		{
			if (wanted)
			{
				Enter();
			}
			else
			{
				Exit();
			}
		}
		if (!Active)
		{
			return;
		}
		ignoreMask &= Pad.Held;
		if (Pressed(PadButton.B))
		{
			ReleaseButtons();
			if (ConfigManagerBridge.IsOpen)
			{
				ConfigManagerBridge.SetOpen(open: false);
			}
			ManualMode = false;
			return;
		}
		if (Pressed(PadButton.Y))
		{
			ReleaseButtons();
			ComboEditor.Open();
			return;
		}
		if (ManualMode)
		{
			Cursor.visible = true;
			if ((int)Cursor.lockState != 0)
			{
				Cursor.lockState = (CursorLockMode)0;
			}
		}
		Mouse current = Mouse.current;
		if (current == null)
		{
			return;
		}
		Vector2 val = ((InputControl<Vector2>)(object)((Pointer)current).position).ReadValue();
		if (positionValid)
		{
			Vector2 val2 = val - lastWarp;
			if (!(((Vector2)(ref val2)).sqrMagnitude > 64f))
			{
				goto IL_00cd;
			}
		}
		position = val;
		lastWarp = val;
		positionValid = true;
		goto IL_00cd;
		IL_00cd:
		Vector2 leftStick = Pad.LeftStick;
		float magnitude = ((Vector2)(ref leftStick)).magnitude;
		if (magnitude > 0.2f)
		{
			float num = Mathf.Clamp01((magnitude - 0.2f) / 0.8f);
			float num2 = Settings.CursorSpeed.Value * ((float)Screen.height / 1080f) * (0.1f + 0.9f * num * num);
			position += leftStick / magnitude * num2 * Time.unscaledDeltaTime;
			position.x = Mathf.Clamp(position.x, 0f, (float)Screen.width - 1f);
			position.y = Mathf.Clamp(position.y, 0f, (float)Screen.height - 1f);
			current.WarpCursorPosition(position);
			lastWarp = position;
		}
		SetButton(ref leftDown, Held(PadButton.A), 2u, 4u);
		SetButton(ref rightDown, Held(PadButton.X), 8u, 16u);
		float y = Pad.RightStick.y;
		if (Mathf.Abs(y) > 0.2f)
		{
			wheelRemainder += y * Settings.ScrollSpeed.Value * 120f * Time.unscaledDeltaTime;
		}
		int num3 = (Held(PadButton.DPadUp) ? 1 : (Held(PadButton.DPadDown) ? 2 : 0));
		if (ScrollRepeat.Fire(num3, 0.35f, 0.08f))
		{
			wheelRemainder += ((num3 == 1) ? 120f : (-120f));
		}
		if (Mathf.Abs(wheelRemainder) >= 30f)
		{
			int num4 = (int)wheelRemainder;
			wheelRemainder -= num4;
			Send(2048u, num4);
		}
	}

	private static bool Pressed(PadButton button)
	{
		if (Pad.WasPressed(button))
		{
			return (ignoreMask & Pad.Bit(button)) == 0;
		}
		return false;
	}

	private static bool Held(PadButton button)
	{
		if (Pad.IsHeld(button))
		{
			return (ignoreMask & Pad.Bit(button)) == 0;
		}
		return false;
	}

	private static void SetButton(ref bool down, bool want, uint downFlag, uint upFlag)
	{
		if (want != down)
		{
			down = want;
			Send(want ? downFlag : upFlag, 0);
		}
	}

	private static void Send(uint flags, int data)
	{
		if (!Application.isFocused)
		{
			return;
		}
		try
		{
			mouse_event(flags, 0u, 0u, data, UIntPtr.Zero);
		}
		catch (Exception ex)
		{
			ControllerChordsPlugin.Log.LogWarning((object)("Mouse emulation failed: " + ex.Message));
			Settings.CursorForConfigManager.Value = false;
			ManualMode = false;
		}
	}

	private static void Enter()
	{
		Active = true;
		positionValid = false;
		wheelRemainder = 0f;
		ignoreMask = Pad.Held;
		ScrollRepeat.Reset();
		GamepadCapture.Set("cursor", on: true);
	}

	public static void Exit()
	{
		if (Active)
		{
			ReleaseButtons();
			Active = false;
			GamepadCapture.Set("cursor", on: false);
		}
	}

	public static void ReleaseButtons()
	{
		if (leftDown)
		{
			leftDown = false;
			Send(4u, 0);
		}
		if (rightDown)
		{
			rightDown = false;
			Send(16u, 0);
		}
		ignoreMask = Pad.Held;
	}

	public static void Draw()
	{
		//IL_0015: 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_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_0195: Unknown result type (might be due to invalid IL or missing references)
		if (Active)
		{
			Event current = Event.current;
			if (current != null)
			{
				Vector2 mousePosition = current.mousePosition;
				float num = 0.55f + 0.15f * Mathf.Sin(Time.unscaledTime * 6f);
				Ui.Outline(new Rect(mousePosition.x - 15f, mousePosition.y - 15f, 30f, 30f), new Color(Ui.Amber.r, Ui.Amber.g, Ui.Amber.b, num), 3f, 15f);
			}
			string label = (ConfigManagerBridge.IsOpen ? "Close menu" : "Exit cursor");
			float num2 = Ui.LegendWidth(30f, "Move", PadButton.LeftStick) + Ui.LegendWidth(30f, "Click", PadButton.A) + Ui.LegendWidth(30f, "Right-click", PadButton.X) + Ui.LegendWidth(30f, "Scroll", PadButton.RightStick) + Ui.LegendWidth(30f, "Combo editor", PadButton.Y) + Ui.LegendWidth(30f, label, PadButton.B) + 24f;
			Rect rect = default(Rect);
			((Rect)(ref rect))..ctor((Ui.VW - num2) * 0.5f, Ui.VH - 64f, num2, 50f);
			Ui.Fill(rect, new Color(0.06f, 0.055f, 0.05f, 0.9f), 25f);
			Ui.Outline(rect, Ui.PanelEdge, 1.5f, 25f);
			float x = ((Rect)(ref rect)).x + 22f;
			float y = ((Rect)(ref rect)).y + 10f;
			Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(x, y, 30f, "Move", PadButton.LeftStick), y, 30f, "Click", PadButton.A), y, 30f, "Right-click", PadButton.X), y, 30f, "Scroll", PadButton.RightStick), y, 30f, "Combo editor", PadButton.Y), y, 30f, label, PadButton.B);
		}
	}
}
internal static class OnScreenKeyboard
{
	private enum Page
	{
		Letters,
		Symbols,
		Accents,
		Numbers
	}

	private enum KeyKind
	{
		Char,
		Backspace,
		Space,
		Shift,
		ToPage,
		Left,
		Right,
		Done
	}

	private sealed class Key
	{
		public KeyKind Kind;

		public string Lower;

		public string Upper;

		public string Label;

		public float Width = 1f;

		public Page Target;

		public float X;

		public float Center => X + Width * 0.5f;
	}

	private const float Units = 12f;

	private static readonly Key[][] Letters = new Key[5][]
	{
		Row(Chars("1234567890"), Special(KeyKind.Backspace, "Backspace", 2f)),
		Row(Chars("qwertyuiop", "QWERTYUIOP"), Chars("-_")),
		Row(Chars("asdfghjkl'", "ASDFGHJKL'"), Special(KeyKind.Done, "Done", 2f)),
		Row(new Key[1] { Special(KeyKind.Shift, "Shift", 1.5f) }, Chars("zxcvbnm,.", "ZXCVBNM,."), new Key[1] { Char("?", "?", 1.5f) }),
		Row(ToPage("&?!", 2f, Page.Symbols), Special(KeyKind.Space, "Space", 6f), Special(KeyKind.Left, "◄", 1f), Special(KeyKind.Right, "►", 1f), ToPage("123", 2f, Page.Numbers))
	};

	private static readonly Key[][] Symbols;

	private static readonly Key[][] Accents;

	private static readonly Key[][] Numbers;

	public static bool IsOpen;

	private static TextTarget target;

	private static string original;

	private static readonly StringBuilder Buffer;

	private static int caret;

	private static Page page;

	private static int row;

	private static int column;

	private static bool shift;

	private static bool caps;

	private static float lastShiftTime;

	private static float openTime;

	private static float flashUntil;

	private static uint ignoreMask;

	private static readonly Repeater NavRepeat;

	private static readonly Repeater ARepeat;

	private static readonly Repeater XRepeat;

	private static readonly Repeater LbRepeat;

	private static readonly Repeater RbRepeat;

	private static Key[][] Rows => page switch
	{
		Page.Symbols => Symbols, 
		Page.Accents => Accents, 
		Page.Numbers => Numbers, 
		_ => Letters, 
	};

	private static Key Current
	{
		get
		{
			Key[][] rows = Rows;
			row = Mathf.Clamp(row, 0, rows.Length - 1);
			column = Mathf.Clamp(column, 0, rows[row].Length - 1);
			return rows[row][column];
		}
	}

	public static void Open(TextTarget newTarget)
	{
		if (!IsOpen && newTarget != null)
		{
			target = newTarget;
			original = newTarget.InitialText ?? string.Empty;
			Buffer.Length = 0;
			Buffer.Append(original);
			caret = Buffer.Length;
			page = (newTarget.PreferNumbers ? Page.Numbers : Page.Letters);
			row = ((page != Page.Numbers) ? 1 : 2);
			column = 0;
			shift = false;
			caps = false;
			ignoreMask = Pad.Held;
			openTime = Time.unscaledTime;
			NavRepeat.Reset();
			ARepeat.Reset();
			XRepeat.Reset();
			LbRepeat.Reset();
			RbRepeat.Reset();
			IsOpen = true;
			GamepadCapture.Set("keyboard", on: true);
		}
	}

	private static void Close()
	{
		IsOpen = false;
		target = null;
		GamepadCapture.Set("keyboard", on: false);
	}

	public static void CloseQuiet()
	{
		if (IsOpen)
		{
			Close();
		}
	}

	private static void Done()
	{
		TextTarget textTarget = target;
		string text = Buffer.ToString();
		Close();
		TextFocusWatcher.Suppress(textTarget.Identity, 0.75f);
		textTarget.Commit(text);
	}

	private static void Cancel()
	{
		TextTarget textTarget = target;
		Close();
		TextFocusWatcher.Suppress(textTarget.Identity, 0.75f);
		textTarget.Cancel(original);
	}

	private static void Handoff()
	{
		TextTarget textTarget = target;
		string text = Buffer.ToString();
		int num = caret;
		Close();
		TextFocusWatcher.Suppress(textTarget.Identity, 2f);
		textTarget.Handoff(text, num);
	}

	private static bool Pressed(PadButton button)
	{
		if (Pad.WasPressed(button))
		{
			return (ignoreMask & Pad.Bit(button)) == 0;
		}
		return false;
	}

	private static bool Held(PadButton button)
	{
		if (Pad.IsHeld(button))
		{
			return (ignoreMask & Pad.Bit(button)) == 0;
		}
		return false;
	}

	public static void Update()
	{
		if (!IsOpen)
		{
			return;
		}
		if (target == null || !target.IsAlive)
		{
			Close();
			return;
		}
		Keyboard current = Keyboard.current;
		if (current != null && ((ButtonControl)current.anyKey).wasPressedThisFrame && Time.unscaledTime - openTime > 0.2f)
		{
			if (((ButtonControl)current.escapeKey).wasPressedThisFrame)
			{
				Cancel();
			}
			else if (((ButtonControl)current.enterKey).wasPressedThisFrame || ((ButtonControl)current.numpadEnterKey).wasPressedThisFrame)
			{
				Done();
			}
			else
			{
				Handoff();
			}
			return;
		}
		ignoreMask &= Pad.Held;
		if (Pressed(PadButton.Start))
		{
			Done();
			return;
		}
		if (Pressed(PadButton.B))
		{
			Cancel();
			return;
		}
		if (NavRepeat.Fire(Pad.NavDirection(ignoreMask), 0.36f, 0.075f))
		{
			Move(Pad.NavDirection(ignoreMask));
		}
		if (ARepeat.Fire(Held(PadButton.A) ? 1 : 0, 0.45f, 0.06f))
		{
			Key current2 = Current;
			if (Pressed(PadButton.A) || IsRepeatable(current2))
			{
				Activate(current2);
				if (!IsOpen)
				{
					return;
				}
			}
		}
		if (XRepeat.Fire(Held(PadButton.X) ? 1 : 0, 0.4f, 0.05f))
		{
			Backspace();
		}
		if (Pressed(PadButton.Y))
		{
			Insert(" ");
		}
		if (LbRepeat.Fire(Held(PadButton.LB) ? 1 : 0, 0.4f, 0.05f))
		{
			caret = Mathf.Max(0, caret - 1);
			Sync();
		}
		if (RbRepeat.Fire(Held(PadButton.RB) ? 1 : 0, 0.4f, 0.05f))
		{
			caret = Mathf.Min(Buffer.Length, caret + 1);
			Sync();
		}
		if (Pressed(PadButton.LT))
		{
			ToggleShift();
		}
		if (Pressed(PadButton.LeftStick))
		{
			caps = !caps;
			shift = false;
		}
		if (Pressed(PadButton.RT))
		{
			SetPage((page == Page.Letters) ? Page.Symbols : ((page == Page.Symbols) ? Page.Accents : Page.Letters));
		}
		if (Pressed(PadButton.Back))
		{
			SetPage((page != Page.Numbers) ? Page.Numbers : Page.Letters);
		}
	}

	private static bool IsRepeatable(Key key)
	{
		if (key.Kind != KeyKind.Backspace && key.Kind != KeyKind.Left)
		{
			return key.Kind == KeyKind.Right;
		}
		return true;
	}

	private static void Move(int direction)
	{
		Key[][] rows = Rows;
		Key current = Current;
		switch (direction)
		{
		case 1:
		case 2:
		{
			int num = row + ((direction != 1) ? 1 : (-1));
			if (num >= 0 && num < rows.Length)
			{
				row = num;
				column = Nearest(rows[row], current.Center);
			}
			break;
		}
		case 3:
			column = (column - 1 + rows[row].Length) % rows[row].Length;
			break;
		case 4:
			column = (column + 1) % rows[row].Length;
			break;
		}
	}

	private static int Nearest(Key[] keys, float center)
	{
		int result = 0;
		float num = float.MaxValue;
		for (int i = 0; i < keys.Length; i++)
		{
			float num2 = Mathf.Abs(keys[i].Center - center);
			if (num2 < num - 0.01f)
			{
				result = i;
				num = num2;
			}
		}
		return result;
	}

	private static void SetPage(Page next)
	{
		float center = Current.Center;
		page = next;
		Key[][] rows = Rows;
		row = Mathf.Clamp(row, 0, rows.Length - 1);
		column = Nearest(rows[row], center);
	}

	private static void ToggleShift()
	{
		float unscaledTime = Time.unscaledTime;
		if (caps)
		{
			caps = false;
			shift = false;
		}
		else if (shift && unscaledTime - lastShiftTime < 0.4f)
		{
			caps = true;
			shift = false;
		}
		else
		{
			shift = !shift;
		}
		lastShiftTime = unscaledTime;
	}

	private static void Activate(Key key)
	{
		switch (key.Kind)
		{
		case KeyKind.Char:
			Insert((shift ^ caps) ? key.Upper : key.Lower);
			if (shift && char.IsLetter(key.Lower[0]))
			{
				shift = false;
			}
			break;
		case KeyKind.Backspace:
			Backspace();
			break;
		case KeyKind.Space:
			Insert(" ");
			break;
		case KeyKind.Shift:
			ToggleShift();
			break;
		case KeyKind.ToPage:
			SetPage(key.Target);
			break;
		case KeyKind.Left:
			caret = Mathf.Max(0, caret - 1);
			Sync();
			break;
		case KeyKind.Right:
			caret = Mathf.Min(Buffer.Length, caret + 1);
			Sync();
			break;
		case KeyKind.Done:
			Done();
			break;
		}
	}

	private static void Insert(string text)
	{
		bool flag = false;
		for (int i = 0; i < text.Length; i++)
		{
			int maxLength = target.MaxLength;
			if (maxLength > 0 && Buffer.Length >= maxLength)
			{
				Flash();
				break;
			}
			char c = target.Validate(Buffer.ToString(), caret, text[i]);
			if (c == '\0')
			{
				Flash();
				continue;
			}
			Buffer.Insert(caret, c);
			caret++;
			flag = true;
		}
		if (flag)
		{
			Sync();
		}
	}

	private static void Backspace()
	{
		if (caret > 0 && Buffer.Length != 0)
		{
			Buffer.Remove(caret - 1, 1);
			caret--;
			Sync();
		}
	}

	private static void Flash()
	{
		flashUntil = Time.unscaledTime + 0.25f;
	}

	private static void Sync()
	{
		if (target != null)
		{
			target.Live(Buffer.ToString(), caret);
		}
	}

	public static void Draw()
	{
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0218: Unknown result type (might be due to invalid IL or missing references)
		//IL_021d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0232: Unknown result type (might be due to invalid IL or missing references)
		//IL_0241: Unknown result type (might be due to invalid IL or missing references)
		//IL_024d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0246: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e3: Unknown result type (might be due to invalid IL or missing references)
		if (!IsOpen || target == null)
		{
			return;
		}
		float unscaledTime = Time.unscaledTime;
		float num = Mathf.Clamp01((unscaledTime - openTime) / 0.14f);
		float num2 = (Ui.Alpha = 1f - (1f - num) * (1f - num) * (1f - num));
		Ui.Fill(new Rect(0f, 0f, Ui.VW, Ui.VH), new Color(0f, 0f, 0f, 0.38f));
		Key[][] rows = Rows;
		float num3 = Mathf.Min(Ui.VW - 60f, 1240f);
		float num4 = 118f;
		float num5 = 52f;
		float num6 = 22f + num4 + (float)rows.Length * 72f + num5 + 22f;
		float num7 = (Ui.VW - num3) * 0.5f;
		float num8 = Ui.VH - num6 - 28f + (1f - num2) * 60f;
		Ui.Panel(new Rect(num7, num8, num3, num6));
		float num9 = num7 + 22f;
		float num10 = num3 - 44f;
		Ui.Text(text: target.Title, rect: new Rect(num9 + 4f, num8 + 22f - 2f, num10 - 160f, 30f), size: 22, color: Ui.Amber, anchor: (TextAnchor)3, bold: true);
		int maxLength = target.MaxLength;
		string text = ((maxLength > 0) ? (Buffer.Length + " / " + maxLength) : Buffer.Length.ToString());
		bool flag = unscaledTime < flashUntil;
		Ui.Text(new Rect(num7 + num3 - 22f - 160f, num8 + 22f - 2f, 156f, 30f), text, 20, flag ? Ui.Warn : Ui.TextDim, (TextAnchor)5);
		Rect val = new Rect(num9, num8 + 22f + 34f, num10, 66f);
		Ui.Fill(val, new Color(0.02f, 0.02f, 0.02f, 1f), 10f);
		Ui.Outline(val, flag ? Ui.Warn : Ui.Amber, 2f, 10f);
		DrawTextLine(val);
		float num11 = num10 / 12f;
		float num12 = num8 + 22f + num4;
		Key current = Current;
		for (int i = 0; i < rows.Length; i++)
		{
			Key[] array = rows[i];
			float num13 = num12 + (float)i * 72f;
			for (int j = 0; j < array.Length; j++)
			{
				Key key = array[j];
				Rect rect = new Rect(num9 + key.X * num11 + 4f, num13, key.Width * num11 - 8f, 64f);
				bool selected = key == current;
				DrawKey(rect, key, selected);
				if (Ui.Clicked(rect))
				{
					row = i;
					column = j;
					Activate(key);
					if (!IsOpen)
					{
						Ui.Alpha = 1f;
						return;
					}
				}
			}
		}
		float y = num8 + num6 - 22f - 36f;
		Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(Ui.Legend(num9 + 4f, y, 28f, "Type", PadButton.A), y, 28f, "Delete", PadButton.X), y, 28f, "Space", PadButton.Y), y, 28f, "Cursor", PadButton.LB, PadButton.RB), y, 28f, caps ? "Caps on" : "Shift", PadButton.LT), y, 28f, "Symbols", PadButton.RT), y, 28f, "123", PadButton.Back);
		float num14 = num7 + num3 - 22f;
		float num15 = Ui.LegendWidth(28f, "Cancel", PadButton.B);
		float num16 = Ui.LegendWidth(28f, "Done", PadButton.Start);
		Ui.Legend(num14 - num15 - num16 + 18f, y, 28f, "Done", PadButton.Start);
		Ui.Legend(num14 - num15 + 18f, y, 28f, "Cancel", PadButton.B);
		Ui.Alpha = 1f;
	}

	private static void DrawTextLine(Rect box)
	{
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		string text = Buffer.ToString();
		if (target.Password)
		{
			text = new string('•', text.Length);
		}
		float num = 16f;
		float num2 = ((Rect)(ref box)).width - num * 2f;
		float num3 = Ui.Measure(text.Substring(0, Mathf.Clamp(caret, 0, text.Length)).Replace(' ', '\u00a0'), 32);
		float num4 = Mathf.Max(0f, num3 - num2 + 12f);
		GUI.BeginGroup(new Rect(((Rect)(ref box)).x + num, ((Rect)(ref box)).y, num2, ((Rect)(ref box)).height));
		if (text.Length == 0)
		{
			Ui.Text(new Rect(0f, 0f, num2, ((Rect)(ref box)).height), "Start typing…", 28, new Color(1f, 1f, 1f, 0.25f), (TextAnchor)3);
		}
		else
		{
			Ui.Text(new Rect(0f - num4, 0f, Mathf.Max(num2, num3 + 4000f), ((Rect)(ref box)).height), text.Replace(' ', '\u00a0'), 32, Ui.TextLight, (TextAnchor)3);
		}
		if ((Time.unscaledTime - openTime) % 1f < 0.6f)
		{
			Ui.Fill(new Rect(num3 - num4, 14f, 3f, ((Rect)(ref box)).height - 28f), Ui.Amber);
		}
		GUI.EndGroup();
	}

	private static void DrawKey(Rect rect, Key key, bool selected)
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: 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_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: 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_0043: 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_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		Color color;
		switch (key.Kind)
		{
		case KeyKind.Char:
		case KeyKind.Space:
			color = Ui.KeyFill;
			break;
		case KeyKind.Done:
			color = Ui.KeyGreen;
			break;
		default:
			color = Ui.KeySpecial;
			break;
		}
		if (key.Kind == KeyKind.Shift && caps)
		{
			color = Ui.AmberDark;
		}
		Rect rect2 = rect;
		if (selected)
		{
			((Rect)(ref rect2))..ctor(((Rect)(ref rect)).x - 3f, ((Rect)(ref rect)).y - 3f, ((Rect)(ref rect)).width + 6f, ((Rect)(ref rect)).height + 6f);
			color = Ui.Amber;
		}
		Ui.Fill(rect2, color, 9f);
		if (!selected)
		{
			Ui.Outline(rect2, new Color(1f, 1f, 1f, 0.07f), 1f, 9f);
		}
		if (key.Kind == KeyKind.Shift && shift && !selected)
		{
			Ui.Outline(rect2, Ui.Amber, 2f, 9f);
		}
		string text;
		int size;
		if (key.Kind == KeyKind.Char)
		{
			text = ((shift ^ caps) ? key.Upper : key.Lower);
			size = 30;
		}
		else
		{
			text = key.Label;
			size = ((key.Label.Length > 3) ? 21 : 26);
		}
		Ui.Text(rect2, text, size, selected ? Ui.TextDark : Ui.TextLight, (TextAnchor)4, bold: true);
	}

	private static Key Char(string lower, string upper, float width)
	{
		return new Key
		{
			Kind = KeyKind.Char,
			Lower = lower,
			Upper = upper,
			Label = lower,
			Width = width
		};
	}

	private static Key[] Chars(string lower, string upper = null)
	{
		Key[] array = new Key[lower.Length];
		for (int i = 0; i < lower.Length; i++)
		{
			string text = lower[i].ToString();
			string upper2 = ((upper != null && i < upper.Length) ? upper[i].ToString() : text);
			array[i] = Char(text, upper2, 1f);
		}
		return array;
	}

	private static Key Special(KeyKind kind, string label, float width)
	{
		return new Key
		{
			Kind = kind,
			Label = label,
			Width = width
		};
	}

	private static Key ToPage(string label, float width, Page target)
	{
		return new Key
		{
			Kind = KeyKind.ToPage,
			Label = label,
			Width = width,
			Target = target
		};
	}

	private static Key[] Row(params object[] parts)
	{
		List<Key> list = new List<Key>();
		for (int i = 0; i < parts.Length; i++)
		{
			if (parts[i] is Key item)
			{
				list.Add(item);
			}
			else if (parts[i] is Key[] collection)
			{
				list.AddRange(collection);
			}
		}
		float num = 0f;
		for (int j = 0; j < list.Count; j++)
		{
			list[j].X = num;
			num += list[j].Width;
		}
		return list.ToArray();
	}

	static OnScreenKeyboard()
	{
		Key[][] obj = new Key[5][]
		{
			Row(Chars("1234567890"), Special(KeyKind.Backspace, "Backspace", 2f)),
			null,
			null,
			null,
			null
		};
		object[] parts = Chars("!@#$%^&*()+=");
		obj[1] = Row(parts);
		obj[2] = Row(Chars("~`|\\/:;\"'<"), Special(KeyKind.Done, "Done", 2f));
		parts = Chars(">{}[]_-,.?€£");
		obj[3]