Decompiled source of FPSdisplay v1.2.0

plugins\FPS Display.dll

Decompiled a month ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("FPSDisplay")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FPSDisplay")]
[assembly: AssemblyTitle("FPSDisplay")]
[assembly: AssemblyVersion("1.0.0.0")]
[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;
		}
	}
}
[BepInPlugin("com.zizzy.fpsdisplay", "FPS Display", "1.0.0")]
public class FPSDisplayPlugin : BaseUnityPlugin
{
	private GameObject canvasGO;

	private TextMeshProUGUI fpsText;

	private RectTransform textRect;

	private ConfigEntry<string> cornerConfig;

	private ConfigEntry<int> fontSizeConfig;

	private ConfigEntry<float> marginTopConfig;

	private ConfigEntry<float> marginBottomConfig;

	private ConfigEntry<float> marginLeftConfig;

	private ConfigEntry<float> marginRightConfig;

	private ConfigEntry<float> updateIntervalConfig;

	private ConfigEntry<float> averageWindowConfig;

	private float updateInterval = 0.25f;

	private float averageWindow = 1f;

	private readonly string fontName = "DarumaDropOne-Regular SDF";

	private readonly Queue<float> sampleTimestamps = new Queue<float>();

	private readonly Queue<float> sampleDeltas = new Queue<float>();

	private float sampleDeltaSum = 0f;

	private void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Expected O, but got Unknown
		//IL_0138: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Expected O, but got Unknown
		updateIntervalConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "UpdateInterval", 0.25f, new ConfigDescription("Time between FPS updates in seconds (0-0.5)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 0.5f), Array.Empty<object>()));
		updateIntervalConfig.SettingChanged += delegate
		{
			updateInterval = Mathf.Clamp(updateIntervalConfig.Value, 0f, 0.5f);
		};
		updateInterval = Mathf.Clamp(updateIntervalConfig.Value, 0f, 0.5f);
		averageWindowConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "AverageWindow", 1f, new ConfigDescription("How long of a rolling window to average FPS over, in seconds (0 = instantaneous, like before)", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 3f), Array.Empty<object>()));
		averageWindowConfig.SettingChanged += delegate
		{
			averageWindow = Mathf.Clamp(averageWindowConfig.Value, 0f, 3f);
		};
		averageWindow = Mathf.Clamp(averageWindowConfig.Value, 0f, 3f);
		cornerConfig = ((BaseUnityPlugin)this).Config.Bind<string>("Display", "Corner", "TopLeft", new ConfigDescription("Corner", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[4] { "TopLeft", "TopRight", "BottomLeft", "BottomRight" }), Array.Empty<object>()));
		fontSizeConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Display", "FontSize", 20, (ConfigDescription)null);
		marginTopConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "MarginTop", 25f, (ConfigDescription)null);
		marginBottomConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "MarginBottom", 0f, (ConfigDescription)null);
		marginLeftConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "MarginLeft", 3f, (ConfigDescription)null);
		marginRightConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "MarginRight", 0f, (ConfigDescription)null);
		cornerConfig.SettingChanged += delegate
		{
			UpdatePosition();
		};
		fontSizeConfig.SettingChanged += delegate
		{
			UpdateFontSize();
		};
		marginTopConfig.SettingChanged += delegate
		{
			UpdatePosition();
		};
		marginBottomConfig.SettingChanged += delegate
		{
			UpdatePosition();
		};
		marginLeftConfig.SettingChanged += delegate
		{
			UpdatePosition();
		};
		marginRightConfig.SettingChanged += delegate
		{
			UpdatePosition();
		};
		((MonoBehaviour)this).StartCoroutine(InitGUI());
	}

	private void Update()
	{
		if (averageWindow <= 0f)
		{
			sampleTimestamps.Clear();
			sampleDeltas.Clear();
			sampleDeltaSum = 0f;
			return;
		}
		float unscaledDeltaTime = Time.unscaledDeltaTime;
		float unscaledTime = Time.unscaledTime;
		sampleTimestamps.Enqueue(unscaledTime);
		sampleDeltas.Enqueue(unscaledDeltaTime);
		sampleDeltaSum += unscaledDeltaTime;
		while (sampleTimestamps.Count > 0 && unscaledTime - sampleTimestamps.Peek() > averageWindow)
		{
			sampleTimestamps.Dequeue();
			sampleDeltaSum -= sampleDeltas.Dequeue();
		}
	}

	private float GetCurrentFPS()
	{
		if (averageWindow <= 0f || sampleDeltas.Count == 0)
		{
			return 1f / Mathf.Max(Time.unscaledDeltaTime, 0.0001f);
		}
		float num = sampleDeltaSum / (float)sampleDeltas.Count;
		return 1f / Mathf.Max(num, 0.0001f);
	}

	private IEnumerator InitGUI()
	{
		yield return null;
		canvasGO = new GameObject("FPSCanvas");
		Canvas canvas = canvasGO.AddComponent<Canvas>();
		canvas.renderMode = (RenderMode)0;
		canvas.sortingOrder = 1000;
		CanvasScaler scaler = canvasGO.AddComponent<CanvasScaler>();
		scaler.uiScaleMode = (ScaleMode)1;
		scaler.referenceResolution = new Vector2(1920f, 1080f);
		canvasGO.AddComponent<GraphicRaycaster>();
		GameObject textGO = new GameObject("FPSText");
		textGO.transform.SetParent(canvasGO.transform, false);
		fpsText = textGO.AddComponent<TextMeshProUGUI>();
		TMP_FontAsset foundFont = null;
		TMP_FontAsset[] fonts = Resources.FindObjectsOfTypeAll<TMP_FontAsset>();
		for (int i = 0; i < fonts.Length; i++)
		{
			if (((Object)fonts[i]).name == fontName)
			{
				foundFont = fonts[i];
				break;
			}
		}
		((TMP_Text)fpsText).font = foundFont ?? Resources.GetBuiltinResource<TMP_FontAsset>("LiberationSans SDF.asset");
		((TMP_Text)fpsText).fontSize = fontSizeConfig.Value;
		((Graphic)fpsText).color = Color.white;
		((TMP_Text)fpsText).enableWordWrapping = false;
		((TMP_Text)fpsText).overflowMode = (TextOverflowModes)0;
		textRect = textGO.GetComponent<RectTransform>();
		UpdatePosition();
		Object.DontDestroyOnLoad((Object)(object)canvasGO);
		((MonoBehaviour)this).StartCoroutine(UpdateFPSLoop());
	}

	private void UpdateFontSize()
	{
		if ((Object)(object)fpsText != (Object)null)
		{
			((TMP_Text)fpsText).fontSize = fontSizeConfig.Value;
		}
	}

	private void UpdatePosition()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: 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_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: 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_008d: 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_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: 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_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: 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_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_012a: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)textRect == (Object)null))
		{
			Vector2 val = Vector2.zero;
			Vector2 zero = Vector2.zero;
			Vector2 zero2 = Vector2.zero;
			switch (cornerConfig.Value)
			{
			case "TopLeft":
				((Vector2)(ref zero))..ctor(0f, 1f);
				val = zero;
				((Vector2)(ref zero2))..ctor(marginLeftConfig.Value, 0f - marginTopConfig.Value);
				break;
			case "TopRight":
				((Vector2)(ref zero))..ctor(1f, 1f);
				val = zero;
				((Vector2)(ref zero2))..ctor(0f - marginRightConfig.Value, 0f - marginTopConfig.Value);
				break;
			case "BottomLeft":
				((Vector2)(ref zero))..ctor(0f, 0f);
				val = zero;
				((Vector2)(ref zero2))..ctor(marginLeftConfig.Value, marginBottomConfig.Value);
				break;
			case "BottomRight":
				((Vector2)(ref zero))..ctor(1f, 0f);
				val = zero;
				((Vector2)(ref zero2))..ctor(0f - marginRightConfig.Value, marginBottomConfig.Value);
				break;
			}
			RectTransform obj = textRect;
			Vector2 anchorMin = (textRect.anchorMax = val);
			obj.anchorMin = anchorMin;
			textRect.pivot = zero;
			textRect.anchoredPosition = zero2;
		}
	}

	private IEnumerator UpdateFPSLoop()
	{
		while (true)
		{
			if ((Object)(object)fpsText != (Object)null)
			{
				((TMP_Text)fpsText).text = $"FPS: {Mathf.RoundToInt(GetCurrentFPS())}";
			}
			yield return (object)new WaitForSeconds(updateInterval);
		}
	}
}