Decompiled source of MapDisplayPrices v1.2.0

plugins/MapDisplayPrices.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("skey4ik")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Displays price label of valuable on the map.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MapDisplayPrices")]
[assembly: AssemblyTitle("MapDisplayPrices")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
}
internal static class ConfigManager
{
	public static ConfigFile ConfigFile { get; private set; }

	public static ConfigEntry<float> FontSize { get; private set; }

	public static ConfigEntry<string> ValuableColorHex { get; private set; }

	public static ConfigEntry<string> CartColorHex { get; private set; }

	public static ConfigEntry<float> OffsetY { get; private set; }

	public static void Initialize(ConfigFile configFile)
	{
		ConfigFile = configFile;
		BindConfigs();
	}

	private static void BindConfigs()
	{
		FontSize = ConfigFile.Bind<float>("Visual Settings", "Font Size", 2f, "The font size of the price text displayed on the radar map.");
		ValuableColorHex = ConfigFile.Bind<string>("Visual Settings", "Valuable Item Text Color (HEX)", "#FFFF00", "The HEX color code for loose valuable item price tags.");
		CartColorHex = ConfigFile.Bind<string>("Visual Settings", "Shopping Cart Text Color (HEX)", "#14C7E6", "The HEX color code for shopping cart total price tags.");
		OffsetY = ConfigFile.Bind<float>("Position Settings", "Radar Text Offset", 0.15f, "The distance/offset of the text label from the radar icon's center.");
	}
}
[BepInPlugin("MapDisplayPrices", "MapDisplayPrices", "1.0.0")]
public class Starter : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		((BaseUnityPlugin)this).Logger.LogInfo((object)"MapDisplayPrices has awoken");
		Harmony val = new Harmony("com.skey4ik.mapdisplayprices");
		ConfigManager.Initialize(((BaseUnityPlugin)this).Config);
		val.PatchAll();
	}
}
namespace MapDisplayPrices
{
	[HarmonyPatch(typeof(Map), "AddValuable")]
	public class MapTextMod
	{
		public static Color GetConfigColor(string value)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			Color result = default(Color);
			if (ColorUtility.TryParseHtmlString(value, ref result))
			{
				return result;
			}
			return Color.white;
		}

		[HarmonyPostfix]
		private static void Postfix(ValuableObject _valuable)
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_valuable == (Object)null))
			{
				MapValuable val = ((Component)_valuable).GetComponentInChildren<MapValuable>();
				if ((Object)(object)val == (Object)null)
				{
					val = ((Object.FindObjectsOfType<MapValuable>().Length != 0) ? Object.FindObjectOfType<MapValuable>() : null);
				}
				if ((Object)(object)val != (Object)null && (Object)(object)val.target == (Object)(object)_valuable && (Object)(object)((Component)val).transform.Find("MapText") == (Object)null)
				{
					MapTextShared.CreateMapText(((Component)val).transform, ((Component)val).gameObject.layer, "...", GetConfigColor(ConfigManager.ValuableColorHex.Value), ConfigManager.FontSize.Value, _valuable, val, null, null);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Map), "AddCustom")]
	public class MapCustomTextMod
	{
		[HarmonyPostfix]
		private static void Postfix(Map __instance, MapCustom mapCustom, Sprite sprite, Color color)
		{
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Expected O, but got Unknown
			if ((Object)(object)mapCustom == (Object)null)
			{
				return;
			}
			string name = ((Object)((Component)mapCustom).gameObject).name;
			if (!name.Contains("Item Cart Medium") && !name.Contains("Item Cart Small"))
			{
				return;
			}
			MapCustomEntity val = null;
			if ((Object)(object)__instance.OverLayerParent != (Object)null)
			{
				foreach (Transform item in ((Component)__instance.OverLayerParent).transform)
				{
					Transform val2 = item;
					if (((Object)val2).name == name && (Object)(object)val2.Find("MapText") == (Object)null)
					{
						val = ((Component)val2).GetComponent<MapCustomEntity>();
						if ((Object)(object)val != (Object)null)
						{
							break;
						}
					}
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				MapCustomEntity[] array = Object.FindObjectsOfType<MapCustomEntity>();
				foreach (MapCustomEntity val3 in array)
				{
					if (((Object)((Component)val3).gameObject).name == name && (Object)(object)((Component)val3).transform.Find("MapText") == (Object)null)
					{
						val = val3;
						break;
					}
				}
			}
			if ((Object)(object)val != (Object)null)
			{
				MapTextHelper component = MapTextShared.CreateMapText(((Component)val).transform, ((Component)val).gameObject.layer, "", MapTextMod.GetConfigColor(ConfigManager.CartColorHex.Value), ConfigManager.FontSize.Value, null, null, val, ((Object)((Component)mapCustom).gameObject).name).GetComponent<MapTextHelper>();
				if ((Object)(object)component != (Object)null)
				{
					component.SetCartWorldPosition(((Component)mapCustom).transform.position);
				}
			}
		}
	}
	public static class MapTextShared
	{
		public static GameObject CreateMapText(Transform parent, int layer, string initialText, Color textColor, float fontSize, ValuableObject targetValuable, MapValuable mapValuable, MapCustomEntity mapCustomEntity, string staticText)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: 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_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Expected O, but got Unknown
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("MapText");
			val.transform.SetParent(parent);
			val.layer = layer;
			val.transform.localScale = Vector3.one;
			val.transform.localPosition = Vector3.zero;
			TextMeshPro val2 = val.AddComponent<TextMeshPro>();
			((TMP_Text)val2).text = initialText;
			((TMP_Text)val2).fontSize = fontSize;
			((TMP_Text)val2).alignment = (TextAlignmentOptions)514;
			((Graphic)val2).color = textColor;
			((TMP_Text)val2).extraPadding = true;
			if ((Object)(object)((TMP_Text)val2).fontMaterial != (Object)null)
			{
				Material val3 = new Material(((TMP_Text)val2).fontMaterial);
				val3.EnableKeyword("UNDERLAY_ON");
				val3.SetColor("_UnderlayColor", new Color(0f, 0f, 0f, 0.8f));
				val3.SetVector("_UnderlayOffset", new Vector4(0.05f, -0.05f, 0f, 0f));
				val3.SetFloat("_UnderlayBlur", 0f);
				val3.renderQueue = 4000;
				val3.SetInt("_ZTest", 8);
				((TMP_Text)val2).fontMaterial = val3;
			}
			val.AddComponent<MapTextHelper>().Init(targetValuable, mapValuable, mapCustomEntity, textColor, staticText);
			return val;
		}
	}
	public class MapTextHelper : MonoBehaviour
	{
		private ValuableObject targetValuable;

		private MapValuable mapValuable;

		private MapCustomEntity mapCustomEntity;

		private Color baseColor;

		private string staticText;

		private TextMeshPro textMesh;

		private Camera mapCamera;

		private float lastPrice = -1f;

		private FieldInfo dollarValueField;

		private FieldInfo itemValueField;

		private Component itemAttributesCache;

		private bool hasCachedFields;

		private const float MinAlphaThreshold = 0.3f;

		private static FieldInfo haulCurrentField;

		private static MethodInfo getCurrentCartMethod;

		private Component targetItemComponent;

		private FieldInfo inExtractionPointField;

		private bool hasCheckedExtractionField;

		private PhysGrabCart boundCart;

		private bool hasCheckedCartBinding;

		private Vector3 cartWorldPos = Vector3.zero;

		private bool hasCartWorldPos;

		public void Init(ValuableObject target, MapValuable mapVal, MapCustomEntity customEntity, Color color, string staticTxt)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			targetValuable = target;
			mapValuable = mapVal;
			mapCustomEntity = customEntity;
			baseColor = color;
			staticText = staticTxt;
		}

		public void SetCartWorldPosition(Vector3 pos)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			cartWorldPos = pos;
			hasCartWorldPos = true;
		}

		private void Start()
		{
			textMesh = ((Component)this).GetComponent<TextMeshPro>();
			mapCamera = Camera.main;
			if (haulCurrentField == null)
			{
				haulCurrentField = typeof(PhysGrabCart).GetField("haulCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			}
			if (getCurrentCartMethod == null)
			{
				getCurrentCartMethod = typeof(ValuableObject).GetMethod("GetCurrentCart", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			}
		}

		private void CacheFields()
		{
			if ((Object)(object)targetValuable != (Object)null)
			{
				dollarValueField = ((object)targetValuable).GetType().GetField("dollarValueCurrent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				itemAttributesCache = ((Component)targetValuable).GetComponent("ItemAttributes");
				if ((Object)(object)itemAttributesCache != (Object)null)
				{
					itemValueField = ((object)itemAttributesCache).GetType().GetField("value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				}
			}
			hasCachedFields = true;
		}

		private PhysGrabCart GetBoundCart()
		{
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: 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)
			if (hasCheckedCartBinding)
			{
				return boundCart;
			}
			if ((Object)(object)mapCustomEntity != (Object)null)
			{
				PhysGrabCart[] array = Object.FindObjectsOfType<PhysGrabCart>();
				float num = float.MaxValue;
				PhysGrabCart val = null;
				string text = (string.IsNullOrEmpty(staticText) ? ((Object)((Component)mapCustomEntity).gameObject).name : staticText);
				text = text.Replace("(Clone)", "").Trim();
				PhysGrabCart[] array2 = array;
				foreach (PhysGrabCart val2 in array2)
				{
					if ((Object)(object)val2 == (Object)null)
					{
						continue;
					}
					string text2 = ((Object)((Component)val2).gameObject).name.Replace("(Clone)", "").Trim();
					if (!text.Contains(text2) && !text2.Contains(text))
					{
						continue;
					}
					if (hasCartWorldPos)
					{
						float num2 = Vector3.Distance(((Component)val2).transform.position, cartWorldPos);
						if (num2 < num)
						{
							num = num2;
							val = val2;
						}
					}
					else
					{
						float num3 = Vector3.Distance(((Component)val2).transform.position, ((Component)mapCustomEntity).transform.position);
						if (num3 < num)
						{
							num = num3;
							val = val2;
						}
					}
				}
				boundCart = val;
			}
			hasCheckedCartBinding = true;
			return boundCart;
		}

		private void CacheExtractionField()
		{
			if ((Object)(object)targetValuable == (Object)null)
			{
				return;
			}
			Component[] components = ((Component)targetValuable).GetComponents<Component>();
			foreach (Component val in components)
			{
				if (!((Object)(object)val == (Object)null))
				{
					FieldInfo field = ((object)val).GetType().GetField("inExtractionPoint", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
					if (field != null)
					{
						targetItemComponent = val;
						inExtractionPointField = field;
						break;
					}
				}
			}
			if (inExtractionPointField == null)
			{
				components = ((Component)targetValuable).GetComponentsInParent<Component>();
				foreach (Component val2 in components)
				{
					if (!((Object)(object)val2 == (Object)null))
					{
						FieldInfo field2 = ((object)val2).GetType().GetField("inExtractionPoint", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
						if (field2 != null)
						{
							targetItemComponent = val2;
							inExtractionPointField = field2;
							break;
						}
					}
				}
			}
			hasCheckedExtractionField = true;
		}

		private bool ShouldHideText()
		{
			if ((Object)(object)mapCustomEntity != (Object)null)
			{
				return false;
			}
			if ((Object)(object)targetValuable == (Object)null || (Object)(object)mapValuable == (Object)null)
			{
				return true;
			}
			if (!((Component)targetValuable).gameObject.activeInHierarchy)
			{
				return true;
			}
			if (!hasCheckedExtractionField)
			{
				CacheExtractionField();
			}
			if ((Object)(object)targetItemComponent != (Object)null && inExtractionPointField != null)
			{
				try
				{
					if ((bool)inExtractionPointField.GetValue(targetItemComponent))
					{
						return true;
					}
				}
				catch
				{
				}
			}
			if (getCurrentCartMethod != null)
			{
				try
				{
					if (getCurrentCartMethod.Invoke(targetValuable, null) != null)
					{
						return true;
					}
				}
				catch
				{
				}
			}
			return false;
		}

		private void LateUpdate()
		{
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0294: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_022e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0301: Unknown result type (might be due to invalid IL or missing references)
			//IL_030b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0318: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0342: Unknown result type (might be due to invalid IL or missing references)
			//IL_0351: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)textMesh == (Object)null)
			{
				return;
			}
			if (ShouldHideText())
			{
				if (((Behaviour)textMesh).enabled)
				{
					((Behaviour)textMesh).enabled = false;
				}
				return;
			}
			if (!((Behaviour)textMesh).enabled)
			{
				((Behaviour)textMesh).enabled = true;
			}
			if ((Object)(object)targetValuable != (Object)null)
			{
				if (!hasCachedFields)
				{
					CacheFields();
				}
				float num = 0f;
				if (dollarValueField != null)
				{
					num = (float)dollarValueField.GetValue(targetValuable);
				}
				if (num <= 0f && (Object)(object)itemAttributesCache != (Object)null && itemValueField != null)
				{
					num = (int)itemValueField.GetValue(itemAttributesCache);
				}
				if (!Mathf.Approximately(num, lastPrice))
				{
					string text = ((num > 0f) ? SemiFunc.DollarGetString(Mathf.CeilToInt(num)) : "...");
					((TMP_Text)textMesh).text = text;
					lastPrice = num;
				}
			}
			else if ((Object)(object)mapCustomEntity != (Object)null)
			{
				PhysGrabCart val = GetBoundCart();
				if ((Object)(object)val != (Object)null && haulCurrentField != null)
				{
					int num2 = (int)haulCurrentField.GetValue(val);
					if (!Mathf.Approximately((float)num2, lastPrice))
					{
						string text2 = ((num2 > 0) ? SemiFunc.DollarGetString(num2) : "");
						((TMP_Text)textMesh).text = text2;
						lastPrice = num2;
					}
				}
			}
			SpriteRenderer val2 = null;
			if ((Object)(object)mapValuable != (Object)null)
			{
				val2 = mapValuable.spriteRenderer;
			}
			else if ((Object)(object)mapCustomEntity != (Object)null)
			{
				val2 = mapCustomEntity.spriteRenderer;
			}
			if ((Object)(object)val2 != (Object)null)
			{
				float num3 = 0f;
				if ((Object)(object)mapCustomEntity != (Object)null && string.IsNullOrEmpty(((TMP_Text)textMesh).text))
				{
					num3 = 0f;
				}
				else
				{
					float a = val2.color.a;
					num3 = ((a > 0f) ? Mathf.Max(a, 0.3f) : 0f);
				}
				if (!Mathf.Approximately(((Graphic)textMesh).color.a, num3))
				{
					baseColor.a = num3;
					((Graphic)textMesh).color = baseColor;
				}
			}
			if ((Object)(object)mapCamera == (Object)null)
			{
				mapCamera = Camera.main;
			}
			if ((Object)(object)mapCamera != (Object)null)
			{
				float num4 = ((Component)mapCamera).transform.eulerAngles.y * (MathF.PI / 180f);
				float num5 = Mathf.Sin(num4) * ConfigManager.OffsetY.Value;
				float num6 = Mathf.Cos(num4) * ConfigManager.OffsetY.Value;
				Vector3 val3 = (((Object)(object)((Component)this).transform.parent != (Object)null) ? ((Component)this).transform.parent.position : ((Component)this).transform.position);
				((Component)this).transform.position = new Vector3(val3.x + num5, val3.y + 0.05f, val3.z + num6);
				((Component)this).transform.rotation = Quaternion.Euler(90f, ((Component)mapCamera).transform.eulerAngles.y, 0f);
			}
		}
	}
}