1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 13:37:51 +08:00
osu-lazer/osu.Game/Beatmaps/BeatmapManager_WorkingBeatmap.cs

126 lines
4.4 KiB
C#
Raw Normal View History

2018-02-09 19:35:54 +08:00
// Copyright (c) 2007-2018 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 System.Linq;
2018-03-15 23:14:29 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
2018-03-09 20:23:03 +08:00
using osu.Framework.Logging;
using osu.Game.Beatmaps.Formats;
using osu.Game.Graphics.Textures;
2018-03-14 19:45:04 +08: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;
2018-03-15 23:14:29 +08:00
private readonly AudioManager audioManager;
2018-03-15 23:14:29 +08:00
public BeatmapManagerWorkingBeatmap(IResourceStore<byte[]> store, BeatmapInfo beatmapInfo, AudioManager audioManager)
: base(beatmapInfo)
{
this.store = store;
2018-03-15 23:14:29 +08:00
this.audioManager = audioManager;
}
protected override Beatmap GetBeatmap()
{
try
{
using (var stream = new StreamReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
2018-03-09 20:23:03 +08:00
return Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
}
catch
{
return null;
}
}
private string getPathForFile(string filename) => BeatmapSetInfo.Files.First(f => string.Equals(f.Filename, filename, StringComparison.InvariantCultureIgnoreCase)).FileInfo.StoragePath;
protected override Texture GetBackground()
{
if (Metadata?.BackgroundFile == null)
return null;
try
{
return new LargeTextureStore(new RawTextureLoaderStore(store)).Get(getPathForFile(Metadata.BackgroundFile));
}
catch
{
return null;
}
}
protected override Track GetTrack()
{
try
{
var trackData = store.GetStream(getPathForFile(Metadata.AudioFile));
return trackData == null ? null : new TrackBass(trackData);
}
catch
{
return new TrackVirtual();
}
}
protected override Waveform GetWaveform() => new Waveform(store.GetStream(getPathForFile(Metadata.AudioFile)));
protected override Storyboard GetStoryboard()
{
Storyboard storyboard;
try
{
2018-03-09 20:23:03 +08:00
using (var stream = new StreamReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
{
2018-03-09 20:23:03 +08:00
var decoder = Decoder.GetDecoder<Storyboard>(stream);
2018-02-19 13:00:48 +08:00
// todo: support loading from both set-wide storyboard *and* beatmap specific.
if (BeatmapSetInfo?.StoryboardFile == null)
2018-03-09 20:23:03 +08:00
storyboard = decoder.Decode(stream);
else
{
2018-03-09 20:23:03 +08:00
using (var secondaryStream = new StreamReader(store.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile))))
storyboard = decoder.Decode(stream, secondaryStream);
}
}
}
2018-03-09 20:23:03 +08:00
catch (Exception e)
{
2018-03-09 20:23:03 +08:00
Logger.Error(e, "Storyboard failed to load");
storyboard = new Storyboard();
}
storyboard.BeatmapInfo = BeatmapInfo;
return storyboard;
}
2018-03-14 19:45:04 +08:00
protected override Skin GetSkin()
{
Skin skin;
try
{
2018-03-15 23:14:29 +08:00
skin = new BeatmapSkin(BeatmapInfo, store, audioManager);
2018-03-14 19:45:04 +08:00
}
catch (Exception e)
{
Logger.Error(e, "Skin failed to load");
skin = new DefaultSkin();
}
return skin;
}
}
}
}