2019-01-24 16:43:03 +08: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 17:19:50 +08:00
using System ;
using System.IO ;
using System.Linq ;
using osu.Framework.Audio ;
using osu.Framework.Audio.Track ;
using osu.Framework.Graphics.Textures ;
using osu.Framework.IO.Stores ;
using osu.Framework.Logging ;
using osu.Game.Beatmaps.Formats ;
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 ;
2019-01-25 10:41:44 +08:00
public BeatmapManagerWorkingBeatmap ( IResourceStore < byte [ ] > store , TextureStore textureStore , BeatmapInfo beatmapInfo , AudioManager audioManager )
2019-05-31 13:40:53 +08:00
: base ( beatmapInfo , audioManager )
2018-04-13 17:19:50 +08:00
{
this . store = store ;
2019-01-25 10:41:44 +08:00
this . textureStore = textureStore ;
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
{
try
{
using ( var stream = new StreamReader ( store . GetStream ( getPathForFile ( BeatmapInfo . Path ) ) ) )
return Decoder . GetDecoder < Beatmap > ( stream ) . Decode ( stream ) ;
}
catch
{
return null ;
}
}
2019-06-26 16:23:12 +08:00
private string getPathForFile ( string filename ) = > BeatmapSetInfo . Files . FirstOrDefault ( f = > string . Equals ( f . Filename , filename , StringComparison . InvariantCultureIgnoreCase ) ) ? . FileInfo . StoragePath ;
2018-04-13 17:19:50 +08:00
2019-01-25 10:41:44 +08:00
private TextureStore textureStore ;
2018-09-06 12:15:43 +08:00
2019-06-04 10:04:28 +08:00
private ITrackStore trackStore ;
2019-05-28 22:54:42 +08:00
2018-09-06 12:15:43 +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
{
2019-01-25 10:41:44 +08:00
return textureStore . Get ( getPathForFile ( Metadata . BackgroundFile ) ) ;
2018-04-13 17:19:50 +08:00
}
catch
{
return null ;
}
}
protected override Track GetTrack ( )
{
try
{
2019-05-28 22:54:42 +08:00
return ( trackStore ? ? ( trackStore = AudioManager . GetTrackStore ( store ) ) ) . Get ( getPathForFile ( Metadata . AudioFile ) ) ;
2018-04-13 17:19:50 +08:00
}
catch
{
2018-06-27 15:02:49 +08:00
return null ;
2018-04-13 17:19:50 +08:00
}
}
2019-05-28 22:54:42 +08:00
public override void RecycleTrack ( )
{
base . RecycleTrack ( ) ;
trackStore ? . Dispose ( ) ;
trackStore = null ;
}
2018-09-06 12:15:43 +08:00
public override void TransferTo ( WorkingBeatmap other )
{
base . TransferTo ( other ) ;
if ( other is BeatmapManagerWorkingBeatmap owb & & textureStore ! = null & & BeatmapInfo . BackgroundEquals ( other . BeatmapInfo ) )
owb . textureStore = textureStore ;
}
2018-06-27 15:07:18 +08:00
protected override Waveform GetWaveform ( )
{
try
{
var trackData = store . GetStream ( getPathForFile ( Metadata . AudioFile ) ) ;
return trackData = = null ? null : new Waveform ( trackData ) ;
}
catch
{
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 StreamReader ( store . GetStream ( getPathForFile ( BeatmapInfo . Path ) ) ) )
{
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 StreamReader ( store . GetStream ( getPathForFile ( BeatmapSetInfo . StoryboardFile ) ) ) )
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 Skin GetSkin ( )
{
try
{
2019-08-26 13:25:35 +08:00
return new LegacyBeatmapSkin ( BeatmapInfo , store , AudioManager ) ;
2018-04-13 17:19:50 +08:00
}
catch ( Exception e )
{
Logger . Error ( e , "Skin failed to load" ) ;
2019-08-26 13:25:35 +08:00
return null ;
2018-04-13 17:19:50 +08:00
}
}
}
}
}