Decompiled source of AmmoCounter v1.0.0

AmmoCounter.dll

Decompiled 2 hours 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 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.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("AmmoCounter")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+68aedd18a34cf6eb917a49fc855a64571235470c")]
[assembly: AssemblyProduct("AmmoCounter")]
[assembly: AssemblyTitle("AmmoCounter")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.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 AmmoCounter
{
	internal sealed class AmmoLabel : MonoBehaviour
	{
		private const float BaseFontSize = 20f;

		private static TMP_FontAsset font;

		private BatteryVisualLogic visual;

		private Settings.Place place;

		private bool inSlot;

		private TextMeshProUGUI text;

		private string shown;

		private ItemBattery battery;

		private bool isGun;

		public static void Tick(BatteryVisualLogic visual)
		{
			if (visual.inUI)
			{
				AmmoLabel ammoLabel = ((Component)visual).GetComponent<AmmoLabel>();
				if (!Object.op_Implicit((Object)(object)ammoLabel))
				{
					ammoLabel = ((Component)visual).gameObject.AddComponent<AmmoLabel>();
					ammoLabel.Init(visual);
				}
				ammoLabel.Refresh();
			}
		}

		private void Init(BatteryVisualLogic owner)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
			visual = owner;
			inSlot = Object.op_Implicit((Object)(object)((Component)owner).GetComponentInParent<InventorySpot>());
			place = (inSlot ? Settings.Inventory : Settings.Hand);
			GameObject val = new GameObject("AmmoCounter", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(((Component)owner).transform, false);
			text = val.AddComponent<TextMeshProUGUI>();
			((TMP_Text)text).font = Font();
			((Graphic)text).raycastTarget = false;
			((TMP_Text)text).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)text).overflowMode = (TextOverflowModes)0;
			((TMP_Text)text).outlineWidth = 0.2f;
			((TMP_Text)text).outlineColor = new Color32((byte)0, (byte)0, (byte)0, (byte)200);
			RectTransform rectTransform = ((TMP_Text)text).rectTransform;
			rectTransform.sizeDelta = new Vector2(120f, 30f);
			Vector2 val2 = default(Vector2);
			if (inSlot)
			{
				((Vector2)(ref val2))..ctor(0.5f, 1f);
				rectTransform.anchorMax = val2;
				rectTransform.anchorMin = val2;
				rectTransform.pivot = new Vector2(0.5f, 0f);
				((TMP_Text)text).alignment = (TextAlignmentOptions)1026;
			}
			else
			{
				((Vector2)(ref val2))..ctor(1f, 0.5f);
				rectTransform.anchorMax = val2;
				rectTransform.anchorMin = val2;
				rectTransform.pivot = new Vector2(0f, 0.5f);
				((TMP_Text)text).alignment = (TextAlignmentOptions)4097;
			}
			Transform transform = ((Component)owner).transform;
			RectTransform val3 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			if (val3 != null)
			{
				ManualLogSource log = Plugin.Log;
				string arg = (inSlot ? "de hueco" : "en la mano");
				Rect rect = val3.rect;
				log.LogDebug((object)$"Indicador {arg}: {((Rect)(ref rect)).size}, escala {((Transform)val3).lossyScale}");
			}
		}

		private void Refresh()
		{
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			string text = (place.Enabled.Value ? Value(visual.itemBattery) : null);
			if (text == null)
			{
				if (((Component)this.text).gameObject.activeSelf)
				{
					((Component)this.text).gameObject.SetActive(false);
				}
				return;
			}
			if (text != shown)
			{
				((TMP_Text)this.text).text = text;
				shown = text;
			}
			((TMP_Text)this.text).fontSize = 20f * place.Scale.Value;
			Vector2 val = (inSlot ? new Vector2(0f, 2f) : new Vector2(6f, 0f));
			((TMP_Text)this.text).rectTransform.anchoredPosition = val + new Vector2(place.OffsetX.Value, place.OffsetY.Value);
			if (!((Component)this.text).gameObject.activeSelf)
			{
				((Component)this.text).gameObject.SetActive(true);
			}
		}

		private string Value(ItemBattery item)
		{
			if (!Object.op_Implicit((Object)(object)item))
			{
				return null;
			}
			if ((Object)(object)item != (Object)(object)battery)
			{
				battery = item;
				isGun = Object.op_Implicit((Object)(object)((Component)item).GetComponent<ItemGun>());
			}
			if (isGun)
			{
				int batteryBars = item.batteryBars;
				int num = Mathf.Clamp(item.batteryLifeInt, 0, batteryBars);
				string arg = ((num == 0) ? "#FF4D4F" : "#FFFFFF");
				return $"<color={arg}>{num}</color><size=60%><color=#FFFFFFAA>/{batteryBars}</color></size>";
			}
			if (Settings.Items.Value != CounterItems.AllBatteryItems)
			{
				return null;
			}
			return $"{Mathf.CeilToInt(Mathf.Clamp(item.batteryLife, 0f, 100f))}<size=60%><color=#FFFFFFAA>%</color></size>";
		}

		private static TMP_FontAsset Font()
		{
			if (Object.op_Implicit((Object)(object)font))
			{
				return font;
			}
			font = ((IEnumerable<TMP_FontAsset>)Resources.FindObjectsOfTypeAll<TMP_FontAsset>()).FirstOrDefault((Func<TMP_FontAsset, bool>)((TMP_FontAsset f) => Object.op_Implicit((Object)(object)f) && ((Object)f).name.StartsWith("Teko", StringComparison.OrdinalIgnoreCase))) ?? TMP_Settings.defaultFontAsset;
			return font;
		}
	}
	[BepInPlugin("vicxelcode.AmmoCounter", "Ammo Counter", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "vicxelcode.AmmoCounter";

		public const string Name = "Ammo Counter";

		public const string Version = "1.0.0";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Settings.Bind(((BaseUnityPlugin)this).Config);
			new Harmony("vicxelcode.AmmoCounter").PatchAll(typeof(Plugin).Assembly);
			Log.LogInfo((object)"Ammo Counter 1.0.0 cargado");
		}
	}
	public enum CounterItems
	{
		Guns,
		AllBatteryItems
	}
	internal static class Settings
	{
		internal sealed class Place
		{
			public ConfigEntry<bool> Enabled;

			public ConfigEntry<float> Scale;

			public ConfigEntry<float> OffsetX;

			public ConfigEntry<float> OffsetY;
		}

		public static ConfigEntry<CounterItems> Items;

		public static Place Inventory;

		public static Place Hand;

		public static void Bind(ConfigFile cfg)
		{
			Items = cfg.Bind<CounterItems>("General", "Items", CounterItems.Guns, "Guns: shots left on guns only. AllBatteryItems: also the battery percentage of drones, staffs and other battery items.");
			Inventory = BindPlace(cfg, "Inventory", "Show the shots left on each inventory slot (extra slots added by other mods included).");
			Hand = BindPlace(cfg, "Held item", "Show the shots left next to the battery of the item you are holding.");
		}

		private static Place BindPlace(ConfigFile cfg, string section, string what)
		{
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			return new Place
			{
				Enabled = cfg.Bind<bool>(section, "Enabled", true, what),
				Scale = cfg.Bind<float>(section, "Scale", 1f, new ConfigDescription("Size of the number.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 2.5f), Array.Empty<object>())),
				OffsetX = cfg.Bind<float>(section, "OffsetX", 0f, new ConfigDescription("Moves the number right (or left if negative), in HUD pixels.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-200f, 200f), Array.Empty<object>())),
				OffsetY = cfg.Bind<float>(section, "OffsetY", 0f, new ConfigDescription("Moves the number up (or down if negative), in HUD pixels.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-200f, 200f), Array.Empty<object>()))
			};
		}
	}
}
namespace AmmoCounter.Patches
{
	[HarmonyPatch(typeof(BatteryVisualLogic), "Update")]
	internal static class BatteryVisualPatch
	{
		private static bool failed;

		private static void Postfix(BatteryVisualLogic __instance)
		{
			if (failed)
			{
				return;
			}
			try
			{
				AmmoLabel.Tick(__instance);
			}
			catch (Exception arg)
			{
				failed = true;
				Plugin.Log.LogError((object)$"Contador desactivado: {arg}");
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}