Decompiled source of PingDisplay v1.0.1

BepInEx\plugins\PingDisplay\PingDisplay.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PingDisplay;

[BepInPlugin("com.peachh.pingdisplay", "PingDisplay", "1.0.1")]
public class Plugin : BaseUnityPlugin
{
	private enum UILanguage
	{
		Chinese,
		English
	}

	private static class Loc
	{
		public const string LabelCN = "Ping: ";

		public const string LabelEN = "Ping: ";

		public const string MaxCN = "最大: ";

		public const string MaxEN = "Max: ";

		public const string AvgCN = "均: ";

		public const string AvgEN = "Avg: ";

		public const string JitCN = "抖: ";

		public const string JitEN = "Jit: ";

		public const string LossCN = "丢: ";

		public const string LossEN = "Loss: ";

		public const string MsCN = "ms";

		public const string MsEN = "ms";

		public const string DisconnectedCN = "未连接";

		public const string DisconnectedEN = "Not Connected";

		public const string ConnectingCN = "连接中";

		public const string ConnectingEN = "Connecting...";

		public const string InLobbyCN = "大厅中";

		public const string InLobbyEN = "In Lobby";
	}

	private enum PingCorner
	{
		TopLeft,
		TopRight,
		BottomLeft,
		BottomRight
	}

	internal static class PluginInfo
	{
		public const string PLUGIN_GUID = "com.peachh.pingdisplay";

		public const string PLUGIN_NAME = "PingDisplay";

		public const string PLUGIN_VERSION = "1.0.1";

		public const string PLUGIN_AUTHOR = "peachh";
	}

	private ConfigEntry<UILanguage> _language;

	private ConfigEntry<PingCorner> _corner;

	private ConfigEntry<float> _paddingLeft;

	private ConfigEntry<float> _paddingRight;

	private ConfigEntry<float> _paddingTop;

	private ConfigEntry<float> _paddingBottom;

	private ConfigEntry<string> _fontName;

	private ConfigEntry<int> _fontSize;

	private ConfigEntry<string> _fontColor;

	private ConfigEntry<bool> _showMsSuffix;

	private ConfigEntry<bool> _showLabel;

	private ConfigEntry<bool> _showInMenu;

	private ConfigEntry<int> _greenThreshold;

	private ConfigEntry<int> _yellowThreshold;

	private ConfigEntry<int> _orangeThreshold;

	private ConfigEntry<bool> _showMaxPing;

	private ConfigEntry<int> _maxPingFontSize;

	private ConfigEntry<string> _maxPingColor;

	private ConfigEntry<int> _maxPingResetInterval;

	private ConfigEntry<float> _regionNotifyDuration;

	private ConfigEntry<bool> _showAvgPing;

	private ConfigEntry<bool> _showJitter;

	private ConfigEntry<bool> _showPacketLoss;

	private Harmony _harmony;

	private static Plugin _instance;

	private static GameObject _container;

	private static Text _pingText;

	private static Text _maxPingText;

	private static Text _statsText;

	private static int _currentPing;

	private static float _lastPingCheck;

	private static float _periodStartTime;

	private static int _periodMaxPing;

	private static int[] _pingHistory;

	private static int _pingHistoryIdx;

	private static int _pingHistoryCount;

	private static int _pingSum;

	private const float PING_CHECK_INTERVAL = 1f;

	private const int PING_HISTORY_SIZE = 10;

	private static bool _wasConnected;

	private static float _regionNotifyTime;

	private static string _regionStr = "";

	private static float _disconnectedSince = -1f;

	private const float DISCONNECT_GRACE = 2f;

	private static int _cachedPing = -1;

	private static int _cachedMaxPing = -1;

	private static string _cachedPingStr = "";

	private static string _cachedMaxStr = "";

	private static string _cachedStatsStr = "";

	private Color _cachedFontColor;

	private Color _cachedMaxPingColor;

	private int _cachedFontSize;

	private int _cachedMaxFontSize;

	private int _cachedGreen;

	private int _cachedYellow;

	private int _cachedOrange;

	private bool _cachedShowLabel;

	private bool _cachedShowMs;

	private bool _cachedShowMax;

	private bool _cachedShowInMenu;

	private bool _cachedShowAvg;

	private bool _cachedShowJitter;

	private bool _cachedShowLoss;

	private static bool _forceRefresh;

	private void Awake()
	{
		_instance = this;
		_pingHistory = new int[10];
		BindConfig();
		CacheConfig();
		InitHarmony();
		CreateCanvasUI();
		SceneManager.sceneLoaded += OnSceneLoaded;
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin PingDisplay v1.0.1 loaded.");
	}

	private void BindConfig()
	{
		_language = ((BaseUnityPlugin)this).Config.Bind<UILanguage>("Language", "Language", UILanguage.English, "界面语言  UI Language");
		_corner = ((BaseUnityPlugin)this).Config.Bind<PingCorner>("Display", "Corner", PingCorner.TopLeft, "显示在屏幕的哪个角  Display corner");
		_paddingLeft = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PaddingLeft", 80f, "距屏幕左边距离  Padding from left (1920x1080 ref)");
		_paddingRight = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PaddingRight", 50f, "距屏幕右边距离  Padding from right");
		_paddingTop = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PaddingTop", 5f, "距屏幕顶部距离  Padding from top");
		_paddingBottom = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PaddingBottom", 50f, "距屏幕底部距离  Padding from bottom");
		_fontName = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "FontName", "Arial", "字体名称  Font name (Arial, Consolas, etc.)");
		_fontSize = ((BaseUnityPlugin)this).Config.Bind<int>("Display", "FontSize", 22, "Ping 字体大小  Ping text font size");
		_fontColor = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "FontColor", "", "自定义颜色 HEX (#00FF00),留空=自动着色  Custom HEX color, empty=auto-color");
		_showMsSuffix = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowMsSuffix", true, "数值后显示 'ms'  Show 'ms' suffix");
		_showLabel = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowLabel", true, "显示标签前缀  Show label prefix");
		_showInMenu = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowInMenu", true, "在菜单/大厅也显示  Show in menu and lobby");
		_greenThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Colors", "GreenThreshold", 50, "<此值绿色  Below this → green (ms)");
		_yellowThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Colors", "YellowThreshold", 100, "<此值黄色  Below this → yellow (ms)");
		_orangeThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("Colors", "OrangeThreshold", 200, "<此值橙色  Below this → orange (>this → red)");
		_showMaxPing = ((BaseUnityPlugin)this).Config.Bind<bool>("MaxPing", "ShowMaxPing", true, "显示最大 Ping 值  Show max ping value");
		_maxPingFontSize = ((BaseUnityPlugin)this).Config.Bind<int>("MaxPing", "MaxPingFontSize", 14, "最大 Ping 字体大小  Max ping font size");
		_maxPingColor = ((BaseUnityPlugin)this).Config.Bind<string>("MaxPing", "MaxPingColor", "", "最大 Ping 颜色 HEX,留空=跟随主文字  Max ping HEX color, empty=follow main");
		_maxPingResetInterval = ((BaseUnityPlugin)this).Config.Bind<int>("MaxPing", "ResetInterval", 60, "最大 Ping 重置间隔(秒),0=不重置  Max ping reset interval in seconds, 0=never reset");
		_regionNotifyDuration = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "RegionNotifyDuration", 5f, "连接后显示服务器地区的时间(秒),0=不显示  Show region for N seconds after connect, 0=off");
		_showAvgPing = ((BaseUnityPlugin)this).Config.Bind<bool>("Stats", "ShowAvgPing", false, "显示平均延迟  Show average ping");
		_showJitter = ((BaseUnityPlugin)this).Config.Bind<bool>("Stats", "ShowJitter", false, "显示抖动 (RTT 方差)  Show jitter");
		_showPacketLoss = ((BaseUnityPlugin)this).Config.Bind<bool>("Stats", "ShowPacketLoss", false, "显示丢包率  Show packet loss %");
		_fontColor.SettingChanged += delegate
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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)
			_cachedFontColor = ParseHexColor(_fontColor.Value, Color.white);
			_forceRefresh = true;
		};
		_maxPingColor.SettingChanged += delegate
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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)
			_cachedMaxPingColor = ParseHexColor(_maxPingColor.Value, Color.white);
			_forceRefresh = true;
		};
		_fontSize.SettingChanged += delegate
		{
			_cachedFontSize = _fontSize.Value;
			_forceRefresh = true;
		};
		_maxPingFontSize.SettingChanged += delegate
		{
			_cachedMaxFontSize = _maxPingFontSize.Value;
			_forceRefresh = true;
		};
		_greenThreshold.SettingChanged += delegate
		{
			_cachedGreen = _greenThreshold.Value;
			_forceRefresh = true;
		};
		_yellowThreshold.SettingChanged += delegate
		{
			_cachedYellow = _yellowThreshold.Value;
			_forceRefresh = true;
		};
		_orangeThreshold.SettingChanged += delegate
		{
			_cachedOrange = _orangeThreshold.Value;
			_forceRefresh = true;
		};
		_showLabel.SettingChanged += delegate
		{
			_cachedShowLabel = _showLabel.Value;
			_forceRefresh = true;
		};
		_showMsSuffix.SettingChanged += delegate
		{
			_cachedShowMs = _showMsSuffix.Value;
			_forceRefresh = true;
		};
		_showMaxPing.SettingChanged += delegate
		{
			_cachedShowMax = _showMaxPing.Value;
			_forceRefresh = true;
		};
		_showInMenu.SettingChanged += delegate
		{
			_cachedShowInMenu = _showInMenu.Value;
			_forceRefresh = true;
		};
		_showAvgPing.SettingChanged += delegate
		{
			_cachedShowAvg = _showAvgPing.Value;
			_forceRefresh = true;
		};
		_showJitter.SettingChanged += delegate
		{
			_cachedShowJitter = _showJitter.Value;
			_forceRefresh = true;
		};
		_showPacketLoss.SettingChanged += delegate
		{
			_cachedShowLoss = _showPacketLoss.Value;
			_forceRefresh = true;
		};
		_language.SettingChanged += delegate
		{
			_forceRefresh = true;
		};
		_corner.SettingChanged += delegate
		{
			RepositionContainer();
		};
		_paddingLeft.SettingChanged += delegate
		{
			RepositionContainer();
		};
		_paddingRight.SettingChanged += delegate
		{
			RepositionContainer();
		};
		_paddingTop.SettingChanged += delegate
		{
			RepositionContainer();
		};
		_paddingBottom.SettingChanged += delegate
		{
			RepositionContainer();
		};
	}

	private void CacheConfig()
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: 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_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		_cachedFontColor = ParseHexColor(_fontColor.Value, Color.white);
		_cachedMaxPingColor = ParseHexColor(_maxPingColor.Value, Color.white);
		_cachedFontSize = _fontSize.Value;
		_cachedMaxFontSize = _maxPingFontSize.Value;
		_cachedGreen = _greenThreshold.Value;
		_cachedYellow = _yellowThreshold.Value;
		_cachedOrange = _orangeThreshold.Value;
		_cachedShowLabel = _showLabel.Value;
		_cachedShowMs = _showMsSuffix.Value;
		_cachedShowMax = _showMaxPing.Value;
		_cachedShowInMenu = _showInMenu.Value;
		_cachedShowAvg = _showAvgPing.Value;
		_cachedShowJitter = _showJitter.Value;
		_cachedShowLoss = _showPacketLoss.Value;
	}

	private void InitHarmony()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected O, but got Unknown
		_harmony = new Harmony("com.peachh.pingdisplay");
		try
		{
			MethodInfo method = typeof(PlayerAvatar).GetMethod("Update", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (method != null)
			{
				_harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnPlayerUpdatePostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"PlayerAvatar.Update not found.");
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Harmony patch failed: " + ex.Message));
		}
	}

	private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
	{
		if ((Object)(object)_pingText == (Object)null || (Object)(object)((Component)_pingText).gameObject == (Object)null)
		{
			CreateCanvasUI();
		}
	}

	private void RepositionContainer()
	{
		if (!((Object)(object)_container == (Object)null))
		{
			RectTransform component = _container.GetComponent<RectTransform>();
			if (!((Object)(object)component == (Object)null))
			{
				SetupCornerPosition(component);
			}
		}
	}

	private void OnDestroy()
	{
		SceneManager.sceneLoaded -= OnSceneLoaded;
	}

	private void CreateCanvasUI()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_007b: 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_009b: Expected O, but got Unknown
		try
		{
			if (!((Object)(object)_pingText != (Object)null) || !((Object)(object)((Component)_pingText).gameObject != (Object)null))
			{
				Font font = LoadFont(_fontName.Value);
				GameObject val = new GameObject("PingDisplay_Canvas");
				Object.DontDestroyOnLoad((Object)(object)val);
				val.layer = 5;
				Canvas obj = val.AddComponent<Canvas>();
				obj.renderMode = (RenderMode)0;
				obj.sortingOrder = 32767;
				CanvasScaler obj2 = val.AddComponent<CanvasScaler>();
				obj2.uiScaleMode = (ScaleMode)1;
				obj2.referenceResolution = new Vector2(1920f, 1080f);
				val.AddComponent<GraphicRaycaster>();
				_container = new GameObject("PingContainer");
				_container.transform.SetParent(val.transform, false);
				_container.layer = 5;
				RectTransform rt = _container.AddComponent<RectTransform>();
				SetupCornerPosition(rt);
				VerticalLayoutGroup obj3 = _container.AddComponent<VerticalLayoutGroup>();
				PingCorner value = _corner.Value;
				((LayoutGroup)obj3).childAlignment = (TextAnchor)((value != PingCorner.TopLeft && value != PingCorner.BottomLeft) ? 2 : 0);
				((HorizontalOrVerticalLayoutGroup)obj3).childForceExpandWidth = false;
				((HorizontalOrVerticalLayoutGroup)obj3).childForceExpandHeight = false;
				((HorizontalOrVerticalLayoutGroup)obj3).spacing = 3f;
				ContentSizeFitter obj4 = _container.AddComponent<ContentSizeFitter>();
				obj4.horizontalFit = (FitMode)2;
				obj4.verticalFit = (FitMode)2;
				_pingText = CreateText("PingText", font, _cachedFontSize, 300f);
				AddOutline(_pingText);
				_maxPingText = CreateText("MaxPingText", font, _cachedMaxFontSize, 260f);
				AddOutline(_maxPingText);
				_statsText = CreateText("StatsText", font, 12, 260f);
				AddOutline(_statsText);
				_periodStartTime = Time.time;
				_periodMaxPing = 0;
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("[CreateCanvasUI] Failed: " + ex.Message));
		}
	}

	private void SetupCornerPosition(RectTransform rt)
	{
		//IL_0047: 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_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: 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_0116: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		float num;
		float num2;
		switch (_corner.Value)
		{
		case PingCorner.TopLeft:
			num = _paddingLeft.Value;
			num2 = 0f - _paddingTop.Value;
			rt.anchorMin = new Vector2(0f, 1f);
			rt.anchorMax = new Vector2(0f, 1f);
			rt.pivot = new Vector2(0f, 1f);
			break;
		case PingCorner.TopRight:
			num = 0f - _paddingRight.Value;
			num2 = 0f - _paddingTop.Value;
			rt.anchorMin = new Vector2(1f, 1f);
			rt.anchorMax = new Vector2(1f, 1f);
			rt.pivot = new Vector2(1f, 1f);
			break;
		case PingCorner.BottomLeft:
			num = _paddingLeft.Value;
			num2 = _paddingBottom.Value;
			rt.anchorMin = new Vector2(0f, 0f);
			rt.anchorMax = new Vector2(0f, 0f);
			rt.pivot = new Vector2(0f, 0f);
			break;
		default:
			num = 0f - _paddingRight.Value;
			num2 = _paddingBottom.Value;
			rt.anchorMin = new Vector2(1f, 0f);
			rt.anchorMax = new Vector2(1f, 0f);
			rt.pivot = new Vector2(1f, 0f);
			break;
		}
		rt.anchoredPosition = new Vector2(num, num2);
		rt.sizeDelta = new Vector2(300f, 80f);
	}

	private Text CreateText(string name, Font font, int size, float width)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: 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_002f: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(name);
		val.transform.SetParent(_container.transform, false);
		val.layer = 5;
		val.AddComponent<RectTransform>().sizeDelta = new Vector2(width, (float)(size + 4));
		Text obj = val.AddComponent<Text>();
		obj.font = font;
		obj.fontSize = size;
		PingCorner value = _corner.Value;
		obj.alignment = (TextAnchor)((value != PingCorner.TopLeft && value != PingCorner.BottomLeft) ? 2 : 0);
		obj.text = "";
		return obj;
	}

	private static void AddOutline(Text text)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		Outline obj = ((Component)text).gameObject.AddComponent<Outline>();
		((Shadow)obj).effectColor = Color.black;
		((Shadow)obj).effectDistance = new Vector2(1f, -1f);
	}

	private static Font LoadFont(string fontName)
	{
		if (string.IsNullOrEmpty(fontName))
		{
			fontName = "Arial";
		}
		Font val = null;
		try
		{
			val = Font.CreateDynamicFontFromOSFont(fontName, 16);
		}
		catch
		{
		}
		if ((Object)(object)val == (Object)null)
		{
			try
			{
				val = Resources.GetBuiltinResource<Font>("Arial.ttf");
			}
			catch
			{
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			try
			{
				Object builtinResource = Resources.GetBuiltinResource(typeof(Font), "LegacyRuntime.ttf");
				val = (Font)(object)((builtinResource is Font) ? builtinResource : null);
			}
			catch
			{
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			try
			{
				val = Font.CreateDynamicFontFromOSFont("Arial", 16);
			}
			catch
			{
			}
		}
		return val;
	}

	private static void OnPlayerUpdatePostfix()
	{
		_instance?.UpdatePingDisplay();
	}

	private void UpdatePingDisplay()
	{
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_076b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d8: Invalid comparison between Unknown and I4
		//IL_024d: Unknown result type (might be due to invalid IL or missing references)
		//IL_044c: Unknown result type (might be due to invalid IL or missing references)
		//IL_043f: Unknown result type (might be due to invalid IL or missing references)
		//IL_051e: Unknown result type (might be due to invalid IL or missing references)
		//IL_05c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_05b5: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_pingText == (Object)null || (Object)(object)((Component)_pingText).gameObject == (Object)null)
		{
			CreateCanvasUI();
			if ((Object)(object)_pingText == (Object)null)
			{
				return;
			}
		}
		if (SemiFunc.MenuLevel() && !_cachedShowInMenu)
		{
			if (_cachedPingStr != "")
			{
				_cachedPingStr = "";
				_cachedMaxStr = "";
				_cachedStatsStr = "";
				_pingText.text = "";
				_maxPingText.text = "";
				_statsText.text = "";
			}
			return;
		}
		bool flag = false;
		if (Time.time - _lastPingCheck >= 1f)
		{
			_lastPingCheck = Time.time;
			int currentPing = GetCurrentPing();
			if (currentPing != _currentPing)
			{
				_currentPing = currentPing;
				flag = true;
			}
			if (_pingHistoryCount == 10)
			{
				_pingSum -= _pingHistory[_pingHistoryIdx];
			}
			_pingHistory[_pingHistoryIdx] = _currentPing;
			_pingSum += _currentPing;
			_pingHistoryIdx = (_pingHistoryIdx + 1) % 10;
			if (_pingHistoryCount < 10)
			{
				_pingHistoryCount++;
			}
			int value = _maxPingResetInterval.Value;
			if (value > 0 && Time.time - _periodStartTime >= (float)value)
			{
				_periodMaxPing = 0;
				_periodStartTime = Time.time;
			}
			if (_currentPing > _periodMaxPing)
			{
				_periodMaxPing = _currentPing;
			}
			flag = true;
		}
		ClientState networkClientState = PhotonNetwork.NetworkClientState;
		if (!PhotonNetwork.IsConnectedAndReady)
		{
			_wasConnected = false;
			bool flag2 = _language.Value == UILanguage.Chinese;
			if (_disconnectedSince < 0f)
			{
				_disconnectedSince = Time.time;
			}
			bool flag3 = Time.time - _disconnectedSince < 2f;
			string text = (((int)networkClientState == 4) ? (flag2 ? "大厅中" : "In Lobby") : ((!flag3) ? (flag2 ? "未连接" : "Not Connected") : (flag2 ? "连接中" : "Connecting...")));
			if (_cachedPingStr != text)
			{
				_cachedPingStr = text;
				_pingText.text = text;
				((Graphic)_pingText).color = new Color(0.7f, 0.7f, 0.7f);
				_pingText.fontSize = _cachedFontSize;
				_maxPingText.text = "";
				_statsText.text = "";
				_cachedMaxStr = "";
				_cachedStatsStr = "";
			}
			return;
		}
		if (!_wasConnected)
		{
			_wasConnected = true;
			_disconnectedSince = -1f;
			if (_regionNotifyDuration.Value > 0f)
			{
				try
				{
					_regionStr = PhotonNetwork.CloudRegion ?? "";
				}
				catch
				{
					_regionStr = "";
				}
				if (!string.IsNullOrEmpty(_regionStr))
				{
					_regionNotifyTime = Time.time;
				}
			}
		}
		if (_forceRefresh)
		{
			_forceRefresh = false;
			_cachedPingStr = "";
			_cachedMaxStr = "";
			_cachedStatsStr = "";
			_cachedPing = -1;
			_cachedMaxPing = -1;
		}
		if (!flag && !_forceRefresh && _currentPing == _cachedPing && _periodMaxPing == _cachedMaxPing)
		{
			return;
		}
		_cachedPing = _currentPing;
		_cachedMaxPing = _periodMaxPing;
		bool flag4 = _language.Value == UILanguage.Chinese;
		string text2 = (flag4 ? "ms" : "ms");
		string text3 = (_cachedShowMs ? $"{_currentPing}{text2}" : $"{_currentPing}");
		string text4 = (flag4 ? "Ping: " : "Ping: ");
		string text5 = (_cachedShowLabel ? (text4 + text3) : text3);
		if (_cachedPingStr != text5 || _cachedPingStr == "")
		{
			_cachedPingStr = text5;
			_pingText.text = text5;
			_pingText.fontSize = _cachedFontSize;
			((Graphic)_pingText).color = (string.IsNullOrEmpty(_fontColor.Value) ? GetAutoPingColor(_currentPing) : _cachedFontColor);
		}
		if (_regionNotifyDuration.Value > 0f && _regionNotifyTime > 0f && Time.time - _regionNotifyTime < _regionNotifyDuration.Value && !string.IsNullOrEmpty(_regionStr))
		{
			((Component)_maxPingText).gameObject.SetActive(true);
			string text6 = (flag4 ? ("地区: " + _regionStr) : ("Region: " + _regionStr));
			if (_cachedMaxStr != text6)
			{
				_cachedMaxStr = text6;
				_maxPingText.text = text6;
				_maxPingText.fontSize = _cachedMaxFontSize;
				((Graphic)_maxPingText).color = new Color(0.4f, 0.8f, 1f);
			}
		}
		else if (_cachedShowMax)
		{
			((Component)_maxPingText).gameObject.SetActive(true);
			string text7 = (flag4 ? "最大: " : "Max: ") + _periodMaxPing + text2;
			if (_cachedMaxStr != text7)
			{
				_cachedMaxStr = text7;
				_maxPingText.text = text7;
				_maxPingText.fontSize = _cachedMaxFontSize;
				((Graphic)_maxPingText).color = (string.IsNullOrEmpty(_maxPingColor.Value) ? ((Graphic)_pingText).color : _cachedMaxPingColor);
			}
		}
		else if (_cachedMaxStr != "")
		{
			_cachedMaxStr = "";
			((Component)_maxPingText).gameObject.SetActive(false);
			_maxPingText.text = "";
		}
		if (_cachedShowAvg || _cachedShowJitter || _cachedShowLoss)
		{
			((Component)_statsText).gameObject.SetActive(true);
			List<string> list = new List<string>(3);
			if (_cachedShowAvg)
			{
				int num = ((_pingHistoryCount > 0) ? (_pingSum / _pingHistoryCount) : _currentPing);
				string arg = (flag4 ? "均: " : "Avg: ");
				list.Add($"{arg}{num}{text2}");
			}
			if (_cachedShowJitter)
			{
				int num2 = 0;
				try
				{
					num2 = GetPeerStat<int>("RoundTripTimeVariance");
				}
				catch
				{
				}
				string arg2 = (flag4 ? "抖: " : "Jit: ");
				list.Add($"{arg2}{num2}{text2}");
			}
			if (_cachedShowLoss)
			{
				int num3 = 0;
				try
				{
					num3 = GetPeerStat<byte>("PacketLossByCrc");
				}
				catch
				{
				}
				string arg3 = (flag4 ? "丢: " : "Loss: ");
				list.Add($"{arg3}{num3}%");
			}
			string text8 = string.Join("  ", list);
			if (_cachedStatsStr != text8)
			{
				_cachedStatsStr = text8;
				_statsText.text = text8;
				_statsText.fontSize = 12;
				((Graphic)_statsText).color = new Color(0.55f, 0.55f, 0.55f);
			}
		}
		else if (_cachedStatsStr != "")
		{
			_cachedStatsStr = "";
			((Component)_statsText).gameObject.SetActive(false);
			_statsText.text = "";
		}
	}

	private static int GetCurrentPing()
	{
		try
		{
			return PhotonNetwork.GetPing();
		}
		catch
		{
			return 0;
		}
	}

	private static T GetPeerStat<T>(string propertyName)
	{
		LoadBalancingPeer loadBalancingPeer = PhotonNetwork.NetworkingClient.LoadBalancingPeer;
		PropertyInfo property = ((object)loadBalancingPeer).GetType().BaseType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);
		if (property != null)
		{
			return (T)property.GetValue(loadBalancingPeer);
		}
		return default(T);
	}

	private Color GetAutoPingColor(int ping)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: 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_006d: Unknown result type (might be due to invalid IL or missing references)
		if (ping <= 0)
		{
			return new Color(0.5f, 0.5f, 0.5f);
		}
		if (ping < _cachedGreen)
		{
			return new Color(0f, 1f, 0.4f);
		}
		if (ping < _cachedYellow)
		{
			return new Color(1f, 0.9f, 0f);
		}
		if (ping < _cachedOrange)
		{
			return new Color(1f, 0.55f, 0f);
		}
		return new Color(1f, 0.2f, 0.2f);
	}

	private static Color ParseHexColor(string hex, Color fallback)
	{
		//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_0008: 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)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		if (string.IsNullOrEmpty(hex))
		{
			return fallback;
		}
		hex = hex.TrimStart(new char[1] { '#' });
		if (hex.Length != 6 && hex.Length != 8)
		{
			return fallback;
		}
		try
		{
			return new Color((float)Convert.ToInt32(hex.Substring(0, 2), 16) / 255f, (float)Convert.ToInt32(hex.Substring(2, 2), 16) / 255f, (float)Convert.ToInt32(hex.Substring(4, 2), 16) / 255f, (hex.Length >= 8) ? ((float)Convert.ToInt32(hex.Substring(6, 2), 16) / 255f) : 1f);
		}
		catch
		{
			return fallback;
		}
	}
}