sinmai-mods/UnlockFrameRate/FrameRatePlugin.cs

113 lines
3.6 KiB
C#
Raw Normal View History

2024-06-01 12:30:03 +00:00
#pragma warning disable IDE0051
// ReSharper disable UnusedMember.Local
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
namespace UnlockFrameRate;
[BepInPlugin("io.github.beerpsi.sinmai.framerate", "FrameRate", "0.1.0")]
[BepInProcess("Sinmai.exe")]
public class FrameRatePlugin : BaseUnityPlugin
{
public new static ManualLogSource Logger = BepInEx.Logging.Logger.CreateLogSource("FrameRate");
public static FrameRatePlugin Instance { get; private set; }
public int TargetFrameRate => _configFrameRate.Value;
public bool PatchJudgementTiming => _configChangeJudgementTiming.Value;
2024-06-01 12:30:03 +00:00
private ConfigEntry<int> _configFrameRate;
private ConfigEntry<int> _configVSyncCount;
private ConfigEntry<bool> _configDisplayFps;
private ConfigEntry<bool> _configChangeJudgementTiming;
2024-06-01 12:30:03 +00:00
private Harmony _harmony;
private void Awake()
{
Instance = this;
2024-06-01 12:30:03 +00:00
_configFrameRate = Config.Bind(
"Config",
"FrameRate",
60,
"The frame rate to run the game at");
_configVSyncCount = Config.Bind(
"Config",
"VSyncCount",
0,
"Supported values are 0 to 4. 0 disables VSync and the rest enables it.\nWhen this is enabled, FrameRate is ignored and the target frame rate\nis calculated by taking the current refresh rate divided by VSyncCount\n(e.g. 120Hz at VSyncCount 2 => 60fps).");
_configDisplayFps = Config.Bind(
"Config",
"DisplayFPS",
false,
"Show an FPS counter");
_configChangeJudgementTiming = Config.Bind(
"Config",
"ChangeJudgementTiming",
true,
"Adjusts JUDGEMENT TIMING options to match the new frame rate.\nIf this is enabled, 0.1 in-game offset is equivalent to 10000 / FrameRate.");
2024-06-01 12:30:03 +00:00
if (_configVSyncCount.Value is > 0 and <= 4)
{
QualitySettings.vSyncCount = _configVSyncCount.Value;
Logger.LogInfo(
"VSync is enabled (VSyncCount={0}), target frame rate is {1}fps",
_configVSyncCount.Value,
TargetFrameRate);
}
else
{
Application.targetFrameRate = TargetFrameRate;
QualitySettings.vSyncCount = 0;
Logger.LogInfo(
"VSync is disabled, target frame rate is {0}fps",
TargetFrameRate);
}
Time.fixedDeltaTime = 1f / TargetFrameRate * Time.timeScale;
Logger.LogDebug(
"Setting Time.fixedDeltaTime to {0}s",
Time.fixedDeltaTime);
Logger.LogDebug("Patching hardcoded frame time usages");
_harmony = new Harmony("io.github.beerpsi.sinmai.framerate");
_harmony.PatchAll();
}
private const int FpsSamples = 100;
private int _currentFps = 0;
private int _currentSampleCount = FpsSamples;
private float _totalTime = 0;
private void OnGUI()
{
if (!_configDisplayFps.Value)
{
return;
}
_totalTime += Time.deltaTime;
_currentSampleCount--;
if (_currentSampleCount == 0)
{
_currentFps = (int)(FpsSamples / _totalTime);
_totalTime = 0f;
_currentSampleCount = FpsSamples;
}
GUI.Label(new Rect(10f, 10f, 150f, 100f), "FPS: " + _currentFps);
}
private void OnDestroy()
{
_harmony?.UnpatchSelf();
}
}