diff --git a/osu.Desktop.VisualTests/Beatmaps/TestWorkingBeatmap.cs b/osu.Desktop.VisualTests/Beatmaps/TestWorkingBeatmap.cs index e6236b041f..0db9991cb8 100644 --- a/osu.Desktop.VisualTests/Beatmaps/TestWorkingBeatmap.cs +++ b/osu.Desktop.VisualTests/Beatmaps/TestWorkingBeatmap.cs @@ -1,32 +1,26 @@ -using System; +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; using osu.Framework.Audio.Track; using osu.Framework.Graphics.Textures; using osu.Game.Beatmaps; using osu.Game.Beatmaps.IO; -namespace osu.Desktop.VisualTests.Beatmaps -{ - public class TestWorkingBeatmap : WorkingBeatmap - { - public TestWorkingBeatmap(Beatmap beatmap) - : base(beatmap.BeatmapInfo, beatmap.BeatmapInfo.BeatmapSet) - { - this.beatmap = beatmap; +namespace osu.Desktop.VisualTests.Beatmaps +{ + public class TestWorkingBeatmap : WorkingBeatmap + { + public TestWorkingBeatmap(Beatmap beatmap) + : base(beatmap.BeatmapInfo, beatmap.BeatmapInfo.BeatmapSet) + { + this.beatmap = beatmap; } - - private Beatmap beatmap; - public override Beatmap Beatmap => beatmap; - public override Texture Background => null; - public override Track Track => null; - - public override void Dispose() - { - // This space intentionally left blank - } - - public override void TransferTo(WorkingBeatmap other) - { - // This space intentionally left blank - } - } -} + + private Beatmap beatmap; + + protected override Beatmap GetBeatmap() => beatmap; + protected override Texture GetBackground() => null; + protected override Track GetTrack() => null; + } +} diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 3be2a40bb0..2a2de1b6ef 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -36,14 +36,64 @@ namespace osu.Game.Beatmaps BeatmapSetInfo = beatmapSetInfo; WithStoryboard = withStoryboard; } + + protected abstract Beatmap GetBeatmap(); + protected abstract Texture GetBackground(); + protected abstract Track GetTrack(); - public abstract Beatmap Beatmap { get; } - public abstract Texture Background { get; } - public abstract Track Track { get; } + private Beatmap beatmap; + private object beatmapLock = new object(); + public Beatmap Beatmap + { + get + { + lock (beatmapLock) + { + return beatmap ?? (beatmap = GetBeatmap()); + } + } + } + + private object backgroundLock = new object(); + private Texture background; + public Texture Background + { + get + { + lock (backgroundLock) + { + return background ?? (background = GetBackground()); + } + } + } - public abstract void TransferTo(WorkingBeatmap other); - public abstract void Dispose(); + private Track track; + private object trackLock = new object(); + public Track Track + { + get + { + lock (trackLock) + { + return track ?? (track = GetTrack()); + } + } + } - public virtual bool TrackLoaded => Track != null; + public bool TrackLoaded => track != null; + + public void TransferTo(WorkingBeatmap other) + { + if (track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo)) + other.track = track; + } + + public virtual void Dispose() + { + track?.Dispose(); + track = null; + background?.Dispose(); + background = null; + } } } diff --git a/osu.Game/Database/DatabaseWorkingBeatmap.cs b/osu.Game/Database/DatabaseWorkingBeatmap.cs index 1584b9137b..56b4a81980 100644 --- a/osu.Game/Database/DatabaseWorkingBeatmap.cs +++ b/osu.Game/Database/DatabaseWorkingBeatmap.cs @@ -1,4 +1,7 @@ -using System.IO; +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.IO; using osu.Framework.Audio.Track; using osu.Framework.Graphics.Textures; using osu.Game.Beatmaps; @@ -20,107 +23,61 @@ namespace osu.Game.Database } private ArchiveReader GetReader() => database?.GetReader(BeatmapSetInfo); - - private Beatmap beatmap; - private object beatmapLock = new object(); - public override Beatmap Beatmap + + protected override Beatmap GetBeatmap() { - get + Beatmap beatmap; + try { - lock (beatmapLock) + using (var reader = GetReader()) { - if (beatmap != null) return beatmap; - - try + BeatmapDecoder decoder; + using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path))) { - using (var reader = GetReader()) - { - BeatmapDecoder decoder; - using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path))) - { - decoder = BeatmapDecoder.GetDecoder(stream); - beatmap = decoder?.Decode(stream); - } - - if (WithStoryboard && beatmap != null && BeatmapSetInfo.StoryboardFile != null) - using (var stream = new StreamReader(reader.GetStream(BeatmapSetInfo.StoryboardFile))) - decoder?.Decode(stream, beatmap); - } + decoder = BeatmapDecoder.GetDecoder(stream); + beatmap = decoder?.Decode(stream); } - catch { return null; } - - return beatmap; + + if (WithStoryboard && beatmap != null && BeatmapSetInfo.StoryboardFile != null) + using (var stream = new StreamReader(reader.GetStream(BeatmapSetInfo.StoryboardFile))) + decoder?.Decode(stream, beatmap); } } + catch { return null; } + return beatmap; } - - private object backgroundLock = new object(); - private Texture background; - public override Texture Background + + protected override Texture GetBackground() { - get + Texture background; + if (BeatmapInfo?.Metadata?.BackgroundFile == null) + return null; + try { - lock (backgroundLock) + using (var reader = GetReader()) { - if (background != null) return background; - - if (BeatmapInfo?.Metadata?.BackgroundFile == null) return null; - - try - { - using (var reader = GetReader()) - { - background = new TextureStore( - new RawTextureLoaderStore(reader), - false).Get(BeatmapInfo.Metadata.BackgroundFile); - } - } - catch { return null; } - - return background; + background = new TextureStore( + new RawTextureLoaderStore(reader), + false).Get(BeatmapInfo.Metadata.BackgroundFile); } } + catch { return null; } + return background; } private ArchiveReader trackReader; - private Track track; - private object trackLock = new object(); - public override Track Track + protected override Track GetTrack() { - get + Track track; + try { - lock (trackLock) - { - if (track != null) return track; - - try - { - //store a reference to the reader as we may continue accessing the stream in the background. - trackReader = GetReader(); - var trackData = trackReader?.GetStream(BeatmapInfo.Metadata.AudioFile); - if (trackData != null) - track = new TrackBass(trackData); - } - catch { return null; } - - return track; - } + //store a reference to the reader as we may continue accessing the stream in the background. + trackReader = GetReader(); + var trackData = trackReader?.GetStream(BeatmapInfo.Metadata.AudioFile); + track = trackData == null ? null : new TrackBass(trackData); } - } - - public override bool TrackLoaded => track != null; - - public override void TransferTo(WorkingBeatmap other) - { - var _other = (DatabaseWorkingBeatmap)other; - if (track != null && BeatmapInfo.AudioEquals(_other.BeatmapInfo)) - _other.track = track; - } - - public override void Dispose() - { - background?.Dispose(); - background = null; + catch { return null; } + return track; } } }