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

148 lines
5.1 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 17:19:50 +08:00
using System;
using System.Diagnostics.CodeAnalysis;
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Framework.Logging;
using osu.Framework.Testing;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
2018-04-13 17:19:50 +08:00
using osu.Game.Skinning;
using osu.Game.Storyboards;
namespace osu.Game.Beatmaps
{
public partial class BeatmapManager
{
[ExcludeFromDynamicCompile]
2020-08-11 12:48:57 +08:00
private class BeatmapManagerWorkingBeatmap : WorkingBeatmap
2018-04-13 17:19:50 +08:00
{
[NotNull]
private readonly IBeatmapResourceProvider resources;
2018-04-13 17:19:50 +08:00
public BeatmapManagerWorkingBeatmap(BeatmapInfo beatmapInfo, [NotNull] IBeatmapResourceProvider resources)
: base(beatmapInfo, resources.AudioManager)
2018-04-13 17:19:50 +08:00
{
this.resources = resources;
2018-04-13 17:19:50 +08:00
}
2018-04-19 19:44:38 +08:00
protected override IBeatmap GetBeatmap()
2018-04-13 17:19:50 +08:00
{
if (BeatmapInfo.Path == null)
2020-09-04 12:13:53 +08:00
return new Beatmap { BeatmapInfo = BeatmapInfo };
2018-04-13 17:19:50 +08:00
try
{
using (var stream = new LineBufferedReader(resources.Files.GetStream(getPathForFile(BeatmapInfo.Path))))
2018-04-13 17:19:50 +08:00
return Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
}
2020-02-10 16:25:11 +08:00
catch (Exception e)
2018-04-13 17:19:50 +08:00
{
2020-02-10 16:25:11 +08:00
Logger.Error(e, "Beatmap failed to load");
2018-04-13 17:19:50 +08:00
return null;
}
}
private string getPathForFile(string filename) => BeatmapSetInfo.Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.FileInfo.StoragePath;
2018-04-13 17:19:50 +08:00
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 17:19:50 +08:00
protected override Texture GetBackground()
{
if (Metadata?.BackgroundFile == null)
return null;
try
{
return resources.LargeTextureStore.Get(getPathForFile(Metadata.BackgroundFile));
2018-04-13 17:19:50 +08:00
}
2020-02-10 16:25:11 +08:00
catch (Exception e)
2018-04-13 17:19:50 +08:00
{
2020-02-10 16:25:11 +08:00
Logger.Error(e, "Background failed to load");
2018-04-13 17:19:50 +08:00
return null;
}
}
2019-08-31 04:19:34 +08:00
2020-08-07 21:31:41 +08:00
protected override Track GetBeatmapTrack()
2018-04-13 17:19:50 +08:00
{
if (Metadata?.AudioFile == null)
return null;
2018-04-13 17:19:50 +08:00
try
{
return resources.Tracks.Get(getPathForFile(Metadata.AudioFile));
2018-04-13 17:19:50 +08:00
}
2020-02-10 16:25:11 +08:00
catch (Exception e)
2018-04-13 17:19:50 +08:00
{
2020-02-10 16:25:11 +08:00
Logger.Error(e, "Track failed to load");
return null;
2018-04-13 17:19:50 +08:00
}
}
protected override Waveform GetWaveform()
{
if (Metadata?.AudioFile == null)
return null;
try
{
var trackData = resources.Files.GetStream(getPathForFile(Metadata.AudioFile));
return trackData == null ? null : new Waveform(trackData);
}
2020-02-10 16:25:11 +08:00
catch (Exception e)
{
2020-02-10 16:25:11 +08:00
Logger.Error(e, "Waveform failed to load");
return null;
}
}
2018-04-13 17:19:50 +08:00
protected override Storyboard GetStoryboard()
{
Storyboard storyboard;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
try
{
using (var stream = new LineBufferedReader(resources.Files.GetStream(getPathForFile(BeatmapInfo.Path))))
2018-04-13 17:19:50 +08: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(resources.Files.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile))))
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
{
try
{
return new LegacyBeatmapSkin(BeatmapInfo, resources.Files, resources);
2018-04-13 17:19:50 +08:00
}
catch (Exception e)
{
Logger.Error(e, "Skin failed to load");
return null;
2018-04-13 17:19:50 +08:00
}
}
}
}
}