Decompiled source of BetterFPSLimit v1.0.0

FPSLimit.dll

Decompiled 5 days 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.Bootstrap;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using RoR2.UI;
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("FPSLimit")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FPSLimit")]
[assembly: AssemblyTitle("FPSLimit")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
namespace BetterFpsLimiter
{
	[BepInPlugin("com.CATAHA_CCCP.betterfpslimiter", "Better FPS Limiter", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BetterFpsLimiterPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "com.CATAHA_CCCP.betterfpslimiter";

		public const string PluginName = "Better FPS Limiter";

		public const string PluginVersion = "1.0.0";

		private const string RiskOfOptionsGUID = "com.rune580.riskofoptions";

		private ConfigEntry<bool> _enableCustomGameplayFps;

		private ConfigEntry<int> _gameplayFpsLimit;

		private ConfigEntry<bool> _forceDisableVSync;

		private ConfigEntry<bool> _limitInMenu;

		private ConfigEntry<int> _menuFpsLimit;

		private ConfigEntry<bool> _limitOnPause;

		private ConfigEntry<int> _pauseFpsLimit;

		private ConfigEntry<bool> _limitWhenUnfocused;

		private ConfigEntry<int> _unfocusedFpsLimit;

		private void Awake()
		{
			InitConfig();
			if (Chainloader.PluginInfos.ContainsKey("com.rune580.riskofoptions"))
			{
				SetupRiskOfOptions();
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Better FPS Limiter v1.0.0 loaded successfully!");
		}

		private void InitConfig()
		{
			_enableCustomGameplayFps = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "Enable Custom Gameplay FPS", true, "If true, applies a custom FPS limit during active runs (allows setting values above the vanilla 144 Hz cap).\nIf false, FPS is uncapped (-1 / Unlimited) during gameplay.");
			_gameplayFpsLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Gameplay", "Gameplay FPS Limit", 240, "Target FPS limit during active gameplay.");
			_forceDisableVSync = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "Force Disable VSync", true, "If true, forces VSync off at all times.\nNOTE: Unity ignores targetFrameRate FPS limits while VSync is enabled.");
			_limitInMenu = ((BaseUnityPlugin)this).Config.Bind<bool>("Menu & Logbook", "Limit FPS in Menu", true, "If true, limits FPS while in the Main Menu, Logbook, and Character Select lobby to prevent unnecessary GPU load.");
			_menuFpsLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Menu & Logbook", "Menu FPS Limit", 60, "Target FPS limit while in the Main Menu, Logbook, and lobby.");
			_limitOnPause = ((BaseUnityPlugin)this).Config.Bind<bool>("Pause", "Limit FPS on Pause", true, "If true, limits FPS while the pause menu (Esc) is open.");
			_pauseFpsLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Pause", "Pause FPS Limit", 60, "Target FPS limit while the game is paused.");
			_limitWhenUnfocused = ((BaseUnityPlugin)this).Config.Bind<bool>("Alt-Tab", "Limit FPS When Unfocused", true, "If true, limits FPS when the game window is unfocused or minimized (Alt-Tab).");
			_unfocusedFpsLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Alt-Tab", "Unfocused FPS Limit", 60, "Target FPS limit when the game window is unfocused or in the background.");
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		private void SetupRiskOfOptions()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Expected O, but got Unknown
			//IL_0068: 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)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			//IL_00a0: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Expected O, but got Unknown
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Expected O, but got Unknown
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Expected O, but got Unknown
			ModSettingsManager.SetModDescription("Better FPS limiter for Alt-Tab, Main Menu/Logbook, and Pause states, plus custom 144+ Hz framerate cap support.");
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(_enableCustomGameplayFps));
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(_gameplayFpsLimit, new IntSliderConfig
			{
				min = 30,
				max = 540
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(_forceDisableVSync));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(_limitInMenu));
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(_menuFpsLimit, new IntSliderConfig
			{
				min = 15,
				max = 240
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(_limitOnPause));
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(_pauseFpsLimit, new IntSliderConfig
			{
				min = 15,
				max = 240
			}));
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(_limitWhenUnfocused));
			ModSettingsManager.AddOption((BaseOption)new IntSliderOption(_unfocusedFpsLimit, new IntSliderConfig
			{
				min = 5,
				max = 144
			}));
		}

		private void Update()
		{
			int num = CalculateDesiredFps();
			if ((_forceDisableVSync.Value || num > 0) && QualitySettings.vSyncCount != 0)
			{
				QualitySettings.vSyncCount = 0;
			}
			if (Application.targetFrameRate != num)
			{
				Application.targetFrameRate = num;
			}
		}

		private int CalculateDesiredFps()
		{
			if (_limitWhenUnfocused.Value && !Application.isFocused)
			{
				return _unfocusedFpsLimit.Value;
			}
			if (_limitInMenu.Value && (Object)(object)Run.instance == (Object)null)
			{
				return _menuFpsLimit.Value;
			}
			if (_limitOnPause.Value && IsGamePaused())
			{
				return _pauseFpsLimit.Value;
			}
			if (_enableCustomGameplayFps.Value)
			{
				return _gameplayFpsLimit.Value;
			}
			return -1;
		}

		private static bool IsGamePaused()
		{
			if (PauseManager.isPaused)
			{
				return true;
			}
			if (PauseScreenController.instancesList != null && PauseScreenController.instancesList.Count > 0)
			{
				return true;
			}
			return false;
		}

		private void OnApplicationQuit()
		{
			Application.targetFrameRate = (_enableCustomGameplayFps.Value ? _gameplayFpsLimit.Value : (-1));
		}
	}
}