From d21eec9542a778d559b2ed679db1489092c8ad91 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 5 Jul 2024 11:55:27 +0900 Subject: [PATCH] Apply nullability to `MusicController` --- osu.Game/Overlays/MusicController.cs | 43 +++++++++++++++------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/osu.Game/Overlays/MusicController.cs b/osu.Game/Overlays/MusicController.cs index 0986c0513c..ef12d1eba2 100644 --- a/osu.Game/Overlays/MusicController.cs +++ b/osu.Game/Overlays/MusicController.cs @@ -1,12 +1,9 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -#nullable disable - using System; using System.Collections.Generic; using System.Linq; -using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Track; @@ -28,7 +25,7 @@ namespace osu.Game.Overlays public partial class MusicController : CompositeDrawable { [Resolved] - private BeatmapManager beatmaps { get; set; } + private BeatmapManager beatmaps { get; set; } = null!; /// /// Point in time after which the current track will be restarted on triggering a "previous track" action. @@ -49,25 +46,28 @@ namespace osu.Game.Overlays /// Fired when the global has changed. /// Includes direction information for display purposes. /// - public event Action TrackChanged; + public event Action? TrackChanged; [Resolved] - private IBindable beatmap { get; set; } + private IBindable beatmap { get; set; } = null!; [Resolved] - private IBindable> mods { get; set; } + private IBindable> mods { get; set; } = null!; - [NotNull] public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000)); [Resolved] - private RealmAccess realm { get; set; } + private RealmAccess realm { get; set; } = null!; protected override void LoadComplete() { base.LoadComplete(); - beatmap.BindValueChanged(b => changeBeatmap(b.NewValue), true); + beatmap.BindValueChanged(b => + { + if (b.NewValue != null) + changeBeatmap(b.NewValue); + }, true); mods.BindValueChanged(_ => ResetTrackAdjustments(), true); } @@ -76,6 +76,9 @@ namespace osu.Game.Overlays /// public void ReloadCurrentTrack() { + if (current == null) + return; + changeTrack(); TrackChanged?.Invoke(current, TrackChangeDirection.None); } @@ -90,7 +93,7 @@ namespace osu.Game.Overlays /// public bool TrackLoaded => CurrentTrack.TrackLoaded; - private ScheduledDelegate seekDelegate; + private ScheduledDelegate? seekDelegate; public void SeekTo(double position) { @@ -192,7 +195,7 @@ namespace osu.Game.Overlays /// Play the previous track or restart the current track if it's current time below . /// /// Invoked when the operation has been performed successfully. - public void PreviousTrack(Action onSuccess = null) => Schedule(() => + public void PreviousTrack(Action? onSuccess = null) => Schedule(() => { PreviousTrackResult res = prev(); if (res != PreviousTrackResult.None) @@ -218,7 +221,7 @@ namespace osu.Game.Overlays queuedDirection = TrackChangeDirection.Prev; - var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault() + var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault() ?? getBeatmapSets().LastOrDefault(); if (playableSet != null) @@ -236,7 +239,7 @@ namespace osu.Game.Overlays /// /// Invoked when the operation has been performed successfully. /// A of the operation. - public void NextTrack(Action onSuccess = null) => Schedule(() => + public void NextTrack(Action? onSuccess = null) => Schedule(() => { bool res = next(); if (res) @@ -250,7 +253,7 @@ namespace osu.Game.Overlays queuedDirection = TrackChangeDirection.Next; - var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current.BeatmapSetInfo)).ElementAtOrDefault(1) + var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo)).ElementAtOrDefault(1) ?? getBeatmapSets().FirstOrDefault(); var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault(); @@ -272,7 +275,7 @@ namespace osu.Game.Overlays Schedule(() => CurrentTrack.RestartAsync()); } - private WorkingBeatmap current; + private WorkingBeatmap? current; private TrackChangeDirection? queuedDirection; @@ -289,7 +292,7 @@ namespace osu.Game.Overlays TrackChangeDirection direction = TrackChangeDirection.None; - bool audioEquals = newWorking?.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true; + bool audioEquals = newWorking.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true; if (current != null) { @@ -304,7 +307,7 @@ namespace osu.Game.Overlays { // figure out the best direction based on order in playlist. int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count(); - int next = newWorking == null ? -1 : getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count(); + int next = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count(); direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next; } @@ -361,7 +364,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(current.LoadTrack()); + var queuedTrack = new DrawableTrack(current!.LoadTrack()); queuedTrack.Completed += onTrackCompleted; return queuedTrack; } @@ -390,7 +393,7 @@ namespace osu.Game.Overlays } } - private AudioAdjustments modTrackAdjustments; + private AudioAdjustments? modTrackAdjustments; /// /// Resets the adjustments currently applied on and applies the mod adjustments if is true.