Decompiled source of DistanceLabel v0.1.0

DistanceLabel.dll

Decompiled 3 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("DistanceLabel")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DistanceLabel")]
[assembly: AssemblyTitle("DistanceLabel")]
[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 DistanceLabel
{
	[BepInPlugin("lch.distancelabel", "DistanceLabel", "0.1.0")]
	public class DistanceLabelPlugin : BaseUnityPlugin
	{
		public const string GUID = "lch.distancelabel";

		public const string MODNAME = "DistanceLabel";

		public const string VERSION = "0.1.0";

		private ConfigEntry<bool> _enable;

		private ConfigEntry<int> _radiusM;

		private readonly List<ValuableObject> _valuables = new List<ValuableObject>();

		private float _refreshTimer;

		private GUIStyle _labelStyle;

		private static FieldInfo _fCurValue;

		private static FieldInfo _fOrigValue;

		private Camera _cachedCam;

		private PlayerAvatar _cachedPlayer;

		private static float GetValue(ValuableObject v)
		{
			if (_fCurValue == null)
			{
				_fCurValue = typeof(ValuableObject).GetField("dollarValueCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				_fOrigValue = typeof(ValuableObject).GetField("dollarValueOriginal", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			if (_fCurValue != null && _fCurValue.GetValue(v) is float num && num > 0f)
			{
				return num;
			}
			if (_fOrigValue != null && _fOrigValue.GetValue(v) is float num2 && num2 > 0f)
			{
				return num2;
			}
			return 0f;
		}

		private void Awake()
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			_enable = ((BaseUnityPlugin)this).Config.Bind<bool>("Settings", "enable", true, "Show distance labels under nearby valuables.");
			_radiusM = ((BaseUnityPlugin)this).Config.Bind<int>("Settings", "radius", 30, new ConfigDescription("Show labels within this radius (meters).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(5, 50), Array.Empty<object>()));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"DistanceLabel loaded.");
		}

		private void RefreshValuables()
		{
			_valuables.Clear();
			ValuableObject[] array = Object.FindObjectsOfType<ValuableObject>();
			foreach (ValuableObject val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					_valuables.Add(val);
				}
			}
		}

		private void Update()
		{
			if (_enable.Value)
			{
				_refreshTimer -= Time.deltaTime;
				if (_refreshTimer <= 0f)
				{
					_refreshTimer = 0.5f;
					RefreshValuables();
				}
			}
		}

		private PlayerAvatar GetLocalPlayer()
		{
			if ((Object)(object)_cachedPlayer != (Object)null)
			{
				return _cachedPlayer;
			}
			FieldInfo field = typeof(PlayerAvatar).GetField("isLocal", BindingFlags.Instance | BindingFlags.NonPublic);
			PlayerAvatar[] array = Object.FindObjectsOfType<PlayerAvatar>();
			bool flag = default(bool);
			foreach (PlayerAvatar val in array)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				int num;
				if (field != null)
				{
					object value = field.GetValue(val);
					if (value is bool)
					{
						flag = (bool)value;
						num = 1;
					}
					else
					{
						num = 0;
					}
				}
				else
				{
					num = 0;
				}
				if (((uint)num & (flag ? 1u : 0u)) != 0)
				{
					_cachedPlayer = val;
					return val;
				}
				if ((Object)(object)val.photonView != (Object)null && val.photonView.IsMine)
				{
					_cachedPlayer = val;
					return val;
				}
			}
			return null;
		}

		private Camera GetCamera()
		{
			if ((Object)(object)_cachedCam != (Object)null && ((Behaviour)_cachedCam).isActiveAndEnabled)
			{
				return _cachedCam;
			}
			_cachedCam = Camera.main;
			if ((Object)(object)_cachedCam == (Object)null)
			{
				PlayerVisionTarget val = Object.FindObjectOfType<PlayerVisionTarget>();
				if ((Object)(object)val != (Object)null)
				{
					ref Camera cachedCam = ref _cachedCam;
					object? obj = typeof(PlayerVisionTarget).GetField("MainCamera", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(val);
					cachedCam = (Camera)((obj is Camera) ? obj : null);
				}
			}
			if ((Object)(object)_cachedCam == (Object)null)
			{
				Camera[] array = Object.FindObjectsOfType<Camera>();
				foreach (Camera val2 in array)
				{
					if ((Object)(object)val2 != (Object)null && ((Behaviour)val2).isActiveAndEnabled && ((Behaviour)val2).enabled)
					{
						_cachedCam = val2;
						break;
					}
				}
			}
			return _cachedCam;
		}

		private void OnGUI()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: 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_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			if (!_enable.Value)
			{
				return;
			}
			PlayerAvatar localPlayer = GetLocalPlayer();
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			Camera camera = GetCamera();
			if ((Object)(object)camera == (Object)null)
			{
				return;
			}
			if (_labelStyle == null)
			{
				GUIStyle val = new GUIStyle(GUI.skin.label)
				{
					fontSize = 14,
					alignment = (TextAnchor)4,
					richText = true
				};
				val.normal.textColor = Color.white;
				_labelStyle = val;
			}
			Vector3 position = localPlayer.playerTransform.position;
			float num = _radiusM.Value;
			Rect val4 = default(Rect);
			foreach (ValuableObject valuable in _valuables)
			{
				if ((Object)(object)valuable == (Object)null)
				{
					continue;
				}
				Vector3 position2 = ((Component)valuable).transform.position;
				float num2 = Vector3.Distance(position, position2);
				if (!(num2 > num))
				{
					Vector3 val2 = camera.WorldToScreenPoint(position2);
					if (!(val2.z <= 0f))
					{
						float x = val2.x;
						float num3 = (float)Screen.height - val2.y + 22f;
						float value = GetValue(valuable);
						string text = ((value > 0f) ? $"{num2:0.0}m <color=#FFD700>{(int)value}$</color>" : $"{num2:0.0}m");
						Vector2 val3 = _labelStyle.CalcSize(new GUIContent(text));
						((Rect)(ref val4))..ctor(x - val3.x / 2f, num3, val3.x, val3.y);
						GUI.color = new Color(0f, 0f, 0f, 0.7f);
						GUI.Label(new Rect(((Rect)(ref val4)).x + 1f, ((Rect)(ref val4)).y + 1f, ((Rect)(ref val4)).width, ((Rect)(ref val4)).height), text, _labelStyle);
						GUI.color = Color.white;
						GUI.Label(val4, text, _labelStyle);
					}
				}
			}
		}
	}
}