Decompiled source of HideUnusedButtons v4.0.0

HideUnusedButtons.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("REPOJP")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("zabuMod")]
[assembly: AssemblyTitle("zabuMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace REPOJP.HideUnusedButtons
{
	[BepInPlugin("REPOJP.HideUnusedButtons", "HideUnusedButtons", "4.0.0")]
	public class HideUnusedButtonsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "REPOJP.HideUnusedButtons";

		public const string PluginName = "HideUnusedButtons";

		public const string PluginVersion = "4.0.0";

		internal static ManualLogSource Log;

		private Harmony harmony;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> HideMainMenuTutorialButton;

		internal static ConfigEntry<bool> DisableMainMenuTutorialEvent;

		internal static ConfigEntry<bool> HideEscapeQuitGameButton;

		internal static ConfigEntry<bool> DisableEscapeQuitGameEvent;

		internal static ConfigEntry<bool> LogBlockedActions;

		private void Awake()
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			try
			{
				((Component)this).transform.parent = null;
				Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
				Log = ((BaseUnityPlugin)this).Logger;
				BindConfig();
				harmony = new Harmony("REPOJP.HideUnusedButtons");
				harmony.PatchAll();
				Log.LogInfo((object)"HideUnusedButtons loaded. Version=4.0.0");
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failure: Awake\n" + ex));
			}
		}

		private void OnDestroy()
		{
			try
			{
				if (harmony != null)
				{
					harmony.UnpatchSelf();
					harmony = null;
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("Failure: OnDestroy\n" + ex));
			}
		}

		private void BindConfig()
		{
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enables this mod.このMODを有効化します。");
			HideMainMenuTutorialButton = ((BaseUnityPlugin)this).Config.Bind<bool>("Main Menu Tutorial", "HideTutorialButton", true, "Hides the Tutorial button from the main menu.メインメニューのTutorialボタンを非表示にします。");
			DisableMainMenuTutorialEvent = ((BaseUnityPlugin)this).Config.Bind<bool>("Main Menu Tutorial", "DisableTutorialButtonEvent", true, "Blocks the Tutorial button event even if it is called directly.Tutorialボタン処理が直接呼ばれてもチュートリアル開始をブロックします。");
			HideEscapeQuitGameButton = ((BaseUnityPlugin)this).Config.Bind<bool>("Escape Menu Quit Game", "HideQuitGameButton", true, "Hides the Quit Game button from the Escape menu.Escape画面のQuit Gameボタンを非表示にします。");
			DisableEscapeQuitGameEvent = ((BaseUnityPlugin)this).Config.Bind<bool>("Escape Menu Quit Game", "DisableQuitGameButtonEvent", true, "Blocks the Quit Game button event from the Escape menu even if it is called directly.Escape画面のQuit Game処理が直接呼ばれてもゲーム終了をブロックします。");
			LogBlockedActions = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "LogBlockedActions", false, "Logs when a blocked button event is called.ブロックしたボタン処理が呼ばれた時にログを出力します。");
		}

		internal static bool IsEnabled()
		{
			return Enabled != null && Enabled.Value;
		}

		internal static bool ShouldLogBlockedActions()
		{
			return LogBlockedActions != null && LogBlockedActions.Value;
		}

		internal static void WriteBlockedLog(string message)
		{
			if (ShouldLogBlockedActions() && Log != null)
			{
				Log.LogInfo((object)message);
			}
		}
	}
	internal static class MenuButtonUtility
	{
		private static readonly FieldInfo MenuButtonDisabledField = AccessTools.Field(typeof(MenuButton), "disabled");

		internal static void HideButton(MenuButton menuButton)
		{
			try
			{
				if (!((Object)(object)menuButton == (Object)null) && !((Object)(object)((Component)menuButton).gameObject == (Object)null))
				{
					if (MenuButtonDisabledField != null)
					{
						MenuButtonDisabledField.SetValue(menuButton, true);
					}
					Button component = ((Component)menuButton).GetComponent<Button>();
					if ((Object)(object)component != (Object)null)
					{
						((Selectable)component).interactable = false;
					}
					((Component)menuButton).gameObject.SetActive(false);
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: HideButton\n" + ex));
				}
			}
		}

		internal static MenuButton FindButtonByPersistentClickMethod(Component root, object expectedTarget, string methodName)
		{
			try
			{
				if ((Object)(object)root == (Object)null || string.IsNullOrEmpty(methodName))
				{
					return null;
				}
				MenuButton[] componentsInChildren = root.GetComponentsInChildren<MenuButton>(true);
				foreach (MenuButton val in componentsInChildren)
				{
					if (!((Object)(object)val == (Object)null))
					{
						Button component = ((Component)val).GetComponent<Button>();
						if (!((Object)(object)component == (Object)null) && component.onClick != null && HasPersistentClickMethod(component, expectedTarget, methodName))
						{
							return val;
						}
					}
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: FindButtonByPersistentClickMethod\n" + ex));
				}
			}
			return null;
		}

		internal static MenuButton FindLikelyButtonByText(Component root, string normalizedText)
		{
			try
			{
				if ((Object)(object)root == (Object)null || string.IsNullOrEmpty(normalizedText))
				{
					return null;
				}
				MenuButton[] componentsInChildren = root.GetComponentsInChildren<MenuButton>(true);
				foreach (MenuButton val in componentsInChildren)
				{
					if (!((Object)(object)val == (Object)null))
					{
						string normalizedButtonText = GetNormalizedButtonText(val);
						string text = Normalize(((Object)((Component)val).gameObject).name);
						if (normalizedButtonText == normalizedText || text.Contains(normalizedText))
						{
							return val;
						}
					}
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: FindLikelyButtonByText\n" + ex));
				}
			}
			return null;
		}

		private static bool HasPersistentClickMethod(Button button, object expectedTarget, string methodName)
		{
			try
			{
				int persistentEventCount = ((UnityEventBase)button.onClick).GetPersistentEventCount();
				for (int i = 0; i < persistentEventCount; i++)
				{
					string persistentMethodName = ((UnityEventBase)button.onClick).GetPersistentMethodName(i);
					Object persistentTarget = ((UnityEventBase)button.onClick).GetPersistentTarget(i);
					if (!(persistentMethodName != methodName) && (expectedTarget == null || persistentTarget == expectedTarget || (persistentTarget != (Object)null && ((object)persistentTarget).GetType() == expectedTarget.GetType())))
					{
						return true;
					}
				}
			}
			catch
			{
			}
			return false;
		}

		private static string GetNormalizedButtonText(MenuButton menuButton)
		{
			try
			{
				TextMeshProUGUI componentInChildren = ((Component)menuButton).GetComponentInChildren<TextMeshProUGUI>(true);
				if ((Object)(object)componentInChildren != (Object)null && !string.IsNullOrEmpty(((TMP_Text)componentInChildren).text))
				{
					return Normalize(((TMP_Text)componentInChildren).text);
				}
			}
			catch
			{
			}
			return string.Empty;
		}

		private static string Normalize(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return string.Empty;
			}
			return text.ToLowerInvariant().Replace(" ", string.Empty).Replace("-", string.Empty)
				.Replace("_", string.Empty)
				.Replace("\t", string.Empty)
				.Replace("\r", string.Empty)
				.Replace("\n", string.Empty);
		}
	}
	[HarmonyPatch(typeof(MenuPageMain), "Start")]
	internal static class MenuPageMainStartPatch
	{
		private static readonly FieldInfo TutorialButtonBlinkActiveField = AccessTools.Field(typeof(MenuPageMain), "tutorialButtonBlinkActive");

		private static void Postfix(MenuPageMain __instance)
		{
			try
			{
				if (HideUnusedButtonsPlugin.IsEnabled() && HideUnusedButtonsPlugin.HideMainMenuTutorialButton != null && HideUnusedButtonsPlugin.HideMainMenuTutorialButton.Value)
				{
					if (TutorialButtonBlinkActiveField != null)
					{
						TutorialButtonBlinkActiveField.SetValue(__instance, false);
					}
					MenuButton val = __instance.tutorialButton;
					if ((Object)(object)val == (Object)null)
					{
						val = MenuButtonUtility.FindButtonByPersistentClickMethod((Component)(object)__instance, __instance, "ButtonEventTutorial");
					}
					if ((Object)(object)val == (Object)null)
					{
						val = MenuButtonUtility.FindLikelyButtonByText((Component)(object)__instance, "tutorial");
					}
					MenuButtonUtility.HideButton(val);
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: MenuPageMain.Start Postfix\n" + ex));
				}
			}
		}
	}
	[HarmonyPatch(typeof(MenuPageMain), "ButtonEventTutorial")]
	internal static class MenuPageMainButtonEventTutorialPatch
	{
		private static bool Prefix()
		{
			try
			{
				if (HideUnusedButtonsPlugin.IsEnabled() && HideUnusedButtonsPlugin.DisableMainMenuTutorialEvent != null && HideUnusedButtonsPlugin.DisableMainMenuTutorialEvent.Value)
				{
					HideUnusedButtonsPlugin.WriteBlockedLog("Blocked Tutorial button event.");
					return false;
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: MenuPageMain.ButtonEventTutorial Prefix\n" + ex));
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(MenuPageEsc), "Start")]
	internal static class MenuPageEscStartPatch
	{
		private static void Postfix(MenuPageEsc __instance)
		{
			try
			{
				if (HideUnusedButtonsPlugin.IsEnabled() && HideUnusedButtonsPlugin.HideEscapeQuitGameButton != null && HideUnusedButtonsPlugin.HideEscapeQuitGameButton.Value)
				{
					MenuButton val = MenuButtonUtility.FindButtonByPersistentClickMethod((Component)(object)__instance, __instance, "ButtonEventQuit");
					if ((Object)(object)val == (Object)null)
					{
						val = MenuButtonUtility.FindLikelyButtonByText((Component)(object)__instance, "quitgame");
					}
					MenuButtonUtility.HideButton(val);
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: MenuPageEsc.Start Postfix\n" + ex));
				}
			}
		}
	}
	[HarmonyPatch(typeof(MenuPageEsc), "ButtonEventQuit")]
	internal static class MenuPageEscButtonEventQuitPatch
	{
		private static bool Prefix()
		{
			try
			{
				if (HideUnusedButtonsPlugin.IsEnabled() && HideUnusedButtonsPlugin.DisableEscapeQuitGameEvent != null && HideUnusedButtonsPlugin.DisableEscapeQuitGameEvent.Value)
				{
					HideUnusedButtonsPlugin.WriteBlockedLog("Blocked Escape menu Quit Game button event.");
					return false;
				}
			}
			catch (Exception ex)
			{
				if (HideUnusedButtonsPlugin.Log != null)
				{
					HideUnusedButtonsPlugin.Log.LogError((object)("Failure: MenuPageEsc.ButtonEventQuit Prefix\n" + ex));
				}
			}
			return true;
		}
	}
}