Decompiled source of BetterPingDistance v1.0.0

BepInEx/plugins/BetterPingDistance/lucyddemon.betterpingdistance.dll

Decompiled a day 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.Logging;
using BetterPingDistance.Patches;
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: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("LucydDemon")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+bb1895a49ca49364a0611607618f04595b72eb8e")]
[assembly: AssemblyProduct("BetterPingDistance")]
[assembly: AssemblyTitle("lucyddemon.betterpingdistance")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BetterPingDistance
{
	[BepInPlugin("lucyddemon.betterpingdistance", "BetterPingDistance", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private readonly Harmony harmony = new Harmony("lucyddemon.betterpingdistance");

		internal static Plugin instance { get; private set; }

		internal static ManualLogSource Log => instance._logger;

		private ManualLogSource _logger => ((BaseUnityPlugin)this).Logger;

		internal static PingDistanceManager PDM { get; private set; }

		private void Awake()
		{
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			((Component)this).gameObject.transform.parent = null;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			PDM = new PingDistanceManager();
			harmony.PatchAll(typeof(GUIManagerPatch));
			harmony.PatchAll(typeof(PointPingerPatch));
			Log.LogInfo((object)"BetterPingDistance v1.0.0 by LucydDemon initialized!");
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
			PDM.OnDestroy();
		}
	}
	public class PingDistance : MonoBehaviour
	{
		public Vector3 position;

		public Color color = Color.white;

		private TextMeshProUGUI? TMP;

		private void LateUpdate()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: 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_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: 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_0099: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)Camera.main == (Object)null))
			{
				float num = Mathf.Round(Vector3.Distance(position, ((Component)Camera.main).transform.position));
				float num2 = Vector3.Angle(((Component)Camera.main).transform.forward, position - ((Component)Camera.main).transform.position);
				if ((Object)(object)TMP == (Object)null)
				{
					TMP = ((Component)this).GetComponentInChildren<TextMeshProUGUI>();
				}
				((Component)this).transform.position = Camera.main.WorldToScreenPoint(position);
				((Graphic)TMP).color = color;
				((TMP_Text)TMP).text = $"{num}m";
				((Component)TMP).gameObject.SetActive(num2 < 90f);
			}
		}
	}
	public class PingDistanceManager
	{
		private TMP_FontAsset font;

		private Material fontMaterial;

		private Canvas pingDistanceCanvas;

		private GameObject template;

		private readonly Dictionary<Character, GameObject> pingDistanceInstances = new Dictionary<Character, GameObject>();

		public bool isInitialized
		{
			get
			{
				if ((Object)(object)Plugin.instance != (Object)null && (Object)(object)pingDistanceCanvas != (Object)null)
				{
					return (Object)(object)template != (Object)null;
				}
				return false;
			}
		}

		public PingDistanceManager()
		{
			Plugin.Log.LogInfo((object)"PingDistanceManager has started");
		}

		public void Initialize()
		{
			if (!isInitialized)
			{
				CreateCanvas();
				CreateTemplate();
				if (isInitialized)
				{
					Plugin.Log.LogInfo((object)"PingDistanceManager UI initialized");
				}
			}
		}

		public void OnDestroy()
		{
			CleanupUI();
			if (!isInitialized)
			{
				Plugin.Log.LogInfo((object)"PingDistanceManager UI cleaned up");
			}
		}

		private void CleanupUI()
		{
			if ((Object)(object)pingDistanceCanvas != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)pingDistanceCanvas).GetComponent<CanvasScaler>());
				Object.Destroy((Object)(object)((Component)pingDistanceCanvas).GetComponent<GraphicRaycaster>());
				Object.Destroy((Object)(object)((Component)pingDistanceCanvas).gameObject);
				pingDistanceCanvas = null;
			}
			template = null;
			pingDistanceInstances.Clear();
		}

		private void FindFont()
		{
			TextMeshProUGUI[] array = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
			TextMeshProUGUI[] array2 = array;
			foreach (TextMeshProUGUI val in array2)
			{
				if (((Object)val).name == "Text" && ((Object)((Graphic)val).materialForRendering).name.Contains("DarumaDropOne-Regular SDF Outline"))
				{
					font = ((TMP_Text)val).font;
					fontMaterial = ((Graphic)val).materialForRendering;
					return;
				}
			}
			if ((Object)(object)font == (Object)null)
			{
				Plugin.Log.LogError((object)"Error finding game font");
			}
		}

		private void CreateCanvas()
		{
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)pingDistanceCanvas != (Object)null))
			{
				pingDistanceInstances.Clear();
				GameObject val = GameObject.Find("GAME/GUIManager");
				GameObject val2 = new GameObject("Canvas_BetterPingDistance", new Type[3]
				{
					typeof(Canvas),
					typeof(CanvasScaler),
					typeof(GraphicRaycaster)
				});
				val2.transform.SetParent(val.transform);
				pingDistanceCanvas = val2.GetComponent<Canvas>();
				pingDistanceCanvas.renderMode = (RenderMode)0;
				pingDistanceCanvas.sortingOrder = 100;
				CanvasScaler component = val2.GetComponent<CanvasScaler>();
				component.uiScaleMode = (ScaleMode)1;
				component.referenceResolution = new Vector2(1920f, 1080f);
				component.screenMatchMode = (ScreenMatchMode)1;
				val2.layer = LayerMask.NameToLayer("UI");
				Plugin.Log.LogInfo((object)"Canvas_BetterPingDistance created");
			}
		}

		private void CreateTemplate()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)template != (Object)null))
			{
				template = new GameObject("PingDistanceIndicator", new Type[3]
				{
					typeof(RectTransform),
					typeof(CanvasRenderer),
					typeof(PingDistance)
				});
				template.transform.SetParent(((Component)pingDistanceCanvas).transform);
				GameObject val = AddTMPChildren("DistanceText", template.transform, new Vector2(100f, 50f));
				val.transform.SetParent(template.transform, true);
				template.SetActive(false);
				Plugin.Log.LogInfo((object)"PingDistanceIndicator template created");
			}
		}

		private GameObject AddTMPChildren(string name, Transform parent, Vector2 size)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: 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)
			if ((Object)(object)font == (Object)null)
			{
				FindFont();
			}
			GameObject val = new GameObject(name, new Type[4]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(TextMeshProUGUI),
				typeof(Shadow)
			});
			val.transform.SetParent(parent, true);
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.pivot = new Vector2(0.5f, 0.5f);
			component.sizeDelta = size;
			TextMeshProUGUI component2 = val.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component2).font = font;
			((TMP_Text)component2).fontMaterial = fontMaterial;
			((TMP_Text)component2).fontSize = 30f;
			((Graphic)component2).color = Color.white;
			((TMP_Text)component2).outlineWidth = 0.1f;
			((TMP_Text)component2).outlineColor = Color32.op_Implicit(Color.black);
			((TMP_Text)component2).enableAutoSizing = false;
			((TMP_Text)component2).alignment = (TextAlignmentOptions)514;
			((TMP_Text)component2).horizontalAlignment = (HorizontalAlignmentOptions)2;
			((TMP_Text)component2).verticalAlignment = (VerticalAlignmentOptions)512;
			Shadow component3 = val.GetComponent<Shadow>();
			component3.effectColor = new Color(0f, 0f, 0f, 0.95f);
			component3.effectDistance = new Vector2(2f, -2f);
			return val;
		}

		public void CreateInstance(Character character, Vector3 position)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)pingDistanceCanvas == (Object)null) && !((Object)(object)template == (Object)null))
			{
				if (pingDistanceInstances.TryGetValue(character, out GameObject value) && (Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)value);
				}
				GameObject val = Object.Instantiate<GameObject>(template, ((Component)pingDistanceCanvas).transform);
				PingDistance component = val.GetComponent<PingDistance>();
				component.color = character.refs.customization.PlayerColor;
				component.position = position;
				val.SetActive(true);
				pingDistanceInstances[character] = val;
				Object.Destroy((Object)(object)val, 1.25f);
			}
		}
	}
	internal static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "lucyddemon.betterpingdistance";

		public const string PLUGIN_NAME = "BetterPingDistance";

		public const string PLUGIN_VERSION = "1.0.0";

		public const string PLUGIN_AUTHOR = "LucydDemon";
	}
}
namespace BetterPingDistance.Patches
{
	[HarmonyPatch(typeof(GUIManager))]
	internal static class GUIManagerPatch
	{
		[HarmonyPatch("Start")]
		[HarmonyPostfix]
		private static void Start_Postfix(GUIManager __instance)
		{
			Plugin.PDM.Initialize();
		}

		[HarmonyPatch("LateUpdate")]
		[HarmonyPostfix]
		private static void LateUpdate_Postfix(GUIManager __instance)
		{
			if (!Plugin.PDM.isInitialized)
			{
				Plugin.PDM.Initialize();
			}
		}

		[HarmonyPatch("OnDestroy")]
		[HarmonyPostfix]
		private static void OnDestroy_Postfix(GUIManager __instance)
		{
			Plugin.PDM.OnDestroy();
		}
	}
	[HarmonyPatch(typeof(PointPinger))]
	internal static class PointPingerPatch
	{
		[HarmonyPatch("ReceivePoint_Rpc")]
		[HarmonyPrefix]
		private static void ReceivePoint_Rpc_Prefix(PointPinger __instance, ref Vector3 point, ref Vector3 hitNormal)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			Plugin.PDM.CreateInstance(__instance.character, point + hitNormal);
		}
	}
}