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