Decompiled source of CombatUIFade v1.0.2

CombatUIFade.dll

Decompiled 2 months ago
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("CombatUIFade")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Azumatt")]
[assembly: AssemblyProduct("CombatUIFade")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("E0E2F92E-557C-4A05-9D89-AA92A0BD75C4")]
[assembly: AssemblyFileVersion("1.0.2")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class ExtensionMarkerAttribute : Attribute
	{
		public ExtensionMarkerAttribute(string name)
		{
		}
	}
}
namespace CombatUIFade
{
	[BepInPlugin("Azumatt.CombatUIFade", "CombatUIFade", "1.0.2")]
	public class CombatUIFadePlugin : BaseUnityPlugin
	{
		public enum Toggle
		{
			On = 1,
			Off = 0
		}

		private class ConfigurationManagerAttributes
		{
			[UsedImplicitly]
			public int? Order;

			[UsedImplicitly]
			public bool? Browsable;

			[UsedImplicitly]
			public string? Category;

			[UsedImplicitly]
			public Action<ConfigEntryBase>? CustomDrawer;
		}

		private class AcceptableShortcuts : AcceptableValueBase
		{
			public AcceptableShortcuts()
				: base(typeof(KeyboardShortcut))
			{
			}

			public override object Clamp(object value)
			{
				return value;
			}

			public override bool IsValid(object value)
			{
				return true;
			}

			public override string ToDescriptionString()
			{
				return "# Acceptable values: " + string.Join(", ", UnityInput.Current.SupportedKeyCodes);
			}
		}

		internal const string ModName = "CombatUIFade";

		internal const string ModVersion = "1.0.2";

		internal const string Author = "Azumatt";

		private const string ModGUID = "Azumatt.CombatUIFade";

		private static string ConfigFileName = "Azumatt.CombatUIFade.cfg";

		private static string ConfigFileFullPath;

		internal static string ConnectionError;

		private readonly Harmony _harmony = new Harmony("Azumatt.CombatUIFade");

		public static readonly ManualLogSource CombatUIFadeLogger;

		internal static float showTimer;

		internal static bool hudFaded;

		private static CanvasGroup hudGroup;

		private static CanvasGroup bowCrosshairGroup;

		private static CanvasGroup buildMenuGroup;

		private static CanvasGroup buildHudGroup;

		private static CanvasGroup crosshairGroup;

		private static ConfigEntry<float> cfgSafeDelay;

		internal static ConfigEntry<float> cfgFadeDuration;

		internal static ConfigEntry<Toggle> cfgHideCrosshair;

		internal static ConfigEntry<Toggle> cfgNoFadeWhileBuilding;

		internal static ConfigEntry<Toggle> cfgCrossbowCrosshair;

		public static CombatUIFadePlugin Instance { get; private set; }

		public void Awake()
		{
			Instance = this;
			bool saveOnConfigSet = ((BaseUnityPlugin)this).Config.SaveOnConfigSet;
			((BaseUnityPlugin)this).Config.SaveOnConfigSet = false;
			cfgSafeDelay = config("1 - General", "SafeDelay", 5f, "Seconds of safety before fade starts");
			cfgFadeDuration = config("1 - General", "FadeDuration", 3f, "Seconds for the fade-out");
			cfgHideCrosshair = config("1 - General", "HideCrosshair", Toggle.On, "Hide the crosshair dot at all times, including while drawing a bow. The bow draw circle is never affected.");
			cfgNoFadeWhileBuilding = config("1 - General", "NoFadeWhileBuilding", Toggle.On, "Keep the HUD fully visible while a build tool (hammer, hoe, cultivator etc.) is out.");
			cfgCrossbowCrosshair = config("1 - General", "ShowCrosshairWithCrossbow", Toggle.On, "Show the crosshair dot while a crossbow is equipped, even if HideCrosshair is on. Crossbows have no draw circle to aim with.");
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			_harmony.PatchAll(executingAssembly);
			SetupWatcher();
			if (saveOnConfigSet)
			{
				((BaseUnityPlugin)this).Config.SaveOnConfigSet = saveOnConfigSet;
				((BaseUnityPlugin)this).Config.Save();
			}
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			if (!File.Exists(ConfigFileFullPath))
			{
				return;
			}
			try
			{
				CombatUIFadeLogger.LogDebug((object)"ReadConfigValues called");
				((BaseUnityPlugin)this).Config.Reload();
			}
			catch
			{
				CombatUIFadeLogger.LogError((object)("There was an issue loading your " + ConfigFileName));
				CombatUIFadeLogger.LogError((object)"Please check your config entries for spelling and format!");
			}
		}

		private static void EnsureGroup()
		{
			if ((!((Object)(object)hudGroup != (Object)null) || !Object.op_Implicit((Object)(object)hudGroup)) && !((Object)(object)Hud.instance == (Object)null))
			{
				hudGroup = Hud.instance.m_rootObject.GetComponent<CanvasGroup>() ?? Hud.instance.m_rootObject.AddComponent<CanvasGroup>();
			}
		}

		internal static void EnsureBowGroup()
		{
			if (!((Object)(object)Hud.instance == (Object)null))
			{
				if ((Object)(object)bowCrosshairGroup == (Object)null || !Object.op_Implicit((Object)(object)bowCrosshairGroup))
				{
					GameObject gameObject = ((Component)Hud.instance.m_crosshairBow).gameObject;
					bowCrosshairGroup = gameObject.GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
					bowCrosshairGroup.ignoreParentGroups = true;
				}
				if (!((Object)(object)crosshairGroup != (Object)null) || !Object.op_Implicit((Object)(object)crosshairGroup))
				{
					GameObject gameObject2 = ((Component)Hud.instance.m_crosshair).gameObject;
					crosshairGroup = gameObject2.GetComponent<CanvasGroup>() ?? gameObject2.AddComponent<CanvasGroup>();
					crosshairGroup.ignoreParentGroups = true;
				}
			}
		}

		internal static void EnsureBuildMenuGroup()
		{
			if (!((Object)(object)Hud.instance == (Object)null))
			{
				if ((Object)(object)buildMenuGroup == (Object)null || !Object.op_Implicit((Object)(object)buildMenuGroup))
				{
					GameObject gameObject = Hud.instance.m_pieceSelectionWindow.gameObject;
					buildMenuGroup = gameObject.GetComponent<CanvasGroup>() ?? gameObject.AddComponent<CanvasGroup>();
					buildMenuGroup.ignoreParentGroups = true;
				}
				if (!((Object)(object)buildHudGroup != (Object)null) || !Object.op_Implicit((Object)(object)buildHudGroup))
				{
					GameObject gameObject2 = Hud.instance.m_buildHud.gameObject;
					buildHudGroup = gameObject2.GetComponent<CanvasGroup>() ?? gameObject2.AddComponent<CanvasGroup>();
					buildHudGroup.ignoreParentGroups = true;
				}
			}
		}

		internal static void UpdateCrosshairVisibility()
		{
			EnsureBowGroup();
			if (!((Object)(object)crosshairGroup == (Object)null) && Object.op_Implicit((Object)(object)crosshairGroup))
			{
				bool flag = cfgHideCrosshair.Value == Toggle.On && (cfgCrossbowCrosshair.Value != Toggle.On || !HoldingCrossbow());
				crosshairGroup.alpha = (flag ? 0f : 1f);
			}
		}

		private static bool HoldingCrossbow()
		{
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				return false;
			}
			return ((Humanoid)Player.m_localPlayer).GetCurrentWeapon()?.m_shared.m_attack.m_requiresReload ?? false;
		}

		internal static bool Building()
		{
			if (cfgNoFadeWhileBuilding.Value == Toggle.On && (Object)(object)Player.m_localPlayer != (Object)null)
			{
				return ((Character)Player.m_localPlayer).InPlaceMode();
			}
			return false;
		}

		internal static void SetAlpha(float alpha)
		{
			EnsureGroup();
			if ((Object)(object)hudGroup != (Object)null)
			{
				hudGroup.alpha = alpha;
			}
		}

		internal static void ForceShow()
		{
			showTimer = cfgSafeDelay.Value + cfgFadeDuration.Value;
			hudFaded = false;
			SetAlpha(1f);
		}

		internal static void ResetHudGroup()
		{
			hudGroup = null;
			bowCrosshairGroup = null;
			crosshairGroup = null;
			buildMenuGroup = null;
			buildHudGroup = null;
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, ConfigDescription description)
		{
			return ((BaseUnityPlugin)this).Config.Bind<T>(group, name, value, description);
		}

		private ConfigEntry<T> config<T>(string group, string name, T value, string description)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			return config(group, name, value, new ConfigDescription(description, (AcceptableValueBase)null, Array.Empty<object>()));
		}

		static CombatUIFadePlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			ConnectionError = "";
			CombatUIFadeLogger = Logger.CreateLogSource("CombatUIFade");
			showTimer = 0f;
			hudFaded = false;
			hudGroup = null;
			bowCrosshairGroup = null;
			buildMenuGroup = null;
			buildHudGroup = null;
			crosshairGroup = null;
			Instance = null;
			cfgSafeDelay = null;
			cfgFadeDuration = null;
			cfgHideCrosshair = null;
			cfgNoFadeWhileBuilding = null;
			cfgCrossbowCrosshair = null;
		}
	}
	public static class KeyboardExtensions
	{
		[SpecialName]
		public sealed class <G>$8D1D3E80A18AA9715780B6CB7003B2F1
		{
			[SpecialName]
			public static class <M>$895AB635D4D087636CF1C26BA650BA11
			{
			}

			[ExtensionMarker("<M>$895AB635D4D087636CF1C26BA650BA11")]
			public bool IsKeyDown()
			{
				throw null;
			}

			[ExtensionMarker("<M>$895AB635D4D087636CF1C26BA650BA11")]
			public bool IsKeyHeld()
			{
				throw null;
			}
		}

		public static bool IsKeyDown(this KeyboardShortcut shortcut)
		{
			//IL_0002: 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)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKeyDown(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}

		public static bool IsKeyHeld(this KeyboardShortcut shortcut)
		{
			//IL_0002: 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)
			if ((int)((KeyboardShortcut)(ref shortcut)).MainKey != 0 && Input.GetKey(((KeyboardShortcut)(ref shortcut)).MainKey))
			{
				return ((KeyboardShortcut)(ref shortcut)).Modifiers.All((Func<KeyCode, bool>)Input.GetKey);
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(Hud), "DamageFlash")]
	internal static class DamageFlash_Patch
	{
		private static void Postfix()
		{
			CombatUIFadePlugin.ForceShow();
		}
	}
	[HarmonyPatch(typeof(Hud), "Awake")]
	internal static class HudAwake_Patch
	{
		private static void Postfix()
		{
			CombatUIFadePlugin.ResetHudGroup();
			CombatUIFadePlugin.ForceShow();
			CombatUIFadePlugin.EnsureBowGroup();
			CombatUIFadePlugin.EnsureBuildMenuGroup();
		}
	}
	[HarmonyPatch(typeof(Hud), "Update")]
	internal static class HudUpdate_Patch
	{
		private static void Postfix(Hud __instance)
		{
			CombatUIFadePlugin.UpdateCrosshairVisibility();
			if (((Component)__instance.m_loadingScreen).gameObject.activeSelf)
			{
				CombatUIFadePlugin.SetAlpha(1f);
				return;
			}
			if (Minimap.IsOpen() || InventoryGui.IsVisible() || Menu.IsActive() || CombatUIFadePlugin.Building())
			{
				CombatUIFadePlugin.ForceShow();
				return;
			}
			if (CombatUIFadePlugin.showTimer <= 0f)
			{
				if (!CombatUIFadePlugin.hudFaded)
				{
					CombatUIFadePlugin.SetAlpha(0f);
					CombatUIFadePlugin.hudFaded = true;
				}
				return;
			}
			CombatUIFadePlugin.showTimer -= Time.deltaTime;
			float value = CombatUIFadePlugin.cfgFadeDuration.Value;
			if (CombatUIFadePlugin.showTimer <= value)
			{
				CombatUIFadePlugin.SetAlpha(Mathf.Clamp01(CombatUIFadePlugin.showTimer / value));
				CombatUIFadePlugin.hudFaded = false;
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Show")]
	internal static class InventoryShow_Patch
	{
		private static void Postfix()
		{
			CombatUIFadePlugin.ForceShow();
		}
	}
	[HarmonyPatch(typeof(Minimap), "SetMapMode")]
	internal static class MapOpen_Patch
	{
		private static void Postfix(MapMode mode)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Invalid comparison between Unknown and I4
			if ((int)mode == 2)
			{
				CombatUIFadePlugin.ForceShow();
			}
		}
	}
}