2019-01-24 17:43:03 +09:00
// 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 18:19:50 +09:00
using System ;
2020-12-22 12:06:10 +09:00
using System.Diagnostics.CodeAnalysis ;
2021-04-17 17:47:13 +02:00
using System.IO ;
2018-04-13 18:19:50 +09:00
using osu.Framework.Audio.Track ;
using osu.Framework.Graphics.Textures ;
using osu.Framework.Logging ;
2020-10-16 14:39:02 +09:00
using osu.Framework.Testing ;
2018-04-13 18:19:50 +09:00
using osu.Game.Beatmaps.Formats ;
2019-09-10 00:43:30 +02:00
using osu.Game.IO ;
2018-04-13 18:19:50 +09:00
using osu.Game.Skinning ;
using osu.Game.Storyboards ;
namespace osu.Game.Beatmaps
{
public partial class BeatmapManager
{
2020-10-16 14:39:02 +09:00
[ExcludeFromDynamicCompile]
2020-08-11 13:48:57 +09:00
private class BeatmapManagerWorkingBeatmap : WorkingBeatmap
2018-04-13 18:19:50 +09:00
{
2020-12-22 12:06:10 +09:00
[NotNull]
2020-12-21 14:06:50 +09:00
private readonly IBeatmapResourceProvider resources ;
2018-04-13 18:19:50 +09:00
2020-12-22 12:06:10 +09:00
public BeatmapManagerWorkingBeatmap ( BeatmapInfo beatmapInfo , [ NotNull ] IBeatmapResourceProvider resources )
: base ( beatmapInfo , resources . AudioManager )
2018-04-13 18:19:50 +09:00
{
2020-12-21 14:06:50 +09:00
this . resources = resources ;
2018-04-13 18:19:50 +09:00
}
2018-04-19 20:44:38 +09:00
protected override IBeatmap GetBeatmap ( )
2018-04-13 18:19:50 +09:00
{
2020-08-24 19:38:05 +09:00
if ( BeatmapInfo . Path = = null )
2020-09-04 13:13:53 +09:00
return new Beatmap { BeatmapInfo = BeatmapInfo } ;
2020-08-24 19:38:05 +09:00
2018-04-13 18:19:50 +09:00
try
{
2021-04-17 17:49:10 +02:00
using ( var stream = new LineBufferedReader ( GetStream ( BeatmapSetInfo . GetPathForFile ( BeatmapInfo . Path ) ) ) )
2018-04-13 18:19:50 +09:00
return Decoder . GetDecoder < Beatmap > ( stream ) . Decode ( stream ) ;
}
2020-02-10 17:25:11 +09:00
catch ( Exception e )
2018-04-13 18:19:50 +09:00
{
2020-02-10 17:25:11 +09:00
Logger . Error ( e , "Beatmap failed to load" ) ;
2018-04-13 18:19:50 +09:00
return null ;
}
}
2018-09-06 13:15:43 +09: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 18:19:50 +09:00
protected override Texture GetBackground ( )
{
if ( Metadata ? . BackgroundFile = = null )
return null ;
try
{
2021-04-17 17:49:10 +02:00
return resources . LargeTextureStore . Get ( BeatmapSetInfo . GetPathForFile ( Metadata . BackgroundFile ) ) ;
2018-04-13 18:19:50 +09:00
}
2020-02-10 17:25:11 +09:00
catch ( Exception e )
2018-04-13 18:19:50 +09:00
{
2020-02-10 17:25:11 +09:00
Logger . Error ( e , "Background failed to load" ) ;
2018-04-13 18:19:50 +09:00
return null ;
}
}
2019-08-30 23:19:34 +03:00
2020-08-07 22:31:41 +09:00
protected override Track GetBeatmapTrack ( )
2018-04-13 18:19:50 +09:00
{
2020-09-01 15:48:13 +09:00
if ( Metadata ? . AudioFile = = null )
return null ;
2018-04-13 18:19:50 +09:00
try
{
2021-04-17 17:49:10 +02:00
return resources . Tracks . Get ( BeatmapSetInfo . GetPathForFile ( Metadata . AudioFile ) ) ;
2018-04-13 18:19:50 +09:00
}
2020-02-10 17:25:11 +09:00
catch ( Exception e )
2018-04-13 18:19:50 +09:00
{
2020-02-10 17:25:11 +09:00
Logger . Error ( e , "Track failed to load" ) ;
2018-06-27 16:02:49 +09:00
return null ;
2018-04-13 18:19:50 +09:00
}
}
2018-06-27 16:07:18 +09:00
protected override Waveform GetWaveform ( )
{
2020-09-01 15:48:13 +09:00
if ( Metadata ? . AudioFile = = null )
return null ;
2018-06-27 16:07:18 +09:00
try
{
2021-04-17 17:49:10 +02:00
var trackData = GetStream ( BeatmapSetInfo . GetPathForFile ( Metadata . AudioFile ) ) ;
2018-06-27 16:07:18 +09:00
return trackData = = null ? null : new Waveform ( trackData ) ;
}
2020-02-10 17:25:11 +09:00
catch ( Exception e )
2018-06-27 16:07:18 +09:00
{
2020-02-10 17:25:11 +09:00
Logger . Error ( e , "Waveform failed to load" ) ;
2018-06-27 16:07:18 +09:00
return null ;
}
}
2018-04-13 18:19:50 +09:00
protected override Storyboard GetStoryboard ( )
{
Storyboard storyboard ;
2019-04-01 12:16:05 +09:00
2018-04-13 18:19:50 +09:00
try
{
2021-04-17 17:49:10 +02:00
using ( var stream = new LineBufferedReader ( GetStream ( BeatmapSetInfo . GetPathForFile ( BeatmapInfo . Path ) ) ) )
2018-04-13 18:19:50 +09: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
{
2021-04-17 17:49:10 +02:00
using ( var secondaryStream = new LineBufferedReader ( GetStream ( BeatmapSetInfo . GetPathForFile ( BeatmapSetInfo . StoryboardFile ) ) ) )
2018-04-13 18:19:50 +09:00
storyboard = decoder . Decode ( stream , secondaryStream ) ;
}
}
}
catch ( Exception e )
{
Logger . Error ( e , "Storyboard failed to load" ) ;
storyboard = new Storyboard ( ) ;
}
storyboard . BeatmapInfo = BeatmapInfo ;
return storyboard ;
}
2019-08-28 19:57:17 +09:00
protected override ISkin GetSkin ( )
2018-04-13 18:19:50 +09:00
{
try
{
2020-12-21 15:14:32 +09:00
return new LegacyBeatmapSkin ( BeatmapInfo , resources . Files , resources ) ;
2018-04-13 18:19:50 +09:00
}
catch ( Exception e )
{
Logger . Error ( e , "Skin failed to load" ) ;
2019-08-26 14:25:35 +09:00
return null ;
2018-04-13 18:19:50 +09:00
}
}
2021-04-17 17:47:13 +02:00
public override Stream GetStream ( string storagePath ) = > resources . Files . GetStream ( storagePath ) ;
2018-04-13 18:19:50 +09:00
}
}
}