From fcf4404430fe49ad618ad7cfea7d6735c086ff2a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Apr 2024 20:21:49 +0800 Subject: [PATCH] Use extension methods to clean up code --- .../Skins/TestSceneBeatmapSkinResources.cs | 4 +-- .../Visual/Audio/TestSceneAudioFilter.cs | 5 +-- .../Editing/TestSceneEditorBeatmapCreation.cs | 2 +- .../Visual/Editing/TimelineTestScene.cs | 2 +- osu.Game/Beatmaps/WorkingBeatmap.cs | 33 ++++++++++++++++++- osu.Game/Overlays/MusicController.cs | 2 +- .../Timelines/Summary/Parts/TimelinePart.cs | 2 +- osu.Game/Screens/Edit/EditorClock.cs | 2 +- .../Edit/Timing/WaveformComparisonDisplay.cs | 2 +- 9 files changed, 43 insertions(+), 11 deletions(-) diff --git a/osu.Game.Tests/Skins/TestSceneBeatmapSkinResources.cs b/osu.Game.Tests/Skins/TestSceneBeatmapSkinResources.cs index f004efbbff..78721f1525 100644 --- a/osu.Game.Tests/Skins/TestSceneBeatmapSkinResources.cs +++ b/osu.Game.Tests/Skins/TestSceneBeatmapSkinResources.cs @@ -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; }); } diff --git a/osu.Game.Tests/Visual/Audio/TestSceneAudioFilter.cs b/osu.Game.Tests/Visual/Audio/TestSceneAudioFilter.cs index d75919c7f0..2dc1cbe43c 100644 --- a/osu.Game.Tests/Visual/Audio/TestSceneAudioFilter.cs +++ b/osu.Game.Tests/Visual/Audio/TestSceneAudioFilter.cs @@ -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 { diff --git a/osu.Game.Tests/Visual/Editing/TestSceneEditorBeatmapCreation.cs b/osu.Game.Tests/Visual/Editing/TestSceneEditorBeatmapCreation.cs index a2c651e550..9841b103ce 100644 --- a/osu.Game.Tests/Visual/Editing/TestSceneEditorBeatmapCreation.cs +++ b/osu.Game.Tests/Visual/Editing/TestSceneEditorBeatmapCreation.cs @@ -99,7 +99,7 @@ namespace osu.Game.Tests.Visual.Editing AddUntilStep("wait for timeline load", () => Editor.ChildrenOfType().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().First(); diff --git a/osu.Game.Tests/Visual/Editing/TimelineTestScene.cs b/osu.Game.Tests/Visual/Editing/TimelineTestScene.cs index 01910530f1..7d85237b6a 100644 --- a/osu.Game.Tests/Visual/Editing/TimelineTestScene.cs +++ b/osu.Game.Tests/Visual/Editing/TimelineTestScene.cs @@ -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); } } diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index bb8ae6c7d2..bd3658a970 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -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 } } + /// + /// Temporary helper extension methods to aid in using with minimal code damage. + /// + 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; + } + } + + /// + /// Temporary redirect class for logging purposes. + /// internal class LoggingTrack : ITrack, IDisposable { public readonly Track UnderlyingTrack; diff --git a/osu.Game/Overlays/MusicController.cs b/osu.Game/Overlays/MusicController.cs index 01739d4341..499ae0c3de 100644 --- a/osu.Game/Overlays/MusicController.cs +++ b/osu.Game/Overlays/MusicController.cs @@ -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; } diff --git a/osu.Game/Screens/Edit/Components/Timelines/Summary/Parts/TimelinePart.cs b/osu.Game/Screens/Edit/Components/Timelines/Summary/Parts/TimelinePart.cs index 493070d9a9..347153ea72 100644 --- a/osu.Game/Screens/Edit/Components/Timelines/Summary/Parts/TimelinePart.cs +++ b/osu.Game/Screens/Edit/Components/Timelines/Summary/Parts/TimelinePart.cs @@ -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); diff --git a/osu.Game/Screens/Edit/EditorClock.cs b/osu.Game/Screens/Edit/EditorClock.cs index cd00259656..101d587678 100644 --- a/osu.Game/Screens/Edit/EditorClock.cs +++ b/osu.Game/Screens/Edit/EditorClock.cs @@ -27,7 +27,7 @@ namespace osu.Game.Screens.Edit private readonly Bindable track = new Bindable(); - 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; diff --git a/osu.Game/Screens/Edit/Timing/WaveformComparisonDisplay.cs b/osu.Game/Screens/Edit/Timing/WaveformComparisonDisplay.cs index 081dcc7520..d8f88cd5ac 100644 --- a/osu.Game/Screens/Edit/Timing/WaveformComparisonDisplay.cs +++ b/osu.Game/Screens/Edit/Timing/WaveformComparisonDisplay.cs @@ -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;