Skip to content

Commit

Permalink
Merge pull request #339 from Damnae/osb_loading
Browse files Browse the repository at this point in the history
Load beatmap data from an optional osb file.
  • Loading branch information
peppy authored Feb 13, 2017
2 parents c86701f + 11643d2 commit bc95666
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 48 deletions.
13 changes: 4 additions & 9 deletions osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,22 @@ public class LegacyFilesystemReader : ArchiveReader
public static void Register() => AddReader<LegacyFilesystemReader>((storage, path) => Directory.Exists(path));

private string basePath { get; set; }
private string[] beatmaps { get; set; }
private Beatmap firstMap { get; set; }

public LegacyFilesystemReader(string path)
{
basePath = path;
beatmaps = Directory.GetFiles(basePath, @"*.osu").Select(f => Path.GetFileName(f)).ToArray();
if (beatmaps.Length == 0)
BeatmapFilenames = Directory.GetFiles(basePath, @"*.osu").Select(f => Path.GetFileName(f)).ToArray();
if (BeatmapFilenames.Length == 0)
throw new FileNotFoundException(@"This directory contains no beatmaps");
using (var stream = new StreamReader(GetStream(beatmaps[0])))
StoryboardFilename = Directory.GetFiles(basePath, @"*.osb").Select(f => Path.GetFileName(f)).FirstOrDefault();
using (var stream = new StreamReader(GetStream(BeatmapFilenames[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
firstMap = decoder.Decode(stream);
}
}

public override string[] ReadBeatmaps()
{
return beatmaps;
}

public override Stream GetStream(string name)
{
return File.OpenRead(Path.Combine(basePath, name));
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void TestReadBeatmaps()
"Soleily - Renatus (MMzz) [Muzukashii].osu",
"Soleily - Renatus (MMzz) [Oni].osu"
};
var maps = reader.ReadBeatmaps();
var maps = reader.BeatmapFilenames;
foreach (var map in expected)
Assert.Contains(map, maps);
}
Expand Down
25 changes: 24 additions & 1 deletion osu.Game/Beatmaps/Formats/BeatmapDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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
{
Expand Down Expand Up @@ -34,14 +36,35 @@ public virtual Beatmap Decode(TextReader stream)
return b;
}

public virtual void Decode(TextReader stream, Beatmap beatmap)
{
ParseFile(stream, beatmap);
}

public virtual Beatmap Process(Beatmap beatmap)
{
ApplyColours(beatmap);

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)
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Beatmaps/Formats/ConstructableBeatmapDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
16 changes: 1 addition & 15 deletions osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,8 @@ private void handleColours(Beatmap beatmap, string key, string val)
});
}

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;
Expand Down Expand Up @@ -309,8 +297,6 @@ protected override Beatmap ParseFile(TextReader stream)
break;
}
}

return beatmap;
}
}
}
15 changes: 11 additions & 4 deletions osu.Game/Beatmaps/IO/ArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private class Reader
}

private static List<Reader> readers { get; } = new List<Reader>();

public static ArchiveReader GetReader(BasicStorage storage, string path)
{
foreach (var reader in readers)
Expand All @@ -29,20 +29,27 @@ public static ArchiveReader GetReader(BasicStorage storage, string path)
}
throw new IOException(@"Unknown file format");
}

protected static void AddReader<T>(Func<BasicStorage, string, bool> test) where T : ArchiveReader
{
readers.Add(new Reader { Test = test, Type = typeof(T) });
}

/// <summary>
/// Reads the beatmap metadata from this archive.
/// </summary>
public abstract BeatmapMetadata ReadMetadata();

/// <summary>
/// Gets a list of beatmap file names.
/// </summary>
public abstract string[] ReadBeatmaps();
public string[] BeatmapFilenames { get; protected set; }

/// <summary>
/// The storyboard filename. Null if no storyboard is present.
/// </summary>
public string StoryboardFilename { get; protected set; }

/// <summary>
/// Opens a stream for reading a specific file from this archive.
/// </summary>
Expand Down
16 changes: 6 additions & 10 deletions osu.Game/Beatmaps/IO/OszArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,25 @@ public static void Register()

private Stream archiveStream;
private ZipFile archive;
private string[] beatmaps;
private Beatmap firstMap;

public OszArchiveReader(Stream archiveStream)
{
this.archiveStream = archiveStream;
archive = ZipFile.Read(archiveStream);
beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
BeatmapFilenames = archive.Entries.Where(e => e.FileName.EndsWith(@".osu"))
.Select(e => e.FileName).ToArray();
if (beatmaps.Length == 0)
if (BeatmapFilenames.Length == 0)
throw new FileNotFoundException(@"This directory contains no beatmaps");
using (var stream = new StreamReader(GetStream(beatmaps[0])))
StoryboardFilename = archive.Entries.Where(e => e.FileName.EndsWith(@".osb"))
.Select(e => e.FileName).FirstOrDefault();
using (var stream = new StreamReader(GetStream(BeatmapFilenames[0])))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
firstMap = decoder.Decode(stream);
}
}

public override string[] ReadBeatmaps()
{
return beatmaps;
}

public override Stream GetStream(string name)
{
ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name);
Expand Down
20 changes: 17 additions & 3 deletions osu.Game/Beatmaps/WorkingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class WorkingBeatmap : IDisposable
public readonly BeatmapSetInfo BeatmapSetInfo;
private readonly BeatmapDatabase database;

public readonly bool WithStoryboard;

private ArchiveReader getReader() => database?.GetReader(BeatmapSetInfo);

private Texture background;
Expand Down Expand Up @@ -58,8 +60,19 @@ public Beatmap Beatmap
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 { }

Expand Down Expand Up @@ -103,11 +116,12 @@ public WorkingBeatmap(Beatmap beatmap)
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;
Expand Down
7 changes: 4 additions & 3 deletions osu.Game/Database/BeatmapDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void Import(string path)

using (var reader = ArchiveReader.GetReader(storage, path))
{
string[] mapNames = reader.ReadBeatmaps();
string[] mapNames = reader.BeatmapFilenames;
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.GetStream(name)))
Expand All @@ -139,6 +139,7 @@ public void Import(string path)

beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
}
beatmapSet.StoryboardFile = reader.StoryboardFilename;
}
}

Expand Down Expand Up @@ -171,7 +172,7 @@ public BeatmapSetInfo GetBeatmapSet(int id)
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);

Expand All @@ -184,7 +185,7 @@ public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap
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);

Expand Down
2 changes: 2 additions & 0 deletions osu.Game/Database/BeatmapSetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class BeatmapSetInfo
public string Hash { get; set; }

public string Path { get; set; }

public string StoryboardFile { get; set; }
}
}

2 changes: 1 addition & 1 deletion osu.Game/Screens/Play/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void load(AudioManager audio, BeatmapDatabase beatmaps, OsuGameBase game
try
{
if (Beatmap == null)
Beatmap = beatmaps.GetWorkingBeatmap(BeatmapInfo);
Beatmap = beatmaps.GetWorkingBeatmap(BeatmapInfo, withStoryboard: true);
}
catch
{
Expand Down

0 comments on commit bc95666

Please sign in to comment.