using ADOFAIRenderer.Core;
using HarmonyLib;
using UnityEngine;
namespace ADOFAIRenderer.Patch.Game;
///
/// Makes the end portal particle systems run on scaled game time during a render.
///
/// Patched member: scrPortalParticles.Start() (the animated portal spawned by scrFloor.SpawnPortalParticles;
/// Start pre-simulates its particle system for 1 second and plays it).
///
///
/// Active from stage Load. RenderManager sets Time.captureDeltaTime to 1 / RenderFPS, which advances scaled time by a
/// fixed step per frame; unscaled time is not affected by captureDeltaTime and follows real time. A particle system with
/// main.useUnscaledTime would therefore play at the real (slower or faster) frame rate of the offline render, so the
/// postfix sets it to false on every particle system under the portal (including inactive children).
/// Reference: https://docs.unity3d.com/ScriptReference/Time-captureDeltaTime.html
///
///
[HarmonyPatch(typeof(scrPortalParticles))]
// ReSharper disable once InconsistentNaming
public static class scrPortalParticlesPatch
{
/// Postfix of scrPortalParticles.Start: sets useUnscaledTime = false on all child particle systems.
/// The portal that started.
[HarmonyPostfix]
[HarmonyPatch("Start")]
// ReSharper disable once InconsistentNaming
public static void ForceScaledTime(scrPortalParticles __instance)
{
if (!RenderManager.Instance || RenderManager.Instance.stage < RenderStage.Load) return;
if (__instance == null) return;
var particleSystems = __instance.GetComponentsInChildren(true);
foreach (var ps in particleSystems)
{
if (ps == null) continue;
var main = ps.main;
main.useUnscaledTime = false;
}
}
}