Decompiled source of PingDistance v0.2.3

plugins/MycopunkPingDistance/MycopunkPingDistance.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MycopunkShared;
using Pigeon.Movement;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MycopunkPingDistance")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.3.0")]
[assembly: AssemblyInformationalVersion("0.2.3+fe8cef1291afd0a7f7ea8a9ddfd28d3607c4cdd5")]
[assembly: AssemblyProduct("MycopunkPingDistance")]
[assembly: AssemblyTitle("MycopunkPingDistance")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.3.0")]
[module: UnverifiableCode]
[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 MycopunkPingDistance
{
	internal sealed class DistanceText
	{
		private int _metres = int.MinValue;

		private string _text = "";

		public string Get(float distance, float minMetres)
		{
			int num = ((distance < minMetres || float.IsNaN(distance)) ? (-1) : ((int)(distance + 0.5f)));
			if (num != _metres)
			{
				_metres = num;
				_text = ((num < 0) ? "" : (num + "m"));
			}
			return _text;
		}
	}
	internal static class MenuText
	{
		private static readonly Dictionary<string, Dictionary<string, string>> Names = new Dictionary<string, Dictionary<string, string>>
		{
			["en"] = new Dictionary<string, string>
			{
				["ping.distance"] = "Distance under pings",
				["ping.color"] = "Distance in ping color"
			},
			["ja"] = new Dictionary<string, string>
			{
				["ping.distance"] = "ピンの下に距離を表示",
				["ping.color"] = "距離をピンと同じ色で表示"
			}
		};

		private static readonly Dictionary<string, Dictionary<string, string>> Descriptions = new Dictionary<string, Dictionary<string, string>> { ["ja"] = new Dictionary<string, string>
		{
			["ping.distance"] = "自分や味方のピン、マップ上のピン、目標マーカーの下に距離をメートルで表示します。この変更は即時に反映されます。",
			["ping.color"] = "距離を白ではなくピンと同じ色で表示します。白のほうが多くの地面で読みやすくなります。"
		} };

		public static object[] Label(string key)
		{
			return ModsMenuTag.Label(key, (IDictionary<string, Dictionary<string, string>>)Names, (IDictionary<string, Dictionary<string, string>>)Descriptions);
		}
	}
	internal sealed class PingDistanceLabels : MonoBehaviour
	{
		private sealed class Label
		{
			public TextMeshProUGUI Text;

			public readonly DistanceText Distance = new DistanceText();

			public string Shown;

			public int Frame;
		}

		private const string LabelName = "MycoPingDistance";

		private readonly Dictionary<Image, Label> _labels = new Dictionary<Image, Label>();

		private readonly List<Image> _stale = new List<Image>();

		private bool _failed;

		private void OnDestroy()
		{
			foreach (Label value in _labels.Values)
			{
				if ((Object)(object)value.Text != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value.Text).gameObject);
				}
			}
			_labels.Clear();
		}

		private void LateUpdate()
		{
			if (_failed)
			{
				return;
			}
			try
			{
				Tick();
			}
			catch (Exception ex)
			{
				_failed = true;
				Plugin.Log.LogWarning((object)("Ping distance labels failed and are turned off: " + ex));
			}
		}

		private void Tick()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: 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_00cd: 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)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
			List<Ping> pings = Highlighter.pings;
			bool value = Plugin.PingDistance.Value;
			int frameCount = Time.frameCount;
			if (pings != null && (value || _labels.Count > 0))
			{
				Vector3 position = PlayerLook.Position;
				Vector2 val3 = default(Vector2);
				for (int i = 0; i < pings.Count; i++)
				{
					Ping val = pings[i];
					if (val == null || (Object)(object)val.graphic == (Object)null || !value || !val.showInWorld)
					{
						continue;
					}
					Label label = Get(val.graphic);
					if (label != null)
					{
						label.Frame = frameCount;
						Vector3 val2 = (((Object)(object)val.target == (Object)null) ? val.localPosition : (val.isPositionOffset ? (val.target.position + val.localPosition) : val.target.TransformPoint(val.localPosition)));
						string text = label.Distance.Get(Vector3.Distance(position, val2), Plugin.MinDistance.Value);
						if ((object)text != label.Shown)
						{
							label.Shown = text;
							((TMP_Text)label.Text).text = text;
						}
						RectTransform rectTransform = ((TMP_Text)label.Text).rectTransform;
						((Vector2)(ref val3))..ctor(0f, 0f - (val.maxSize * 0.5f + 2f));
						if (rectTransform.anchoredPosition != val3)
						{
							rectTransform.anchoredPosition = val3;
						}
						float value2 = Plugin.FontSize.Value;
						if (((TMP_Text)label.Text).fontSize != value2)
						{
							((TMP_Text)label.Text).fontSize = value2;
						}
						Color color = ((Graphic)val.graphic).color;
						Color val4 = (Color)(Plugin.UsePingColor.Value ? color : new Color(1f, 1f, 1f, color.a));
						if (((Graphic)label.Text).color != val4)
						{
							((Graphic)label.Text).color = val4;
						}
					}
				}
			}
			foreach (KeyValuePair<Image, Label> label2 in _labels)
			{
				if ((Object)(object)label2.Key == (Object)null || (Object)(object)label2.Value.Text == (Object)null)
				{
					_stale.Add(label2.Key);
				}
				else if (label2.Value.Frame != frameCount && label2.Value.Shown != "")
				{
					label2.Value.Shown = "";
					((TMP_Text)label2.Value.Text).text = "";
				}
			}
			if (_stale.Count <= 0)
			{
				return;
			}
			foreach (Image item in _stale)
			{
				_labels.Remove(item);
			}
			_stale.Clear();
		}

		private Label Get(Image graphic)
		{
			//IL_003b: 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_0051: 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_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			if (_labels.TryGetValue(graphic, out var value))
			{
				return value;
			}
			TextMeshProUGUI val = FontSource();
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			GameObject val2 = new GameObject("MycoPingDistance", new Type[1] { typeof(RectTransform) })
			{
				layer = ((Component)graphic).gameObject.layer
			};
			RectTransform val3 = (RectTransform)val2.transform;
			((Transform)val3).SetParent((Transform)(object)((Graphic)graphic).rectTransform, false);
			Vector2 val4 = default(Vector2);
			((Vector2)(ref val4))..ctor(0.5f, 0.5f);
			val3.anchorMax = val4;
			val3.anchorMin = val4;
			val3.pivot = new Vector2(0.5f, 1f);
			val3.sizeDelta = new Vector2(200f, 30f);
			TextMeshProUGUI val5 = val2.AddComponent<TextMeshProUGUI>();
			((TMP_Text)val5).font = ((TMP_Text)val).font;
			((TMP_Text)val5).fontSharedMaterial = ((TMP_Text)val).fontSharedMaterial;
			((TMP_Text)val5).fontSize = Plugin.FontSize.Value;
			((TMP_Text)val5).alignment = (TextAlignmentOptions)258;
			((TMP_Text)val5).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)val5).overflowMode = (TextOverflowModes)0;
			((Graphic)val5).raycastTarget = false;
			((TMP_Text)val5).text = "";
			value = new Label
			{
				Text = val5,
				Shown = ""
			};
			_labels[graphic] = value;
			return value;
		}

		private static TextMeshProUGUI FontSource()
		{
			TextMeshProUGUI val = null;
			Highlighter val2 = default(Highlighter);
			if (Highlighter.TryGetInstance(ref val2) && (Object)(object)val2.Map != (Object)null)
			{
				val = val2.Map.topText;
			}
			if ((Object)(object)val == (Object)null || (Object)(object)((TMP_Text)val).font == (Object)null)
			{
				PlayerLook instance = PlayerLook.Instance;
				val = (((Object)(object)instance != (Object)null) ? instance.PlayerTitleText : null);
			}
			if (!((Object)(object)val != (Object)null) || !((Object)(object)((TMP_Text)val).font != (Object)null))
			{
				return null;
			}
			return val;
		}
	}
	[BepInPlugin("t4t5u0.mycopunk.pingdistance", "Mycopunk Ping Distance", "0.2.3")]
	[BepInDependency("doku_macaron.mycopunk.macaroncore", "1.1.0")]
	[MycoMod(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "t4t5u0.mycopunk.pingdistance";

		public const string Name = "Mycopunk Ping Distance";

		public const string Version = "0.2.3";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> PingDistance;

		internal static ConfigEntry<float> MinDistance;

		internal static ConfigEntry<float> FontSize;

		internal static ConfigEntry<bool> UsePingColor;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			PingDistance = ((BaseUnityPlugin)this).Config.Bind<bool>("Pings", "ShowDistance", true, new ConfigDescription("Show the distance in metres under every on-screen ping: your and your teammates' pings, pins placed on the map and objective markers. Takes effect at once.", (AcceptableValueBase)null, MenuText.Label("ping.distance")));
			MinDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Pings", "MinDistance", 3f, "Hide the distance when the ping is closer than this many metres.");
			FontSize = ((BaseUnityPlugin)this).Config.Bind<float>("Pings", "FontSize", 18f, "Font size of the distance text, in HUD units.");
			UsePingColor = ((BaseUnityPlugin)this).Config.Bind<bool>("Style", "UsePingColor", false, new ConfigDescription("Draw the distance in the ping's color instead of white. White reads better on most ground.", (AcceptableValueBase)null, MenuText.Label("ping.color")));
			_harmony = new Harmony("t4t5u0.mycopunk.pingdistance");
			ModSetup.Try(Log, "ping distance labels", (Action)delegate
			{
				((Component)this).gameObject.AddComponent<PingDistanceLabels>();
			});
			ModSetup.Try(Log, "MODS tab", (Action)delegate
			{
				ModsMenu.Install(_harmony, (BaseUnityPlugin)(object)this);
			});
			ModSetup.Try(Log, "hotkey report", (Action)delegate
			{
				Hotkeys.ReportConflicts((BaseUnityPlugin)(object)this);
			});
			Log.LogInfo((object)("Mycopunk Ping Distance 0.2.3 loaded (ping distance=" + PingDistance.Value + ")."));
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}