1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 07:22:55 +08:00

Lazy parse beatmap; Don't reload audio track if it hasn't changed.

This commit is contained in:
Dean Herbert 2016-10-28 20:24:14 +09:00
parent 74f1a9622f
commit 10ef34b805
4 changed files with 56 additions and 16 deletions

View File

@ -44,13 +44,15 @@ namespace osu.Desktop.VisualTests.Tests
time += 500; time += 500;
} }
Add(new Player() Add(new Player
{ {
Beatmap = new WorkingBeatmap( Beatmap = new WorkingBeatmap
new Beatmap {
Beatmap = new Beatmap
{ {
HitObjects = objects HitObjects = objects
}) }
}
}); });
} }

View File

@ -2,15 +2,38 @@
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE //Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System; using System;
using System.IO;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO; using osu.Game.Beatmaps.IO;
using osu.Game.Database;
namespace osu.Game.Beatmaps namespace osu.Game.Beatmaps
{ {
public class WorkingBeatmap : IDisposable public class WorkingBeatmap : IDisposable
{ {
private BeatmapInfo beatmapInfo;
public readonly ArchiveReader Reader; public readonly ArchiveReader Reader;
public readonly Beatmap Beatmap;
private Beatmap beatmap;
public Beatmap Beatmap
{
get
{
if (beatmap != null) return beatmap;
try
{
using (var stream = new StreamReader(Reader.ReadFile(beatmapInfo.Path)))
beatmap = BeatmapDecoder.GetDecoder(stream)?.Decode(stream);
}
catch { }
return beatmap;
}
set { beatmap = value; }
}
private AudioTrack track; private AudioTrack track;
public AudioTrack Track public AudioTrack Track
@ -21,7 +44,7 @@ namespace osu.Game.Beatmaps
try try
{ {
var trackData = Reader.ReadFile(Beatmap.Metadata.AudioFile); var trackData = Reader.ReadFile(beatmapInfo.Metadata.AudioFile);
if (trackData != null) if (trackData != null)
track = new AudioTrackBass(trackData); track = new AudioTrackBass(trackData);
} }
@ -32,9 +55,9 @@ namespace osu.Game.Beatmaps
set { track = value; } set { track = value; }
} }
public WorkingBeatmap(Beatmap beatmap, ArchiveReader reader = null) public WorkingBeatmap(BeatmapInfo beatmapInfo = null, ArchiveReader reader = null)
{ {
Beatmap = beatmap; this.beatmapInfo = beatmapInfo;
Reader = reader; Reader = reader;
} }
@ -54,5 +77,11 @@ namespace osu.Game.Beatmaps
{ {
Dispose(true); Dispose(true);
} }
public void TransferTo(WorkingBeatmap working)
{
if (track != null && working.beatmapInfo.Metadata.AudioFile == beatmapInfo.Metadata.AudioFile && working.beatmapInfo.BeatmapSet.Path == beatmapInfo.BeatmapSet.Path)
working.track = track;
}
} }
} }

View File

@ -136,16 +136,26 @@ namespace osu.Game.Database
return Query<BeatmapSetInfo>().Where(s => s.BeatmapSetID == id).FirstOrDefault(); return Query<BeatmapSetInfo>().Where(s => s.BeatmapSetID == id).FirstOrDefault();
} }
public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo) public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null)
{ {
var beatmapSet = Query<BeatmapSetInfo>().Where(s => s.BeatmapSetID == beatmapInfo.BeatmapSetID).FirstOrDefault(); var beatmapSetInfo = Query<BeatmapSetInfo>().FirstOrDefault(s => s.BeatmapSetID == beatmapInfo.BeatmapSetID);
if (beatmapSet == null)
//we need metadata
GetChildren(beatmapSetInfo);
if (beatmapSetInfo == null)
throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetID} is not in the local database."); throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetID} is not in the local database.");
var reader = GetReader(beatmapSet); var reader = GetReader(beatmapSetInfo);
using (var stream = new StreamReader(reader.ReadFile(beatmapInfo.Path))) if (beatmapInfo.Metadata == null)
return new WorkingBeatmap(BeatmapDecoder.GetDecoder(stream)?.Decode(stream), reader); beatmapInfo.Metadata = beatmapSetInfo.Metadata;
var working = new WorkingBeatmap(beatmapInfo, reader);
previous?.TransferTo(working);
return working;
} }
public Beatmap GetBeatmap(BeatmapInfo beatmapInfo) public Beatmap GetBeatmap(BeatmapInfo beatmapInfo)

View File

@ -152,7 +152,6 @@ namespace osu.Game.GameModes.Play
protected override void OnResuming(GameMode last) protected override void OnResuming(GameMode last)
{ {
ensurePlayingSelected(); ensurePlayingSelected();
base.OnResuming(last); base.OnResuming(last);
} }
@ -195,7 +194,7 @@ namespace osu.Game.GameModes.Play
if (!beatmap.Equals(Beatmap?.Beatmap?.BeatmapInfo)) if (!beatmap.Equals(Beatmap?.Beatmap?.BeatmapInfo))
{ {
Beatmap = database.GetWorkingBeatmap(beatmap); Beatmap = database.GetWorkingBeatmap(beatmap, Beatmap);
} }
ensurePlayingSelected(); ensurePlayingSelected();