Merge branch 'master' into fix-beatmap-carousel-lag

This commit is contained in:
Dean Herbert
2017-12-07 15:30:50 +09:00
committed by GitHub
45 changed files with 1101 additions and 136 deletions

View File

@ -32,7 +32,7 @@ namespace osu.Game.Beatmaps
Mods.ValueChanged += mods => applyRateAdjustments();
beatmap = new AsyncLazy<Beatmap>(populateBeatmap);
background = new AsyncLazy<Texture>(populateBackground);
background = new AsyncLazy<Texture>(populateBackground, b => b == null || !b.IsDisposed);
track = new AsyncLazy<Track>(populateTrack);
waveform = new AsyncLazy<Waveform>(populateWaveform);
storyboard = new AsyncLazy<Storyboard>(populateStoryboard);
@ -44,7 +44,7 @@ namespace osu.Game.Beatmaps
protected virtual Waveform GetWaveform() => new Waveform();
protected virtual Storyboard GetStoryboard() => new Storyboard();
public bool BeatmapLoaded => beatmap.IsValueCreated;
public bool BeatmapLoaded => beatmap.IsResultAvailable;
public Beatmap Beatmap => beatmap.Value.Result;
public async Task<Beatmap> GetBeatmapAsync() => await beatmap.Value;
@ -60,14 +60,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<Texture> GetBackgroundAsync() => await background.Value;
private AsyncLazy<Texture> background;
private Texture populateBackground() => GetBackground();
public bool TrackLoaded => track.IsValueCreated;
public bool TrackLoaded => track.IsResultAvailable;
public Track Track => track.Value.Result;
public async Task<Track> GetTrackAsync() => await track.Value;
private AsyncLazy<Track> track;
@ -80,7 +80,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<Waveform> GetWaveformAsync() => await waveform.Value;
private readonly AsyncLazy<Waveform> waveform;
@ -96,10 +96,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;
}
@ -110,14 +110,15 @@ namespace osu.Game.Beatmaps
if (StoryboardLoaded) Storyboard?.Dispose();
}
public void DisposeTrack()
{
if (TrackLoaded) Track?.Dispose();
}
/// <summary>
/// Eagerly dispose of the audio track associated with this <see cref="WorkingBeatmap"/> (if any).
/// Accessing track again will load a fresh instance.
/// </summary>
public void RecycleTrack() => track.Recycle();
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();
@ -125,12 +126,65 @@ namespace osu.Game.Beatmaps
mod.ApplyToClock(t);
}
public class AsyncLazy<T> : Lazy<Task<T>>
public class AsyncLazy<T>
{
public AsyncLazy(Func<T> valueFactory)
: base(() => Task.Run(valueFactory))
private Lazy<Task<T>> lazy;
private readonly Func<T> valueFactory;
private readonly Func<T, bool> stillValidFunction;
private readonly object initLock = new object();
public AsyncLazy(Func<T> valueFactory, Func<T, bool> stillValidFunction = null)
{
this.valueFactory = valueFactory;
this.stillValidFunction = stillValidFunction;
recreate();
}
public void Recycle()
{
if (!IsResultAvailable) return;
(lazy.Value.Result as IDisposable)?.Dispose();
recreate();
}
public bool IsResultAvailable
{
get
{
recreateIfInvalid();
return lazy.Value.IsCompleted;
}
}
public Task<T> Value
{
get
{
recreateIfInvalid();
return lazy.Value;
}
}
private void recreateIfInvalid()
{
lock (initLock)
{
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 recreate() => lazy = new Lazy<Task<T>>(() => Task.Run(valueFactory));
}
}
}