From 15ed3b4aac3c87ae277c1c53696f02b3c1b1097e Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 6 Dec 2017 09:49:12 +0900 Subject: [PATCH 1/3] Fix IsValueCreated method not cecking whether the async task was completed Caused potential stutters for components that relied on this check. --- osu.Game/Beatmaps/WorkingBeatmap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 8c96074352..a23d74f288 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -144,7 +144,7 @@ namespace osu.Game.Beatmaps get { ensureValid(); - return lazy.IsValueCreated; + return lazy.Value.IsCompleted; } } From 04ae64e9fd7bb412cb20cb3424d2325afd94ced0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 6 Dec 2017 16:20:49 +0900 Subject: [PATCH 2/3] Add missing IsCompleted check to ensureValid --- osu.Game/Beatmaps/WorkingBeatmap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index a23d74f288..6378bb7568 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -161,7 +161,7 @@ namespace osu.Game.Beatmaps { lock (initLock) { - if (!lazy.IsValueCreated || (stillValidFunction?.Invoke(lazy.Value.Result) ?? true)) return; + if (!lazy.IsValueCreated || !lazy.Value.IsCompleted || (stillValidFunction?.Invoke(lazy.Value.Result) ?? true)) return; init(); } } From 34596b3368591d2b9eba717a5139854dd1ce4ba1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 6 Dec 2017 21:47:48 +0900 Subject: [PATCH 3/3] Rename and comment for clarification --- osu.Game/Beatmaps/WorkingBeatmap.cs | 44 ++++++++++++++++------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 6378bb7568..bc826ea140 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -41,7 +41,7 @@ namespace osu.Game.Beatmaps protected abstract Track GetTrack(); protected virtual Waveform GetWaveform() => new Waveform(); - public bool BeatmapLoaded => beatmap.IsValueCreated; + public bool BeatmapLoaded => beatmap.IsResultAvailable; public Beatmap Beatmap => beatmap.Value.Result; public async Task GetBeatmapAsync() => await beatmap.Value; @@ -57,14 +57,14 @@ namespace osu.Game.Beatmaps return b; } - public bool BackgroundLoaded => background.IsValueCreated; + public bool BackgroundLoaded => background.IsResultAvailable; public Texture Background => background.Value.Result; public async Task GetBackgroundAsync() => await background.Value; private AsyncLazy background; private Texture populateBackground() => GetBackground(); - public bool TrackLoaded => track.IsValueCreated; + public bool TrackLoaded => track.IsResultAvailable; public Track Track => track.Value.Result; public async Task GetTrackAsync() => await track.Value; private AsyncLazy track; @@ -77,7 +77,7 @@ namespace osu.Game.Beatmaps return t; } - public bool WaveformLoaded => waveform.IsValueCreated; + public bool WaveformLoaded => waveform.IsResultAvailable; public Waveform Waveform => waveform.Value.Result; public async Task GetWaveformAsync() => await waveform.Value; private readonly AsyncLazy waveform; @@ -86,10 +86,10 @@ namespace osu.Game.Beatmaps public void TransferTo(WorkingBeatmap other) { - if (track.IsValueCreated && Track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo)) + if (track.IsResultAvailable && Track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo)) other.track = track; - if (background.IsValueCreated && Background != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo)) + if (background.IsResultAvailable && Background != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo)) other.background = background; } @@ -107,7 +107,7 @@ namespace osu.Game.Beatmaps private void applyRateAdjustments(Track t = null) { - if (t == null && track.IsValueCreated) t = Track; + if (t == null && track.IsResultAvailable) t = Track; if (t == null) return; t.ResetSpeedAdjustments(); @@ -128,22 +128,22 @@ namespace osu.Game.Beatmaps this.valueFactory = valueFactory; this.stillValidFunction = stillValidFunction; - init(); + recreate(); } public void Recycle() { - if (!IsValueCreated) return; + if (!IsResultAvailable) return; (lazy.Value.Result as IDisposable)?.Dispose(); - init(); + recreate(); } - public bool IsValueCreated + public bool IsResultAvailable { get { - ensureValid(); + recreateIfInvalid(); return lazy.Value.IsCompleted; } } @@ -152,24 +152,28 @@ namespace osu.Game.Beatmaps { get { - ensureValid(); + recreateIfInvalid(); return lazy.Value; } } - private void ensureValid() + private void recreateIfInvalid() { lock (initLock) { - if (!lazy.IsValueCreated || !lazy.Value.IsCompleted || (stillValidFunction?.Invoke(lazy.Value.Result) ?? true)) return; - init(); + if (!lazy.IsValueCreated || !lazy.Value.IsCompleted) + // we have not yet been initialised or haven't run the task. + return; + + if (stillValidFunction?.Invoke(lazy.Value.Result) ?? true) + // we are still in a valid state. + return; + + recreate(); } } - private void init() - { - lazy = new Lazy>(() => Task.Run(valueFactory)); - } + private void recreate() => lazy = new Lazy>(() => Task.Run(valueFactory)); } } }