Decompiled source of InventoryHelper v1.0.0

InventoryHelper.dll

Decompiled 19 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 BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[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: AssemblyCompany("InventoryHelper")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("InventoryHelper")]
[assembly: AssemblyTitle("InventoryHelper")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 InventoryHelper
{
	[BepInPlugin("mythicalbird.valheim.inventoryhelper", "InventoryHelper", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private class Row
		{
			private readonly TextMeshProUGUI _text;

			private readonly LayoutElement _icon;

			private readonly string _prefix;

			private readonly float _iconScale;

			public readonly string IconName;

			public Row(Transform parent, TMP_Text font, Sprite icon, string fallbackLabel, float iconScale = 1f)
			{
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Expected O, but got Unknown
				//IL_0091: Unknown result type (might be due to invalid IL or missing references)
				//IL_0097: Expected O, but got Unknown
				//IL_0122: Unknown result type (might be due to invalid IL or missing references)
				//IL_0128: Expected O, but got Unknown
				//IL_0185: Unknown result type (might be due to invalid IL or missing references)
				_iconScale = iconScale;
				GameObject val = new GameObject("Row", new Type[1] { typeof(RectTransform) });
				val.transform.SetParent(parent, false);
				HorizontalLayoutGroup obj = val.AddComponent<HorizontalLayoutGroup>();
				((LayoutGroup)obj).childAlignment = (TextAnchor)3;
				((HorizontalOrVerticalLayoutGroup)obj).spacing = 4f;
				((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = (((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = true);
				((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = (((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false);
				if ((Object)(object)icon != (Object)null)
				{
					GameObject val2 = new GameObject("Icon", new Type[1] { typeof(RectTransform) });
					val2.transform.SetParent(val.transform, false);
					Image obj2 = val2.AddComponent<Image>();
					obj2.sprite = icon;
					obj2.preserveAspect = true;
					((Graphic)obj2).raycastTarget = false;
					_icon = val2.AddComponent<LayoutElement>();
					IconName = ((Object)icon).name;
				}
				else
				{
					_prefix = (string.IsNullOrEmpty(fallbackLabel) ? "" : (fallbackLabel + " "));
					IconName = "none";
				}
				GameObject val3 = new GameObject("Text", new Type[1] { typeof(RectTransform) });
				val3.transform.SetParent(val.transform, false);
				_text = val3.AddComponent<TextMeshProUGUI>();
				if ((Object)(object)font != (Object)null && (Object)(object)font.font != (Object)null)
				{
					((TMP_Text)_text).font = font.font;
					((TMP_Text)_text).fontSharedMaterial = font.fontSharedMaterial;
				}
				((Graphic)_text).color = Color.white;
				((TMP_Text)_text).textWrappingMode = (TextWrappingModes)0;
				((Graphic)_text).raycastTarget = false;
			}

			public void Set(string value, float fontSize, Color iconColor)
			{
				//IL_0064: Unknown result type (might be due to invalid IL or missing references)
				((TMP_Text)_text).text = _prefix + value;
				((TMP_Text)_text).fontSize = fontSize;
				if (!((Object)(object)_icon == (Object)null))
				{
					LayoutElement icon = _icon;
					float preferredWidth = (_icon.preferredHeight = fontSize * 1.2f * _iconScale);
					icon.preferredWidth = preferredWidth;
					((Graphic)((Component)_icon).GetComponent<Image>()).color = iconColor;
				}
			}
		}

		public const string Guid = "mythicalbird.valheim.inventoryhelper";

		public const string ModName = "InventoryHelper";

		public const string Version = "1.0.0";

		private ConfigEntry<bool> _enabled;

		private ConfigEntry<float> _fontSize;

		private ConfigEntry<Vector2> _offset;

		private RectTransform _root;

		private Row _weight;

		private Row _divider;

		private Row _slots;

		private float _nextRefresh;

		private static readonly Color Orange = new Color(1f, 0.6f, 0.1f);

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show inventory weight and free slots under the health bar.");
			_fontSize = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FontSize", 18f, new ConfigDescription("Text size.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(8f, 40f), Array.Empty<object>()));
			_offset = ((BaseUnityPlugin)this).Config.Bind<Vector2>("General", "OffsetFromHealthBar", new Vector2(0f, -2f), "Position relative to the food column's left edge, under the fork and heart icons (negative y = further down).");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"InventoryHelper 1.0.0 loaded");
		}

		private void Update()
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: 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_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			Hud instance = Hud.instance;
			if (!((Object)(object)localPlayer == (Object)null) && !((Object)(object)instance == (Object)null) && !(Time.time < _nextRefresh))
			{
				_nextRefresh = Time.time + 0.25f;
				if ((Object)(object)_root == (Object)null)
				{
					CreateLabel(instance);
				}
				((Component)_root).gameObject.SetActive(_enabled.Value);
				if (instance.m_foodIcons != null && instance.m_foodIcons.Length != 0 && !((Object)(object)instance.m_foodIcon == (Object)null))
				{
					Vector3[] array = (Vector3[])(object)new Vector3[4];
					((Graphic)instance.m_foodIcons[0]).rectTransform.GetWorldCorners(array);
					float x = array[0].x;
					((Graphic)instance.m_foodIcon).rectTransform.GetWorldCorners(array);
					((Transform)_root).position = new Vector3(x, array[0].y, ((Transform)_root).position.z);
					RectTransform root = _root;
					root.anchoredPosition += _offset.Value + new Vector2(0f, -6f);
					Inventory inventory = ((Humanoid)localPlayer).GetInventory();
					float value = _fontSize.Value;
					float totalWeight = inventory.GetTotalWeight();
					float maxCarryWeight = localPlayer.GetMaxCarryWeight();
					_weight.Set($"{Mathf.FloorToInt(totalWeight)}/{Mathf.FloorToInt(maxCarryWeight)}", value, FreeColor(maxCarryWeight - totalWeight, maxCarryWeight));
					_divider.Set("|", value, Color.white);
					_slots.Set(inventory.GetEmptySlots().ToString(), value, Color.white);
				}
			}
		}

		private static Color FreeColor(float free, float all)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			float num = ((all > 0f) ? (free / all) : 0f);
			if (!(num <= 0.1f))
			{
				if (!(num <= 0.3f))
				{
					return Color.white;
				}
				return Orange;
			}
			return Color.red;
		}

		private void CreateLabel(Hud hud)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("InventoryHelper_Label", new Type[1] { typeof(RectTransform) });
			_root = (RectTransform)val.transform;
			((Transform)_root).SetParent(hud.m_rootObject.transform, false);
			RectTransform root = _root;
			RectTransform root2 = _root;
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0f, 1f);
			root2.anchorMax = val2;
			root.anchorMin = val2;
			_root.pivot = new Vector2(0f, 1f);
			HorizontalLayoutGroup obj = val.AddComponent<HorizontalLayoutGroup>();
			((LayoutGroup)obj).childAlignment = (TextAnchor)3;
			((HorizontalOrVerticalLayoutGroup)obj).spacing = 8f;
			bool childControlWidth = (((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = true);
			((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = childControlWidth;
			childControlWidth = (((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false);
			((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = childControlWidth;
			ContentSizeFitter obj2 = val.AddComponent<ContentSizeFitter>();
			FitMode horizontalFit = (FitMode)2;
			obj2.verticalFit = (FitMode)2;
			obj2.horizontalFit = horizontalFit;
			TMP_Text healthText = hud.m_healthText;
			_weight = new Row((Transform)(object)_root, healthText, WeightIcon(), "Weight");
			_divider = new Row((Transform)(object)_root, healthText, null, null);
			_slots = new Row((Transform)(object)_root, healthText, SlotsIcon(), "Slots", 2f / 3f);
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Icons: weight '" + _weight.IconName + "', slots '" + _slots.IconName + "'"));
		}

		private static Sprite WeightIcon()
		{
			TMP_Text weight = (Object.op_Implicit((Object)(object)InventoryGui.instance) ? InventoryGui.instance.m_weight : null);
			if ((Object)(object)weight == (Object)null)
			{
				return null;
			}
			List<Image> source = (from i in ((Component)weight.transform.parent).GetComponentsInChildren<Image>(true)
				where (Object)(object)i.sprite != (Object)null && (Object)(object)((Component)i).transform != (Object)(object)weight.transform.parent
				select i).ToList();
			Image? obj = ((IEnumerable<Image>)source).FirstOrDefault((Func<Image, bool>)((Image i) => ((Object)i.sprite).name.ToLower().Contains("weight"))) ?? source.FirstOrDefault();
			if (obj == null)
			{
				return null;
			}
			return obj.sprite;
		}

		private static Sprite SlotsIcon()
		{
			//IL_0006: 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)
			//IL_0017: Expected O, but got Unknown
			//IL_009a: 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)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(32, 32, (TextureFormat)4, false)
			{
				name = "InventoryHelper_Slots"
			};
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(0.85f, 0.85f, 0.85f, 1f);
			for (int i = 0; i < 32; i++)
			{
				for (int j = 0; j < 32; j++)
				{
					bool flag = (j < 14 || j >= 18) && (i < 14 || i >= 18);
					val.SetPixel(j, i, flag ? val2 : Color.clear);
				}
			}
			val.Apply();
			Sprite obj = Sprite.Create(val, new Rect(0f, 0f, 32f, 32f), new Vector2(0.5f, 0.5f));
			((Object)obj).name = ((Object)val).name;
			return obj;
		}
	}
}