using System.Diagnostics.CodeAnalysis;
using HarmonyLib;
namespace ADOFAIRenderer.Patch.Game;
///
/// Protects the camera render texture scrCamera.camRT when the custom frame rate effect is turned off while it is
/// already off.
///
/// Patched member: scrCamera.SetCustomFrameRate(bool enable, float frame) (level effect that updates the screen at a
/// lower frame rate). Enabling assigns a new RenderTexture to the camera quad material and refreshes it at frame fps.
/// Disabling calls Release() on the quad material's current mainTexture and assigns camRT back. When
/// the effect was never enabled, mainTexture is camRT itself, so the original releases the live camera
/// render texture.
///
/// Always active (independent of the render stage).
///
[HarmonyPatch(typeof(scrCamera))]
// ReSharper disable once InconsistentNaming
public class scrCameraPatch
{
///
/// Prefix of scrCamera.SetCustomFrameRate. For a disable request while enableCustomFPS is already false,
/// stores the arguments in enableCustomFPS / frameRate and skips the original (no texture is released).
/// Enable requests and disable requests while the effect is on run the original.
///
/// The camera.
/// True to turn the custom frame rate on, false to turn it off.
/// Custom frame rate in frames per second.
/// True to run the original method; false to skip it.
[HarmonyPrefix]
[HarmonyPatch("SetCustomFrameRate")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
private static bool FixCamRT(scrCamera __instance, bool enable, float frame)
{
// Disable request while the effect is off: the quad material's mainTexture is camRT, which the original would Release().
if (enable || __instance.enableCustomFPS != enable) return true;
__instance.enableCustomFPS = enable;
__instance.frameRate = frame;
return false;
}
}