Decompiled source of BetterPickupNotifications v1.3.3

BetterPickupNotifications.dll

Decompiled 3 weeks ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
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.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BetterPickupNotifications")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterPickupNotifications")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a8d07b3e-fc2c-4b2d-9b46-176aab287633")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace BetterPickupNotifications;

[HarmonyPatch(typeof(MessageHud))]
public static class MessageHudPatches
{
	private class PickupMessage : MsgData
	{
		public float Timer;
	}

	private class PickupDisplay
	{
		public GameObject RootGO;

		private Image IconComp;

		private TMP_Text TextComp;

		private int Index;

		public PickupDisplay(int index)
		{
			Index = index;
			CreateUI();
		}

		private void CreateUI()
		{
			//IL_0085: 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_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: 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_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: 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_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			RootGO = Object.Instantiate<GameObject>(((Component)((Component)MessageHud.instance.m_messageText).gameObject.transform.parent).gameObject, ((Component)MessageHud.instance.m_messageText).gameObject.transform.parent.parent);
			RootGO.transform.SetAsFirstSibling();
			IconComp = RootGO.GetComponentInChildren<Image>();
			TextComp = RootGO.GetComponentInChildren<TMP_Text>();
			Transform transform = RootGO.transform;
			Vector3 position = transform.position;
			Vector3 up = Vector3.up;
			Rect rect = ((Graphic)IconComp).rectTransform.rect;
			transform.position = position + up * (0f - ((Rect)(ref rect)).height * Plugin.MessageVerticalSpacingModifier.Value) * (float)(Index + 1);
			Transform transform2 = ((Component)TextComp).gameObject.transform;
			Vector3 position2 = transform2.position;
			Vector3 up2 = Vector3.up;
			rect = ((Graphic)IconComp).rectTransform.rect;
			Vector3 val = up2 * (0f - ((Rect)(ref rect)).height) * Plugin.MessageTextVerticalModifier.Value;
			Vector3 right = Vector3.right;
			rect = ((Graphic)IconComp).rectTransform.rect;
			transform2.position = position2 + (val + right * ((Rect)(ref rect)).width * Plugin.MessageTextHorizontalSpacingModifier.Value);
		}

		public void Display(PickupMessage msg)
		{
			try
			{
				((Graphic)TextComp).canvasRenderer.SetAlpha(1f);
				((Graphic)TextComp).CrossFadeAlpha(1f, 0f, true);
				if (((MsgData)msg).m_amount > 1)
				{
					TextComp.text = ((MsgData)msg).m_text + " x" + ((MsgData)msg).m_amount;
				}
				else
				{
					TextComp.text = ((MsgData)msg).m_text;
				}
				IconComp.sprite = ((MsgData)msg).m_icon;
				((Graphic)IconComp).canvasRenderer.SetAlpha(1f);
				((Graphic)IconComp).CrossFadeAlpha(1f, 0f, true);
			}
			catch
			{
				if (msg != null && (Object)(object)((MsgData)msg).m_icon != (Object)null)
				{
					CreateUI();
					Display(msg);
				}
			}
		}

		public void FadeAway()
		{
			((Graphic)TextComp).CrossFadeAlpha(0f, Plugin.MessageFadeTime.Value, true);
			((Graphic)IconComp).CrossFadeAlpha(0f, Plugin.MessageFadeTime.Value, true);
		}
	}

	private static List<PickupMessage> PickupMessages;

	private static List<PickupDisplay> PickupDisplays;

	[HarmonyPrefix]
	[HarmonyPatch("ShowMessage")]
	public static bool ShowMessagePrefix(MessageHud __instance, MessageType type, string text, int amount, Sprite icon)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Invalid comparison between Unknown and I4
		if (Hud.IsUserHidden())
		{
			return false;
		}
		text = Localization.instance.Localize(text);
		if ((int)type == 2 || string.IsNullOrWhiteSpace(text) || amount < 1 || (Object)(object)icon == (Object)null)
		{
			return true;
		}
		int i;
		for (i = 0; i < PickupMessages.Count && (PickupMessages[i] == null || !(((MsgData)PickupMessages[i]).m_text == text)); i++)
		{
		}
		if (i == PickupMessages.Count)
		{
			i = PickupMessages.IndexOf(null);
			if (i < 0)
			{
				i = PickupMessages.Count;
				PickupMessages.Add(null);
				PickupDisplays.Add(new PickupDisplay(i));
			}
			PickupMessages[i] = new PickupMessage
			{
				m_text = text,
				m_amount = amount,
				m_icon = icon,
				Timer = Plugin.MessageLifetime.Value
			};
			PickupDisplays[i].Display(PickupMessages[i]);
		}
		else
		{
			PickupMessage pickupMessage = PickupMessages[i];
			((MsgData)pickupMessage).m_amount = ((MsgData)pickupMessage).m_amount + amount;
			if (Plugin.ResetMessageTimerOnDupePickup.Value)
			{
				PickupMessages[i].Timer = Plugin.MessageLifetime.Value;
			}
			else
			{
				PickupMessages[i].Timer += Plugin.MessageBumpTime.Value;
				if (PickupMessages[i].Timer > Plugin.MessageLifetime.Value)
				{
					PickupMessages[i].Timer = Plugin.MessageLifetime.Value;
				}
				PickupDisplays[i].Display(PickupMessages[i]);
			}
		}
		return false;
	}

	[HarmonyPrefix]
	[HarmonyPatch("UpdateMessage")]
	public static bool UpdateMessagePrefix(MessageHud __instance, float dt)
	{
		for (int i = 0; i < PickupMessages.Count; i++)
		{
			if (PickupMessages[i] != null)
			{
				PickupMessages[i].Timer -= dt;
				if (PickupMessages[i].Timer <= 0f)
				{
					PickupMessages[i] = null;
					PickupDisplays[i].FadeAway();
				}
			}
		}
		return true;
	}

	[HarmonyPostfix]
	[HarmonyPatch("Awake")]
	public static void AwakePostfix()
	{
		PickupMessages = new List<PickupMessage>();
		PickupDisplays = new List<PickupDisplay>();
	}

	[HarmonyPostfix]
	[HarmonyPatch("OnDestroy")]
	public static void OnDestroyPostfix()
	{
		PickupMessages = null;
		PickupDisplays = null;
	}
}
[BepInPlugin("pfhoenix.betterpickupnotifications", "Better Pickup Notifications", "1.3.3")]
public class Plugin : BaseUnityPlugin
{
	public const string Version = "1.3.3";

	public const string ModName = "Better Pickup Notifications";

	private Harmony _Harmony;

	public static ManualLogSource Log;

	public static ConfigEntry<float> MessageLifetime;

	public static ConfigEntry<float> MessageFadeTime;

	public static ConfigEntry<float> MessageBumpTime;

	public static ConfigEntry<bool> ResetMessageTimerOnDupePickup;

	public static ConfigEntry<float> MessageVerticalSpacingModifier;

	public static ConfigEntry<float> MessageTextHorizontalSpacingModifier;

	public static ConfigEntry<float> MessageTextVerticalModifier;

	private void Awake()
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		Log = new ManualLogSource((string)null);
		MessageLifetime = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageLifetime", 4f, "How long a notification displays on the HUD before fading away");
		MessageFadeTime = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageFadeTime", 2f, "How long a notification takes to fade away");
		MessageBumpTime = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageBumpTime", 1f, "How much time to add to the life of a notification when picking up a duplicate item");
		ResetMessageTimerOnDupePickup = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ResetMessageTimerOnDupePickup", false, "Resets a notification's timer to max lifetime when picking up a duplicate item");
		MessageVerticalSpacingModifier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageVerticalSpacingModifier", 1.25f, "How much to modify the vertical separation space between messages");
		MessageTextHorizontalSpacingModifier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageTextHorizontalSpacingModifier", 2f, "How much to modify the horizontal spacing between icon and text for messages");
		MessageTextVerticalModifier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MessageTextVerticalModifier", 1f, "How much to modify the vertical alignment of the text for messages");
		_Harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	private void OnDestroy()
	{
		if (_Harmony != null)
		{
			_Harmony.UnpatchSelf();
		}
	}
}