1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 23:47:24 +08:00

Merge pull request #1633 from peppy/fix-track-disposal

Fix DisposeTrack not working as expected
This commit is contained in:
Dean Herbert 2017-12-01 23:35:28 +09:00 committed by GitHub
commit 33572a7326
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 10 deletions

@ -1 +1 @@
Subproject commit 87d68cda0015d51dc3da56d2322fa10d399fc4ed Subproject commit cc013fc4063dda0843f38c1c73568a413abcf229

View File

@ -31,7 +31,7 @@ namespace osu.Game.Beatmaps
Mods.ValueChanged += mods => applyRateAdjustments(); Mods.ValueChanged += mods => applyRateAdjustments();
beatmap = new AsyncLazy<Beatmap>(populateBeatmap); 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); track = new AsyncLazy<Track>(populateTrack);
waveform = new AsyncLazy<Waveform>(populateWaveform); waveform = new AsyncLazy<Waveform>(populateWaveform);
} }
@ -99,10 +99,11 @@ namespace osu.Game.Beatmaps
if (WaveformLoaded) Waveform?.Dispose(); if (WaveformLoaded) Waveform?.Dispose();
} }
public void DisposeTrack() /// <summary>
{ /// Eagerly dispose of the audio track associated with this <see cref="WorkingBeatmap"/> (if any).
if (TrackLoaded) Track?.Dispose(); /// Accessing track again will load a fresh instance.
} /// </summary>
public void RecycleTrack() => track.Recycle();
private void applyRateAdjustments(Track t = null) private void applyRateAdjustments(Track t = null)
{ {
@ -114,11 +115,60 @@ namespace osu.Game.Beatmaps
mod.ApplyToClock(t); mod.ApplyToClock(t);
} }
public class AsyncLazy<T> : Lazy<Task<T>> public class AsyncLazy<T>
{ {
public AsyncLazy(Func<T> valueFactory) private Lazy<Task<T>> lazy;
: base(() => Task.Run(valueFactory)) 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;
init();
}
public void Recycle()
{
if (!IsValueCreated) return;
(lazy.Value.Result as IDisposable)?.Dispose();
init();
}
public bool IsValueCreated
{
get
{
ensureValid();
return lazy.IsValueCreated;
}
}
public Task<T> Value
{
get
{
ensureValid();
return lazy.Value;
}
}
private void ensureValid()
{
lock (initLock)
{
if (!lazy.IsValueCreated || (stillValidFunction?.Invoke(lazy.Value.Result) ?? true)) return;
init();
}
}
private void init()
{
lazy = new Lazy<Task<T>>(() => Task.Run(valueFactory));
} }
} }
} }

View File

@ -154,7 +154,7 @@ namespace osu.Game
Debug.Assert(lastBeatmap != null); Debug.Assert(lastBeatmap != null);
Debug.Assert(lastBeatmap.Track != null); Debug.Assert(lastBeatmap.Track != null);
lastBeatmap.DisposeTrack(); lastBeatmap.RecycleTrack();
} }
Audio.Track.AddItem(b.Track); Audio.Track.AddItem(b.Track);