Decompiled source of BetterPauseMenu v1.0.0

BetterPauseMenu.dll

Decompiled 3 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BetterPauseMenu")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+6b4794e8ffa47a806dd923f8fa5752a54bd84cb3")]
[assembly: AssemblyProduct("BetterPauseMenu")]
[assembly: AssemblyTitle("BetterPauseMenu")]
[assembly: AssemblyVersion("1.0.0.0")]
public class ConfirmationHandler : MonoBehaviour
{
	public Action onConfirm;

	public void Confirm()
	{
		onConfirm?.Invoke();
		Object.Destroy((Object)(object)this);
	}

	public void Cancel()
	{
		Object.Destroy((Object)(object)this);
	}
}
namespace BetterPauseMenu;

[HarmonyPatch(typeof(PauseMenu), "Pause")]
internal class PauseMenuPatch
{
	internal static bool IsInConfirmation;

	private static bool Prefix()
	{
		if (IsInConfirmation)
		{
			return false;
		}
		return true;
	}

	private static void Postfix(PauseMenu __instance)
	{
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: 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_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Expected O, but got Unknown
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		FieldInfo fieldInfo = AccessTools.Field(typeof(PauseMenu), "pauseMenu");
		object? value = fieldInfo.GetValue(__instance);
		GameObject val = (GameObject)((value is GameObject) ? value : null);
		if ((Object)(object)val == (Object)null || (Object)(object)val.transform.Find("Reset") != (Object)null)
		{
			return;
		}
		Transform val2 = val.transform.Find("Quit to Menu");
		if ((Object)(object)val2 == (Object)null)
		{
			return;
		}
		RectTransform component = ((Component)val2).GetComponent<RectTransform>();
		AddButton(val, val2, component, "Reset", -34f, delegate
		{
			ShowConfirmation(delegate
			{
				__instance.UnPause();
				SceneManager.LoadScene("GameScene");
			});
		});
		AddButton(val, val2, component, "Quit to Desktop", -68f, delegate
		{
			ShowConfirmation(delegate
			{
				__instance.UnPause();
				Application.Quit();
			});
		});
		RectTransform component2 = val.GetComponent<RectTransform>();
		if ((Object)(object)component2 != (Object)null)
		{
			Vector2 sizeDelta = component2.sizeDelta;
			sizeDelta.y += 80f;
			component2.sizeDelta = sizeDelta;
		}
		foreach (Transform item in val.transform)
		{
			Transform val3 = item;
			RectTransform component3 = ((Component)val3).GetComponent<RectTransform>();
			if ((Object)(object)component3 != (Object)null)
			{
				component3.anchoredPosition += new Vector2(0f, 30f);
			}
		}
	}

	private static void AddButton(GameObject pauseMenu, Transform templateButton, RectTransform templateRect, string name, float yOffset, Action onClick)
	{
		//IL_0039: 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_004a: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			GameObject val = Object.Instantiate<GameObject>(((Component)templateButton).gameObject, pauseMenu.transform);
			((Object)val).name = name;
			val.transform.SetAsLastSibling();
			RectTransform component = val.GetComponent<RectTransform>();
			CopyRectTransformLayout(component, templateRect);
			component.anchoredPosition = templateRect.anchoredPosition + new Vector2(0f, yOffset);
			ReplaceButtonComponent(val, ((Component)templateButton).GetComponent<Button>(), onClick, name);
			val.SetActive(true);
		}
		catch (Exception arg)
		{
			Plugin.Log.LogError((object)$"Error creating {name} button: {arg}");
		}
	}

	private static void ShowConfirmation(Action onConfirm)
	{
		//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Expected O, but got Unknown
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ee: Expected O, but got Unknown
		PauseMenu instance = PauseMenu.instance;
		if ((Object)(object)instance == (Object)null)
		{
			Plugin.Log.LogWarning((object)"[BetterPauseMenu] PauseMenu instance not found. Quitting immediately.");
			onConfirm();
			return;
		}
		FieldInfo fieldInfo = AccessTools.Field(typeof(PauseMenu), "areYouSureMenu");
		GameObject areYouSureMenu = default(GameObject);
		ref GameObject reference = ref areYouSureMenu;
		object? value = fieldInfo.GetValue(instance);
		reference = (GameObject)((value is GameObject) ? value : null);
		FieldInfo fieldInfo2 = AccessTools.Field(typeof(PauseMenu), "pauseMenu");
		GameObject pauseMenu = default(GameObject);
		ref GameObject reference2 = ref pauseMenu;
		object? value2 = fieldInfo2.GetValue(instance);
		reference2 = (GameObject)((value2 is GameObject) ? value2 : null);
		if ((Object)(object)areYouSureMenu == (Object)null)
		{
			Plugin.Log.LogWarning((object)"[BetterPauseMenu] Confirmation menu not found. Quitting immediately.");
			onConfirm();
			return;
		}
		IsInConfirmation = true;
		areYouSureMenu.SetActive(true);
		if ((Object)(object)pauseMenu != (Object)null)
		{
			pauseMenu.SetActive(false);
		}
		Transform val = areYouSureMenu.transform.Find("Yes");
		Transform val2 = areYouSureMenu.transform.Find("No");
		if ((Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
		{
			Plugin.Log.LogError((object)"[BetterPauseMenu] Could not find Yes/No buttons. Quitting immediately as fallback.");
			areYouSureMenu.SetActive(false);
			if ((Object)(object)pauseMenu != (Object)null)
			{
				pauseMenu.SetActive(true);
			}
			IsInConfirmation = false;
			onConfirm();
			return;
		}
		Button component = ((Component)val).GetComponent<Button>();
		Button component2 = ((Component)val2).GetComponent<Button>();
		((UnityEventBase)component.onClick).RemoveAllListeners();
		((UnityEventBase)component2.onClick).RemoveAllListeners();
		((UnityEvent)component.onClick).AddListener((UnityAction)delegate
		{
			areYouSureMenu.SetActive(false);
			if ((Object)(object)pauseMenu != (Object)null)
			{
				pauseMenu.SetActive(true);
			}
			IsInConfirmation = false;
			GameObject val3 = GameObject.Find("GameOver");
			GameObject val4 = GameObject.Find("Victory");
			GameObject val5 = GameObject.Find("DamageTotals");
			if ((Object)(object)val3 != (Object)null)
			{
				val3.SetActive(false);
			}
			if ((Object)(object)val4 != (Object)null)
			{
				val4.SetActive(false);
			}
			if ((Object)(object)val5 != (Object)null)
			{
				val5.SetActive(false);
			}
			onConfirm();
		});
		((UnityEvent)component2.onClick).AddListener((UnityAction)delegate
		{
			areYouSureMenu.SetActive(false);
			if ((Object)(object)pauseMenu != (Object)null)
			{
				pauseMenu.SetActive(true);
			}
			IsInConfirmation = false;
		});
	}

	private static void CopyRectTransformLayout(RectTransform target, RectTransform source)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: 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_002a: Unknown result type (might be due to invalid IL or missing references)
		target.anchorMin = source.anchorMin;
		target.anchorMax = source.anchorMax;
		target.pivot = source.pivot;
		target.sizeDelta = source.sizeDelta;
	}

	private static void ReplaceButtonComponent(GameObject buttonObj, Button templateButton, Action onClickAction, string buttonText)
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Expected O, but got Unknown
		Button component = buttonObj.GetComponent<Button>();
		if ((Object)(object)component != (Object)null)
		{
			Object.DestroyImmediate((Object)(object)component);
		}
		Button val = buttonObj.AddComponent<Button>();
		CopyButtonVisuals(val, templateButton);
		((UnityEvent)val.onClick).AddListener((UnityAction)delegate
		{
			onClickAction();
		});
		Text componentInChildren = buttonObj.GetComponentInChildren<Text>();
		if ((Object)(object)componentInChildren != (Object)null)
		{
			componentInChildren.text = buttonText;
		}
	}

	private static void CopyButtonVisuals(Button target, Button source)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: 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_006a: 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)
		((Selectable)target).transition = ((Selectable)source).transition;
		((Selectable)target).colors = ((Selectable)source).colors;
		((Selectable)target).spriteState = ((Selectable)source).spriteState;
		((Selectable)target).animationTriggers = ((Selectable)source).animationTriggers;
		Image component = ((Component)target).GetComponent<Image>();
		Image component2 = ((Component)source).GetComponent<Image>();
		if ((Object)(object)component != (Object)null && (Object)(object)component2 != (Object)null)
		{
			component.sprite = component2.sprite;
			component.type = component2.type;
			((Graphic)component).color = ((Graphic)component2).color;
			((Graphic)component).raycastTarget = true;
			((Selectable)target).targetGraphic = (Graphic)(object)component;
		}
	}
}
[BepInPlugin("AgusBut.BetterPauseMenu", "BetterPauseMenu", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public static Plugin Instance { get; private set; }

	public static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Loading [BetterPauseMenu 1.0.0]");
		Harmony val = new Harmony("AgusBut.BetterPauseMenu");
		val.PatchAll();
	}
}