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

128 lines
3.7 KiB
C#
Raw Normal View History

//Copyright (c) 2007-2016 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;
private ArchiveReader reader => 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;
try
{
background = new TextureStore(new RawTextureLoaderStore(reader)).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;
private object beatmapLock;
public Beatmap Beatmap
{
get
{
lock (beatmapLock)
{
if (beatmap != null) return beatmap;
try
{
using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path)))
beatmap = BeatmapDecoder.GetDecoder(stream)?.Decode(stream);
}
catch { }
return beatmap;
}
}
set { lock (beatmapLock) beatmap = value; }
}
private AudioTrack track;
private object trackLock = new object();
public AudioTrack Track
{
get
{
lock (trackLock)
{
if (track != null) return track;
try
{
var trackData = reader.GetStream(BeatmapInfo.Metadata.AudioFile);
if (trackData != null)
track = new AudioTrackBass(trackData);
}
catch { }
return track;
}
}
set { lock (trackLock) track = value; }
}
public WorkingBeatmap(Beatmap beatmap)
{
this.beatmap = beatmap;
}
public WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, BeatmapDatabase database)
{
2016-10-28 20:08:32 +08:00
this.BeatmapInfo = beatmapInfo;
this.BeatmapSetInfo = beatmapSetInfo;
this.database = database;
}
private bool isDisposed;
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
track?.Dispose();
reader?.Dispose();
isDisposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
public void TransferTo(WorkingBeatmap working)
{
2016-10-28 20:08:32 +08:00
if (track != null && working.BeatmapInfo.Metadata.AudioFile == BeatmapInfo.Metadata.AudioFile && working.BeatmapInfo.BeatmapSet.Path == BeatmapInfo.BeatmapSet.Path)
working.track = track;
}
}
}