Skip to content

Commit

Permalink
time initialize before the first update
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Aug 20, 2023
1 parent 0a9e375 commit 0faa91c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions UniTAS/Patcher/Implementations/VirtualEnvironment/TimeEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using HarmonyLib;
using UniTAS.Patcher.Interfaces.DependencyInjection;
using UniTAS.Patcher.Interfaces.Events.MonoBehaviourEvents.DontRunIfPaused;
using UniTAS.Patcher.Interfaces.Events.MonoBehaviourEvents.RunEvenPaused;
using UniTAS.Patcher.Interfaces.Events.SoftRestart;
using UniTAS.Patcher.Models.DependencyInjection;
using UniTAS.Patcher.Services;
Expand All @@ -15,7 +16,7 @@ namespace UniTAS.Patcher.Implementations.VirtualEnvironment;
[Singleton(RegisterPriority.TimeEnv)]
[ExcludeRegisterIfTesting]
public class TimeEnv : ITimeEnv, IOnPreUpdatesActual, IOnGameRestartResume, IOnStartActual, IOnLastUpdateActual,
IOnFixedUpdateActual
IOnFixedUpdateActual, IOnUpdateUnconditional
{
private readonly ConfigEntry<float> _defaultFps;

Expand All @@ -36,15 +37,23 @@ public TimeEnv(IConfig config, ITimeWrapper timeWrap, IPatchReverseInvoker patch

_timeWrap = timeWrap;

FrameTime = 0f;
TimeTolerance = _timeWrap.IntFPSOnly ? 1.0 / int.MaxValue : float.Epsilon;
}

private bool _initialTimeSet;

public void UpdateUnconditional()
{
if (_initialTimeSet) return;
_initialTimeSet = true;

// stupid but slightly fixes accuracy on game first start
var initialFt = 1.0 / _defaultFps.Value;
RealtimeSinceStartup += initialFt;
UnscaledTime += initialFt;
ScaledTime += initialFt;
ScaledTime += initialFt * Time.timeScale;
SecondsSinceStartUp += initialFt;

FrameTime = 0f;
TimeTolerance = _timeWrap.IntFPSOnly ? 1.0 / int.MaxValue : float.Epsilon;
}

public double TimeTolerance { get; }
Expand Down

0 comments on commit 0faa91c

Please sign in to comment.