Decompiled source of SullysFloatingChestIcons v1.1.1

BepInEx/plugins/SullysFloatingChestIcons_v1.1.1.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("SullysFloatingChestIcons")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SullysFloatingChestIcons")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("aa9a582d-b0f5-4129-b520-544b12e59a84")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.sullydev.SullysFloatingChestIcons", "Sullys Floating Chest Icons", "1.0.2")]
public class SullysFloatingChestIcons : BaseUnityPlugin
{
	[CompilerGenerated]
	private sealed class <FadeCanvas>d__23 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public SullysFloatingChestIcons <>4__this;

		public float from;

		public float to;

		private float <elapsed>5__2;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <FadeCanvas>d__23(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			int num = <>1__state;
			SullysFloatingChestIcons sullysFloatingChestIcons = <>4__this;
			switch (num)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<elapsed>5__2 = 0f;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if (<elapsed>5__2 < sullysFloatingChestIcons.fadeDuration)
			{
				sullysFloatingChestIcons.canvasGroup.alpha = Mathf.Lerp(from, to, <elapsed>5__2 / sullysFloatingChestIcons.fadeDuration);
				<elapsed>5__2 += Time.deltaTime;
				<>2__current = null;
				<>1__state = 1;
				return true;
			}
			sullysFloatingChestIcons.canvasGroup.alpha = to;
			if (to == 0f)
			{
				sullysFloatingChestIcons.displayRoot.SetActive(false);
			}
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <HoverLoop>d__15 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public SullysFloatingChestIcons <>4__this;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <HoverLoop>d__15(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			int num = <>1__state;
			SullysFloatingChestIcons sullysFloatingChestIcons = <>4__this;
			if (num != 0)
			{
				if (num != 1)
				{
					return false;
				}
				<>1__state = -1;
			}
			else
			{
				<>1__state = -1;
			}
			sullysFloatingChestIcons.CheckHover();
			<>2__current = (object)new WaitForSeconds(0.5f);
			<>1__state = 1;
			return true;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <WaitForGameLoad>d__14 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public SullysFloatingChestIcons <>4__this;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <WaitForGameLoad>d__14(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			int num = <>1__state;
			SullysFloatingChestIcons sullysFloatingChestIcons = <>4__this;
			switch (num)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if ((Object)(object)ZNetScene.instance == (Object)null || (Object)(object)ObjectDB.instance == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null || (Object)(object)((Component)Player.m_localPlayer).transform == (Object)null || (Object)(object)((Component)Player.m_localPlayer).gameObject == (Object)null || !((Component)Player.m_localPlayer).gameObject.activeInHierarchy)
			{
				<>2__current = (object)new WaitForSeconds(1f);
				<>1__state = 1;
				return true;
			}
			sullysFloatingChestIcons.CreateDisplay();
			sullysFloatingChestIcons.CacheIcons();
			((MonoBehaviour)sullysFloatingChestIcons).StartCoroutine(sullysFloatingChestIcons.HoverLoop());
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private GameObject displayRoot;

	private CanvasGroup canvasGroup;

	private List<Image> iconImages = new List<Image>();

	private Dictionary<string, Sprite> iconCache = new Dictionary<string, Sprite>();

	private Container lastContainer;

	private float fadeDuration = 0.25f;

	private Coroutine fadeCoroutine;

	private int maxIcons = 4;

	private float iconSize = 0.25f;

	private float iconSpacing = 0.25f;

	private Vector3 displayOffset = new Vector3(0f, 0.4f, 0f);

	private float raycastDistance = 3f;

	private float iconDisplayBias = 0.4f;

	private void Awake()
	{
		((MonoBehaviour)this).StartCoroutine(WaitForGameLoad());
	}

	[IteratorStateMachine(typeof(<WaitForGameLoad>d__14))]
	private IEnumerator WaitForGameLoad()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <WaitForGameLoad>d__14(0)
		{
			<>4__this = this
		};
	}

	[IteratorStateMachine(typeof(<HoverLoop>d__15))]
	private IEnumerator HoverLoop()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <HoverLoop>d__15(0)
		{
			<>4__this = this
		};
	}

	private void OnDestroy()
	{
		if ((Object)(object)displayRoot != (Object)null)
		{
			Object.Destroy((Object)(object)displayRoot);
		}
	}

	private void CreateDisplay()
	{
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)displayRoot != (Object)null)
		{
			Object.Destroy((Object)(object)displayRoot);
			iconImages.Clear();
		}
		displayRoot = new GameObject("SullysChestIconDisplay");
		Canvas obj = displayRoot.AddComponent<Canvas>();
		obj.renderMode = (RenderMode)2;
		obj.sortingOrder = 999;
		displayRoot.transform.localScale = Vector3.one;
		displayRoot.AddComponent<CanvasScaler>().dynamicPixelsPerUnit = 10f;
		canvasGroup = displayRoot.AddComponent<CanvasGroup>();
		canvasGroup.alpha = 0f;
		for (int i = 0; i < maxIcons; i++)
		{
			GameObject val = new GameObject("Icon" + i);
			val.transform.SetParent(displayRoot.transform, false);
			Image val2 = val.AddComponent<Image>();
			((Graphic)val2).rectTransform.sizeDelta = new Vector2(iconSize, iconSize);
			val2.preserveAspect = true;
			iconImages.Add(val2);
		}
		displayRoot.SetActive(false);
	}

	private void CacheIcons()
	{
		iconCache.Clear();
		if (!Object.op_Implicit((Object)(object)ObjectDB.instance))
		{
			return;
		}
		foreach (GameObject item in ObjectDB.instance.m_items)
		{
			if (!Object.op_Implicit((Object)(object)item))
			{
				continue;
			}
			ItemDrop component = item.GetComponent<ItemDrop>();
			if (!((Object)(object)component == (Object)null) && component.m_itemData?.m_shared != null)
			{
				string name = ((Object)item).name;
				Sprite[] icons = component.m_itemData.m_shared.m_icons;
				if (!iconCache.ContainsKey(name) && icons != null && icons.Length != 0)
				{
					iconCache[name] = icons[0];
				}
			}
		}
	}

	private void CheckHover()
	{
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)displayRoot == (Object)null || (Object)(object)canvasGroup == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null || (Object)(object)Camera.main == (Object)null)
		{
			return;
		}
		RaycastHit[] array = Physics.RaycastAll(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)), raycastDistance, -1, (QueryTriggerInteraction)2);
		for (int i = 0; i < array.Length; i++)
		{
			RaycastHit val = array[i];
			GameObject gameObject = ((Component)((RaycastHit)(ref val)).collider).gameObject;
			if ((Object)(object)gameObject == (Object)(object)displayRoot || gameObject.transform.IsChildOf(displayRoot.transform))
			{
				continue;
			}
			Container componentInParent = gameObject.GetComponentInParent<Container>();
			if (!((Object)(object)componentInParent == (Object)null) && componentInParent.GetInventory() != null && componentInParent.GetInventory().NrOfItems() != 0 && ((Object)componentInParent).name.ToLower().Contains("chest"))
			{
				if (!((Object)(object)componentInParent == (Object)(object)lastContainer))
				{
					lastContainer = componentInParent;
					UpdateDisplay(componentInParent);
					ShowDisplay(((Component)componentInParent).transform);
				}
				return;
			}
		}
		HideDisplay();
	}

	private void UpdateDisplay(Container container)
	{
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		List<string> list = (from i in container.GetInventory().GetAllItems()
			group i by (!((Object)(object)i.m_dropPrefab != (Object)null)) ? i.m_shared.m_name : ((Object)i.m_dropPrefab).name into g
			select g.Key).Take(maxIcons).ToList();
		int count = list.Count;
		float num = (float)(count - 1) * iconSpacing;
		for (int j = 0; j < maxIcons; j++)
		{
			if (j < count)
			{
				string key = list[j];
				iconCache.TryGetValue(key, out var value);
				iconImages[j].sprite = value;
				((Component)iconImages[j]).gameObject.SetActive(true);
				float num2 = (0f - num) / 2f + (float)j * iconSpacing;
				((Graphic)iconImages[j]).rectTransform.anchoredPosition = new Vector2(num2, 0f);
			}
			else
			{
				((Component)iconImages[j]).gameObject.SetActive(false);
			}
		}
	}

	private void ShowDisplay(Transform target)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: 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_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: 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)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: 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_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)Camera.main == (Object)null))
		{
			Vector3 val = target.position + Vector3.up * displayOffset.y;
			Vector3 position = ((Component)Camera.main).transform.position;
			Vector3 position2 = Vector3.Lerp(val, position, iconDisplayBias);
			displayRoot.transform.position = position2;
			displayRoot.transform.rotation = Quaternion.LookRotation(((Component)Camera.main).transform.forward);
			displayRoot.SetActive(true);
			if (fadeCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(fadeCoroutine);
			}
			fadeCoroutine = ((MonoBehaviour)this).StartCoroutine(FadeCanvas(canvasGroup.alpha, 1f));
		}
	}

	private void HideDisplay()
	{
		if (!((Object)(object)displayRoot == (Object)null) && !((Object)(object)canvasGroup == (Object)null) && displayRoot.activeSelf)
		{
			if (fadeCoroutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(fadeCoroutine);
			}
			fadeCoroutine = ((MonoBehaviour)this).StartCoroutine(FadeCanvas(canvasGroup.alpha, 0f));
			lastContainer = null;
		}
	}

	[IteratorStateMachine(typeof(<FadeCanvas>d__23))]
	private IEnumerator FadeCanvas(float from, float to)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <FadeCanvas>d__23(0)
		{
			<>4__this = this,
			from = from,
			to = to
		};
	}
}