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

Use lazy for WorkingBeatmap component loading

This commit is contained in:
Dean Herbert 2017-11-17 18:35:54 +09:00
parent c1fd2a3950
commit d75e3d8e81

View File

@ -28,16 +28,10 @@ namespace osu.Game.Beatmaps
Metadata = beatmapInfo.Metadata ?? BeatmapSetInfo?.Metadata ?? new BeatmapMetadata();
Mods.ValueChanged += mods => applyRateAdjustments();
}
private void applyRateAdjustments()
{
var t = track;
if (t == null) return;
t.ResetSpeedAdjustments();
foreach (var mod in Mods.Value.OfType<IApplicableToClock>())
mod.ApplyToClock(t);
beatmap = new Lazy<Beatmap>(populateBeatmap);
background = new Lazy<Texture>(populateBackground);
track = new Lazy<Track>(populateTrack);
}
protected abstract Beatmap GetBeatmap();
@ -45,98 +39,75 @@ namespace osu.Game.Beatmaps
protected abstract Track GetTrack();
protected virtual Waveform GetWaveform() => new Waveform();
private Beatmap beatmap;
private readonly object beatmapLock = new object();
public Beatmap Beatmap
public bool BeatmapLoaded => beatmap.IsValueCreated;
public Beatmap Beatmap => beatmap.Value;
private readonly Lazy<Beatmap> beatmap;
private Beatmap populateBeatmap()
{
get
{
lock (beatmapLock)
{
if (beatmap != null) return beatmap;
var b = GetBeatmap() ?? new Beatmap();
beatmap = GetBeatmap() ?? new Beatmap();
// use the database-backed info.
b.BeatmapInfo = BeatmapInfo;
// use the database-backed info.
beatmap.BeatmapInfo = BeatmapInfo;
return beatmap;
}
}
return b;
}
private readonly object backgroundLock = new object();
private Texture background;
public Texture Background
public bool BackgroundLoaded => background.IsValueCreated;
public Texture Background => background.Value;
private Lazy<Texture> background;
private Texture populateBackground() => GetBackground();
public bool TrackLoaded => track.IsValueCreated;
public Track Track => track.Value;
private Lazy<Track> track;
private Track populateTrack()
{
get
{
lock (backgroundLock)
{
return background ?? (background = GetBackground());
}
}
// we want to ensure that we always have a track, even if it's a fake one.
var t = GetTrack() ?? new TrackVirtual();
applyRateAdjustments(t);
return t;
}
private Track track;
private readonly object trackLock = new object();
public Track Track
{
get
{
lock (trackLock)
{
if (track != null) return track;
public bool WaveformLoaded => waveform.IsValueCreated;
public Waveform Waveform => waveform.Value;
private Lazy<Waveform> waveform;
// we want to ensure that we always have a track, even if it's a fake one.
track = GetTrack() ?? new TrackVirtual();
applyRateAdjustments();
return track;
}
}
}
private Waveform waveform;
private readonly object waveformLock = new object();
public Waveform Waveform
{
get
{
lock (waveformLock)
return waveform ?? (waveform = GetWaveform());
}
}
public bool TrackLoaded => track != null;
private Waveform populateWaveform() => GetWaveform();
public void TransferTo(WorkingBeatmap other)
{
lock (trackLock)
{
if (track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo))
other.track = track;
}
if (track.IsValueCreated && track.Value != null && BeatmapInfo.AudioEquals(other.BeatmapInfo))
other.track = track;
if (background != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo))
if (background.IsValueCreated && background.Value != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo))
other.background = background;
}
public virtual void Dispose()
{
background?.Dispose();
background = null;
waveform?.Dispose();
if (BackgroundLoaded) Background.Dispose();
if (WaveformLoaded) Waveform.Dispose();
}
public void DisposeTrack()
{
lock (trackLock)
{
track?.Dispose();
track = null;
}
if (!track.IsValueCreated) return;
track.Value?.Dispose();
track = null;
}
private void applyRateAdjustments(Track t = null)
{
if (t == null && track.IsValueCreated) t = track.Value;
if (t == null) return;
t.ResetSpeedAdjustments();
foreach (var mod in Mods.Value.OfType<IApplicableToClock>())
mod.ApplyToClock(t);
}
}
}