Decompiled source of TooManyItems v1.0.1

plugins/TooManyItems.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Newtonsoft.Json;
using On.RoR2;
using On.RoR2.UI;
using RoR2;
using RoR2.UI;
using RoR2ItemInfo.hud;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("TooManyItems")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0f0df431e3545b1424bdef3709dbdafb224afe8d")]
[assembly: AssemblyProduct("TooManyItems")]
[assembly: AssemblyTitle("TooManyItems")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
}
namespace RoR2ItemInfo
{
	public class StatEntry
	{
		[JsonProperty("stat")]
		public string Stat = null;

		[JsonProperty("value")]
		public string Value = null;

		[JsonProperty("stack")]
		public string Stack = null;

		[JsonProperty("add")]
		public string Add = null;
	}
	public class ItemData
	{
		[JsonProperty("name")]
		public string Name = null;

		[JsonProperty("item_id")]
		public string ItemId = null;

		[JsonProperty("rarity")]
		public string Rarity = null;

		[JsonProperty("category")]
		public string Category = null;

		[JsonProperty("caption")]
		public string Caption = null;

		[JsonProperty("description")]
		public string Description = null;

		[JsonProperty("token")]
		public string Token = null;

		[JsonProperty("desc_token")]
		public string DescToken = null;

		[JsonProperty("stats")]
		public List<StatEntry> Stats = null;
	}
	public static class ItemDatabase
	{
		public static Dictionary<string, ItemData> ById = new Dictionary<string, ItemData>();

		public static Dictionary<string, ItemData> ByToken = new Dictionary<string, ItemData>();

		public static Dictionary<string, ItemData> ByName = new Dictionary<string, ItemData>();

		public static void Load()
		{
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			using Stream stream = executingAssembly.GetManifestResourceStream("TooManyItems.ror2_items.json");
			using StreamReader streamReader = new StreamReader(stream);
			List<ItemData> list = JsonConvert.DeserializeObject<List<ItemData>>(streamReader.ReadToEnd());
			if (list == null)
			{
				return;
			}
			foreach (ItemData item in list)
			{
				if (!string.IsNullOrEmpty(item.ItemId))
				{
					ById[item.ItemId] = item;
				}
				if (!string.IsNullOrEmpty(item.Token))
				{
					ByToken[item.Token] = item;
				}
				if (!string.IsNullOrEmpty(item.Name))
				{
					ByName[item.Name] = item;
				}
			}
			Plugin.Log.LogInfo((object)$"ItemDatabase: loaded {ById.Count} items.");
		}
	}
	[BepInPlugin("me.garnet14.tooManyItems", "Too Many Items", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource Log;

		private static BaseHUD? _overworldHUD;

		private static BaseHUD? _commandHUD;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			ItemDatabase.Load();
			_overworldHUD = new OverworldHUD();
			_overworldHUD.Init();
			_commandHUD = new ArtifactOfCommandHUD();
			_commandHUD.Init();
			Log.LogInfo((object)"RoR2 Item Info loaded!");
		}
	}
}
namespace RoR2ItemInfo.hud
{
	public class ArtifactOfCommandHUD : BaseHUD
	{
		protected override string GetPanelName()
		{
			return "ArtifactOfCommandInfoPanel";
		}

		protected override float GetDefaultWidth()
		{
			return 400f;
		}

		public override void Init()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			base.Init();
			PickupPickerPanel.OnCreateButton += new hook_OnCreateButton(OnPickupPickerCreateButton);
		}

		private void OnPickupPickerCreateButton(orig_OnCreateButton orig, PickupPickerPanel self, int index, MPButton button)
		{
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Expected O, but got Unknown
			//IL_0107: 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_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Expected O, but got Unknown
			orig.Invoke(self, index, button);
			try
			{
				FieldInfo field = typeof(PickupPickerPanel).GetField("pickupOptions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field == null || !(field.GetValue(self) is Option[] array) || index >= array.Length)
				{
					return;
				}
				PickupDef pickupDef = PickupCatalog.GetPickupDef(((Option)(ref array[index])).pickupIndex);
				if (pickupDef == null)
				{
					return;
				}
				ItemData data = ResolveFromPickupDef(pickupDef);
				if (data != null)
				{
					EnsurePanelOnTop(self);
					EventTrigger val = ((Component)button).gameObject.GetComponent<EventTrigger>() ?? ((Component)button).gameObject.AddComponent<EventTrigger>();
					Entry val2 = new Entry
					{
						eventID = (EventTriggerType)0
					};
					((UnityEvent<BaseEventData>)(object)val2.callback).AddListener((UnityAction<BaseEventData>)delegate
					{
						ShowPanel(getInfoText(data));
					});
					val.triggers.Add(val2);
					Entry val3 = new Entry
					{
						eventID = (EventTriggerType)1
					};
					((UnityEvent<BaseEventData>)(object)val3.callback).AddListener((UnityAction<BaseEventData>)delegate
					{
						HidePanel();
					});
					val.triggers.Add(val3);
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"ArtifactOfCommandHUD error: {arg}");
			}
		}

		private void EnsurePanelOnTop(PickupPickerPanel pickerPanel)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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_00ba: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_panel == (Object)null))
			{
				Canvas componentInParent = ((Component)pickerPanel).GetComponentInParent<Canvas>();
				if ((Object)(object)componentInParent != (Object)null && (Object)(object)_panel.transform.parent != (Object)(object)((Component)componentInParent).transform)
				{
					_panel.transform.SetParent(((Component)componentInParent).transform, false);
					RectTransform component = _panel.GetComponent<RectTransform>();
					component.anchorMin = new Vector2(0f, 0.5f);
					component.anchorMax = new Vector2(0f, 0.5f);
					component.pivot = new Vector2(0f, 0.5f);
					component.anchoredPosition = new Vector2(350f, 0f);
				}
			}
		}

		private static ItemData? ResolveFromPickupDef(PickupDef pickupDef)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			string text = null;
			if ((int)pickupDef.itemIndex != -1)
			{
				text = ItemCatalog.GetItemDef(pickupDef.itemIndex)?.nameToken;
			}
			else if ((int)pickupDef.equipmentIndex != -1)
			{
				text = EquipmentCatalog.GetEquipmentDef(pickupDef.equipmentIndex)?.nameToken;
			}
			if (text != null && ItemDatabase.ByToken.TryGetValue(text, out ItemData value))
			{
				return value;
			}
			return null;
		}
	}
	public abstract class BaseHUD
	{
		protected GameObject? _panel;

		protected HGTextMeshProUGUI? _panelText;

		protected HUD? _hud;

		protected virtual float GetDefaultWidth()
		{
			return 360f;
		}

		public virtual void Init()
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			HUD.Awake += new hook_Awake(OnHUDAwake);
		}

		private void OnHUDAwake(orig_Awake orig, HUD self)
		{
			orig.Invoke(self);
			_hud = self;
			CreatePanel();
		}

		protected virtual void OnHUDUpdate(orig_Update orig, HUD self)
		{
			orig.Invoke(self);
			if ((Object)(object)_panel != (Object)null && !Object.op_Implicit((Object)(object)_panel))
			{
				_panel = null;
				_panelText = null;
			}
		}

		protected virtual void CreatePanel(float posX = 8f, float posY = 0f, float width = 360f, float height = 200f)
		{
			if (!((Object)(object)_hud == (Object)null))
			{
				Transform val = ((Component)_hud).transform.Find("MainContainer/MainUIArea/SpringCanvas/UpperLeftCluster");
				if (!((Object)(object)val == (Object)null))
				{
					_panel = Object.Instantiate<GameObject>(((Component)val).gameObject, val.parent);
					((Object)_panel).name = GetPanelName();
					CleanupPanel();
					SetupPositionAndSize(posX, posY, width, height);
					SetupText();
					_panel.SetActive(false);
				}
			}
		}

		protected abstract string GetPanelName();

		protected virtual void CleanupPanel()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_0126: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_panel == (Object)null)
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			foreach (Transform item in _panel.transform)
			{
				Transform val = item;
				list.Add(((Component)val).gameObject);
			}
			foreach (GameObject item2 in list)
			{
				Object.DestroyImmediate((Object)(object)item2);
			}
			VerticalLayoutGroup component = _panel.GetComponent<VerticalLayoutGroup>();
			if ((Object)(object)component != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component);
			}
			ContentSizeFitter component2 = _panel.GetComponent<ContentSizeFitter>();
			if ((Object)(object)component2 != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component2);
			}
			Canvas component3 = _panel.GetComponent<Canvas>();
			if ((Object)(object)component3 != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component3);
			}
			Image component4 = _panel.GetComponent<Image>();
			if ((Object)(object)component4 != (Object)null)
			{
				((Graphic)component4).color = new Color(((Graphic)component4).color.r, ((Graphic)component4).color.g, ((Graphic)component4).color.b, 0.75f);
			}
		}

		protected virtual void SetupPositionAndSize(float posX, float posY, float width, float height)
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_panel == (Object)null))
			{
				RectTransform component = _panel.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(0f, 0.5f);
				component.anchorMax = new Vector2(0f, 0.5f);
				component.pivot = new Vector2(0f, 0.5f);
				component.anchoredPosition = new Vector2(posX, posY);
				component.sizeDelta = new Vector2(width, height);
			}
		}

		protected virtual void SetupText()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_panel == (Object)null) && !((Object)(object)_hud == (Object)null))
			{
				GameObject val = new GameObject("PanelText");
				val.transform.SetParent(_panel.transform, false);
				RectTransform val2 = val.AddComponent<RectTransform>();
				val2.anchorMin = Vector2.zero;
				val2.anchorMax = Vector2.one;
				val2.offsetMin = new Vector2(12f, 12f);
				val2.offsetMax = new Vector2(-12f, -12f);
				_panelText = val.AddComponent<HGTextMeshProUGUI>();
				((TMP_Text)_panelText).fontSize = 14f;
				((TMP_Text)_panelText).enableWordWrapping = true;
				((Graphic)_panelText).color = Color.white;
				HGTextMeshProUGUI componentInChildren = ((Component)_hud).GetComponentInChildren<HGTextMeshProUGUI>();
				if ((Object)(object)componentInChildren != (Object)null)
				{
					((TMP_Text)_panelText).font = ((TMP_Text)componentInChildren).font;
				}
			}
		}

		protected virtual string getInfoText(ItemData data)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<color=#FFFFFF><b>    " + data.Name + "</b></color>");
			stringBuilder.Append("<color=#CDCDCD>\n" + data.Description + "</color>");
			if (data.Stats != null && data.Stats.Count > 0)
			{
				stringBuilder.Append("\n\n<color=#61CCE8><b>    Stats</b></color>");
				foreach (StatEntry stat in data.Stats)
				{
					stringBuilder.Append("\n<color=#E5C962>" + stat.Stat + "</color>: " + stat.Value);
					if (!string.IsNullOrEmpty(stat.Add))
					{
						stringBuilder.Append("<color=#CDCDCD>  (" + stat.Add + " per stack)</color>");
					}
				}
			}
			return stringBuilder.ToString();
		}

		public virtual void ShowPanel(string content)
		{
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_panel == (Object)null) && !((Object)(object)_panelText == (Object)null))
			{
				_panel.SetActive(true);
				((TMP_Text)_panelText).text = content;
				((TMP_Text)_panelText).ForceMeshUpdate(false, false);
				float num = ((TMP_Text)_panelText).preferredHeight + 24f;
				RectTransform component = _panel.GetComponent<RectTransform>();
				component.sizeDelta = new Vector2(GetDefaultWidth(), num);
				LayoutRebuilder.ForceRebuildLayoutImmediate(component);
			}
		}

		public virtual void HidePanel()
		{
			if ((Object)(object)_panel != (Object)null && _panel.activeSelf)
			{
				_panel.SetActive(false);
			}
		}
	}
	public class OverworldHUD : BaseHUD
	{
		private string? _lastItemId;

		private GameObject? _currentTarget;

		public override void Init()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Expected O, but got Unknown
			base.Init();
			Interactor.FindBestInteractableObject += new hook_FindBestInteractableObject(OnFindBestInteractableObject);
			HUD.Update += new hook_Update(OnHUDUpdate);
		}

		protected override string GetPanelName()
		{
			return "OverworldItemInfoPanel";
		}

		protected override void OnHUDUpdate(orig_Update orig, HUD self)
		{
			base.OnHUDUpdate(orig, self);
			if ((Object)(object)_currentTarget == (Object)null)
			{
				HidePanel();
			}
			_currentTarget = null;
		}

		private GameObject? OnFindBestInteractableObject(orig_FindBestInteractableObject orig, Interactor self, Ray raycastRay, float maxRaycastDistance, Vector3 overlapPosition, float overlapRadius)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = orig.Invoke(self, raycastRay, maxRaycastDistance * 2f, overlapPosition, overlapRadius);
			try
			{
				if ((Object)(object)val == (Object)null)
				{
					return val;
				}
				ItemData itemDataFromTarget = GetItemDataFromTarget(val);
				if (itemDataFromTarget == null)
				{
					HidePanel();
					return val;
				}
				_currentTarget = val;
				if (_lastItemId == itemDataFromTarget.ItemId && (Object)(object)_panel != (Object)null)
				{
					_panel.SetActive(true);
					return val;
				}
				_lastItemId = itemDataFromTarget.ItemId;
				ShowPanel(getInfoText(itemDataFromTarget));
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"OverworldHUD error: {arg}");
			}
			return val;
		}

		private static ItemData? GetItemDataFromTarget(GameObject target)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			GenericPickupController component = target.GetComponent<GenericPickupController>();
			if ((Object)(object)component != (Object)null)
			{
				PickupDef pickupDef = PickupCatalog.GetPickupDef(component.pickupIndex);
				if (pickupDef != null)
				{
					return ResolveFromPickupDef(pickupDef);
				}
			}
			ShopTerminalBehavior component2 = target.GetComponent<ShopTerminalBehavior>();
			if ((Object)(object)component2 != (Object)null)
			{
				PickupDef pickupDef2 = PickupCatalog.GetPickupDef(component2.CurrentPickupIndex());
				if (pickupDef2 != null)
				{
					return ResolveFromPickupDef(pickupDef2);
				}
			}
			return null;
		}

		private static ItemData? ResolveFromPickupDef(PickupDef pickupDef)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			string text = null;
			if ((int)pickupDef.itemIndex != -1)
			{
				text = ItemCatalog.GetItemDef(pickupDef.itemIndex)?.nameToken;
			}
			else if ((int)pickupDef.equipmentIndex != -1)
			{
				text = EquipmentCatalog.GetEquipmentDef(pickupDef.equipmentIndex)?.nameToken;
			}
			if (text != null && ItemDatabase.ByToken.TryGetValue(text, out ItemData value))
			{
				return value;
			}
			return null;
		}
	}
}