Decompiled source of HudToggle v1.0.1

plugins/HudToggle.dll

Decompiled a week ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.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 HudToggle
{
	internal static class HudHider
	{
		private const string ConfigManagerGuid = "com.bepis.bepinex.configurationmanager";

		private static bool s_menuWasOpen;

		private static bool s_minimapHidden;

		private static bool s_chatHidden;

		private static float s_chatAlpha = 1f;

		private static bool s_chatRaycasts = true;

		private static bool s_managerLooked;

		private static object s_manager;

		private static PropertyInfo s_managerOpen;

		public static bool Hidden => Hud.IsUserHidden();

		public static bool Toggle()
		{
			if ((Object)(object)Hud.instance == (Object)null)
			{
				return false;
			}
			Hud.instance.m_userHidden = !Hud.instance.m_userHidden;
			return true;
		}

		public static bool Set(bool hidden)
		{
			if ((Object)(object)Hud.instance == (Object)null)
			{
				return false;
			}
			Hud.instance.m_userHidden = hidden;
			return true;
		}

		public static bool KeyMayAct()
		{
			if ((Object)(object)Player.m_localPlayer == (Object)null || (Object)(object)Hud.instance == (Object)null)
			{
				return false;
			}
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return false;
			}
			if (Console.IsVisible() || TextInput.IsVisible() || Minimap.InTextInput())
			{
				return false;
			}
			if ((Object)(object)Hud.instance.m_buildUi != (Object)null && Hud.instance.m_buildUi.SearchFieldFocused)
			{
				return false;
			}
			return !ConfigManagerOpen();
		}

		public static void Apply()
		{
			FollowMenu();
			bool hidden = Hidden;
			HideMinimap(hidden && Settings.HideMinimap.Value);
			HideChat(hidden && Settings.HideChat.Value);
			KeepMenuOnScreen(hidden);
		}

		private static void KeepMenuOnScreen(bool hidden)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			Menu instance = Menu.instance;
			if (hidden && (Object)(object)instance != (Object)null && (Object)(object)instance.m_root != (Object)null && ((Component)instance.m_root).gameObject.activeSelf)
			{
				instance.m_root.localPosition = Vector3.zero;
			}
		}

		public static void Restore()
		{
			HideMinimap(hide: false);
			HideChat(hide: false);
			s_menuWasOpen = false;
		}

		private static void FollowMenu()
		{
			int num;
			if ((Object)(object)Menu.instance != (Object)null && (Object)(object)Menu.instance.m_root != (Object)null)
			{
				num = (((Component)Menu.instance.m_root).gameObject.activeSelf ? 1 : 0);
				if (num != 0 && !s_menuWasOpen && (Object)(object)Hud.instance != (Object)null && Hud.instance.m_userHidden)
				{
					Hud.instance.m_userHidden = false;
				}
			}
			else
			{
				num = 0;
			}
			s_menuWasOpen = (byte)num != 0;
		}

		private static void HideMinimap(bool hide)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Invalid comparison between Unknown and I4
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Invalid comparison between Unknown and I4
			Minimap instance = Minimap.instance;
			if (hide)
			{
				if ((Object)(object)instance != (Object)null && (int)instance.m_mode == 1 && instance.m_smallRoot.activeSelf)
				{
					instance.m_smallRoot.SetActive(false);
					s_minimapHidden = true;
				}
			}
			else if (s_minimapHidden)
			{
				s_minimapHidden = false;
				if ((Object)(object)instance != (Object)null && (int)instance.m_mode == 1)
				{
					instance.m_smallRoot.SetActive(true);
				}
			}
		}

		private static void HideChat(bool hide)
		{
			Chat instance = Chat.instance;
			if (hide && (Object)(object)instance != (Object)null && (Object)(object)((Terminal)instance).m_chatWindow != (Object)null && !instance.HasFocus())
			{
				CanvasGroup val = ((Component)((Terminal)instance).m_chatWindow).GetComponent<CanvasGroup>();
				if ((Object)(object)val == (Object)null)
				{
					val = ((Component)((Terminal)instance).m_chatWindow).gameObject.AddComponent<CanvasGroup>();
				}
				if (!s_chatHidden)
				{
					s_chatAlpha = val.alpha;
					s_chatRaycasts = val.blocksRaycasts;
					s_chatHidden = true;
				}
				val.alpha = 0f;
				val.blocksRaycasts = false;
			}
			else if (s_chatHidden)
			{
				s_chatHidden = false;
				CanvasGroup val2 = (((Object)(object)instance != (Object)null && (Object)(object)((Terminal)instance).m_chatWindow != (Object)null) ? ((Component)((Terminal)instance).m_chatWindow).GetComponent<CanvasGroup>() : null);
				if (!((Object)(object)val2 == (Object)null))
				{
					val2.alpha = s_chatAlpha;
					val2.blocksRaycasts = s_chatRaycasts;
				}
			}
		}

		private static bool ConfigManagerOpen()
		{
			if (!s_managerLooked)
			{
				s_managerLooked = true;
				if (Chainloader.PluginInfos.TryGetValue("com.bepis.bepinex.configurationmanager", out var value) && (Object)(object)value.Instance != (Object)null)
				{
					s_manager = value.Instance;
					s_managerOpen = AccessTools.Property(((object)value.Instance).GetType(), "DisplayingWindow");
				}
			}
			if (s_manager == null || s_managerOpen == null)
			{
				return false;
			}
			try
			{
				return (bool)s_managerOpen.GetValue(s_manager, null);
			}
			catch
			{
				return false;
			}
		}
	}
	internal class Runner : MonoBehaviour
	{
		private void Update()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			if (KeyDown(Settings.ToggleKey.Value) && HudHider.KeyMayAct())
			{
				HudHider.Toggle();
			}
		}

		private static bool KeyDown(KeyboardShortcut shortcut)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			KeyCode mainKey = ((KeyboardShortcut)(ref shortcut)).MainKey;
			if ((int)mainKey != 0 && Input.GetKeyDown(mainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}

		private void LateUpdate()
		{
			HudHider.Apply();
		}
	}
	[BepInPlugin("hansenbansen.hudtoggle", "Hud Toggle", "1.0.1")]
	public class HudTogglePlugin : BaseUnityPlugin
	{
		public const string Guid = "hansenbansen.hudtoggle";

		public const string Version = "1.0.1";

		public static ManualLogSource Log;

		private static Harmony s_harmony;

		private void Awake()
		{
			Install(((BaseUnityPlugin)this).Config, ((BaseUnityPlugin)this).Logger, Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location));
		}

		public static void Install(ConfigFile config, ManualLogSource logger, string pluginDirectory)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = logger;
			Settings.Bind(config);
			s_harmony = new Harmony("hansenbansen.hudtoggle");
			s_harmony.PatchAll(typeof(HudTogglePlugin).Assembly);
			Runtime.Install();
			Log.LogInfo((object)"Hud Toggle 1.0.1 loaded");
		}

		public static void Uninstall()
		{
			Runtime.Uninstall();
			Harmony obj = s_harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			s_harmony = null;
		}
	}
	internal static class Runtime
	{
		private static GameObject s_runner;

		public static void Install()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected O, but got Unknown
			s_runner = new GameObject("HudToggle_Runner");
			Object.DontDestroyOnLoad((Object)(object)s_runner);
			s_runner.AddComponent<Runner>();
		}

		public static void Uninstall()
		{
			if ((Object)(object)s_runner != (Object)null)
			{
				Object.Destroy((Object)(object)s_runner);
			}
			s_runner = null;
			HudHider.Restore();
		}
	}
	internal static class Settings
	{
		public static ConfigEntry<KeyboardShortcut> ToggleKey;

		public static ConfigEntry<bool> HideMinimap;

		public static ConfigEntry<bool> HideChat;

		public static void Bind(ConfigFile config)
		{
			//IL_0015: 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_002f: Expected O, but got Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			ToggleKey = config.Bind<KeyboardShortcut>("General", "ToggleKey", new KeyboardShortcut((KeyCode)289, Array.Empty<KeyCode>()), new ConfigDescription("The key that hides the whole HUD and brings it back. Set it in the F1 menu (a modifier can be added: LeftControl + H). The game's own Ctrl+F3 does the same.", (AcceptableValueBase)null, Array.Empty<object>()));
			HideMinimap = config.Bind<bool>("Hidden", "HideMinimap", true, new ConfigDescription("Hide the minimap in the corner too while the HUD is hidden. The large map (M) still opens.", (AcceptableValueBase)null, Array.Empty<object>()));
			HideChat = config.Bind<bool>("Hidden", "HideChat", true, new ConfigDescription("Hide the chat window too while the HUD is hidden. Opening the chat to write shows it for as long as you write.", (AcceptableValueBase)null, Array.Empty<object>()));
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		internal IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}