1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 12:35:34 +08:00

Expose resources to beatmaps in a saner way

This commit is contained in:
Dean Herbert 2020-12-21 14:06:50 +09:00
parent 7c804be4d3
commit 0ffbe12fcc
3 changed files with 64 additions and 20 deletions

View File

@ -16,6 +16,7 @@ using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Framework.Lists;
using osu.Framework.Logging;
using osu.Framework.Platform;
@ -28,8 +29,8 @@ using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects;
using osu.Game.Users;
using osu.Game.Skinning;
using osu.Game.Users;
using Decoder = osu.Game.Beatmaps.Formats.Decoder;
namespace osu.Game.Beatmaps
@ -38,7 +39,7 @@ namespace osu.Game.Beatmaps
/// Handles the storage and retrieval of Beatmaps/WorkingBeatmaps.
/// </summary>
[ExcludeFromDynamicCompile]
public partial class BeatmapManager : DownloadableArchiveModelManager<BeatmapSetInfo, BeatmapSetFileInfo>, IDisposable
public partial class BeatmapManager : DownloadableArchiveModelManager<BeatmapSetInfo, BeatmapSetFileInfo>, IDisposable, IBeatmapResourceProvider
{
/// <summary>
/// Fired when a single difficulty has been hidden.
@ -71,6 +72,8 @@ namespace osu.Game.Beatmaps
private readonly LargeTextureStore largeTextureStore;
private readonly ITrackStore trackStore;
private readonly GameHost host;
[CanBeNull]
private readonly BeatmapOnlineLookupQueue onlineLookupQueue;
@ -80,6 +83,7 @@ namespace osu.Game.Beatmaps
{
this.rulesets = rulesets;
this.audioManager = audioManager;
this.host = host;
DefaultBeatmap = defaultBeatmap;
@ -302,7 +306,7 @@ namespace osu.Game.Beatmaps
beatmapInfo.Metadata ??= beatmapInfo.BeatmapSet.Metadata;
workingCache.Add(working = new BeatmapManagerWorkingBeatmap(Files.Store, largeTextureStore, trackStore, beatmapInfo, audioManager));
workingCache.Add(working = new BeatmapManagerWorkingBeatmap(beatmapInfo, this));
return working;
}
@ -492,6 +496,12 @@ namespace osu.Game.Beatmaps
onlineLookupQueue?.Dispose();
}
TextureStore IBeatmapResourceProvider.LargeTextureStore => largeTextureStore;
ITrackStore IBeatmapResourceProvider.Tracks => trackStore;
AudioManager IBeatmapResourceProvider.AudioManager => audioManager;
IResourceStore<byte[]> IBeatmapResourceProvider.Files => Files.Store;
IResourceStore<TextureUpload> IBeatmapResourceProvider.CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore) => host.CreateTextureLoaderStore(underlyingStore);
/// <summary>
/// A dummy WorkingBeatmap for the purpose of retrieving a beatmap for star difficulty calculation.
/// </summary>

View File

@ -3,10 +3,8 @@
using System;
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.Framework.Testing;
using osu.Game.Beatmaps.Formats;
@ -21,16 +19,12 @@ namespace osu.Game.Beatmaps
[ExcludeFromDynamicCompile]
private class BeatmapManagerWorkingBeatmap : WorkingBeatmap
{
private readonly IResourceStore<byte[]> store;
private readonly TextureStore textureStore;
private readonly ITrackStore trackStore;
private readonly IBeatmapResourceProvider resources;
public BeatmapManagerWorkingBeatmap(IResourceStore<byte[]> store, TextureStore textureStore, ITrackStore trackStore, BeatmapInfo beatmapInfo, AudioManager audioManager)
: base(beatmapInfo, audioManager)
public BeatmapManagerWorkingBeatmap(BeatmapInfo beatmapInfo, IBeatmapResourceProvider resources)
: base(beatmapInfo, resources?.AudioManager)
{
this.store = store;
this.textureStore = textureStore;
this.trackStore = trackStore;
this.resources = resources;
}
protected override IBeatmap GetBeatmap()
@ -40,7 +34,7 @@ namespace osu.Game.Beatmaps
try
{
using (var stream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
using (var stream = new LineBufferedReader(resources.Files.GetStream(getPathForFile(BeatmapInfo.Path))))
return Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
}
catch (Exception e)
@ -61,7 +55,7 @@ namespace osu.Game.Beatmaps
try
{
return textureStore.Get(getPathForFile(Metadata.BackgroundFile));
return resources.LargeTextureStore.Get(getPathForFile(Metadata.BackgroundFile));
}
catch (Exception e)
{
@ -77,7 +71,7 @@ namespace osu.Game.Beatmaps
try
{
return trackStore.Get(getPathForFile(Metadata.AudioFile));
return resources.Tracks.Get(getPathForFile(Metadata.AudioFile));
}
catch (Exception e)
{
@ -93,7 +87,7 @@ namespace osu.Game.Beatmaps
try
{
var trackData = store.GetStream(getPathForFile(Metadata.AudioFile));
var trackData = resources.Files.GetStream(getPathForFile(Metadata.AudioFile));
return trackData == null ? null : new Waveform(trackData);
}
catch (Exception e)
@ -109,7 +103,7 @@ namespace osu.Game.Beatmaps
try
{
using (var stream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapInfo.Path))))
using (var stream = new LineBufferedReader(resources.Files.GetStream(getPathForFile(BeatmapInfo.Path))))
{
var decoder = Decoder.GetDecoder<Storyboard>(stream);
@ -118,7 +112,7 @@ namespace osu.Game.Beatmaps
storyboard = decoder.Decode(stream);
else
{
using (var secondaryStream = new LineBufferedReader(store.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile))))
using (var secondaryStream = new LineBufferedReader(resources.Files.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile))))
storyboard = decoder.Decode(stream, secondaryStream);
}
}
@ -138,7 +132,7 @@ namespace osu.Game.Beatmaps
{
try
{
return new LegacyBeatmapSkin(BeatmapInfo, store, AudioManager);
return new LegacyBeatmapSkin(BeatmapInfo, resources.Files, AudioManager);
}
catch (Exception e)
{

View File

@ -0,0 +1,40 @@
// 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.
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
namespace osu.Game.Beatmaps
{
public interface IBeatmapResourceProvider
{
/// <summary>
/// Retrieve a global large texture store, used for loading beatmap backgrounds.
/// </summary>
TextureStore LargeTextureStore { get; }
/// <summary>
/// Access a global track store for retrieving beatmap tracks from.
/// </summary>
ITrackStore Tracks { get; }
/// <summary>
/// Retrieve the game-wide audio manager.
/// </summary>
AudioManager AudioManager { get; }
/// <summary>
/// Access game-wide user files.
/// </summary>
IResourceStore<byte[]> Files { get; }
/// <summary>
/// Create a texture loader store based on an underlying data store.
/// </summary>
/// <param name="underlyingStore">The underlying provider of texture data (in arbitrary image formats).</param>
/// <returns>A texture loader store.</returns>
IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore);
}
}