2021-09-30 14:40:41 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-09 19:35:54 +08:00
|
|
|
using System;
|
2021-04-17 23:47:13 +08:00
|
|
|
using System.IO;
|
2021-10-04 16:00:22 +08:00
|
|
|
using System.Linq;
|
2021-09-30 14:40:41 +08:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
using osu.Framework.Audio;
|
2018-02-09 18:32:18 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2021-09-30 14:40:41 +08:00
|
|
|
using osu.Framework.IO.Stores;
|
|
|
|
using osu.Framework.Lists;
|
2018-03-09 20:23:03 +08:00
|
|
|
using osu.Framework.Logging;
|
2021-09-30 14:40:41 +08:00
|
|
|
using osu.Framework.Platform;
|
2022-01-11 13:40:47 +08:00
|
|
|
using osu.Framework.Statistics;
|
2020-10-16 13:39:02 +08:00
|
|
|
using osu.Framework.Testing;
|
2018-02-09 18:32:18 +08:00
|
|
|
using osu.Game.Beatmaps.Formats;
|
2021-12-14 13:19:43 +08:00
|
|
|
using osu.Game.Database;
|
2019-09-10 06:43:30 +08:00
|
|
|
using osu.Game.IO;
|
2018-03-14 19:45:04 +08:00
|
|
|
using osu.Game.Skinning;
|
2018-02-09 18:32:18 +08:00
|
|
|
using osu.Game.Storyboards;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
{
|
2021-09-30 15:45:32 +08:00
|
|
|
public class WorkingBeatmapCache : IBeatmapResourceProvider, IWorkingBeatmapCache
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2021-09-30 14:40:41 +08:00
|
|
|
private readonly WeakList<BeatmapManagerWorkingBeatmap> workingCache = new WeakList<BeatmapManagerWorkingBeatmap>();
|
|
|
|
|
2022-04-14 16:32:47 +08: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 14:40:41 +08: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;
|
|
|
|
private readonly ITrackStore trackStore;
|
|
|
|
private readonly IResourceStore<byte[]> files;
|
|
|
|
|
|
|
|
[CanBeNull]
|
|
|
|
private readonly GameHost host;
|
|
|
|
|
2022-04-14 16:32:47 +08:00
|
|
|
public WorkingBeatmapCache(ITrackStore trackStore, AudioManager audioManager, IResourceStore<byte[]> resources, IResourceStore<byte[]> files, WorkingBeatmap defaultBeatmap = null,
|
|
|
|
GameHost host = null)
|
2021-09-30 14:40:41 +08:00
|
|
|
{
|
|
|
|
DefaultBeatmap = defaultBeatmap;
|
|
|
|
|
|
|
|
this.audioManager = audioManager;
|
|
|
|
this.resources = resources;
|
|
|
|
this.host = host;
|
|
|
|
this.files = files;
|
|
|
|
largeTextureStore = new LargeTextureStore(host?.CreateTextureLoaderStore(files));
|
2021-11-09 16:27:07 +08:00
|
|
|
this.trackStore = trackStore;
|
2021-09-30 14:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Invalidate(BeatmapSetInfo info)
|
|
|
|
{
|
|
|
|
foreach (var b in info.Beatmaps)
|
|
|
|
Invalidate(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Invalidate(BeatmapInfo info)
|
|
|
|
{
|
|
|
|
lock (workingCache)
|
|
|
|
{
|
2021-11-24 11:49:57 +08:00
|
|
|
var working = workingCache.FirstOrDefault(w => info.Equals(w.BeatmapInfo));
|
2021-10-14 12:58:36 +08:00
|
|
|
|
2021-09-30 14:40:41 +08:00
|
|
|
if (working != null)
|
2021-10-14 12:58:36 +08:00
|
|
|
{
|
|
|
|
Logger.Log($"Invalidating working beatmap cache for {info}");
|
2021-09-30 14:40:41 +08:00
|
|
|
workingCache.Remove(working);
|
2021-10-14 12:58:36 +08:00
|
|
|
}
|
2021-09-30 14:40:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo)
|
|
|
|
{
|
2022-01-11 13:40:47 +08:00
|
|
|
if (beatmapInfo?.BeatmapSet == null)
|
|
|
|
return DefaultBeatmap;
|
|
|
|
|
|
|
|
lock (workingCache)
|
2021-09-30 14:40:41 +08:00
|
|
|
{
|
2022-01-11 13:40:47 +08:00
|
|
|
var working = workingCache.FirstOrDefault(w => beatmapInfo.Equals(w.BeatmapInfo));
|
2021-09-30 14:40:41 +08:00
|
|
|
|
2022-01-11 13:40:47 +08:00
|
|
|
if (working != null)
|
|
|
|
return working;
|
2022-01-07 13:17:22 +08:00
|
|
|
|
2022-01-11 20:36:34 +08:00
|
|
|
beatmapInfo = beatmapInfo.Detach();
|
2022-01-11 13:40:47 +08:00
|
|
|
|
|
|
|
workingCache.Add(working = new BeatmapManagerWorkingBeatmap(beatmapInfo, this));
|
2021-09-30 14:40:41 +08:00
|
|
|
|
2022-01-11 13:40:47 +08:00
|
|
|
// best effort; may be higher than expected.
|
|
|
|
GlobalStatistics.Get<int>("Beatmaps", $"Cached {nameof(WorkingBeatmap)}s").Value = workingCache.Count();
|
|
|
|
|
|
|
|
return working;
|
|
|
|
}
|
2021-09-30 14:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#region IResourceStorageProvider
|
|
|
|
|
|
|
|
TextureStore IBeatmapResourceProvider.LargeTextureStore => largeTextureStore;
|
|
|
|
ITrackStore IBeatmapResourceProvider.Tracks => trackStore;
|
|
|
|
AudioManager IStorageResourceProvider.AudioManager => audioManager;
|
2022-01-24 18:59:58 +08:00
|
|
|
RealmAccess IStorageResourceProvider.RealmAccess => null;
|
2021-09-30 14:40:41 +08:00
|
|
|
IResourceStore<byte[]> IStorageResourceProvider.Files => files;
|
|
|
|
IResourceStore<byte[]> IStorageResourceProvider.Resources => resources;
|
|
|
|
IResourceStore<TextureUpload> IStorageResourceProvider.CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore) => host?.CreateTextureLoaderStore(underlyingStore);
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2020-10-16 13:39:02 +08:00
|
|
|
[ExcludeFromDynamicCompile]
|
2020-08-11 12:48:57 +08:00
|
|
|
private class BeatmapManagerWorkingBeatmap : WorkingBeatmap
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-12-22 11:06:10 +08:00
|
|
|
[NotNull]
|
2020-12-21 13:06:50 +08:00
|
|
|
private readonly IBeatmapResourceProvider resources;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2020-12-22 11:06:10 +08:00
|
|
|
public BeatmapManagerWorkingBeatmap(BeatmapInfo beatmapInfo, [NotNull] IBeatmapResourceProvider resources)
|
|
|
|
: base(beatmapInfo, resources.AudioManager)
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-12-21 13:06:50 +08:00
|
|
|
this.resources = resources;
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-04-19 19:44:38 +08:00
|
|
|
protected override IBeatmap GetBeatmap()
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-08-24 18:38:05 +08:00
|
|
|
if (BeatmapInfo.Path == null)
|
2020-09-04 12:13:53 +08:00
|
|
|
return new Beatmap { BeatmapInfo = BeatmapInfo };
|
2020-08-24 18:38:05 +08:00
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-17 23:49:10 +08:00
|
|
|
using (var stream = new LineBufferedReader(GetStream(BeatmapSetInfo.GetPathForFile(BeatmapInfo.Path))))
|
2018-03-09 20:23:03 +08:00
|
|
|
return Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2020-02-10 16:25:11 +08:00
|
|
|
catch (Exception e)
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-02-10 16:25:11 +08:00
|
|
|
Logger.Error(e, "Beatmap failed to load");
|
2018-02-09 18:32:18 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
protected override Texture GetBackground()
|
|
|
|
{
|
2021-11-04 13:11:21 +08:00
|
|
|
if (string.IsNullOrEmpty(Metadata?.BackgroundFile))
|
2018-02-09 18:32:18 +08:00
|
|
|
return null;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-17 23:49:10 +08:00
|
|
|
return resources.LargeTextureStore.Get(BeatmapSetInfo.GetPathForFile(Metadata.BackgroundFile));
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2020-02-10 16:25:11 +08:00
|
|
|
catch (Exception e)
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-02-10 16:25:11 +08:00
|
|
|
Logger.Error(e, "Background failed to load");
|
2018-02-09 18:32:18 +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-02-09 18:32:18 +08:00
|
|
|
{
|
2021-11-04 13:01:01 +08:00
|
|
|
if (string.IsNullOrEmpty(Metadata?.AudioFile))
|
2020-09-01 14:48:13 +08:00
|
|
|
return null;
|
|
|
|
|
2022-04-14 16:32:47 +08:00
|
|
|
if (Metadata.AudioFile == virtual_track_filename)
|
|
|
|
return null;
|
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-17 23:49:10 +08:00
|
|
|
return resources.Tracks.Get(BeatmapSetInfo.GetPathForFile(Metadata.AudioFile));
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2020-02-10 16:25:11 +08:00
|
|
|
catch (Exception e)
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2020-02-10 16:25:11 +08:00
|
|
|
Logger.Error(e, "Track failed to load");
|
2018-06-27 15:02:49 +08:00
|
|
|
return null;
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-06-27 15:07:18 +08:00
|
|
|
protected override Waveform GetWaveform()
|
|
|
|
{
|
2021-11-04 13:01:01 +08:00
|
|
|
if (string.IsNullOrEmpty(Metadata?.AudioFile))
|
2021-12-22 18:14:18 +08:00
|
|
|
return null;
|
2020-09-01 14:48:13 +08:00
|
|
|
|
2022-04-14 16:32:47 +08:00
|
|
|
if (Metadata.AudioFile == virtual_track_filename)
|
|
|
|
return null;
|
|
|
|
|
2018-06-27 15:07:18 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-17 23:49:10 +08:00
|
|
|
var trackData = GetStream(BeatmapSetInfo.GetPathForFile(Metadata.AudioFile));
|
2021-12-22 18:14:18 +08:00
|
|
|
return trackData == null ? null : new Waveform(trackData);
|
2018-06-27 15:07:18 +08:00
|
|
|
}
|
2020-02-10 16:25:11 +08:00
|
|
|
catch (Exception e)
|
2018-06-27 15:07:18 +08:00
|
|
|
{
|
2020-02-10 16:25:11 +08:00
|
|
|
Logger.Error(e, "Waveform failed to load");
|
2021-12-22 18:14:18 +08:00
|
|
|
return null;
|
2018-06-27 15:07:18 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
protected override Storyboard GetStoryboard()
|
|
|
|
{
|
2018-02-16 11:07:59 +08:00
|
|
|
Storyboard storyboard;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2022-01-11 20:36:34 +08:00
|
|
|
if (BeatmapInfo.Path == null)
|
|
|
|
return new Storyboard();
|
|
|
|
|
2018-02-09 18:32:18 +08:00
|
|
|
try
|
|
|
|
{
|
2021-04-17 23:49:10 +08:00
|
|
|
using (var stream = new LineBufferedReader(GetStream(BeatmapSetInfo.GetPathForFile(BeatmapInfo.Path))))
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
var decoder = Decoder.GetDecoder<Storyboard>(stream);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
string storyboardFilename = BeatmapSetInfo?.Files.FirstOrDefault(f => f.Filename.EndsWith(".osb", StringComparison.OrdinalIgnoreCase))?.Filename;
|
2021-10-04 15:50:29 +08:00
|
|
|
|
2018-02-19 13:00:48 +08:00
|
|
|
// todo: support loading from both set-wide storyboard *and* beatmap specific.
|
2021-10-04 15:50:29 +08:00
|
|
|
if (string.IsNullOrEmpty(storyboardFilename))
|
2018-03-09 20:23:03 +08:00
|
|
|
storyboard = decoder.Decode(stream);
|
2018-02-16 11:07:59 +08:00
|
|
|
else
|
|
|
|
{
|
2021-10-04 15:50:29 +08:00
|
|
|
using (var secondaryStream = new LineBufferedReader(GetStream(BeatmapSetInfo.GetPathForFile(storyboardFilename))))
|
2018-03-09 20:23:03 +08:00
|
|
|
storyboard = decoder.Decode(stream, secondaryStream);
|
2018-02-16 11:07:59 +08:00
|
|
|
}
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 20:23:03 +08:00
|
|
|
catch (Exception e)
|
2018-02-09 18:32:18 +08:00
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
Logger.Error(e, "Storyboard failed to load");
|
2018-02-16 11:07:59 +08:00
|
|
|
storyboard = new Storyboard();
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-16 11:07:59 +08:00
|
|
|
storyboard.BeatmapInfo = BeatmapInfo;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-02-16 11:07:59 +08:00
|
|
|
return storyboard;
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2021-08-16 00:38:01 +08:00
|
|
|
protected internal override ISkin GetSkin()
|
2018-03-14 19:45:04 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-03-22 18:23:22 +08:00
|
|
|
return new LegacyBeatmapSkin(BeatmapInfo, resources);
|
2018-03-14 19:45:04 +08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, "Skin failed to load");
|
2019-08-26 13:25:35 +08:00
|
|
|
return null;
|
2018-03-14 19:45:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 23:47:13 +08:00
|
|
|
|
|
|
|
public override Stream GetStream(string storagePath) => resources.Files.GetStream(storagePath);
|
2018-02-09 18:32:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|