Decompiled source of PingDisplay v1.1.0

BepInEx/plugins/PingDisplay.dll

Decompiled 5 months ago
using System;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using Unity.Netcode;
using UnityEngine;

[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("Kesomannen")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Adds a ping/latency display to the HUD.")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("PingDisplay")]
[assembly: AssemblyTitle("PingDisplay")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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.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("PingDisplay", "PingDisplay", "1.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		private enum DisplayPosition
		{
			TopLeft,
			TopRight,
			BottomLeft,
			BottomRight
		}

		private static TMP_Text _pingText;

		private static float _lastUpdateTime;

		private static ConfigEntry<bool> _showPingConfig;

		private static ConfigEntry<DisplayPosition> _displayPositionConfig;

		private const float UpdateInterval = 0.5f;

		private const float Margin = 0.03f;

		private void Awake()
		{
			SetupConfig(_showPingConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowPing", true, "Show your latency to the host in the top left corner of the screen"), delegate(bool value)
			{
				((Behaviour)_pingText).enabled = value;
			});
			SetupConfig<DisplayPosition>(_displayPositionConfig = ((BaseUnityPlugin)this).Config.Bind<DisplayPosition>("General", "DisplayPosition", DisplayPosition.TopLeft, "Where on the HUD to display your latency"), PositionDisplay);
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin loaded!");
		}

		private static void PositionDisplay(DisplayPosition position)
		{
			//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_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_005f: 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_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: 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_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_pingText == (Object)null))
			{
				RectTransform rectTransform = _pingText.rectTransform;
				Vector2 val2 = (rectTransform.pivot = (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(), 
				}));
				AddMargin(ref val2.x);
				AddMargin(ref val2.y);
				Vector2 anchorMin = (rectTransform.anchorMax = val2);
				rectTransform.anchorMin = anchorMin;
				rectTransform.anchoredPosition = Vector2.zero;
				TMP_Text pingText = _pingText;
				pingText.alignment = (TextAlignmentOptions)(position switch
				{
					DisplayPosition.TopLeft => 257, 
					DisplayPosition.TopRight => 260, 
					DisplayPosition.BottomLeft => 1025, 
					DisplayPosition.BottomRight => 1028, 
					_ => throw new ArgumentOutOfRangeException(), 
				});
			}
			static void AddMargin(ref float value)
			{
				if (value == 0f)
				{
					value += 0.03f;
				}
				else
				{
					value -= 0.03f;
				}
			}
		}

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

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "Awake")]
		private static void HUDManager_Start_Postfix(HUDManager __instance)
		{
			//IL_0018: 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_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: 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_00cf: Expected O, but got Unknown
			GameObject val = new GameObject("PingDisplay", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(__instance.HUDContainer.transform, false);
			_pingText = (TMP_Text)(object)val.AddComponent<TextMeshProUGUI>();
			_pingText.faceColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)40);
			_pingText.font = ((TMP_Text)__instance.weightCounter).font;
			_pingText.fontSize = 12f;
			PositionDisplay(_displayPositionConfig.Value);
			((Behaviour)_pingText).enabled = _showPingConfig.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;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HUDManager), "Update")]
		private static void HUDManager_Update_Postfix(HUDManager __instance)
		{
			if (!((Object)(object)_pingText == (Object)null) && ((Behaviour)_pingText).enabled && !(Time.time - _lastUpdateTime < 0.5f))
			{
				_lastUpdateTime = Time.time;
				if (NetworkManager.Singleton.IsHost)
				{
					_pingText.text = "Ping: ---";
					return;
				}
				ulong currentRtt = NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetCurrentRtt(0uL);
				_pingText.text = $"Ping: {currentRtt}ms";
			}
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "PingDisplay";

		public const string PLUGIN_NAME = "PingDisplay";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}