Decompiled source of MainMenuConfirm v0.1.0

MainMenuConfirm.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("gnomesko")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("Gnome Main Menu Confirm")]
[assembly: AssemblyTitle("MainMenuConfirm")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[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 MainMenuConfirm
{
	[BepInPlugin("gnomesko.MainMenuConfirm", "MainMenuConfirm", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(MenuController), "SetStatusText")]
		private static class CaptureStyle
		{
			private static void Postfix(MenuController __instance)
			{
				//IL_0030: 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)
				if (!Object.op_Implicit((Object)(object)_font))
				{
					if (Object.op_Implicit((Object)(object)__instance.statusText))
					{
						_font = ((TMP_Text)__instance.statusText).font;
						_textColor = ((Graphic)__instance.statusText).color;
					}
					_panelSprite = FirstSprite(__instance.statusUpdatePanel);
					if (Object.op_Implicit((Object)(object)__instance.closeStatusButton))
					{
						Image component = __instance.closeStatusButton.GetComponent<Image>();
						_buttonSprite = ((component != null) ? component.sprite : null);
					}
				}
			}
		}

		[HarmonyPatch(typeof(PlayerController), "GoToMainMenu")]
		private static class MainMenuPatch
		{
			[HarmonyPriority(800)]
			private static bool Prefix(PlayerController __instance)
			{
				return Gate("Return to the Main Menu?", "Main Menu", delegate
				{
					_bypass = true;
					__instance.GoToMainMenu();
				});
			}
		}

		[HarmonyPatch(typeof(PlayerController), "CloseGame")]
		private static class QuitPatch
		{
			[HarmonyPriority(800)]
			private static bool Prefix(PlayerController __instance)
			{
				return Gate("Quit to desktop?", "Quit", delegate
				{
					_bypass = true;
					__instance.CloseGame();
				});
			}
		}

		private const string ModGuid = "gnomesko.MainMenuConfirm";

		private static Plugin _instance;

		private static bool _bypass;

		private static Action _onConfirm;

		private static TMP_FontAsset _font;

		private static Sprite _panelSprite;

		private static Sprite _buttonSprite;

		private static Color _textColor = Color.white;

		private static GameObject _root;

		private static TextMeshProUGUI _promptText;

		private static TextMeshProUGUI _confirmText;

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			_instance = this;
			new Harmony("gnomesko.MainMenuConfirm").PatchAll();
		}

		private static bool Gate(string prompt, string confirmLabel, Action onConfirm)
		{
			if (_bypass)
			{
				_bypass = false;
				return true;
			}
			try
			{
				EnsureUI();
				_onConfirm = onConfirm;
				((TMP_Text)_promptText).text = prompt;
				((TMP_Text)_confirmText).text = confirmLabel;
				_root.SetActive(true);
				return false;
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)_instance).Logger.LogError((object)$"Confirm dialog failed, allowing action: {arg}");
				return true;
			}
		}

		private static void Cancel()
		{
			if (Object.op_Implicit((Object)(object)_root))
			{
				_root.SetActive(false);
			}
			_onConfirm = null;
		}

		private static void Confirm()
		{
			Action onConfirm = _onConfirm;
			Cancel();
			((MonoBehaviour)_instance).StartCoroutine(Run(onConfirm));
		}

		private static IEnumerator Run(Action action)
		{
			yield return null;
			try
			{
				action();
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)_instance).Logger.LogError((object)$"Confirmed action failed: {arg}");
			}
		}

		private static void EnsureUI()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: 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_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)_root))
			{
				_root = new GameObject("GnomeConfirmCanvas");
				Object.DontDestroyOnLoad((Object)(object)_root);
				Canvas obj = _root.AddComponent<Canvas>();
				obj.renderMode = (RenderMode)0;
				obj.sortingOrder = 30000;
				CanvasScaler obj2 = _root.AddComponent<CanvasScaler>();
				obj2.uiScaleMode = (ScaleMode)1;
				obj2.referenceResolution = new Vector2(1920f, 1080f);
				obj2.matchWidthOrHeight = 0.5f;
				_root.AddComponent<GraphicRaycaster>();
				Image obj3 = NewImage("Blocker", _root.transform);
				Stretch(((Graphic)obj3).rectTransform);
				((Graphic)obj3).color = new Color(0f, 0f, 0f, 0.6f);
				Image val = NewImage("Panel", _root.transform);
				ApplySprite(val, _panelSprite, new Color(0.12f, 0.12f, 0.12f, 0.95f));
				((Graphic)val).rectTransform.sizeDelta = new Vector2(680f, 300f);
				_promptText = NewText("Prompt", ((Component)val).transform, 36f);
				RectTransform rectTransform = ((TMP_Text)_promptText).rectTransform;
				rectTransform.anchorMin = new Vector2(0f, 0.38f);
				rectTransform.anchorMax = Vector2.one;
				rectTransform.offsetMin = new Vector2(40f, 0f);
				rectTransform.offsetMax = new Vector2(-40f, -30f);
				_confirmText = NewButton(((Component)val).transform, new Vector2(-150f, 60f), Confirm);
				((TMP_Text)NewButton(((Component)val).transform, new Vector2(150f, 60f), Cancel)).text = "Cancel";
				_root.SetActive(false);
			}
		}

		private static TextMeshProUGUI NewButton(Transform parent, Vector2 anchoredPos, Action onClick)
		{
			//IL_0033: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Expected O, but got Unknown
			Image val = NewImage("Button", parent);
			ApplySprite(val, _buttonSprite, new Color(0.25f, 0.25f, 0.25f, 1f));
			RectTransform rectTransform = ((Graphic)val).rectTransform;
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0.5f, 0f);
			rectTransform.anchorMax = val2;
			rectTransform.anchorMin = val2;
			rectTransform.sizeDelta = new Vector2(250f, 80f);
			rectTransform.anchoredPosition = anchoredPos;
			((UnityEvent)((Component)val).gameObject.AddComponent<Button>().onClick).AddListener((UnityAction)delegate
			{
				onClick();
			});
			TextMeshProUGUI obj = NewText("Text", ((Component)val).transform, 30f);
			Stretch(((TMP_Text)obj).rectTransform);
			return obj;
		}

		private static RectTransform NewRect(string name, Transform parent)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			GameObject val = new GameObject(name, new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(parent, false);
			return (RectTransform)val.transform;
		}

		private static Image NewImage(string name, Transform parent)
		{
			return ((Component)NewRect(name, parent)).gameObject.AddComponent<Image>();
		}

		private static TextMeshProUGUI NewText(string name, Transform parent, float fontSize)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			TextMeshProUGUI val = ((Component)NewRect(name, parent)).gameObject.AddComponent<TextMeshProUGUI>();
			if (Object.op_Implicit((Object)(object)_font))
			{
				((TMP_Text)val).font = _font;
			}
			((TMP_Text)val).fontSize = fontSize;
			((Graphic)val).color = _textColor;
			((TMP_Text)val).alignment = (TextAlignmentOptions)514;
			((Graphic)val).raycastTarget = false;
			return val;
		}

		private static void ApplySprite(Image img, Sprite sprite, Color fallback)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)sprite))
			{
				img.sprite = sprite;
				img.type = (Type)((sprite.border != Vector4.zero) ? 1 : 0);
			}
			else
			{
				((Graphic)img).color = fallback;
			}
		}

		private static void Stretch(RectTransform rt)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//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_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			rt.anchorMin = Vector2.zero;
			rt.anchorMax = Vector2.one;
			Vector2 offsetMin = (rt.offsetMax = Vector2.zero);
			rt.offsetMin = offsetMin;
		}

		private static Sprite FirstSprite(GameObject go)
		{
			if (!Object.op_Implicit((Object)(object)go))
			{
				return null;
			}
			Image[] componentsInChildren = go.GetComponentsInChildren<Image>(true);
			foreach (Image val in componentsInChildren)
			{
				if (Object.op_Implicit((Object)(object)val.sprite))
				{
					return val.sprite;
				}
			}
			return null;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}