1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00

Merge pull request #19779 from peppy/no-gameplay-clock

Remove all remaining usage of `GameplayClock`
This commit is contained in:
Dan Balasescu 2022-08-18 18:52:57 +09:00 committed by GitHub
commit fa167b1d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 176 deletions

View File

@ -1,39 +1,31 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Timing;
using osu.Game.Screens.Play;
namespace osu.Game.Tests.NonVisual
{
[TestFixture]
public class GameplayClockTest
public class GameplayClockContainerTest
{
[TestCase(0)]
[TestCase(1)]
public void TestTrueGameplayRateWithZeroAdjustment(double underlyingClockRate)
{
var framedClock = new FramedClock(new ManualClock { Rate = underlyingClockRate });
var gameplayClock = new TestGameplayClock(framedClock);
gameplayClock.MutableNonGameplayAdjustments.Add(new BindableDouble());
var gameplayClock = new TestGameplayClockContainer(framedClock);
Assert.That(gameplayClock.TrueGameplayRate, Is.EqualTo(0));
}
private class TestGameplayClock : GameplayClock
private class TestGameplayClockContainer : GameplayClockContainer
{
public List<Bindable<double>> MutableNonGameplayAdjustments { get; } = new List<Bindable<double>>();
public override IEnumerable<double> NonGameplayAdjustments => new[] { 0.0 };
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value);
public TestGameplayClock(IFrameBasedClock underlyingClock)
public TestGameplayClockContainer(IFrameBasedClock underlyingClock)
: base(underlyingClock)
{
}

View File

@ -39,7 +39,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
// best way to check without exposing.
private Drawable hideTarget => hudOverlay.KeyCounter;

View File

@ -30,7 +30,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
[SetUpSteps]
public void SetUpSteps()

View File

@ -37,7 +37,7 @@ namespace osu.Game.Tests.Visual.Gameplay
private GameplayState gameplayState = TestGameplayState.Create(new OsuRuleset());
[Cached(typeof(IGameplayClock))]
private readonly IGameplayClock gameplayClock = new GameplayClock(new FramedClock());
private readonly IGameplayClock gameplayClock = new GameplayClockContainer(new FramedClock());
private IEnumerable<HUDOverlay> hudOverlays => CreatedDrawables.OfType<HUDOverlay>();

View File

@ -1,76 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play
{
/// <summary>
/// A clock which is used for gameplay elements that need to follow audio time 1:1.
/// Exposed via DI by <see cref="GameplayClockContainer"/>.
/// <remarks>
/// The main purpose of this clock is to stop components using it from accidentally processing the main
/// <see cref="IFrameBasedClock"/>, as this should only be done once to ensure accuracy.
/// </remarks>
/// </summary>
public class GameplayClock : IGameplayClock
{
internal readonly IFrameBasedClock UnderlyingClock;
public readonly BindableBool IsPaused = new BindableBool();
IBindable<bool> IGameplayClock.IsPaused => IsPaused;
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
public GameplayClock(IFrameBasedClock underlyingClock)
{
UnderlyingClock = underlyingClock;
}
public double? StartTime { get; internal set; }
public double CurrentTime => UnderlyingClock.CurrentTime;
public double Rate => UnderlyingClock.Rate;
public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
public bool IsRunning => UnderlyingClock.IsRunning;
public void ProcessFrame()
{
// intentionally not updating the underlying clock (handled externally).
}
public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime;
public double FramesPerSecond => UnderlyingClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo;
public IClock Source => UnderlyingClock;
}
}

View File

@ -3,18 +3,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Timing;
using osu.Framework.Utils;
namespace osu.Game.Screens.Play
{
/// <summary>
/// Encapsulates gameplay timing logic and provides a <see cref="GameplayClock"/> via DI for gameplay components to use.
/// Encapsulates gameplay timing logic and provides a <see cref="IGameplayClock"/> via DI for gameplay components to use.
/// </summary>
public class GameplayClockContainer : Container, IAdjustableClock, IGameplayClock
{
@ -23,15 +24,8 @@ namespace osu.Game.Screens.Play
/// </summary>
public IBindable<bool> IsPaused => isPaused;
private readonly BindableBool isPaused = new BindableBool(true);
/// <summary>
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
/// </summary>
protected readonly DecoupleableInterpolatingFramedClock AdjustableSource;
/// <summary>
/// The source clock.
/// The source clock. Should generally not be used for any timekeeping purposes.
/// </summary>
public IClock SourceClock { get; private set; }
@ -40,8 +34,6 @@ namespace osu.Game.Screens.Play
/// </summary>
public event Action? OnSeek;
private double? startTime;
/// <summary>
/// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>.
/// </summary>
@ -49,24 +41,21 @@ namespace osu.Game.Screens.Play
/// If not set, a value of zero will be used.
/// Importantly, the value will be inferred from the current ruleset in <see cref="MasterGameplayClockContainer"/> unless specified.
/// </remarks>
public double? StartTime
{
get => startTime;
set
{
startTime = value;
public double? StartTime { get; set; }
if (GameplayClock.IsNotNull())
GameplayClock.StartTime = value;
}
}
public IEnumerable<double> NonGameplayAdjustments => GameplayClock.NonGameplayAdjustments;
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
/// <summary>
/// The final clock which is exposed to gameplay components.
/// </summary>
protected GameplayClock GameplayClock { get; private set; } = null!;
protected IFrameBasedClock FramedClock { get; private set; }
private readonly BindableBool isPaused = new BindableBool(true);
/// <summary>
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
/// </summary>
private readonly DecoupleableInterpolatingFramedClock decoupledClock;
/// <summary>
/// Creates a new <see cref="GameplayClockContainer"/>.
@ -78,21 +67,21 @@ namespace osu.Game.Screens.Play
RelativeSizeAxes = Axes.Both;
AdjustableSource = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
decoupledClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
IsPaused.BindValueChanged(OnIsPausedChanged);
// this will be replaced during load, but non-null for tests which don't add this component to the hierarchy.
FramedClock = new FramedClock();
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
GameplayClock = CreateGameplayClock(AdjustableSource);
FramedClock = CreateGameplayClock(decoupledClock);
dependencies.CacheAs<IGameplayClock>(this);
GameplayClock.StartTime = StartTime;
GameplayClock.IsPaused.BindTo(isPaused);
return dependencies;
}
@ -103,13 +92,13 @@ namespace osu.Game.Screens.Play
{
ensureSourceClockSet();
if (!AdjustableSource.IsRunning)
if (!decoupledClock.IsRunning)
{
// Seeking the decoupled clock to its current time ensures that its source clock will be seeked to the same time
// This accounts for the clock source potentially taking time to enter a completely stopped state
Seek(GameplayClock.CurrentTime);
Seek(FramedClock.CurrentTime);
AdjustableSource.Start();
decoupledClock.Start();
}
isPaused.Value = false;
@ -123,10 +112,10 @@ namespace osu.Game.Screens.Play
{
Logger.Log($"{nameof(GameplayClockContainer)} seeking to {time}");
AdjustableSource.Seek(time);
decoupledClock.Seek(time);
// Manually process to make sure the gameplay clock is correctly updated after a seek.
GameplayClock.UnderlyingClock.ProcessFrame();
FramedClock.ProcessFrame();
OnSeek?.Invoke();
}
@ -143,7 +132,7 @@ namespace osu.Game.Screens.Play
public void Reset(bool startClock = false)
{
// Manually stop the source in order to not affect the IsPaused state.
AdjustableSource.Stop();
decoupledClock.Stop();
if (!IsPaused.Value || startClock)
Start();
@ -156,10 +145,10 @@ namespace osu.Game.Screens.Play
/// Changes the source clock.
/// </summary>
/// <param name="sourceClock">The new source.</param>
protected void ChangeSource(IClock sourceClock) => AdjustableSource.ChangeSource(SourceClock = sourceClock);
protected void ChangeSource(IClock sourceClock) => decoupledClock.ChangeSource(SourceClock = sourceClock);
/// <summary>
/// Ensures that the <see cref="AdjustableSource"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet.
/// Ensures that the <see cref="decoupledClock"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet.
/// This is usually done before a seek to avoid accidentally seeking only the adjustable source in decoupled mode,
/// but not the actual source clock.
/// That will pretty much only happen on the very first call of this method, as the source clock is passed in the constructor,
@ -167,39 +156,39 @@ namespace osu.Game.Screens.Play
/// </summary>
private void ensureSourceClockSet()
{
if (AdjustableSource.Source == null)
if (decoupledClock.Source == null)
ChangeSource(SourceClock);
}
protected override void Update()
{
if (!IsPaused.Value)
GameplayClock.UnderlyingClock.ProcessFrame();
FramedClock.ProcessFrame();
base.Update();
}
/// <summary>
/// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="AdjustableSource"/> clock.
/// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="decoupledClock"/> clock.
/// </summary>
/// <param name="isPaused">Whether the clock should now be paused.</param>
protected virtual void OnIsPausedChanged(ValueChangedEvent<bool> isPaused)
{
if (isPaused.NewValue)
AdjustableSource.Stop();
decoupledClock.Stop();
else
AdjustableSource.Start();
decoupledClock.Start();
}
/// <summary>
/// Creates the final <see cref="GameplayClock"/> which is exposed via DI to be used by gameplay components.
/// Creates the final <see cref="FramedClock"/> which is exposed via DI to be used by gameplay components.
/// </summary>
/// <remarks>
/// Any intermediate clocks such as platform offsets should be applied here.
/// </remarks>
/// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param>
/// <returns>The final <see cref="GameplayClock"/>.</returns>
protected virtual GameplayClock CreateGameplayClock(IFrameBasedClock source) => new GameplayClock(source);
/// <returns>The final <see cref="FramedClock"/>.</returns>
protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source;
#region IAdjustableClock
@ -215,15 +204,15 @@ namespace osu.Game.Screens.Play
double IAdjustableClock.Rate
{
get => GameplayClock.Rate;
get => FramedClock.Rate;
set => throw new NotSupportedException();
}
public double Rate => GameplayClock.Rate;
public double Rate => FramedClock.Rate;
public double CurrentTime => GameplayClock.CurrentTime;
public double CurrentTime => FramedClock.CurrentTime;
public bool IsRunning => GameplayClock.IsRunning;
public bool IsRunning => FramedClock.IsRunning;
#endregion
@ -232,12 +221,28 @@ namespace osu.Game.Screens.Play
// Handled via update. Don't process here to safeguard from external usages potentially processing frames additional times.
}
public double ElapsedFrameTime => GameplayClock.ElapsedFrameTime;
public double ElapsedFrameTime => FramedClock.ElapsedFrameTime;
public double FramesPerSecond => GameplayClock.FramesPerSecond;
public double FramesPerSecond => FramedClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo;
public FrameTimeInfo TimeInfo => FramedClock.TimeInfo;
public double TrueGameplayRate => GameplayClock.TrueGameplayRate;
public double TrueGameplayRate
{
get
{
double baseRate = Rate;
foreach (double adjustment in NonGameplayAdjustments)
{
if (Precision.AlmostEquals(adjustment, 0))
return 0;
baseRate /= adjustment;
}
return baseRate;
}
}
}
}

View File

@ -35,8 +35,6 @@ namespace osu.Game.Screens.Play
/// </summary>
public const double MINIMUM_SKIP_TIME = 1000;
protected Track Track => (Track)SourceClock;
public readonly BindableNumber<double> UserPlaybackRate = new BindableDouble(1)
{
Default = 1,
@ -54,7 +52,6 @@ namespace osu.Game.Screens.Play
private HardwareCorrectionOffsetClock userGlobalOffsetClock = null!;
private HardwareCorrectionOffsetClock userBeatmapOffsetClock = null!;
private HardwareCorrectionOffsetClock platformOffsetClock = null!;
private MasterGameplayClock masterGameplayClock = null!;
private Bindable<double> userAudioOffset = null!;
private IDisposable? beatmapOffsetSubscription;
@ -134,7 +131,7 @@ namespace osu.Game.Screens.Play
this.TransformBindableTo(pauseFreqAdjust, 0, 200, Easing.Out).OnComplete(_ =>
{
if (IsPaused.Value == isPaused.NewValue)
AdjustableSource.Stop();
base.OnIsPausedChanged(isPaused);
});
}
else
@ -143,14 +140,14 @@ namespace osu.Game.Screens.Play
else
{
if (isPaused.NewValue)
AdjustableSource.Stop();
base.OnIsPausedChanged(isPaused);
// If not yet loaded, we still want to ensure relevant state is correct, as it is used for offset calculations.
pauseFreqAdjust.Value = isPaused.NewValue ? 0 : 1;
// We must also process underlying gameplay clocks to update rate-adjusted offsets with the new frequency adjustment.
// Without doing this, an initial seek may be performed with the wrong offset.
GameplayClock.UnderlyingClock.ProcessFrame();
FramedClock.ProcessFrame();
}
}
@ -179,19 +176,19 @@ namespace osu.Game.Screens.Play
/// </summary>
public void Skip()
{
if (GameplayClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME)
if (FramedClock.CurrentTime > skipTargetTime - MINIMUM_SKIP_TIME)
return;
double skipTarget = skipTargetTime - MINIMUM_SKIP_TIME;
if (GameplayClock.CurrentTime < 0 && skipTarget > 6000)
if (FramedClock.CurrentTime < 0 && skipTarget > 6000)
// double skip exception for storyboards with very long intros
skipTarget = 0;
Seek(skipTarget);
}
protected override GameplayClock CreateGameplayClock(IFrameBasedClock source)
protected override IFrameBasedClock CreateGameplayClock(IFrameBasedClock source)
{
// Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited.
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
@ -199,9 +196,7 @@ namespace osu.Game.Screens.Play
// the final usable gameplay clock with user-set offsets applied.
userGlobalOffsetClock = new HardwareCorrectionOffsetClock(platformOffsetClock, pauseFreqAdjust);
userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
return masterGameplayClock = new MasterGameplayClock(userBeatmapOffsetClock);
return userBeatmapOffsetClock = new HardwareCorrectionOffsetClock(userGlobalOffsetClock, pauseFreqAdjust);
}
/// <summary>
@ -221,11 +216,14 @@ namespace osu.Game.Screens.Play
if (speedAdjustmentsApplied)
return;
Track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
if (SourceClock is not Track track)
return;
masterGameplayClock.MutableNonGameplayAdjustments.Add(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Add(UserPlaybackRate);
track.AddAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
track.AddAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
nonGameplayAdjustments.Add(pauseFreqAdjust);
nonGameplayAdjustments.Add(UserPlaybackRate);
speedAdjustmentsApplied = true;
}
@ -235,11 +233,14 @@ namespace osu.Game.Screens.Play
if (!speedAdjustmentsApplied)
return;
Track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
Track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
if (SourceClock is not Track track)
return;
masterGameplayClock.MutableNonGameplayAdjustments.Remove(pauseFreqAdjust);
masterGameplayClock.MutableNonGameplayAdjustments.Remove(UserPlaybackRate);
track.RemoveAdjustment(AdjustableProperty.Frequency, pauseFreqAdjust);
track.RemoveAdjustment(AdjustableProperty.Tempo, UserPlaybackRate);
nonGameplayAdjustments.Remove(pauseFreqAdjust);
nonGameplayAdjustments.Remove(UserPlaybackRate);
speedAdjustmentsApplied = false;
}
@ -252,7 +253,7 @@ namespace osu.Game.Screens.Play
}
ControlPointInfo IBeatSyncProvider.ControlPoints => beatmap.Beatmap.ControlPointInfo;
IClock IBeatSyncProvider.Clock => GameplayClock;
IClock IBeatSyncProvider.Clock => this;
ChannelAmplitudes IHasAmplitudes.CurrentAmplitudes => beatmap.TrackLoaded ? beatmap.Track.CurrentAmplitudes : ChannelAmplitudes.Empty;
private class HardwareCorrectionOffsetClock : FramedOffsetClock
@ -300,15 +301,8 @@ namespace osu.Game.Screens.Play
}
}
private class MasterGameplayClock : GameplayClock
{
public readonly List<Bindable<double>> MutableNonGameplayAdjustments = new List<Bindable<double>>();
public override IEnumerable<double> NonGameplayAdjustments => MutableNonGameplayAdjustments.Select(b => b.Value);
private readonly List<Bindable<double>> nonGameplayAdjustments = new List<Bindable<double>>();
public MasterGameplayClock(FramedOffsetClock underlyingClock)
: base(underlyingClock)
{
}
}
public override IEnumerable<double> NonGameplayAdjustments => nonGameplayAdjustments.Select(b => b.Value);
}
}