Decompiled source of PingDisplay v1.3.1

PingDisplay.dll

Decompiled 9 months ago
using System;
using System.Collections;
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 Steamworks;
using TMPro;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.0.0.0")]
[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 PingDisplay
{
	[BepInPlugin("DeathWrench.PingDisplay", "PingDisplay", "1.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		public enum DisplayPosition
		{
			TopLeft,
			TopRight,
			BottomLeft,
			BottomRight
		}

		[HarmonyPatch(typeof(HUDManager))]
		internal class HudManagerPatch
		{
			[HarmonyPatch("Start")]
			[HarmonyPostfix]
			private static void PatchHudManagerStart(ref HUDManager __instance)
			{
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				//IL_001f: Expected O, but got Unknown
				//IL_0059: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ce: 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_00e1: Expected O, but got Unknown
				GameObject val = new GameObject("PingDisplay", new Type[1] { typeof(RectTransform) });
				val.transform.SetParent(__instance.HUDContainer.transform, false);
				_displayText = val.AddComponent<TextMeshProUGUI>();
				((TMP_Text)_displayText).faceColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)40);
				((TMP_Text)_displayText).font = ((TMP_Text)__instance.weightCounter).font;
				((TMP_Text)_displayText).fontSize = fontSizeConfig.Value;
				PositionDisplay(displayPositionConfig.Value);
				((Behaviour)_displayText).enabled = pingEnabledConfig.Value;
				CanvasGroup val2 = val.AddComponent<CanvasGroup>();
				val2.alpha = 1f;
				HUDElement val3 = new HUDElement
				{
					canvasGroup = val2,
					targetAlpha = 1f
				};
				HUDElement[] array = (HUDElement[])AccessTools.Field(typeof(HUDManager), "HUDElements").GetValue(__instance);
				Array.Resize(ref array, array.Length + 1);
				array[^1] = val3;
			}

			[HarmonyPatch("Update")]
			[HarmonyPostfix]
			private static void PatchHudManagerUpdate(ref HUDManager __instance)
			{
				if (pingEnabledConfig.Value)
				{
					if (((NetworkBehaviour)__instance).NetworkManager.IsHost)
					{
						((TMP_Text)_displayText).text = "Ping: Host";
					}
					else
					{
						((TMP_Text)_displayText).text = $"Ping: {PingManager.Ping}ms";
					}
				}
			}
		}

		public static ConfigEntry<bool> pingEnabledConfig;

		public static ConfigEntry<int> fontSizeConfig;

		public static ConfigEntry<DisplayPosition> displayPositionConfig;

		public static float Margin;

		public static TextMeshProUGUI _displayText;

		public static Plugin Instance;

		public static PingManager PingManager;

		private readonly Harmony _harmony = new Harmony("DeathWrench.PingDisplay");

		public void Start()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			SetupConfig(pingEnabledConfig = ((BaseUnityPlugin)Instance).Config.Bind<bool>("General", "Ping Enabled", true, "Toggle to enable/disable ping display"), delegate(bool value)
			{
				((Behaviour)_displayText).enabled = value;
			});
			SetupConfig<DisplayPosition>(displayPositionConfig = ((BaseUnityPlugin)this).Config.Bind<DisplayPosition>("General", "Display Position", DisplayPosition.TopRight, "Where on the HUD to display your latency"), PositionDisplay);
			SetupConfig(fontSizeConfig = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Font Size", 12, ""), delegate(int value)
			{
				((TMP_Text)_displayText).fontSize = value;
			});
			if (pingEnabledConfig.Value)
			{
				_harmony.PatchAll(typeof(HudManagerPatch));
			}
			InitPingManager();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Ping Display loaded!");
		}

		private static void SetupConfig<T>(ConfigEntry<T> config, Action<T> changedHandler)
		{
			Action<T> changedHandler2 = changedHandler;
			ConfigEntry<T> config2 = config;
			config2.SettingChanged += delegate
			{
				changedHandler2(config2.Value);
			};
		}

		private void InitPingManager()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			GameObject val = new GameObject("PingManager");
			Object.DontDestroyOnLoad((Object)(object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<PingManager>();
			PingManager = val.GetComponent<PingManager>();
		}

		public static void Log(string message)
		{
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)message);
		}

		private static void PositionDisplay(DisplayPosition position)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: 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_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: 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_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: 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_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_displayText == (Object)null))
			{
				RectTransform rectTransform = ((TMP_Text)_displayText).rectTransform;
				if (1 == 0)
				{
				}
				Vector2 val = (Vector2)(position switch
				{
					DisplayPosition.TopLeft => new Vector2(0f, 1f), 
					DisplayPosition.TopRight => new Vector2(1f, 1f), 
					DisplayPosition.BottomLeft => new Vector2(0f, 0f), 
					DisplayPosition.BottomRight => new Vector2(1f, 0f), 
					_ => throw new ArgumentOutOfRangeException(), 
				});
				if (1 == 0)
				{
				}
				Vector2 val3 = (rectTransform.pivot = val);
				AddMargins(ref val3.x);
				AddMargins(ref val3.y);
				val = (rectTransform.anchorMax = val3);
				rectTransform.anchorMin = val;
				rectTransform.anchoredPosition = Vector2.zero;
				TextMeshProUGUI displayText = _displayText;
				if (1 == 0)
				{
				}
				TextAlignmentOptions alignment = (TextAlignmentOptions)(position switch
				{
					DisplayPosition.TopLeft => 257, 
					DisplayPosition.TopRight => 260, 
					DisplayPosition.BottomLeft => 1025, 
					DisplayPosition.BottomRight => 1028, 
					_ => throw new ArgumentOutOfRangeException(), 
				});
				if (1 == 0)
				{
				}
				((TMP_Text)displayText).alignment = alignment;
			}
		}

		public static void AddMargins(ref float value)
		{
			if (value == 0f)
			{
				value += Margin;
			}
			else
			{
				value -= Margin;
			}
		}
	}
	public class PingManager : MonoBehaviour
	{
		public int Ping { get; private set; }

		private void Start()
		{
			if (Plugin.pingEnabledConfig.Value)
			{
				((MonoBehaviour)this).StartCoroutine(UpdatePingData());
			}
		}

		private void OnDestroy()
		{
			((MonoBehaviour)this).StopCoroutine("UpdatePingData");
		}

		private IEnumerator UpdatePingData()
		{
			while ((Object)(object)StartOfRound.Instance == (Object)null)
			{
				yield return (object)new WaitForSeconds(3f);
			}
			while (true)
			{
				if (SteamNetworkingUtils.LocalPingLocation.HasValue && SteamNetworkingUtils.LocalPingLocation.HasValue)
				{
					Ping = SteamNetworkingUtils.EstimatePingTo(SteamNetworkingUtils.LocalPingLocation.Value);
					yield return (object)new WaitForSeconds(0.5f);
				}
				else
				{
					Plugin.Log("Could not update ping data. Retrying in 10 seconds.");
					yield return (object)new WaitForSeconds(10f);
				}
			}
		}
	}
}