Decompiled source of Ammo Info Display Ammo Count by aedenthorn FIXED v0.4.1

AmmoCount.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;
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.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+84ecdf67601019596bca6ee59e7c1b3bb50936f4")]
[assembly: AssemblyVersion("1.0.0.0")]
public class AedenthornUtils
{
	public static bool IgnoreKeyPresses(bool extra = false)
	{
		if (!extra)
		{
			int result;
			if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
			{
				Chat instance = Chat.instance;
				result = ((instance != null && instance.HasFocus()) ? 1 : 0);
			}
			else
			{
				result = 1;
			}
			return (byte)result != 0;
		}
		int result2;
		if (!((Object)(object)ZNetScene.instance == (Object)null) && !((Object)(object)Player.m_localPlayer == (Object)null) && !Minimap.IsOpen() && !Console.IsVisible() && !TextInput.IsVisible() && !ZNet.instance.InPasswordDialog())
		{
			Chat instance2 = Chat.instance;
			if ((instance2 == null || !instance2.HasFocus()) && !StoreGui.IsVisible() && !InventoryGui.IsVisible() && !Menu.IsVisible())
			{
				TextViewer instance3 = TextViewer.instance;
				result2 = ((instance3 != null && instance3.IsVisible()) ? 1 : 0);
				goto IL_00d2;
			}
		}
		result2 = 1;
		goto IL_00d2;
		IL_00d2:
		return (byte)result2 != 0;
	}

	public static bool CheckKeyDown(string value)
	{
		try
		{
			return Input.GetKeyDown(value.ToLower());
		}
		catch
		{
			return false;
		}
	}

	public static bool CheckKeyHeld(string value, bool req = true)
	{
		try
		{
			return Input.GetKey(value.ToLower());
		}
		catch
		{
			return !req;
		}
	}
}
namespace AmmoCount;

[BepInPlugin("aedenthorn.AmmoCount", "Ammo Count", "0.4.0")]
public class BepInExPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(HotkeyBar), "Update")]
	private static class HotkeyBar_Update_Patch
	{
		private static void Postfix(HotkeyBar __instance, List<ItemData> ___m_items)
		{
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Expected O, but got Unknown
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a4: Expected O, but got Unknown
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Expected O, but got Unknown
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_020d: Expected O, but got Unknown
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_0252: Unknown result type (might be due to invalid IL or missing references)
			//IL_0284: Unknown result type (might be due to invalid IL or missing references)
			//IL_0296: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Expected O, but got Unknown
			//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			if (!modEnabled.Value || !Object.op_Implicit((Object)(object)Player.m_localPlayer))
			{
				return;
			}
			IEnumerable<object> enumerable = elementsAmmo.GetValue(__instance) as IEnumerable<object>;
			if (reset)
			{
				currentFont = GetFont(ammoStringFont.Value, ammoStringSize.Value);
			}
			using (IEnumerator<object> enumerator = enumerable.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current != null)
					{
						GameObject val = (GameObject)AccessTools.Field(enumerator.Current.GetType(), "m_go").GetValue(enumerator.Current);
						Transform val2 = val.transform.Find("AmmoCount");
						if ((Object)(object)val2 != (Object)null && (reset || !(bool)AccessTools.Field(enumerator.Current.GetType(), "m_used").GetValue(enumerator.Current)))
						{
							Object.Destroy((Object)(object)((Component)val2).gameObject);
						}
					}
				}
			}
			reset = false;
			for (int i = 0; i < ___m_items.Count(); i++)
			{
				int x = ___m_items[i].m_gridPos.x;
				if (x < 0 || x >= enumerable.Count() || enumerable.ElementAt(x) == null)
				{
					continue;
				}
				GameObject val3 = (GameObject)AccessTools.Field(enumerable.ElementAt(x).GetType(), "m_go").GetValue(enumerable.ElementAt(x));
				Transform val4 = val3.transform.Find("AmmoCount");
				if (val4 == null)
				{
					GameObject val5 = new GameObject("AmmoCount");
					val5.transform.SetParent(val3.transform);
					val5.AddComponent<RectTransform>().anchoredPosition = Vector2.zero;
					val4 = val5.transform;
					GameObject val6 = new GameObject("Text");
					val6.transform.SetParent(val5.transform);
					TextMeshProUGUI val7 = val6.AddComponent<TextMeshProUGUI>();
					val6.GetComponent<RectTransform>().anchoredPosition = ammoStringPosition.Value;
					val6.GetComponent<RectTransform>().sizeDelta = new Vector2(90f, 90f);
					((TMP_Text)val7).font = currentFont;
					((TMP_Text)val7).fontSize = ammoStringSize.Value;
					((TMP_Text)val7).fontStyle = ammoStringFontStyle.Value;
					((TMP_Text)val7).alignment = ammoStringAlignment.Value;
					((Graphic)val7).color = ammoStringColor.Value;
					if (showIcon.Value)
					{
						GameObject val8 = new GameObject("Icon");
						val8.transform.SetParent(val5.transform);
						val8.AddComponent<Image>();
						val8.GetComponent<RectTransform>().anchoredPosition = iconPosition.Value;
						val8.GetComponent<RectTransform>().sizeDelta = iconSize.Value;
					}
				}
				CheckAmmo(___m_items[i], ((Component)val4).gameObject);
			}
		}

		private static void CheckAmmo(ItemData itemData, GameObject go)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			if (string.IsNullOrEmpty(itemData.m_shared.m_ammoType) || (int)itemData.m_shared.m_itemType == 9)
			{
				go.SetActive(false);
				return;
			}
			ItemData val = FindAmmo(itemData);
			if (val == null)
			{
				go.SetActive(false);
				return;
			}
			go.SetActive(true);
			if (!string.IsNullOrEmpty(ammoStringFormat.Value))
			{
				((TMP_Text)((Component)go.transform.Find("Text")).GetComponent<TextMeshProUGUI>()).text = ammoStringFormat.Value.Replace("{amount}", val.m_stack.ToString()).Replace("{name}", Localization.instance.Localize(val.m_shared.m_name));
			}
			if (showIcon.Value)
			{
				((Component)go.transform.Find("Icon")).GetComponent<Image>().sprite = val.GetIcon();
			}
		}

		private static ItemData FindAmmo(ItemData weapon)
		{
			if (string.IsNullOrEmpty(weapon.m_shared.m_ammoType))
			{
				return null;
			}
			ItemData val = ((Humanoid)Player.m_localPlayer).GetAmmoItem();
			if (val != null && (!((Humanoid)Player.m_localPlayer).GetInventory().ContainsItem(val) || val.m_shared.m_ammoType != weapon.m_shared.m_ammoType))
			{
				val = null;
			}
			if (val == null)
			{
				val = ((Humanoid)Player.m_localPlayer).GetInventory().GetAmmoItem(weapon.m_shared.m_ammoType, (string)null);
			}
			return val;
		}
	}

	[HarmonyPatch(typeof(Terminal), "InputText")]
	private static class InputText_Patch
	{
		private static bool Prefix(Terminal __instance)
		{
			if (!modEnabled.Value)
			{
				return true;
			}
			string text = ((TMP_InputField)__instance.m_input).text;
			if (text.ToLower().Equals(typeof(BepInExPlugin).Namespace.ToLower() + " reset"))
			{
				((BaseUnityPlugin)context).Config.Reload();
				((BaseUnityPlugin)context).Config.Save();
				currentFont = GetFont(ammoStringFont.Value, ammoStringSize.Value);
				reset = true;
				Traverse.Create((object)__instance).Method("AddString", new object[1] { text }).GetValue();
				Traverse.Create((object)__instance).Method("AddString", new object[1] { ((BaseUnityPlugin)context).Info.Metadata.Name + " config reloaded" }).GetValue();
				return false;
			}
			return true;
		}
	}

	private static readonly bool isDebug = true;

	private static BepInExPlugin context;

	private Harmony harmony;

	public static ConfigEntry<bool> modEnabled;

	public static ConfigEntry<int> nexusID;

	public static ConfigEntry<bool> showIcon;

	public static ConfigEntry<Vector2> iconPosition;

	public static ConfigEntry<Vector2> iconSize;

	public static ConfigEntry<string> ammoStringFormat;

	public static ConfigEntry<int> ammoStringSize;

	public static ConfigEntry<string> ammoStringFont;

	public static ConfigEntry<FontStyles> ammoStringFontStyle;

	public static ConfigEntry<TextAlignmentOptions> ammoStringAlignment;

	public static ConfigEntry<Color> ammoStringColor;

	public static ConfigEntry<Vector2> ammoStringPosition;

	public static FieldInfo elementsAmmo;

	public static TMP_FontAsset currentFont;

	public static bool reset = true;

	public static void Dbgl(object obj, bool pref = true)
	{
		if (isDebug)
		{
			((BaseUnityPlugin)context).Logger.Log((LogLevel)16, (object)((pref ? (typeof(BepInExPlugin).Namespace + " ") : "") + obj));
		}
	}

	private void Awake()
	{
		//IL_0085: 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_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f4: Expected O, but got Unknown
		context = this;
		modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod");
		nexusID = ((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 2273, "Nexus mod ID for updates");
		showIcon = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "ShowIcon", true, "Show Ammo Icon");
		iconPosition = ((BaseUnityPlugin)this).Config.Bind<Vector2>("Options", "IconPosition", new Vector2(0f, 32f), "Icon position");
		iconSize = ((BaseUnityPlugin)this).Config.Bind<Vector2>("Options", "IconSize", new Vector2(20f, 20f), "Icon Size");
		ammoStringFormat = ((BaseUnityPlugin)this).Config.Bind<string>("Options", "AmmoTextFormat", "{amount}", "Ammo string. Use {amount} and/or {name}");
		ammoStringSize = ((BaseUnityPlugin)this).Config.Bind<int>("Options", "AmmoTextSize", 16, "Ammo count size.");
		ammoStringFont = ((BaseUnityPlugin)this).Config.Bind<string>("Options", "AmmoTextFont", "AveriaSansLibre-Bold", "Ammo count font");
		ammoStringFontStyle = ((BaseUnityPlugin)this).Config.Bind<FontStyles>("Options", "AmmoTextFontStyle", (FontStyles)1, "Ammo count font style");
		ammoStringAlignment = ((BaseUnityPlugin)this).Config.Bind<TextAlignmentOptions>("Options", "AmmoTextAlignment", (TextAlignmentOptions)260, "Ammo count alignment");
		ammoStringColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Options", "AmmoTextColor", new Color(1f, 1f, 1f, 1f), "Ammo count color");
		ammoStringPosition = ((BaseUnityPlugin)this).Config.Bind<Vector2>("Options", "AmmoTextPosition", new Vector2(-6f, -4f), "Ammo count position offset");
		elementsAmmo = AccessTools.Field(typeof(HotkeyBar), "m_elements");
		ammoStringFont.SettingChanged += SettingChanged;
		showIcon.SettingChanged += SettingChanged;
		iconPosition.SettingChanged += SettingChanged;
		iconSize.SettingChanged += SettingChanged;
		ammoStringFormat.SettingChanged += SettingChanged;
		ammoStringSize.SettingChanged += SettingChanged;
		ammoStringFontStyle.SettingChanged += SettingChanged;
		ammoStringAlignment.SettingChanged += SettingChanged;
		ammoStringColor.SettingChanged += SettingChanged;
		ammoStringPosition.SettingChanged += SettingChanged;
		harmony = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
		harmony.PatchAll();
	}

	private void SettingChanged(object sender, EventArgs e)
	{
		reset = true;
	}

	private void OnDestroy()
	{
		Dbgl("Destroying plugin");
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchAll((string)null);
		}
	}

	private static TMP_FontAsset GetFont(string fontName, int fontSize)
	{
		try
		{
			TMP_FontAsset[] array = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
			TMP_FontAsset[] array2 = array;
			foreach (TMP_FontAsset val in array2)
			{
				if (((Object)val).name == fontName)
				{
					return val;
				}
			}
		}
		catch
		{
		}
		return currentFont;
	}
}