2021-09-30 06:40:41 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2019-01-24 08:43:03 +00:00
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
2018-02-09 11:35:54 +00:00
using System ;
2021-04-17 15:47:13 +00:00
using System.IO ;
2021-10-04 08:00:22 +00:00
using System.Linq ;
2021-09-30 06:40:41 +00:00
using JetBrains.Annotations ;
using osu.Framework.Audio ;
2018-02-09 10:32:18 +00:00
using osu.Framework.Audio.Track ;
2022-08-02 10:50:57 +00:00
using osu.Framework.Graphics.Rendering ;
using osu.Framework.Graphics.Rendering.Dummy ;
2018-02-09 10:32:18 +00:00
using osu.Framework.Graphics.Textures ;
2021-09-30 06:40:41 +00:00
using osu.Framework.IO.Stores ;
using osu.Framework.Lists ;
2018-03-09 12:23:03 +00:00
using osu.Framework.Logging ;
2021-09-30 06:40:41 +00:00
using osu.Framework.Platform ;
2022-01-11 05:40:47 +00:00
using osu.Framework.Statistics ;
2018-02-09 10:32:18 +00:00
using osu.Game.Beatmaps.Formats ;
2021-12-14 05:19:43 +00:00
using osu.Game.Database ;
2019-09-09 22:43:30 +00:00
using osu.Game.IO ;
2018-03-14 11:45:04 +00:00
using osu.Game.Skinning ;
2018-02-09 10:32:18 +00:00
using osu.Game.Storyboards ;
2018-04-13 09:19:50 +00:00
2018-02-09 10:32:18 +00:00
namespace osu.Game.Beatmaps
{
2021-09-30 07:45:32 +00:00
public class WorkingBeatmapCache : IBeatmapResourceProvider , IWorkingBeatmapCache
2018-02-09 10:32:18 +00:00
{
2021-09-30 06:40:41 +00:00
private readonly WeakList < BeatmapManagerWorkingBeatmap > workingCache = new WeakList < BeatmapManagerWorkingBeatmap > ( ) ;
2022-04-14 08:32:47 +00:00
/// <summary>
/// Beatmap files may specify this filename to denote that they don't have an audio track.
/// </summary>
private const string virtual_track_filename = @"virtual" ;
2021-09-30 06:40:41 +00:00
/// <summary>
/// A default representation of a WorkingBeatmap to use when no beatmap is available.
/// </summary>
public readonly WorkingBeatmap DefaultBeatmap ;
private readonly AudioManager audioManager ;
private readonly IResourceStore < byte [ ] > resources ;
private readonly LargeTextureStore largeTextureStore ;
2023-06-08 07:30:14 +00:00
private readonly LargeTextureStore beatmapPanelTextureStore ;
2021-09-30 06:40:41 +00:00
private readonly ITrackStore trackStore ;
private readonly IResourceStore < byte [ ] > files ;
[CanBeNull]
private readonly GameHost host ;
2022-04-14 08:32:47 +00:00
public WorkingBeatmapCache ( ITrackStore trackStore , AudioManager audioManager , IResourceStore < byte [ ] > resources , IResourceStore < byte [ ] > files , WorkingBeatmap defaultBeatmap = null ,
GameHost host = null )
2021-09-30 06:40:41 +00:00
{
DefaultBeatmap = defaultBeatmap ;
this . audioManager = audioManager ;
this . resources = resources ;
this . host = host ;
this . files = files ;
2022-08-02 10:50:57 +00:00
largeTextureStore = new LargeTextureStore ( host ? . Renderer ? ? new DummyRenderer ( ) , host ? . CreateTextureLoaderStore ( files ) ) ;
2023-06-08 07:30:14 +00:00
beatmapPanelTextureStore = new LargeTextureStore ( host ? . Renderer ? ? new DummyRenderer ( ) , new BeatmapPanelBackgroundTextureLoaderStore ( host ? . CreateTextureLoaderStore ( files ) ) ) ;
2021-11-09 08:27:07 +00:00
this . trackStore = trackStore ;
2021-09-30 06:40:41 +00:00
}
public void Invalidate ( BeatmapSetInfo info )
{
foreach ( var b in info . Beatmaps )
Invalidate ( b ) ;
}
public void Invalidate ( BeatmapInfo info )
{
lock ( workingCache )
{
2021-11-24 03:49:57 +00:00
var working = workingCache . FirstOrDefault ( w = > info . Equals ( w . BeatmapInfo ) ) ;
2021-10-14 04:58:36 +00:00
2021-09-30 06:40:41 +00:00
if ( working ! = null )
2021-10-14 04:58:36 +00:00
{
Logger . Log ( $"Invalidating working beatmap cache for {info}" ) ;
2021-09-30 06:40:41 +00:00
workingCache . Remove ( working ) ;
2022-06-20 10:48:46 +00:00
OnInvalidated ? . Invoke ( working ) ;
2021-10-14 04:58:36 +00:00
}
2021-09-30 06:40:41 +00:00
}
}
2022-06-20 10:48:46 +00:00
public event Action < WorkingBeatmap > OnInvalidated ;
2021-09-30 06:40:41 +00:00
public virtual WorkingBeatmap GetWorkingBeatmap ( BeatmapInfo beatmapInfo )
{
2022-01-11 05:40:47 +00:00
if ( beatmapInfo ? . BeatmapSet = = null )
return DefaultBeatmap ;
lock ( workingCache )
2021-09-30 06:40:41 +00:00
{
2022-01-11 05:40:47 +00:00
var working = workingCache . FirstOrDefault ( w = > beatmapInfo . Equals ( w . BeatmapInfo ) ) ;
2021-09-30 06:40:41 +00:00
2022-01-11 05:40:47 +00:00
if ( working ! = null )
return working ;
2022-01-07 05:17:22 +00:00
2022-01-11 12:36:34 +00:00
beatmapInfo = beatmapInfo . Detach ( ) ;
2022-01-11 05:40:47 +00:00
workingCache . Add ( working = new BeatmapManagerWorkingBeatmap ( beatmapInfo , this ) ) ;
2021-09-30 06:40:41 +00:00
2022-01-11 05:40:47 +00:00
// best effort; may be higher than expected.
GlobalStatistics . Get < int > ( "Beatmaps" , $"Cached {nameof(WorkingBeatmap)}s" ) . Value = workingCache . Count ( ) ;
return working ;
}
2021-09-30 06:40:41 +00:00
}
#region IResourceStorageProvider
TextureStore IBeatmapResourceProvider . LargeTextureStore = > largeTextureStore ;
2023-06-08 07:30:14 +00:00
TextureStore IBeatmapResourceProvider . BeatmapPanelTextureStore = > beatmapPanelTextureStore ;
2021-09-30 06:40:41 +00:00
ITrackStore IBeatmapResourceProvider . Tracks = > trackStore ;
2022-08-02 10:50:57 +00:00
IRenderer IStorageResourceProvider . Renderer = > host ? . Renderer ? ? new DummyRenderer ( ) ;
2021-09-30 06:40:41 +00:00
AudioManager IStorageResourceProvider . AudioManager = > audioManager ;
2022-01-24 10:59:58 +00:00
RealmAccess IStorageResourceProvider . RealmAccess = > null ;
2021-09-30 06:40:41 +00:00
IResourceStore < byte [ ] > IStorageResourceProvider . Files = > files ;
IResourceStore < byte [ ] > IStorageResourceProvider . Resources = > resources ;
IResourceStore < TextureUpload > IStorageResourceProvider . CreateTextureLoaderStore ( IResourceStore < byte [ ] > underlyingStore ) = > host ? . CreateTextureLoaderStore ( underlyingStore ) ;
#endregion
2020-08-11 04:48:57 +00:00
private class BeatmapManagerWorkingBeatmap : WorkingBeatmap
2018-02-09 10:32:18 +00:00
{
2020-12-22 03:06:10 +00:00
[NotNull]
2020-12-21 05:06:50 +00:00
private readonly IBeatmapResourceProvider resources ;
2018-04-13 09:19:50 +00:00
2020-12-22 03:06:10 +00:00
public BeatmapManagerWorkingBeatmap ( BeatmapInfo beatmapInfo , [ NotNull ] IBeatmapResourceProvider resources )
: base ( beatmapInfo , resources . AudioManager )
2018-02-09 10:32:18 +00:00
{
2020-12-21 05:06:50 +00:00
this . resources = resources ;
2018-02-09 10:32:18 +00:00
}
2018-04-13 09:19:50 +00:00
2018-04-19 11:44:38 +00:00
protected override IBeatmap GetBeatmap ( )
2018-02-09 10:32:18 +00:00
{
2020-08-24 10:38:05 +00:00
if ( BeatmapInfo . Path = = null )
2020-09-04 04:13:53 +00:00
return new Beatmap { BeatmapInfo = BeatmapInfo } ;
2020-08-24 10:38:05 +00:00
2018-02-09 10:32:18 +00:00
try
{
2022-07-07 05:29:15 +00:00
string fileStorePath = BeatmapSetInfo . GetPathForFile ( BeatmapInfo . Path ) ;
2022-10-13 09:20:49 +00:00
// TODO: check validity of file
2022-07-07 05:29:15 +00:00
var stream = GetStream ( fileStorePath ) ;
if ( stream = = null )
{
Logger . Log ( $"Beatmap failed to load (file {BeatmapInfo.Path} not found on disk at expected location {fileStorePath})." , level : LogLevel . Error ) ;
return null ;
}
using ( var reader = new LineBufferedReader ( stream ) )
return Decoder . GetDecoder < Beatmap > ( reader ) . Decode ( reader ) ;
2018-02-09 10:32:18 +00:00
}
2020-02-10 08:25:11 +00:00
catch ( Exception e )
2018-02-09 10:32:18 +00:00
{
2020-02-10 08:25:11 +00:00
Logger . Error ( e , "Beatmap failed to load" ) ;
2018-02-09 10:32:18 +00:00
return null ;
}
}
2018-04-13 09:19:50 +00:00
2023-06-08 07:30:14 +00:00
public override Texture GetPanelBackground ( ) = > getBackgroundFromStore ( resources . BeatmapPanelTextureStore ) ;
public override Texture GetBackground ( ) = > getBackgroundFromStore ( resources . LargeTextureStore ) ;
private Texture getBackgroundFromStore ( TextureStore store )
2018-02-09 10:32:18 +00:00
{
2021-11-04 05:11:21 +00:00
if ( string . IsNullOrEmpty ( Metadata ? . BackgroundFile ) )
2018-02-09 10:32:18 +00:00
return null ;
2018-04-13 09:19:50 +00:00
2018-02-09 10:32:18 +00:00
try
{
2022-07-07 05:29:15 +00:00
string fileStorePath = BeatmapSetInfo . GetPathForFile ( Metadata . BackgroundFile ) ;
2023-06-08 07:30:14 +00:00
var texture = store . Get ( fileStorePath ) ;
2022-07-07 05:29:15 +00:00
if ( texture = = null )
{
2022-07-17 12:20:50 +00:00
Logger . Log ( $"Beatmap background failed to load (file {Metadata.BackgroundFile} not found on disk at expected location {fileStorePath})." ) ;
2022-07-07 05:29:15 +00:00
return null ;
}
return texture ;
2018-02-09 10:32:18 +00:00
}
2020-02-10 08:25:11 +00:00
catch ( Exception e )
2018-02-09 10:32:18 +00:00
{
2020-02-10 08:25:11 +00:00
Logger . Error ( e , "Background failed to load" ) ;
2018-02-09 10:32:18 +00:00
return null ;
}
}
2019-08-30 20:19:34 +00:00
2020-08-07 13:31:41 +00:00
protected override Track GetBeatmapTrack ( )
2018-02-09 10:32:18 +00:00
{
2021-11-04 05:01:01 +00:00
if ( string . IsNullOrEmpty ( Metadata ? . AudioFile ) )
2020-09-01 06:48:13 +00:00
return null ;
2022-04-14 08:32:47 +00:00
if ( Metadata . AudioFile = = virtual_track_filename )
return null ;
2018-02-09 10:32:18 +00:00
try
{
2022-07-07 05:29:15 +00:00
string fileStorePath = BeatmapSetInfo . GetPathForFile ( Metadata . AudioFile ) ;
var track = resources . Tracks . Get ( fileStorePath ) ;
if ( track = = null )
{
Logger . Log ( $"Beatmap failed to load (file {Metadata.AudioFile} not found on disk at expected location {fileStorePath})." , level : LogLevel . Error ) ;
return null ;
}
return track ;
2018-02-09 10:32:18 +00:00
}
2020-02-10 08:25:11 +00:00
catch ( Exception e )
2018-02-09 10:32:18 +00:00
{
2020-02-10 08:25:11 +00:00
Logger . Error ( e , "Track failed to load" ) ;
2018-06-27 07:02:49 +00:00
return null ;
2018-02-09 10:32:18 +00:00
}
}
2018-04-13 09:19:50 +00:00
2018-06-27 07:07:18 +00:00
protected override Waveform GetWaveform ( )
{
2021-11-04 05:01:01 +00:00
if ( string . IsNullOrEmpty ( Metadata ? . AudioFile ) )
2021-12-22 10:14:18 +00:00
return null ;
2020-09-01 06:48:13 +00:00
2022-04-14 08:32:47 +00:00
if ( Metadata . AudioFile = = virtual_track_filename )
return null ;
2018-06-27 07:07:18 +00:00
try
{
2022-07-07 05:29:15 +00:00
string fileStorePath = BeatmapSetInfo . GetPathForFile ( Metadata . AudioFile ) ;
var trackData = GetStream ( fileStorePath ) ;
if ( trackData = = null )
{
Logger . Log ( $"Beatmap waveform failed to load (file {Metadata.AudioFile} not found on disk at expected location {fileStorePath})." , level : LogLevel . Error ) ;
return null ;
}
return new Waveform ( trackData ) ;
2018-06-27 07:07:18 +00:00
}
2020-02-10 08:25:11 +00:00
catch ( Exception e )
2018-06-27 07:07:18 +00:00
{
2020-02-10 08:25:11 +00:00
Logger . Error ( e , "Waveform failed to load" ) ;
2021-12-22 10:14:18 +00:00
return null ;
2018-06-27 07:07:18 +00:00
}
}
2018-04-13 09:19:50 +00:00
2018-02-09 10:32:18 +00:00
protected override Storyboard GetStoryboard ( )
{
2018-02-16 03:07:59 +00:00
Storyboard storyboard ;
2019-04-01 03:16:05 +00:00
2022-01-11 12:36:34 +00:00
if ( BeatmapInfo . Path = = null )
return new Storyboard ( ) ;
2018-02-09 10:32:18 +00:00
try
{
2022-07-07 05:29:15 +00:00
string fileStorePath = BeatmapSetInfo . GetPathForFile ( BeatmapInfo . Path ) ;
2022-07-07 05:33:17 +00:00
var beatmapFileStream = GetStream ( fileStorePath ) ;
2022-07-07 05:29:15 +00:00
2022-07-07 05:33:17 +00:00
if ( beatmapFileStream = = null )
2018-02-09 10:32:18 +00:00
{
2022-07-07 05:29:15 +00:00
Logger . Log ( $"Beatmap failed to load (file {BeatmapInfo.Path} not found on disk at expected location {fileStorePath})" , level : LogLevel . Error ) ;
2023-06-12 08:22:11 +00:00
return new Storyboard ( ) ;
2022-07-07 05:29:15 +00:00
}
2022-07-07 05:33:17 +00:00
using ( var reader = new LineBufferedReader ( beatmapFileStream ) )
2022-07-07 05:29:15 +00:00
{
var decoder = Decoder . GetDecoder < Storyboard > ( reader ) ;
2018-04-13 09:19:50 +00:00
2022-07-07 05:33:17 +00:00
Stream storyboardFileStream = null ;
2021-10-04 07:50:29 +00:00
2023-01-26 06:09:36 +00:00
string mainStoryboardFilename = getMainStoryboardFilename ( BeatmapSetInfo . Metadata ) ;
if ( BeatmapSetInfo ? . Files . FirstOrDefault ( f = > f . Filename . Equals ( mainStoryboardFilename , StringComparison . OrdinalIgnoreCase ) ) ? . Filename is string
storyboardFilename )
2018-02-16 03:07:59 +00:00
{
2022-07-07 05:33:17 +00:00
string storyboardFileStorePath = BeatmapSetInfo ? . GetPathForFile ( storyboardFilename ) ;
storyboardFileStream = GetStream ( storyboardFileStorePath ) ;
2022-07-07 05:29:15 +00:00
2022-07-07 05:33:17 +00:00
if ( storyboardFileStream = = null )
Logger . Log ( $"Storyboard failed to load (file {storyboardFilename} not found on disk at expected location {storyboardFileStorePath})" , level : LogLevel . Error ) ;
}
2022-07-07 05:29:15 +00:00
2022-07-07 05:33:17 +00:00
if ( storyboardFileStream ! = null )
{
// Stand-alone storyboard was found, so parse in addition to the beatmap's local storyboard.
using ( var secondaryReader = new LineBufferedReader ( storyboardFileStream ) )
2022-07-07 05:29:15 +00:00
storyboard = decoder . Decode ( reader , secondaryReader ) ;
2018-02-16 03:07:59 +00:00
}
2022-07-07 05:33:17 +00:00
else
storyboard = decoder . Decode ( reader ) ;
2018-02-09 10:32:18 +00:00
}
}
2018-03-09 12:23:03 +00:00
catch ( Exception e )
2018-02-09 10:32:18 +00:00
{
2018-03-09 12:23:03 +00:00
Logger . Error ( e , "Storyboard failed to load" ) ;
2018-02-16 03:07:59 +00:00
storyboard = new Storyboard ( ) ;
2018-02-09 10:32:18 +00:00
}
2018-04-13 09:19:50 +00:00
2018-02-16 03:07:59 +00:00
storyboard . BeatmapInfo = BeatmapInfo ;
2018-04-13 09:19:50 +00:00
2018-02-16 03:07:59 +00:00
return storyboard ;
2018-02-09 10:32:18 +00:00
}
2018-04-13 09:19:50 +00:00
2021-08-15 16:38:01 +00:00
protected internal override ISkin GetSkin ( )
2018-03-14 11:45:04 +00:00
{
try
{
2022-03-22 10:23:22 +00:00
return new LegacyBeatmapSkin ( BeatmapInfo , resources ) ;
2018-03-14 11:45:04 +00:00
}
catch ( Exception e )
{
Logger . Error ( e , "Skin failed to load" ) ;
2019-08-26 05:25:35 +00:00
return null ;
2018-03-14 11:45:04 +00:00
}
}
2021-04-17 15:47:13 +00:00
public override Stream GetStream ( string storagePath ) = > resources . Files . GetStream ( storagePath ) ;
2023-01-26 06:09:36 +00:00
private string getMainStoryboardFilename ( IBeatmapMetadataInfo metadata )
{
// Matches stable implementation, because it's probably simpler than trying to do anything else.
// This may need to be reconsidered after we begin storing storyboards in the new editor.
return windowsFilenameStrip (
( metadata . Artist . Length > 0 ? metadata . Artist + @" - " + metadata . Title : Path . GetFileNameWithoutExtension ( metadata . AudioFile ) )
+ ( metadata . Author . Username . Length > 0 ? @" (" + metadata . Author . Username + @")" : string . Empty )
+ @".osb" ) ;
string windowsFilenameStrip ( string entry )
{
// Inlined from Path.GetInvalidFilenameChars() to ensure the windows characters are used (to match stable).
char [ ] invalidCharacters =
{
' \ x00 ' , ' \ x01 ' , ' \ x02 ' , ' \ x03 ' , ' \ x04 ' , ' \ x05 ' , ' \ x06 ' , ' \ x07 ' ,
' \ x08 ' , ' \ x09 ' , ' \ x0A ' , ' \ x0B ' , ' \ x0C ' , ' \ x0D ' , ' \ x0E ' , ' \ x0F ' , ' \ x10 ' , ' \ x11 ' , ' \ x12 ' ,
' \ x13 ' , ' \ x14 ' , ' \ x15 ' , ' \ x16 ' , ' \ x17 ' , ' \ x18 ' , ' \ x19 ' , ' \ x1A ' , ' \ x1B ' , ' \ x1C ' , ' \ x1D ' ,
' \ x1E ' , ' \ x1F ' , ' \ x22 ' , ' \ x3C ' , ' \ x3E ' , ' \ x7C ' , ':' , '*' , '?' , '\\' , '/'
} ;
foreach ( char c in invalidCharacters )
entry = entry . Replace ( c . ToString ( ) , string . Empty ) ;
return entry ;
}
}
2018-02-09 10:32:18 +00:00
}
}
}