1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:17:26 +08:00

Remove RecyclableLazy

This commit is contained in:
Dean Herbert 2021-12-22 18:16:57 +09:00
parent f3f491374b
commit 7344c87b16
2 changed files with 13 additions and 82 deletions

View File

@ -38,6 +38,10 @@ namespace osu.Game.Beatmaps
private readonly object beatmapFetchLock = new object();
private readonly Lazy<Waveform> waveform;
private readonly Lazy<Storyboard> storyboard;
private readonly Lazy<ISkin> skin;
protected WorkingBeatmap(BeatmapInfo beatmapInfo, AudioManager audioManager)
{
this.audioManager = audioManager;
@ -45,12 +49,16 @@ namespace osu.Game.Beatmaps
BeatmapInfo = beatmapInfo;
BeatmapSetInfo = beatmapInfo.BeatmapSet;
background = new RecyclableLazy<Texture>(GetBackground, BackgroundStillValid);
waveform = new RecyclableLazy<Waveform>(GetWaveform);
storyboard = new RecyclableLazy<Storyboard>(GetStoryboard);
skin = new RecyclableLazy<ISkin>(GetSkin);
waveform = new Lazy<Waveform>(GetWaveform);
storyboard = new Lazy<Storyboard>(GetStoryboard);
skin = new Lazy<ISkin>(GetSkin);
}
public Waveform Waveform => waveform.Value;
public Storyboard Storyboard => storyboard.Value;
public Texture Background => GetBackground(); // Texture uses ref counting, so we want to return a new instance every usage.
public ISkin Skin => skin.Value;
#region Load checks
public virtual bool TrackLoaded => loadedTrack != null;
@ -97,15 +105,6 @@ namespace osu.Game.Beatmaps
#endregion
#region Background
public Texture Background => background.Value;
private readonly RecyclableLazy<Texture> background;
protected virtual bool BackgroundStillValid(Texture b) => b == null || b.Available;
#endregion
#region Track
private Track loadedTrack;
@ -179,28 +178,6 @@ namespace osu.Game.Beatmaps
#endregion
#region Waveform
public Waveform Waveform => waveform.Value;
private readonly RecyclableLazy<Waveform> waveform;
#endregion
#region Storyboard
public Storyboard Storyboard => storyboard.Value;
private readonly RecyclableLazy<Storyboard> storyboard;
#endregion
#region Skin
private readonly RecyclableLazy<ISkin> skin;
public ISkin Skin => skin.Value;
#endregion
#region Beatmap
public IBeatmap Beatmap
@ -356,53 +333,9 @@ namespace osu.Game.Beatmaps
public override string ToString() => BeatmapInfo.ToString();
IBeatmapInfo IWorkingBeatmap.BeatmapInfo => BeatmapInfo;
public abstract Stream GetStream(string storagePath);
public class RecyclableLazy<T>
{
private Lazy<T> lazy;
private readonly Func<T> valueFactory;
private readonly Func<T, bool> stillValidFunction;
private readonly object fetchLock = new object();
public RecyclableLazy(Func<T> valueFactory, Func<T, bool> stillValidFunction = null)
{
this.valueFactory = valueFactory;
this.stillValidFunction = stillValidFunction;
recreate();
}
public void Recycle()
{
if (!IsResultAvailable) return;
(lazy.Value as IDisposable)?.Dispose();
recreate();
}
public bool IsResultAvailable => stillValid;
public T Value
{
get
{
lock (fetchLock)
{
if (!stillValid)
recreate();
return lazy.Value;
}
}
}
private bool stillValid => lazy.IsValueCreated && (stillValidFunction?.Invoke(lazy.Value) ?? true);
private void recreate() => lazy = new Lazy<T>(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication);
}
IBeatmapInfo IWorkingBeatmap.BeatmapInfo => BeatmapInfo;
private class BeatmapLoadTimeoutException : TimeoutException
{

View File

@ -145,8 +145,6 @@ namespace osu.Game.Beatmaps
}
}
protected override bool BackgroundStillValid(Texture b) => false; // bypass lazy logic. we want to return a new background each time for refcounting purposes.
protected override Texture GetBackground()
{
if (string.IsNullOrEmpty(Metadata?.BackgroundFile))