osu/osu.Game/Beatmaps/BeatmapManager_WorkingBeatm...

177 lines
6.0 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using System;
using System.Linq;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
2019-08-30 20:19:34 +00:00
using osu.Framework.Graphics.Video;
2018-04-13 09:19:50 +00:00
using osu.Framework.IO.Stores;
using osu.Framework.Logging;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
2018-04-13 09:19:50 +00:00
using osu.Game.Skinning;
using osu.Game.Storyboards;
namespace osu.Game.Beatmaps
{
public partial class BeatmapManager
{
protected class BeatmapManagerWorkingBeatmap : WorkingBeatmap
{
private readonly IResourceStore<byte[]> store;
public BeatmapManagerWorkingBeatmap(IResourceStore<byte[]> store, TextureStore textureStore, BeatmapInfo beatmapInfo, AudioManager audioManager)
: base(beatmapInfo, audioManager)
2018-04-13 09:19:50 +00:00
{
this.store = store;
this.textureStore = textureStore;
2018-04-13 09:19:50 +00:00
}
2018-04-19 11:44:38 +00:00
protected override IBeatmap GetBeatmap()
2018-04-13 09:19:50 +00:00
{
try
{
using (var stream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
2018-04-13 09:19:50 +00:00
return Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
}
2020-02-10 08:25:11 +00:00
catch (Exception e)
2018-04-13 09:19:50 +00:00
{
2020-02-10 08:25:11 +00:00
Logger.Error(e, "Beatmap failed to load");
2018-04-13 09:19:50 +00:00
return null;
}
}
2019-11-28 14:39:09 +00:00
private string getPathForFile(string filename) => BeatmapSetInfo.Files.FirstOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
2018-04-13 09:19:50 +00:00
private TextureStore textureStore;
private ITrackStore trackStore;
protected override bool BackgroundStillValid(Texture b) => false; // bypass lazy logic. we want to return a new background each time for refcounting purposes.
2018-04-13 09:19:50 +00:00
protected override Texture GetBackground()
{
if (Metadata?.BackgroundFile == null)
return null;
try
{
return textureStore.Get(getPathForFile(Metadata.BackgroundFile));
2018-04-13 09:19:50 +00:00
}
2020-02-10 08:25:11 +00:00
catch (Exception e)
2018-04-13 09:19:50 +00:00
{
2020-02-10 08:25:11 +00:00
Logger.Error(e, "Background failed to load");
2018-04-13 09:19:50 +00:00
return null;
}
}
2019-08-30 20:19:34 +00:00
protected override VideoSprite GetVideo()
{
if (Metadata?.VideoFile == null)
return null;
try
{
var stream = textureStore.GetStream(getPathForFile(Metadata.VideoFile));
return stream == null ? null : new VideoSprite(stream);
2019-08-30 20:19:34 +00:00
}
2020-02-10 08:25:11 +00:00
catch (Exception e)
2019-08-30 20:19:34 +00:00
{
2020-02-10 08:25:11 +00:00
Logger.Error(e, "Video failed to load");
2019-08-30 20:19:34 +00:00
return null;
}
}
2018-04-13 09:19:50 +00:00
protected override Track GetTrack()
{
try
{
2019-11-12 10:35:08 +00:00
return (trackStore ??= AudioManager.GetTrackStore(store)).Get(getPathForFile(Metadata.AudioFile));
2018-04-13 09:19:50 +00:00
}
2020-02-10 08:25:11 +00:00
catch (Exception e)
2018-04-13 09:19:50 +00:00
{
2020-02-10 08:25:11 +00:00
Logger.Error(e, "Track failed to load");
return null;
2018-04-13 09:19:50 +00:00
}
}
public override void RecycleTrack()
{
base.RecycleTrack();
trackStore?.Dispose();
trackStore = null;
}
public override void TransferTo(WorkingBeatmap other)
{
base.TransferTo(other);
if (other is BeatmapManagerWorkingBeatmap owb && textureStore != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo))
owb.textureStore = textureStore;
}
protected override Waveform GetWaveform()
{
try
{
var trackData = store.GetStream(getPathForFile(Metadata.AudioFile));
return trackData == null ? null : new Waveform(trackData);
}
2020-02-10 08:25:11 +00:00
catch (Exception e)
{
2020-02-10 08:25:11 +00:00
Logger.Error(e, "Waveform failed to load");
return null;
}
}
2018-04-13 09:19:50 +00:00
protected override Storyboard GetStoryboard()
{
Storyboard storyboard;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
try
{
using (var stream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
2018-04-13 09:19:50 +00:00
{
var decoder = Decoder.GetDecoder<Storyboard>(stream);
// todo: support loading from both set-wide storyboard *and* beatmap specific.
if (BeatmapSetInfo?.StoryboardFile == null)
storyboard = decoder.Decode(stream);
else
{
using (var secondaryStream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile))))
2018-04-13 09:19:50 +00:00
storyboard = decoder.Decode(stream, secondaryStream);
}
}
}
catch (Exception e)
{
Logger.Error(e, "Storyboard failed to load");
storyboard = new Storyboard();
}
storyboard.BeatmapInfo = BeatmapInfo;
return storyboard;
}
protected override ISkin GetSkin()
2018-04-13 09:19:50 +00:00
{
try
{
return new LegacyBeatmapSkin(BeatmapInfo, store, AudioManager);
2018-04-13 09:19:50 +00:00
}
catch (Exception e)
{
Logger.Error(e, "Skin failed to load");
return null;
2018-04-13 09:19:50 +00:00
}
}
}
}
}