1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 18:27:26 +08:00
osu-lazer/osu.Game/Beatmaps/WorkingBeatmap.cs

152 lines
4.8 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.IO;
using osu.Framework.Audio.Track;
2016-11-05 19:00:14 +08:00
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
using osu.Game.Database;
namespace osu.Game.Beatmaps
{
public class WorkingBeatmap : IDisposable
{
public readonly BeatmapInfo BeatmapInfo;
public readonly BeatmapSetInfo BeatmapSetInfo;
private readonly BeatmapDatabase database;
public readonly bool WithStoryboard;
2017-02-07 15:15:45 +08:00
private ArchiveReader getReader() => database?.GetReader(BeatmapSetInfo);
2016-11-05 19:00:14 +08:00
private Texture background;
private object backgroundLock = new object();
2016-11-05 19:00:14 +08:00
public Texture Background
{
get
{
lock (backgroundLock)
2016-11-05 19:00:14 +08:00
{
if (background != null) return background;
if (BeatmapInfo?.Metadata?.BackgroundFile == null) return null;
try
{
2017-02-07 15:15:45 +08:00
using (var reader = getReader())
background = new TextureStore(new RawTextureLoaderStore(reader), false).Get(BeatmapInfo.Metadata.BackgroundFile);
}
catch { }
2016-11-05 19:00:14 +08:00
return background;
}
2016-11-05 19:00:14 +08:00
}
set { lock (backgroundLock) background = value; }
2016-11-05 19:00:14 +08:00
}
private Beatmap beatmap;
2016-11-05 20:21:08 +08:00
private object beatmapLock = new object();
public Beatmap Beatmap
{
get
{
lock (beatmapLock)
{
if (beatmap != null) return beatmap;
try
{
2017-02-07 15:15:45 +08:00
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);
}
}
catch { }
return beatmap;
}
}
set { lock (beatmapLock) beatmap = value; }
}
private ArchiveReader trackReader;
private AudioTrack track;
private object trackLock = new object();
public AudioTrack Track
{
get
{
lock (trackLock)
{
if (track != null) return track;
try
{
//store a reference to the reader as we may continue accessing the stream in the background.
2017-02-07 15:15:45 +08:00
trackReader = getReader();
var trackData = trackReader?.GetStream(BeatmapInfo.Metadata.AudioFile);
if (trackData != null)
track = new AudioTrackBass(trackData);
}
catch { }
return track;
}
}
set { lock (trackLock) track = value; }
}
2016-11-22 19:47:28 +08:00
public bool TrackLoaded => track != null;
public WorkingBeatmap(Beatmap beatmap)
{
this.beatmap = beatmap;
}
public WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, BeatmapDatabase database, bool withStoryboard = false)
{
2017-02-09 21:18:08 +08:00
BeatmapInfo = beatmapInfo;
BeatmapSetInfo = beatmapSetInfo;
this.database = database;
this.WithStoryboard = withStoryboard;
}
private bool isDisposed;
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
track?.Dispose();
background?.Dispose();
isDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void TransferTo(WorkingBeatmap working)
{
2016-11-07 21:52:23 +08:00
if (track != null && BeatmapInfo.AudioEquals(working.BeatmapInfo))
working.track = track;
}
}
}