mirror of
https://github.com/ppy/osu.git
synced 2025-01-14 23:22:55 +08:00
Centralise access to WorkingBeatmaps.
They can now only be instantiated from BeatmapDatabase and are abstract (to avoid misuse).
This commit is contained in:
parent
355bbb6324
commit
3a89348413
@ -8,6 +8,7 @@ using osu.Game.Beatmaps;
|
||||
using osu.Game.Beatmaps.Formats;
|
||||
using OpenTK;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Beatmaps.IO;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.Modes;
|
||||
using osu.Game.Modes.Objects;
|
||||
@ -72,7 +73,7 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
|
||||
decoder.Process(b);
|
||||
|
||||
beatmap = new WorkingBeatmap(b);
|
||||
beatmap = new TestWorkingBeatmap(b);
|
||||
}
|
||||
|
||||
Add(new Box
|
||||
@ -90,5 +91,16 @@ namespace osu.Desktop.VisualTests.Tests
|
||||
Beatmap = beatmap
|
||||
});
|
||||
}
|
||||
|
||||
class TestWorkingBeatmap : WorkingBeatmap
|
||||
{
|
||||
public TestWorkingBeatmap(Beatmap beatmap)
|
||||
: base(beatmap.BeatmapInfo, beatmap.BeatmapInfo.BeatmapSet)
|
||||
{
|
||||
Beatmap = beatmap;
|
||||
}
|
||||
|
||||
protected override ArchiveReader GetReader() => null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ namespace osu.Game.Tests.Beatmaps.IO
|
||||
|
||||
Assert.IsTrue(set.Beatmaps.Count > 0);
|
||||
|
||||
var beatmap = osu.Dependencies.Get<BeatmapDatabase>().GetBeatmap(set.Beatmaps.First(b => b.Mode == PlayMode.Osu));
|
||||
var beatmap = osu.Dependencies.Get<BeatmapDatabase>().GetWorkingBeatmap(set.Beatmaps.First(b => b.Mode == PlayMode.Osu))?.Beatmap;
|
||||
|
||||
Assert.IsTrue(beatmap.HitObjects.Count > 0);
|
||||
}
|
||||
|
@ -11,16 +11,22 @@ using osu.Game.Database;
|
||||
|
||||
namespace osu.Game.Beatmaps
|
||||
{
|
||||
public class WorkingBeatmap : IDisposable
|
||||
public abstract class WorkingBeatmap : IDisposable
|
||||
{
|
||||
public readonly BeatmapInfo BeatmapInfo;
|
||||
|
||||
public readonly BeatmapSetInfo BeatmapSetInfo;
|
||||
private readonly BeatmapDatabase database;
|
||||
|
||||
public readonly bool WithStoryboard;
|
||||
|
||||
private ArchiveReader getReader() => database?.GetReader(BeatmapSetInfo);
|
||||
protected abstract ArchiveReader GetReader();
|
||||
|
||||
protected WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, bool withStoryboard = false)
|
||||
{
|
||||
BeatmapInfo = beatmapInfo;
|
||||
BeatmapSetInfo = beatmapSetInfo;
|
||||
WithStoryboard = withStoryboard;
|
||||
}
|
||||
|
||||
private Texture background;
|
||||
private object backgroundLock = new object();
|
||||
@ -36,7 +42,7 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
try
|
||||
{
|
||||
using (var reader = getReader())
|
||||
using (var reader = GetReader())
|
||||
background = new TextureStore(new RawTextureLoaderStore(reader), false).Get(BeatmapInfo.Metadata.BackgroundFile);
|
||||
}
|
||||
catch { }
|
||||
@ -59,7 +65,7 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
try
|
||||
{
|
||||
using (var reader = getReader())
|
||||
using (var reader = GetReader())
|
||||
{
|
||||
BeatmapDecoder decoder;
|
||||
using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path)))
|
||||
@ -95,7 +101,7 @@ namespace osu.Game.Beatmaps
|
||||
try
|
||||
{
|
||||
//store a reference to the reader as we may continue accessing the stream in the background.
|
||||
trackReader = getReader();
|
||||
trackReader = GetReader();
|
||||
var trackData = trackReader?.GetStream(BeatmapInfo.Metadata.AudioFile);
|
||||
if (trackData != null)
|
||||
track = new TrackBass(trackData);
|
||||
@ -110,21 +116,6 @@ namespace osu.Game.Beatmaps
|
||||
|
||||
public bool TrackLoaded => track != null;
|
||||
|
||||
public WorkingBeatmap(Beatmap beatmap)
|
||||
{
|
||||
this.beatmap = beatmap;
|
||||
BeatmapInfo = beatmap.BeatmapInfo;
|
||||
BeatmapSetInfo = beatmap.BeatmapInfo.BeatmapSet;
|
||||
}
|
||||
|
||||
public WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, BeatmapDatabase database, bool withStoryboard = false)
|
||||
{
|
||||
BeatmapInfo = beatmapInfo;
|
||||
BeatmapSetInfo = beatmapSetInfo;
|
||||
this.database = database;
|
||||
this.WithStoryboard = withStoryboard;
|
||||
}
|
||||
|
||||
private bool isDisposed;
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
|
@ -183,19 +183,13 @@ namespace osu.Game.Database
|
||||
if (beatmapInfo.Metadata == null)
|
||||
beatmapInfo.Metadata = beatmapSetInfo.Metadata;
|
||||
|
||||
var working = new WorkingBeatmap(beatmapInfo, beatmapSetInfo, this, withStoryboard);
|
||||
WorkingBeatmap working = new DatabaseWorkingBeatmap(this, beatmapInfo, beatmapSetInfo, withStoryboard);
|
||||
|
||||
previous?.TransferTo(working);
|
||||
|
||||
return working;
|
||||
}
|
||||
|
||||
public Beatmap GetBeatmap(BeatmapInfo beatmapInfo)
|
||||
{
|
||||
using (WorkingBeatmap data = GetWorkingBeatmap(beatmapInfo))
|
||||
return data.Beatmap;
|
||||
}
|
||||
|
||||
public TableQuery<T> Query<T>() where T : class
|
||||
{
|
||||
return connection.Table<T>();
|
||||
@ -237,5 +231,20 @@ namespace osu.Game.Database
|
||||
else
|
||||
connection.Update(record);
|
||||
}
|
||||
|
||||
public bool Exists(BeatmapSetInfo beatmapSet) => storage.Exists(beatmapSet.Path);
|
||||
|
||||
private class DatabaseWorkingBeatmap : WorkingBeatmap
|
||||
{
|
||||
private readonly BeatmapDatabase database;
|
||||
|
||||
public DatabaseWorkingBeatmap(BeatmapDatabase database, BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, bool withStoryboard = false)
|
||||
: base(beatmapInfo, beatmapSetInfo, withStoryboard)
|
||||
{
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
protected override ArchiveReader GetReader() => database?.GetReader(BeatmapSetInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,8 +87,7 @@ namespace osu.Game.Database
|
||||
|
||||
internal void ComputeDifficulty(BeatmapDatabase database)
|
||||
{
|
||||
WorkingBeatmap wb = new WorkingBeatmap(this, BeatmapSet, database);
|
||||
StarDifficulty = (float)Ruleset.GetRuleset(Mode).CreateDifficultyCalculator(wb.Beatmap).GetDifficulty();
|
||||
StarDifficulty = (float)Ruleset.GetRuleset(Mode).CreateDifficultyCalculator(database.GetWorkingBeatmap(this).Beatmap).GetDifficulty();
|
||||
}
|
||||
|
||||
public bool Equals(BeatmapInfo other)
|
||||
|
@ -328,9 +328,7 @@ namespace osu.Game.Screens.Select
|
||||
b.ComputeDifficulty(database);
|
||||
beatmapSet.Beatmaps = beatmapSet.Beatmaps.OrderBy(b => b.StarDifficulty).ToList();
|
||||
|
||||
var beatmap = new WorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault(), beatmapSet, database);
|
||||
|
||||
var group = new BeatmapGroup(beatmap)
|
||||
var group = new BeatmapGroup(database.GetWorkingBeatmap(beatmapSet.Beatmaps.FirstOrDefault()))
|
||||
{
|
||||
SelectionChanged = selectionChanged,
|
||||
StartRequested = b => footer.StartButton.TriggerClick()
|
||||
|
Loading…
Reference in New Issue
Block a user