Decompiled source of ShotgunAmmoHUD v1.0.1

ShotgunAmmoHUD.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ShotgunAmmoHUD")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+cd5dafe21e728ea2f872690e16c5bcb57674debd")]
[assembly: AssemblyProduct("ShotgunAmmoHUD")]
[assembly: AssemblyTitle("ShotgunAmmoHUD")]
[assembly: AssemblyVersion("1.0.1.0")]
[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 ShotgunAmmoHUD
{
	[BepInPlugin("oddninja.shotgunammohud", "ShotgunAmmoHUD", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "oddninja.shotgunammohud";

		public const string Name = "ShotgunAmmoHUD";

		public const string Version = "1.0.1";

		internal const string LethalConfigGuid = "ainavt.lc.lethalconfig";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Enabled;

		internal static ConfigEntry<bool> OnlyWhenHeld;

		internal static ConfigEntry<bool> HideInTerminal;

		internal static ConfigEntry<bool> ShowWhenEmpty;

		internal static ConfigEntry<int> MaxShells;

		internal static ConfigEntry<string> Format;

		private static GameObject _runner;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show the shotgun ammo line.");
			OnlyWhenHeld = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowOnlyWhenHeld", true, "Only show while a shotgun is the actively held item. If false, also shows a shotgun in another inventory slot.");
			HideInTerminal = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HideInTerminal", true, "Hide the line while using the terminal.");
			ShowWhenEmpty = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowWhenEmpty", true, "Show the line even when 0 shells are loaded.");
			MaxShells = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxShells", 2, "Capacity shown as the denominator (vanilla shotgun holds 2).");
			Format = ((BaseUnityPlugin)this).Config.Bind<string>("Text", "Format", "Shells : [{ammo} / {max}]", "Text of the line. Tokens: {ammo} = shells loaded, {max} = MaxShells.");
			SceneManager.sceneLoaded += delegate
			{
				//IL_001c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0026: Expected O, but got Unknown
				if ((Object)(object)_runner != (Object)null)
				{
					Object.Destroy((Object)(object)_runner);
				}
				_runner = new GameObject("ShotgunAmmoHUD_Runner");
				Object.DontDestroyOnLoad((Object)(object)_runner);
				_runner.AddComponent<HudRunner>();
			};
			Log.LogInfo((object)"ShotgunAmmoHUD 1.0.1 loaded.");
		}
	}
	internal sealed class HudRunner : MonoBehaviour
	{
		private static readonly FieldRef<ShotgunItem, int> ShellsLoadedRef = AccessTools.FieldRefAccess<ShotgunItem, int>("shellsLoaded");

		private static readonly FieldRef<HUDManager, TextMeshProUGUI[]> ControlTipLinesRef = AccessTools.FieldRefAccess<HUDManager, TextMeshProUGUI[]>("controlTipLines");

		private const string LineName = "ShotgunAmmoLine";

		private TextMeshProUGUI _line;

		private void LateUpdate()
		{
			if (!Plugin.Enabled.Value)
			{
				Hide();
				return;
			}
			if (!TryGetShells(out var shells))
			{
				Hide();
				return;
			}
			TextMeshProUGUI[] controlTipLines = GetControlTipLines();
			if (controlTipLines != null && controlTipLines.Length != 0 && !((Object)(object)controlTipLines[0] == (Object)null) && EnsureLine(controlTipLines[0]))
			{
				string text = Plugin.Format.Value.Replace("{ammo}", shells.ToString()).Replace("{max}", Plugin.MaxShells.Value.ToString());
				((TMP_Text)_line).text = text;
				PositionAboveTopTip(controlTipLines);
				if (!((Component)_line).gameObject.activeSelf)
				{
					((Component)_line).gameObject.SetActive(true);
				}
			}
		}

		private bool TryGetShells(out int shells)
		{
			shells = 0;
			PlayerControllerB val = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.localPlayerController : null);
			if ((Object)(object)val == (Object)null || (Plugin.HideInTerminal.Value && val.inTerminalMenu))
			{
				return false;
			}
			ShotgunItem shotgun = GetShotgun(val);
			if ((Object)(object)shotgun == (Object)null)
			{
				return false;
			}
			shells = Mathf.Max(0, ShellsLoadedRef.Invoke(shotgun));
			if (shells <= 0 && !Plugin.ShowWhenEmpty.Value)
			{
				return false;
			}
			return true;
		}

		private static ShotgunItem GetShotgun(PlayerControllerB player)
		{
			GrabbableObject currentlyHeldObjectServer = player.currentlyHeldObjectServer;
			ShotgunItem val = (ShotgunItem)(object)((currentlyHeldObjectServer is ShotgunItem) ? currentlyHeldObjectServer : null);
			if (val != null)
			{
				return val;
			}
			if (Plugin.OnlyWhenHeld.Value)
			{
				return null;
			}
			GrabbableObject[] itemSlots = player.ItemSlots;
			if (itemSlots != null)
			{
				GrabbableObject[] array = itemSlots;
				foreach (GrabbableObject obj in array)
				{
					ShotgunItem val2 = (ShotgunItem)(object)((obj is ShotgunItem) ? obj : null);
					if (val2 != null)
					{
						return val2;
					}
				}
			}
			return null;
		}

		private static TextMeshProUGUI[] GetControlTipLines()
		{
			HUDManager instance = HUDManager.Instance;
			if (!((Object)(object)instance != (Object)null))
			{
				return null;
			}
			return ControlTipLinesRef.Invoke(instance);
		}

		private bool EnsureLine(TextMeshProUGUI template)
		{
			if ((Object)(object)_line != (Object)null)
			{
				return true;
			}
			Transform parent = ((TMP_Text)template).transform.parent;
			if ((Object)(object)parent != (Object)null)
			{
				Transform val = parent.Find("ShotgunAmmoLine");
				if ((Object)(object)val != (Object)null)
				{
					_line = ((Component)val).GetComponent<TextMeshProUGUI>();
				}
			}
			if ((Object)(object)_line == (Object)null)
			{
				_line = Object.Instantiate<TextMeshProUGUI>(template, ((TMP_Text)template).transform.parent);
				((Object)_line).name = "ShotgunAmmoLine";
			}
			((Behaviour)_line).enabled = true;
			((TMP_Text)_line).richText = true;
			return (Object)(object)_line != (Object)null;
		}

		private void PositionAboveTopTip(TextMeshProUGUI[] tips)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			RectTransform rectTransform = ((TMP_Text)tips[0]).rectTransform;
			RectTransform rectTransform2 = ((TMP_Text)_line).rectTransform;
			rectTransform2.anchorMin = rectTransform.anchorMin;
			rectTransform2.anchorMax = rectTransform.anchorMax;
			rectTransform2.pivot = rectTransform.pivot;
			rectTransform2.sizeDelta = rectTransform.sizeDelta;
			float num = 0f;
			if (tips.Length > 1 && (Object)(object)tips[1] != (Object)null)
			{
				num = Mathf.Abs(rectTransform.anchoredPosition.y - ((TMP_Text)tips[1]).rectTransform.anchoredPosition.y);
			}
			if (num < 1f)
			{
				Rect rect = rectTransform.rect;
				float num2;
				if (!(((Rect)(ref rect)).height > 1f))
				{
					num2 = 26f;
				}
				else
				{
					rect = rectTransform.rect;
					num2 = ((Rect)(ref rect)).height;
				}
				num = num2;
			}
			rectTransform2.anchoredPosition = rectTransform.anchoredPosition + new Vector2(0f, num);
		}

		private void Hide()
		{
			if ((Object)(object)_line != (Object)null && ((Component)_line).gameObject.activeSelf)
			{
				((Component)_line).gameObject.SetActive(false);
			}
		}
	}
}