Decompiled source of MenuPresets v1.0.0

plugins/MenuPresets.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnboundLib;
using UnboundLib.Utils;
using UnboundLib.Utils.UI;
using UnityEngine;
using UnityEngine.Events;
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")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MenuPresets
{
	[BepInPlugin("com.mrnoa.rounds.menupresets", "MenuPresets", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Rounds.exe")]
	public class MenuPresetsPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.mrnoa.rounds.menupresets";

		public const string Name = "MenuPresets";

		public const string Version = "1.0.0";

		internal static MenuPresetsPlugin Instance;

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> SlotCount;

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			SlotCount = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SlotCount", 5, new ConfigDescription("Number of preset slots in each menu (restart the game after changing).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 10), Array.Empty<object>()));
			Unbound.RegisterClientSideMod("com.mrnoa.rounds.menupresets");
			GameObject val = new GameObject("MenuPresets");
			Object.DontDestroyOnLoad((Object)val);
			val.AddComponent<PresetController>();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"MenuPresets 1.0.0 loaded.");
		}

		internal ConfigEntry<string> NameEntry(string kind, int slot)
		{
			return ((BaseUnityPlugin)this).Config.Bind<string>(kind + " Presets", $"Slot{slot}Name", "", $"Name of {kind.ToLower()} preset {slot}.");
		}

		internal ConfigEntry<string> DataEntry(string kind, int slot)
		{
			return ((BaseUnityPlugin)this).Config.Bind<string>(kind + " Presets", $"Slot{slot}Data", "", $"Saved selection for {kind.ToLower()} preset {slot}. Written by the game; not meant to be edited by hand.");
		}
	}
	internal class PresetController : MonoBehaviour
	{
		private const string CategoryKey = "#category:";

		private PresetPanel cards;

		private PresetPanel maps;

		private float nextTry;

		private int errors;

		private void Update()
		{
			cards?.Tick();
			maps?.Tick();
			if (Time.unscaledTime < nextTry)
			{
				return;
			}
			nextTry = Time.unscaledTime + 0.15f;
			try
			{
				if (cards == null || !cards.Alive)
				{
					cards = BuildCards();
				}
			}
			catch (Exception ex)
			{
				if (errors++ < 3)
				{
					MenuPresetsPlugin.Log.LogWarning((object)("Card preset panel could not be built: " + ex));
				}
			}
			try
			{
				if (maps == null || !maps.Alive)
				{
					maps = BuildMaps();
				}
			}
			catch (Exception ex2)
			{
				if (errors++ < 3)
				{
					MenuPresetsPlugin.Log.LogWarning((object)("Map preset panel could not be built: " + ex2));
				}
			}
		}

		private PresetPanel BuildCards()
		{
			ToggleCardsMenuHandler instance = ToggleCardsMenuHandler.instance;
			GameObject cardMenuCanvas = ToggleCardsMenuHandler.cardMenuCanvas;
			if ((Object)(object)instance == (Object)null || (Object)(object)cardMenuCanvas == (Object)null || !cardMenuCanvas.activeInHierarchy)
			{
				return null;
			}
			if (CardManager.cards == null || CardManager.cards.Count == 0)
			{
				return null;
			}
			Transform content = cardMenuCanvas.transform.Find("CardMenu/Top/Categories/ButtonsScroll/Viewport/Content");
			Transform val = cardMenuCanvas.transform.Find("CardMenu/Top/CardAmount");
			if ((Object)(object)content == (Object)null || (Object)(object)val == (Object)null || content.childCount == 0)
			{
				return null;
			}
			object? obj = AccessTools.Field(typeof(ToggleCardsMenuHandler), "categoryButtonAsset")?.GetValue(instance);
			GameObject val2 = (GameObject)((obj is GameObject) ? obj : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return null;
			}
			Transform inputTemplate = cardMenuCanvas.transform.Find("CardMenu/Top/InputField");
			return new PresetPanel("Cards", cardMenuCanvas.transform, val, content, val2, inputTemplate, () => CategoryStates(content).Concat(CardManager.cards.Select((KeyValuePair<string, Card> kv) => new KeyValuePair<string, bool>(kv.Key, kv.Value != null && kv.Value.enabled))), delegate(Func<string, bool> want)
			{
				ApplyCards(content, want);
			}, delegate
			{
				if (ToggleCardsMenuHandler.menuOpenFromOutside)
				{
					return "NOT AVAILABLE HERE";
				}
				object obj2 = AccessTools.Field(typeof(ToggleCardsMenuHandler), "disabled")?.GetValue(null);
				bool flag = default(bool);
				int num;
				if (obj2 is bool)
				{
					flag = (bool)obj2;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (((uint)num & (flag ? 1u : 0u)) == 0) ? null : "HOST ONLY";
			}, delegate(bool open)
			{
				ToggleCardsMenuHandler.disableEscapeButton = open;
			});
		}

		private static void ApplyCards(Transform content, Func<string, bool> want)
		{
			ConfigFile val = UnboundConfig();
			bool saveOnConfigSet = val != null && val.SaveOnConfigSet;
			if (val != null)
			{
				val.SaveOnConfigSet = false;
			}
			try
			{
				ApplyCategories(content, want);
				foreach (KeyValuePair<string, Card> item in CardManager.cards.ToList())
				{
					Card value = item.Value;
					if (value == null || (Object)(object)value.cardInfo == (Object)null)
					{
						continue;
					}
					bool flag = want(item.Key);
					if (value.enabled != flag)
					{
						if (flag)
						{
							CardManager.EnableCard(value.cardInfo, true);
						}
						else
						{
							CardManager.DisableCard(value.cardInfo, true);
						}
					}
				}
			}
			finally
			{
				if (val != null)
				{
					val.SaveOnConfigSet = saveOnConfigSet;
					val.Save();
				}
			}
			AccessTools.Method(typeof(ToggleCardsMenuHandler), "RestoreCardToggleVisuals", Type.EmptyTypes, (Type[])null)?.Invoke(null, null);
		}

		private PresetPanel BuildMaps()
		{
			ToggleLevelMenuHandler handler = ToggleLevelMenuHandler.instance;
			if ((Object)(object)handler == (Object)null || (Object)(object)handler.mapMenuCanvas == (Object)null || !handler.mapMenuCanvas.activeInHierarchy)
			{
				return null;
			}
			if (LevelManager.levels == null || LevelManager.levels.Count == 0)
			{
				return null;
			}
			Transform transform = handler.mapMenuCanvas.transform;
			Transform content = transform.Find("MapMenu/Top/Categories/ButtonsScroll/Viewport/Content");
			Transform val = transform.Find("MapMenu/Top/MapAmount");
			if ((Object)(object)content == (Object)null || (Object)(object)val == (Object)null || content.childCount == 0)
			{
				return null;
			}
			object? obj = AccessTools.Field(typeof(ToggleLevelMenuHandler), "categoryButton")?.GetValue(handler);
			GameObject val2 = (GameObject)((obj is GameObject) ? obj : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return null;
			}
			Transform inputTemplate = transform.Find("MapMenu/Top/InputField");
			return new PresetPanel("Maps", transform, val, content, val2, inputTemplate, () => CategoryStates(content).Concat(LevelManager.levels.Select((KeyValuePair<string, Level> kv) => new KeyValuePair<string, bool>(kv.Key, kv.Value != null && kv.Value.enabled))), delegate(Func<string, bool> want)
			{
				ApplyMaps(handler, content, want);
			}, delegate
			{
				object obj2 = AccessTools.Field(typeof(ToggleLevelMenuHandler), "disabled")?.GetValue(handler);
				bool flag = default(bool);
				int num;
				if (obj2 is bool)
				{
					flag = (bool)obj2;
					num = 1;
				}
				else
				{
					num = 0;
				}
				return (((uint)num & (flag ? 1u : 0u)) == 0) ? null : "HOST ONLY";
			}, null);
		}

		private static void ApplyMaps(ToggleLevelMenuHandler handler, Transform content, Func<string, bool> want)
		{
			ConfigFile val = UnboundConfig();
			bool saveOnConfigSet = val != null && val.SaveOnConfigSet;
			if (val != null)
			{
				val.SaveOnConfigSet = false;
			}
			try
			{
				ApplyCategories(content, want);
				foreach (KeyValuePair<string, Level> item in LevelManager.levels.ToList())
				{
					if (item.Value == null)
					{
						continue;
					}
					bool flag = want(item.Key);
					if (item.Value.enabled != flag)
					{
						if (flag)
						{
							LevelManager.EnableLevel(item.Key, true);
						}
						else
						{
							LevelManager.DisableLevel(item.Key, true);
						}
					}
				}
			}
			finally
			{
				if (val != null)
				{
					val.SaveOnConfigSet = saveOnConfigSet;
					val.Save();
				}
			}
			if (handler.lvlObjs == null)
			{
				return;
			}
			foreach (GameObject lvlObj in handler.lvlObjs)
			{
				if ((Object)(object)lvlObj != (Object)null)
				{
					ToggleLevelMenuHandler.UpdateVisualsLevelObj(lvlObj);
				}
			}
		}

		private static IEnumerable<KeyValuePair<string, bool>> CategoryStates(Transform content)
		{
			foreach (Transform item in content)
			{
				Transform val = item;
				Toggle componentInChildren = ((Component)val).GetComponentInChildren<Toggle>(true);
				if ((Object)(object)componentInChildren != (Object)null)
				{
					yield return new KeyValuePair<string, bool>("#category:" + ((Object)val).name, componentInChildren.isOn);
				}
			}
		}

		private static void ApplyCategories(Transform content, Func<string, bool> want)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			foreach (Transform item in content)
			{
				Transform val = item;
				Toggle componentInChildren = ((Component)val).GetComponentInChildren<Toggle>(true);
				if (!((Object)(object)componentInChildren == (Object)null))
				{
					bool flag = want("#category:" + ((Object)val).name);
					if (componentInChildren.isOn != flag)
					{
						componentInChildren.isOn = flag;
					}
				}
			}
		}

		private static ConfigFile UnboundConfig()
		{
			foreach (KeyValuePair<string, Card> card in CardManager.cards)
			{
				if (card.Value != null && card.Value.config != null)
				{
					return ((ConfigEntryBase)card.Value.config).ConfigFile;
				}
			}
			return null;
		}
	}
	internal class PresetPanel
	{
		private enum Mode
		{
			None,
			Save,
			Rename,
			Delete
		}

		private readonly string kind;

		private readonly Transform canvasRoot;

		private readonly GameObject asset;

		private readonly Transform inputTemplate;

		private readonly Func<IEnumerable<KeyValuePair<string, bool>>> getStates;

		private readonly Action<Func<string, bool>> apply;

		private readonly Func<string> lockReason;

		private readonly Action<bool> onPopup;

		private readonly int slots;

		private readonly float rowW;

		private readonly float rowH;

		private readonly GameObject root;

		private readonly List<TextMeshProUGUI> slotTexts = new List<TextMeshProUGUI>();

		private readonly TextMeshProUGUI headerText;

		private readonly TextMeshProUGUI saveText;

		private readonly TextMeshProUGUI renameText;

		private readonly TextMeshProUGUI deleteText;

		private Mode mode;

		private string flash;

		private float flashUntil;

		private GameObject popup;

		private TMP_InputField popupField;

		private int focusFrames;

		public bool Alive => (Object)(object)root != (Object)null;

		public PresetPanel(string kind, Transform canvasRoot, Transform block, Transform content, GameObject asset, Transform inputTemplate, Func<IEnumerable<KeyValuePair<string, bool>>> getStates, Action<Func<string, bool>> apply, Func<string> lockReason, Action<bool> onPopup)
		{
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: 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_00f3: 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_0134: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Expected O, but got Unknown
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: 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)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: 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_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fd: Expected O, but got Unknown
			//IL_031e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0323: Unknown result type (might be due to invalid IL or missing references)
			//IL_032b: 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_0332: Unknown result type (might be due to invalid IL or missing references)
			//IL_0334: Unknown result type (might be due to invalid IL or missing references)
			//IL_0339: Unknown result type (might be due to invalid IL or missing references)
			//IL_033a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0341: Unknown result type (might be due to invalid IL or missing references)
			//IL_0348: Unknown result type (might be due to invalid IL or missing references)
			//IL_0353: Unknown result type (might be due to invalid IL or missing references)
			//IL_035d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0360: Unknown result type (might be due to invalid IL or missing references)
			//IL_036c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0379: Unknown result type (might be due to invalid IL or missing references)
			//IL_0383: Unknown result type (might be due to invalid IL or missing references)
			//IL_0388: Unknown result type (might be due to invalid IL or missing references)
			//IL_0394: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0200: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_055e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0572: Unknown result type (might be due to invalid IL or missing references)
			//IL_0586: Unknown result type (might be due to invalid IL or missing references)
			//IL_059a: Unknown result type (might be due to invalid IL or missing references)
			//IL_05b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_05bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_05cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_05d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_05fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0611: Unknown result type (might be due to invalid IL or missing references)
			this.kind = kind;
			this.canvasRoot = canvasRoot;
			this.asset = asset;
			this.inputTemplate = inputTemplate;
			this.getStates = getStates;
			this.apply = apply;
			this.lockReason = lockReason;
			this.onPopup = onPopup;
			slots = Mathf.Clamp(MenuPresetsPlugin.SlotCount.Value, 1, 10);
			Transform transform = asset.transform;
			RectTransform val = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			RectTransform val2 = (RectTransform)((content.childCount > 0) ? /*isinst with value type is only supported in some contexts*/: null);
			float[] array = new float[4];
			float num;
			Rect rect;
			if (!((Object)(object)val2 != (Object)null))
			{
				num = 0f;
			}
			else
			{
				rect = val2.rect;
				num = ((Rect)(ref rect)).width;
			}
			array[0] = num;
			Transform obj = ((content is RectTransform) ? content : null);
			float num2;
			if (obj == null)
			{
				num2 = 0f;
			}
			else
			{
				rect = ((RectTransform)obj).rect;
				num2 = ((Rect)(ref rect)).width;
			}
			array[1] = num2;
			array[2] = (((Object)(object)val != (Object)null) ? val.sizeDelta.x : 0f);
			array[3] = 220f;
			rowW = FirstAbove(1f, array);
			float[] array2 = new float[3];
			float num3;
			if (!((Object)(object)val2 != (Object)null))
			{
				num3 = 0f;
			}
			else
			{
				rect = val2.rect;
				num3 = ((Rect)(ref rect)).height;
			}
			array2[0] = num3;
			array2[1] = (((Object)(object)val != (Object)null) ? val.sizeDelta.y : 0f);
			array2[2] = 40f;
			rowH = FirstAbove(1f, array2);
			RectTransform val3 = (RectTransform)block.parent;
			Bounds val4 = RectTransformUtility.CalculateRelativeRectTransformBounds((Transform)(object)val3, block);
			Slider[] componentsInChildren = ((Component)val3).GetComponentsInChildren<Slider>(false);
			foreach (Slider val5 in componentsInChildren)
			{
				if (!((Component)val5).transform.IsChildOf(block))
				{
					Bounds val6 = RectTransformUtility.CalculateRelativeRectTransformBounds((Transform)(object)val3, ((Component)val5).transform);
					bool num4 = ((Bounds)(ref val6)).min.x < ((Bounds)(ref val4)).max.x && ((Bounds)(ref val6)).max.x > ((Bounds)(ref val4)).min.x;
					bool flag = ((Bounds)(ref val6)).min.y < ((Bounds)(ref val4)).max.y + rowH * 3f && ((Bounds)(ref val6)).max.y > ((Bounds)(ref val4)).min.y - rowH * 3f;
					if (num4 && flag)
					{
						((Bounds)(ref val4)).Encapsulate(val6);
					}
				}
			}
			VerticalLayoutGroup component = ((Component)content).GetComponent<VerticalLayoutGroup>();
			float num5 = (((Object)(object)component != (Object)null) ? ((HorizontalOrVerticalLayoutGroup)component).spacing : (rowH * 0.1f));
			int num6 = slots + 4;
			float num7 = rowH * 0.35f;
			float num8 = Mathf.Max(((Bounds)(ref val4)).size.x, rowW);
			float num9 = (float)num6 * rowH + (float)(num6 - 1) * num5;
			root = new GameObject(kind + "PresetPanel", new Type[1] { typeof(RectTransform) });
			root.layer = ((Component)block).gameObject.layer;
			RectTransform val7 = (RectTransform)root.transform;
			((Transform)val7).SetParent((Transform)(object)val3, false);
			((Transform)val7).SetAsLastSibling();
			val7.anchorMin = (val7.anchorMax = val3.pivot);
			val7.pivot = new Vector2(0.5f, 1f);
			val7.anchoredPosition = new Vector2(((Bounds)(ref val4)).center.x, ((Bounds)(ref val4)).min.y - num7);
			val7.sizeDelta = new Vector2(num8, num9);
			float num10 = ((Bounds)(ref val4)).min.y - num7;
			rect = val3.rect;
			float num11 = num10 - ((Rect)(ref rect)).yMin - num7;
			float num12 = ((num11 > rowH * 3f && num9 > num11) ? Mathf.Clamp(num11 / num9, 0.5f, 1f) : 1f);
			((Transform)val7).localScale = new Vector3(num12, num12, 1f);
			VerticalLayoutGroup obj2 = root.AddComponent<VerticalLayoutGroup>();
			((HorizontalOrVerticalLayoutGroup)obj2).spacing = num5;
			((LayoutGroup)obj2).childAlignment = (TextAnchor)1;
			((HorizontalOrVerticalLayoutGroup)obj2).childControlWidth = true;
			((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandWidth = true;
			((HorizontalOrVerticalLayoutGroup)obj2).childControlHeight = true;
			((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandHeight = false;
			Row(root.transform, "PresetsHeader", out headerText, null);
			for (int j = 1; j <= slots; j++)
			{
				int slot = j;
				Row(root.transform, "PresetSlot" + j, out var label, delegate
				{
					SlotClicked(slot);
				});
				slotTexts.Add(label);
			}
			Row(root.transform, "PresetSave", out saveText, delegate
			{
				Arm(Mode.Save);
			});
			Row(root.transform, "PresetRename", out renameText, delegate
			{
				Arm(Mode.Rename);
			});
			Row(root.transform, "PresetDelete", out deleteText, delegate
			{
				Arm(Mode.Delete);
			});
			Refresh();
			ManualLogSource log = MenuPresetsPlugin.Log;
			object[] obj3 = new object[16]
			{
				kind,
				((Object)block).name,
				((Bounds)(ref val4)).min.x,
				((Bounds)(ref val4)).max.x,
				((Bounds)(ref val4)).min.y,
				((Bounds)(ref val4)).max.y,
				((Object)val3).name,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null,
				null
			};
			rect = val3.rect;
			obj3[7] = ((Rect)(ref rect)).yMin;
			rect = val3.rect;
			obj3[8] = ((Rect)(ref rect)).yMax;
			obj3[9] = num8;
			obj3[10] = num9;
			obj3[11] = ((Bounds)(ref val4)).center.x;
			obj3[12] = ((Bounds)(ref val4)).min.y - num7;
			obj3[13] = num12;
			obj3[14] = rowW;
			obj3[15] = rowH;
			log.LogInfo((object)string.Format("{0} preset panel added under '{1}': block x {2:F0}..{3:F0}, y {4:F0}..{5:F0}; parent '{6}' y {7:F0}..{8:F0}; panel {9:F0} x {10:F0} at ({11:F0}, {12:F0}), scale {13:F2}; button {14:F0} x {15:F0}.", obj3));
		}

		private static float FirstAbove(float min, params float[] values)
		{
			foreach (float num in values)
			{
				if (num > min)
				{
					return num;
				}
			}
			return values[^1];
		}

		private GameObject Row(Transform parent, string name, out TextMeshProUGUI label, Action onClick)
		{
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Expected O, but got Unknown
			GameObject val = Object.Instantiate<GameObject>(asset, parent);
			((Object)val).name = name;
			val.SetActive(true);
			label = val.GetComponentInChildren<TextMeshProUGUI>(true);
			Toggle[] componentsInChildren = val.GetComponentsInChildren<Toggle>(true);
			foreach (Toggle val2 in componentsInChildren)
			{
				if ((Object)(object)label != (Object)null && ((TMP_Text)label).transform.IsChildOf(((Component)val2).transform))
				{
					((Selectable)val2).interactable = false;
					if ((Object)(object)val2.graphic != (Object)null)
					{
						((Behaviour)val2.graphic).enabled = false;
					}
					if ((Object)(object)((Selectable)val2).targetGraphic != (Object)null)
					{
						((Behaviour)((Selectable)val2).targetGraphic).enabled = false;
					}
				}
				else
				{
					((Component)val2).gameObject.SetActive(false);
				}
			}
			LayoutElement val3 = val.GetComponent<LayoutElement>() ?? val.AddComponent<LayoutElement>();
			if (val3.preferredHeight <= 0f)
			{
				val3.preferredHeight = rowH;
			}
			if (val3.minHeight <= 0f)
			{
				val3.minHeight = rowH;
			}
			Button val4 = val.GetComponent<Button>() ?? val.GetComponentInChildren<Button>(true);
			if ((Object)(object)val4 != (Object)null && onClick != null)
			{
				((UnityEvent)val4.onClick).AddListener((UnityAction)delegate
				{
					try
					{
						onClick();
					}
					catch (Exception arg)
					{
						MenuPresetsPlugin.Log.LogWarning((object)$"{kind} preset action failed: {arg}");
						Flash("FAILED - SEE LOG");
					}
				});
			}
			return val;
		}

		private ConfigEntry<string> NameOf(int slot)
		{
			return MenuPresetsPlugin.Instance.NameEntry(kind, slot);
		}

		private ConfigEntry<string> DataOf(int slot)
		{
			return MenuPresetsPlugin.Instance.DataEntry(kind, slot);
		}

		private void Arm(Mode m)
		{
			mode = ((mode != m) ? m : Mode.None);
			Refresh();
		}

		private void SlotClicked(int slot)
		{
			Mode mode = this.mode;
			this.mode = Mode.None;
			bool flag = DataOf(slot).Value.Length > 0;
			switch (mode)
			{
			case Mode.Save:
				AskNameThenSave(slot);
				break;
			case Mode.Rename:
				AskName("RENAME PRESET " + slot, NameOf(slot).Value, delegate(string text)
				{
					if (text.Length > 0)
					{
						NameOf(slot).Value = text;
						Flash("RENAMED");
					}
				});
				break;
			case Mode.Delete:
				DataOf(slot).Value = "";
				NameOf(slot).Value = "";
				Flash($"SLOT {slot} DELETED");
				break;
			default:
				if (flag)
				{
					Load(slot);
				}
				else
				{
					AskNameThenSave(slot);
				}
				break;
			}
			Refresh();
		}

		private void AskNameThenSave(int slot)
		{
			AskName("NAME PRESET " + slot, NameOf(slot).Value, delegate(string text)
			{
				List<string> list = new List<string>();
				List<string> list2 = new List<string>();
				foreach (KeyValuePair<string, bool> item in getStates())
				{
					(item.Value ? list : list2).Add(item.Key);
				}
				bool flag = list.Count <= list2.Count;
				DataOf(slot).Value = (flag ? "E|" : "D|") + string.Join("|", (flag ? list : list2).Select((string k) => k.Replace("|", "%7C")));
				NameOf(slot).Value = ((text.Length > 0) ? text : ("PRESET " + slot));
				Flash("SAVED");
			});
		}

		private void Load(int slot)
		{
			string text = lockReason();
			if (text != null)
			{
				Flash(text);
				return;
			}
			string value = DataOf(slot).Value;
			if (value.Length >= 2)
			{
				bool listedMeansOn = value[0] == 'E';
				HashSet<string> listed = new HashSet<string>(from k in value.Substring(2).Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries)
					select k.Replace("%7C", "|"));
				apply((string key) => listed.Contains(key) == listedMeansOn);
				Flash("LOADED");
			}
		}

		private void AskName(string title, string initial, Action<string> onOk)
		{
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Expected O, but got Unknown
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Expected O, but got Unknown
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Expected O, but got Unknown
			//IL_0201: Unknown result type (might be due to invalid IL or missing references)
			//IL_0208: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: 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_0263: Unknown result type (might be due to invalid IL or missing references)
			//IL_0268: Unknown result type (might be due to invalid IL or missing references)
			//IL_0279: Unknown result type (might be due to invalid IL or missing references)
			//IL_027f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_028d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: 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)
			//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d1: 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_03ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_03f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0406: Unknown result type (might be due to invalid IL or missing references)
			//IL_041d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0432: Unknown result type (might be due to invalid IL or missing references)
			ClosePopup();
			float num = rowH * 0.5f;
			float num2 = Mathf.Max(rowW * 2f + num * 3f, rowH * 12f);
			RectTransform val = (RectTransform)(((Object)(object)inputTemplate != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null);
			float height;
			if ((Object)(object)val != (Object)null)
			{
				Rect rect = val.rect;
				if (((Rect)(ref rect)).height > 1f)
				{
					rect = val.rect;
					height = ((Rect)(ref rect)).height;
					goto IL_00a0;
				}
			}
			height = rowH;
			goto IL_00a0;
			IL_00a0:
			float num3 = height;
			float num4 = num + rowH + num * 0.5f + num3 + num + rowH + num;
			popup = new GameObject(kind + "PresetNamePopup", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			popup.layer = ((Component)canvasRoot).gameObject.layer;
			RectTransform val2 = (RectTransform)popup.transform;
			((Transform)val2).SetParent(canvasRoot, false);
			((Transform)val2).SetAsLastSibling();
			val2.anchorMin = Vector2.zero;
			val2.anchorMax = Vector2.one;
			val2.offsetMin = Vector2.zero;
			val2.offsetMax = Vector2.zero;
			((Graphic)popup.GetComponent<Image>()).color = new Color(0f, 0f, 0f, 0.7f);
			GameObject val3 = new GameObject("Box", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			})
			{
				layer = popup.layer
			};
			RectTransform val4 = (RectTransform)val3.transform;
			((Transform)val4).SetParent((Transform)(object)val2, false);
			Vector2 val5 = default(Vector2);
			((Vector2)(ref val5))..ctor(0.5f, 0.5f);
			val4.pivot = val5;
			Vector2 val6 = (val4.anchorMin = (val4.anchorMax = val5));
			val4.sizeDelta = new Vector2(num2, num4);
			((Graphic)val3.GetComponent<Image>()).color = new Color(0.07f, 0.08f, 0.11f, 0.98f);
			GameObject val9 = new GameObject("Title", new Type[1] { typeof(RectTransform) })
			{
				layer = popup.layer
			};
			RectTransform val10 = (RectTransform)val9.transform;
			((Transform)val10).SetParent((Transform)(object)val4, false);
			val10.anchorMin = new Vector2(0f, 1f);
			val10.anchorMax = new Vector2(1f, 1f);
			val10.pivot = new Vector2(0.5f, 1f);
			val10.anchoredPosition = new Vector2(0f, 0f - num);
			val10.sizeDelta = new Vector2((0f - num) * 2f, rowH);
			TextMeshProUGUI val11 = val9.AddComponent<TextMeshProUGUI>();
			if ((Object)(object)headerText != (Object)null)
			{
				((TMP_Text)val11).font = ((TMP_Text)headerText).font;
				((TMP_Text)val11).fontSharedMaterial = ((TMP_Text)headerText).fontSharedMaterial;
				((Graphic)val11).color = ((Graphic)headerText).color;
			}
			((TMP_Text)val11).text = title;
			((TMP_Text)val11).alignment = (TextAlignmentOptions)514;
			((TMP_Text)val11).enableAutoSizing = true;
			((TMP_Text)val11).fontSizeMin = 8f;
			((TMP_Text)val11).fontSizeMax = rowH * 0.7f;
			((Graphic)val11).raycastTarget = false;
			popupField = null;
			if ((Object)(object)inputTemplate != (Object)null)
			{
				GameObject val12 = Object.Instantiate<GameObject>(((Component)inputTemplate).gameObject, (Transform)(object)val4);
				((Object)val12).name = "NameInput";
				val12.SetActive(true);
				RectTransform val13 = (RectTransform)val12.transform;
				((Vector2)(ref val6))..ctor(0.5f, 1f);
				val13.anchorMax = val6;
				val13.anchorMin = val6;
				val13.pivot = new Vector2(0.5f, 1f);
				val13.anchoredPosition = new Vector2(0f, 0f - (num + rowH + num * 0.5f));
				val13.sizeDelta = new Vector2(num2 - num * 2f, num3);
				popupField = val12.GetComponent<TMP_InputField>() ?? val12.GetComponentInChildren<TMP_InputField>(true);
			}
			if ((Object)(object)popupField != (Object)null)
			{
				((UnityEventBase)popupField.onValueChanged).RemoveAllListeners();
				((UnityEventBase)popupField.onEndEdit).RemoveAllListeners();
				((UnityEventBase)popupField.onSubmit).RemoveAllListeners();
				popupField.characterLimit = 24;
				popupField.text = initial ?? "";
				Graphic placeholder = popupField.placeholder;
				TMP_Text val14 = (TMP_Text)(object)((placeholder is TMP_Text) ? placeholder : null);
				if (val14 != null)
				{
					val14.text = "Type a name, then press Enter";
				}
				((UnityEvent<string>)(object)popupField.onSubmit).AddListener((UnityAction<string>)delegate
				{
					Confirm(onOk);
				});
				focusFrames = 2;
			}
			float num5 = (num2 - num * 3f) * 0.5f;
			PopupButton(val4, "OK", (0f - (num5 + num)) * 0.5f, num, num5, delegate
			{
				Confirm(onOk);
			});
			PopupButton(val4, "CANCEL", (num5 + num) * 0.5f, num, num5, ClosePopup);
			onPopup?.Invoke(obj: true);
		}

		private void PopupButton(RectTransform box, string text, float x, float y, float width, Action onClick)
		{
			//IL_001b: 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_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: 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)
			TextMeshProUGUI label;
			RectTransform val = (RectTransform)Row((Transform)(object)box, "Popup" + text, out label, onClick).transform;
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0.5f, 0f);
			val.anchorMax = val2;
			val.anchorMin = val2;
			val.pivot = new Vector2(0.5f, 0f);
			val.anchoredPosition = new Vector2(x, y);
			val.sizeDelta = new Vector2(width, rowH);
			if ((Object)(object)label != (Object)null)
			{
				((TMP_Text)label).text = text;
			}
		}

		private void Confirm(Action<string> onOk)
		{
			if (!((Object)(object)popup == (Object)null))
			{
				string obj = (((Object)(object)popupField != (Object)null) ? (popupField.text ?? "").Trim() : "");
				ClosePopup();
				onOk(obj);
				Refresh();
			}
		}

		private void ClosePopup()
		{
			if (!((Object)(object)popup == (Object)null))
			{
				GameObject obj = popup;
				popup = null;
				popupField = null;
				Object.Destroy((Object)(object)obj);
				onPopup?.Invoke(obj: false);
			}
		}

		private void Flash(string message)
		{
			flash = message;
			flashUntil = Time.unscaledTime + 1.8f;
		}

		public void Tick()
		{
			if (flash != null && Time.unscaledTime >= flashUntil)
			{
				flash = null;
				if (Alive)
				{
					Refresh();
				}
			}
			if (focusFrames > 0 && --focusFrames == 0 && (Object)(object)popupField != (Object)null)
			{
				((Selectable)popupField).Select();
				popupField.ActivateInputField();
			}
		}

		private void Refresh()
		{
			if (!Alive)
			{
				return;
			}
			string text = "PRESETS";
			if (flash != null)
			{
				text = flash;
			}
			else if (mode == Mode.Save)
			{
				text = "PICK SLOT TO SAVE";
			}
			else if (mode == Mode.Rename)
			{
				text = "PICK SLOT TO RENAME";
			}
			else if (mode == Mode.Delete)
			{
				text = "PICK SLOT TO DELETE";
			}
			if ((Object)(object)headerText != (Object)null)
			{
				((TMP_Text)headerText).text = text;
			}
			for (int i = 0; i < slotTexts.Count; i++)
			{
				if (!((Object)(object)slotTexts[i] == (Object)null))
				{
					int num = i + 1;
					string arg = NameOf(num).Value.Trim();
					bool flag = DataOf(num).Value.Length > 0;
					((TMP_Text)slotTexts[i]).text = (flag ? $"{num}. {arg}" : $"{num}. EMPTY");
				}
			}
			if ((Object)(object)saveText != (Object)null)
			{
				((TMP_Text)saveText).text = ((mode == Mode.Save) ? "CANCEL" : "SAVE OVER...");
			}
			if ((Object)(object)renameText != (Object)null)
			{
				((TMP_Text)renameText).text = ((mode == Mode.Rename) ? "CANCEL" : "RENAME...");
			}
			if ((Object)(object)deleteText != (Object)null)
			{
				((TMP_Text)deleteText).text = ((mode == Mode.Delete) ? "CANCEL" : "DELETE...");
			}
		}
	}
}