1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 22:02:56 +08:00

Load beatmap data from an optional osb file.

This commit is contained in:
Damnae 2017-02-09 15:09:48 +01:00
parent c5cf6a6909
commit a504c73f33
10 changed files with 68 additions and 24 deletions

View File

@ -21,6 +21,7 @@ namespace osu.Desktop.Beatmaps.IO
private string basePath { get; set; }
private string[] beatmaps { get; set; }
private string storyboard { get; set; }
private Beatmap firstMap { get; set; }
public LegacyFilesystemReader(string path)
@ -29,6 +30,7 @@ namespace osu.Desktop.Beatmaps.IO
beatmaps = Directory.GetFiles(basePath, @"*.osu").Select(f => Path.GetFileName(f)).ToArray();
if (beatmaps.Length == 0)
throw new FileNotFoundException(@"This directory contains no beatmaps");
storyboard = Directory.GetFiles(basePath, @"*.osb").Select(f => Path.GetFileName(f)).FirstOrDefault();
using (var stream = new StreamReader(GetStream(beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
@ -41,6 +43,11 @@ namespace osu.Desktop.Beatmaps.IO
return beatmaps;
}
public override string ReadStoryboard()
{
return storyboard;
}
public override Stream GetStream(string name)
{
return File.OpenRead(Path.Combine(basePath, name));

View File

@ -7,6 +7,8 @@ using System.IO;
using osu.Game.Modes.Objects;
using OpenTK.Graphics;
using osu.Game.Graphics;
using osu.Game.Beatmaps.Timing;
using osu.Game.Database;
namespace osu.Game.Beatmaps.Formats
{
@ -34,6 +36,11 @@ namespace osu.Game.Beatmaps.Formats
return b;
}
public virtual void Decode(TextReader stream, Beatmap beatmap)
{
ParseFile(stream, beatmap);
}
public virtual Beatmap Process(Beatmap beatmap)
{
ApplyColours(beatmap);
@ -41,7 +48,23 @@ namespace osu.Game.Beatmaps.Formats
return beatmap;
}
protected abstract Beatmap ParseFile(TextReader stream);
protected virtual Beatmap ParseFile(TextReader stream)
{
var beatmap = new Beatmap
{
HitObjects = new List<HitObject>(),
ControlPoints = new List<ControlPoint>(),
ComboColors = new List<Color4>(),
BeatmapInfo = new BeatmapInfo
{
Metadata = new BeatmapMetadata(),
BaseDifficulty = new BaseDifficulty(),
},
};
ParseFile(stream, beatmap);
return beatmap;
}
protected abstract void ParseFile(TextReader stream, Beatmap beatmap);
public virtual void ApplyColours(Beatmap b)
{

View File

@ -12,7 +12,7 @@ namespace osu.Game.Beatmaps.Formats
{
public class ConstructableBeatmapDecoder : BeatmapDecoder
{
protected override Beatmap ParseFile(TextReader stream)
protected override void ParseFile(TextReader stream, Beatmap beatmap)
{
throw new NotImplementedException();
}

View File

@ -233,20 +233,8 @@ namespace osu.Game.Beatmaps.Formats
});
}
protected override Beatmap ParseFile(TextReader stream)
protected override void ParseFile(TextReader stream, Beatmap beatmap)
{
var beatmap = new Beatmap
{
HitObjects = new List<HitObject>(),
ControlPoints = new List<ControlPoint>(),
ComboColors = new List<Color4>(),
BeatmapInfo = new BeatmapInfo
{
Metadata = new BeatmapMetadata(),
BaseDifficulty = new BaseDifficulty(),
},
};
HitObjectParser parser = null;
var section = Section.None;
@ -309,8 +297,6 @@ namespace osu.Game.Beatmaps.Formats
break;
}
}
return beatmap;
}
}
}

View File

@ -44,6 +44,10 @@ namespace osu.Game.Beatmaps.IO
/// </summary>
public abstract string[] ReadBeatmaps();
/// <summary>
/// Gets the storyboard file name.
/// </summary>
public abstract string ReadStoryboard();
/// <summary>
/// Opens a stream for reading a specific file from this archive.
/// </summary>
public abstract Stream GetStream(string name);

View File

@ -26,8 +26,9 @@ namespace osu.Game.Beatmaps.IO
private Stream archiveStream;
private ZipFile archive;
private string[] beatmaps;
private string storyboard;
private Beatmap firstMap;
public OszArchiveReader(Stream archiveStream)
{
this.archiveStream = archiveStream;
@ -36,6 +37,8 @@ namespace osu.Game.Beatmaps.IO
.Select(e => e.FileName).ToArray();
if (beatmaps.Length == 0)
throw new FileNotFoundException(@"This directory contains no beatmaps");
storyboard = archive.Entries.Where(e => e.FileName.EndsWith(@".osb"))
.Select(e => e.FileName).FirstOrDefault();
using (var stream = new StreamReader(GetStream(beatmaps[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
@ -48,6 +51,11 @@ namespace osu.Game.Beatmaps.IO
return beatmaps;
}
public override string ReadStoryboard()
{
return storyboard;
}
public override Stream GetStream(string name)
{
ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name);

View File

@ -18,6 +18,8 @@ namespace osu.Game.Beatmaps
public readonly BeatmapSetInfo BeatmapSetInfo;
private readonly BeatmapDatabase database;
public readonly bool WithStoryboard;
private ArchiveReader getReader() => database?.GetReader(BeatmapSetInfo);
private Texture background;
@ -58,8 +60,18 @@ namespace osu.Game.Beatmaps
try
{
using (var reader = getReader())
using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path)))
beatmap = BeatmapDecoder.GetDecoder(stream)?.Decode(stream);
{
BeatmapDecoder decoder;
using (var stream = new StreamReader(reader.GetStream(BeatmapInfo.Path)))
{
decoder = BeatmapDecoder.GetDecoder(stream);
beatmap = decoder?.Decode(stream);
}
if (WithStoryboard && beatmap != null && BeatmapSetInfo.StoryboardFile != null)
using (var stream = new StreamReader(reader.GetStream(BeatmapSetInfo.StoryboardFile)))
decoder?.Decode(stream, beatmap);
}
}
catch { }
@ -103,11 +115,12 @@ namespace osu.Game.Beatmaps
this.beatmap = beatmap;
}
public WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, BeatmapDatabase database)
public WorkingBeatmap(BeatmapInfo beatmapInfo, BeatmapSetInfo beatmapSetInfo, BeatmapDatabase database, bool withStoryboard = false)
{
BeatmapInfo = beatmapInfo;
BeatmapSetInfo = beatmapSetInfo;
this.database = database;
this.WithStoryboard = withStoryboard;
}
private bool isDisposed;

View File

@ -137,6 +137,7 @@ namespace osu.Game.Database
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
}
}
beatmapSet.StoryboardFile = reader.ReadStoryboard();
}
Import(new[] { beatmapSet });
@ -169,7 +170,7 @@ namespace osu.Game.Database
return Query<BeatmapSetInfo>().FirstOrDefault(s => s.OnlineBeatmapSetID == id);
}
public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null)
public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null, bool withStoryboard = false)
{
var beatmapSetInfo = Query<BeatmapSetInfo>().FirstOrDefault(s => s.ID == beatmapInfo.BeatmapSetInfoID);
@ -182,7 +183,7 @@ namespace osu.Game.Database
if (beatmapInfo.Metadata == null)
beatmapInfo.Metadata = beatmapSetInfo.Metadata;
var working = new WorkingBeatmap(beatmapInfo, beatmapSetInfo, this);
var working = new WorkingBeatmap(beatmapInfo, beatmapSetInfo, this, withStoryboard);
previous?.TransferTo(working);

View File

@ -27,6 +27,8 @@ namespace osu.Game.Database
public string Hash { get; set; }
public string Path { get; set; }
public string StoryboardFile { get; set; }
}
}

View File

@ -74,7 +74,7 @@ namespace osu.Game.Screens.Play
try
{
if (Beatmap == null)
Beatmap = beatmaps.GetWorkingBeatmap(BeatmapInfo);
Beatmap = beatmaps.GetWorkingBeatmap(BeatmapInfo, withStoryboard: true);
}
catch
{