1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 07:32:55 +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(); Metadata = beatmapInfo.Metadata ?? BeatmapSetInfo?.Metadata ?? new BeatmapMetadata();
Mods.ValueChanged += mods => applyRateAdjustments(); Mods.ValueChanged += mods => applyRateAdjustments();
}
private void applyRateAdjustments() beatmap = new Lazy<Beatmap>(populateBeatmap);
{ background = new Lazy<Texture>(populateBackground);
var t = track; track = new Lazy<Track>(populateTrack);
if (t == null) return;
t.ResetSpeedAdjustments();
foreach (var mod in Mods.Value.OfType<IApplicableToClock>())
mod.ApplyToClock(t);
} }
protected abstract Beatmap GetBeatmap(); protected abstract Beatmap GetBeatmap();
@ -45,98 +39,75 @@ namespace osu.Game.Beatmaps
protected abstract Track GetTrack(); protected abstract Track GetTrack();
protected virtual Waveform GetWaveform() => new Waveform(); protected virtual Waveform GetWaveform() => new Waveform();
private Beatmap beatmap; public bool BeatmapLoaded => beatmap.IsValueCreated;
private readonly object beatmapLock = new object(); public Beatmap Beatmap => beatmap.Value;
public Beatmap Beatmap private readonly Lazy<Beatmap> beatmap;
private Beatmap populateBeatmap()
{ {
get var b = GetBeatmap() ?? new Beatmap();
{
lock (beatmapLock)
{
if (beatmap != null) return beatmap;
beatmap = GetBeatmap() ?? new Beatmap(); // use the database-backed info.
b.BeatmapInfo = BeatmapInfo;
// use the database-backed info. return b;
beatmap.BeatmapInfo = BeatmapInfo;
return beatmap;
}
}
} }
private readonly object backgroundLock = new object(); public bool BackgroundLoaded => background.IsValueCreated;
private Texture background; public Texture Background => background.Value;
public Texture Background 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 // we want to ensure that we always have a track, even if it's a fake one.
{ var t = GetTrack() ?? new TrackVirtual();
lock (backgroundLock) applyRateAdjustments(t);
{ return t;
return background ?? (background = GetBackground());
}
}
} }
private Track track; public bool WaveformLoaded => waveform.IsValueCreated;
private readonly object trackLock = new object(); public Waveform Waveform => waveform.Value;
public Track Track private Lazy<Waveform> waveform;
{
get
{
lock (trackLock)
{
if (track != null) return track;
// we want to ensure that we always have a track, even if it's a fake one. private Waveform populateWaveform() => GetWaveform();
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;
public void TransferTo(WorkingBeatmap other) public void TransferTo(WorkingBeatmap other)
{ {
lock (trackLock) if (track.IsValueCreated && track.Value != null && BeatmapInfo.AudioEquals(other.BeatmapInfo))
{ other.track = track;
if (track != 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; other.background = background;
} }
public virtual void Dispose() public virtual void Dispose()
{ {
background?.Dispose(); if (BackgroundLoaded) Background.Dispose();
background = null; if (WaveformLoaded) Waveform.Dispose();
waveform?.Dispose();
} }
public void DisposeTrack() public void DisposeTrack()
{ {
lock (trackLock) if (!track.IsValueCreated) return;
{
track?.Dispose(); track.Value?.Dispose();
track = null; 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);
} }
} }
} }