mirror of
https://github.com/ppy/osu.git
synced 2026-06-02 05:30:17 +08:00
Use extension methods to clean up code
This commit is contained in:
@@ -30,8 +30,8 @@ namespace osu.Game.Tests.Skins
|
||||
AddAssert("sample is non-null", () => beatmap.Skin.GetSample(new SampleInfo(@"sample")) != null);
|
||||
AddAssert("track is non-null", () =>
|
||||
{
|
||||
using (var track = ((LoggingTrack)beatmap.LoadTrack()))
|
||||
return track.UnderlyingTrack is not TrackVirtual;
|
||||
using (var track = beatmap.LoadTrack().GetUnderlyingTrack())
|
||||
return track is not TrackVirtual;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ using ManagedBass.Fx;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio;
|
||||
using osu.Framework.Audio.Track;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
@@ -27,7 +28,7 @@ namespace osu.Game.Tests.Visual.Audio
|
||||
private OsuSpriteText highPassText;
|
||||
private AudioFilter highPassFilter;
|
||||
|
||||
private LoggingTrack track;
|
||||
private Track track;
|
||||
|
||||
private WaveformTestBeatmap beatmap;
|
||||
|
||||
@@ -38,7 +39,7 @@ namespace osu.Game.Tests.Visual.Audio
|
||||
private void load(AudioManager audio)
|
||||
{
|
||||
beatmap = new WaveformTestBeatmap(audio);
|
||||
track = (LoggingTrack)beatmap.LoadTrack();
|
||||
track = beatmap.LoadTrack().GetUnderlyingTrack();
|
||||
|
||||
Add(new FillFlowContainer
|
||||
{
|
||||
|
||||
@@ -99,7 +99,7 @@ namespace osu.Game.Tests.Visual.Editing
|
||||
AddUntilStep("wait for timeline load", () => Editor.ChildrenOfType<Timeline>().FirstOrDefault()?.IsLoaded == true);
|
||||
|
||||
AddStep("enter setup mode", () => InputManager.Key(Key.F4));
|
||||
AddAssert("track is virtual", () => ((LoggingTrack)Beatmap.Value.Track).UnderlyingTrack is TrackVirtual);
|
||||
AddAssert("track is virtual", () => Beatmap.Value.Track.GetUnderlyingTrack() is TrackVirtual);
|
||||
AddAssert("switch track to real track", () =>
|
||||
{
|
||||
var setup = Editor.ChildrenOfType<SetupScreen>().First();
|
||||
|
||||
@@ -121,7 +121,7 @@ namespace osu.Game.Tests.Visual.Editing
|
||||
{
|
||||
base.Update();
|
||||
|
||||
if (((LoggingTrack)beatmap.Value.Track).IsLoaded)
|
||||
if (beatmap.Value.Track.IsLoaded())
|
||||
marker.X = (float)(editorClock.CurrentTime / beatmap.Value.Track.Length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
if (Track.RestartPoint == -1)
|
||||
{
|
||||
if (!((LoggingTrack)Track).IsLoaded)
|
||||
if (!Track.IsLoaded())
|
||||
{
|
||||
// force length to be populated (https://github.com/ppy/osu-framework/issues/4202)
|
||||
Track.Seek(Track.CurrentTime);
|
||||
@@ -356,6 +356,37 @@ namespace osu.Game.Beatmaps
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Temporary helper extension methods to aid in using <see cref="LoggingTrack"/> with minimal code damage.
|
||||
/// </summary>
|
||||
internal static class TrackExtensions
|
||||
{
|
||||
public static Track GetUnderlyingTrack(this ITrack track)
|
||||
{
|
||||
if (track is Track t)
|
||||
return t;
|
||||
|
||||
if (track is LoggingTrack lt)
|
||||
return lt.UnderlyingTrack;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool IsLoaded(this ITrack track)
|
||||
{
|
||||
if (track is Track t)
|
||||
return t.IsLoaded;
|
||||
|
||||
if (track is LoggingTrack lt)
|
||||
return lt.UnderlyingTrack.IsLoaded;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Temporary redirect class for logging purposes.
|
||||
/// </summary>
|
||||
internal class LoggingTrack : ITrack, IDisposable
|
||||
{
|
||||
public readonly Track UnderlyingTrack;
|
||||
|
||||
@@ -361,7 +361,7 @@ namespace osu.Game.Overlays
|
||||
{
|
||||
// Important to keep this in its own method to avoid inadvertently capturing unnecessary variables in the callback.
|
||||
// Can lead to leaks.
|
||||
var queuedTrack = new DrawableTrack(((LoggingTrack)current.LoadTrack()).UnderlyingTrack);
|
||||
var queuedTrack = new DrawableTrack(current.LoadTrack().GetUnderlyingTrack());
|
||||
queuedTrack.Completed += onTrackCompleted;
|
||||
return queuedTrack;
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace osu.Game.Screens.Edit.Components.Timelines.Summary.Parts
|
||||
private void updateRelativeChildSize()
|
||||
{
|
||||
// If the track is not loaded, assign a default sane length otherwise relative positioning becomes meaningless.
|
||||
var track = (LoggingTrack)beatmap.Value.Track;
|
||||
var track = beatmap.Value.Track.GetUnderlyingTrack();
|
||||
|
||||
double trackLength = track.IsLoaded ? track.Length : 60000;
|
||||
content.RelativeChildSize = new Vector2((float)Math.Max(1, trackLength), 1);
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace osu.Game.Screens.Edit
|
||||
|
||||
private readonly Bindable<ITrack> track = new Bindable<ITrack>();
|
||||
|
||||
public double TrackLength => (track.Value as LoggingTrack)?.IsLoaded == true ? track.Value.Length : 60000;
|
||||
public double TrackLength => track.Value?.IsLoaded() == true ? track.Value.Length : 60000;
|
||||
|
||||
public ControlPointInfo ControlPointInfo => Beatmap.ControlPointInfo;
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ namespace osu.Game.Screens.Edit.Timing
|
||||
private void regenerateDisplay(bool animated)
|
||||
{
|
||||
// Before a track is loaded, it won't have a valid length, which will break things.
|
||||
if (!((LoggingTrack)beatmap.Value.Track).IsLoaded)
|
||||
if (beatmap.Value.Track.IsLoaded())
|
||||
{
|
||||
Scheduler.AddOnce(regenerateDisplay, animated);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user