From a504c73f33fd340f35a018448fc657fb916d3612 Mon Sep 17 00:00:00 2001 From: Damnae Date: Thu, 9 Feb 2017 15:09:48 +0100 Subject: [PATCH 1/2] Load beatmap data from an optional osb file. --- .../Beatmaps/IO/LegacyFilesystemReader.cs | 7 ++++++ osu.Game/Beatmaps/Formats/BeatmapDecoder.cs | 25 ++++++++++++++++++- .../Formats/ConstructableBeatmapDecoder.cs | 2 +- osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs | 16 +----------- osu.Game/Beatmaps/IO/ArchiveReader.cs | 4 +++ osu.Game/Beatmaps/IO/OszArchiveReader.cs | 10 +++++++- osu.Game/Beatmaps/WorkingBeatmap.cs | 19 +++++++++++--- osu.Game/Database/BeatmapDatabase.cs | 5 ++-- osu.Game/Database/BeatmapSetInfo.cs | 2 ++ osu.Game/Screens/Play/Player.cs | 2 +- 10 files changed, 68 insertions(+), 24 deletions(-) diff --git a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs index 9b699a3d76ea..420141e9a196 100644 --- a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs +++ b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs @@ -21,6 +21,7 @@ public class LegacyFilesystemReader : ArchiveReader 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 @@ public LegacyFilesystemReader(string path) 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 @@ public override string[] ReadBeatmaps() return beatmaps; } + public override string ReadStoryboard() + { + return storyboard; + } + public override Stream GetStream(string name) { return File.OpenRead(Path.Combine(basePath, name)); diff --git a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs index ba99b206b59b..4846cf376dba 100644 --- a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs +++ b/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs @@ -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 { @@ -34,6 +36,11 @@ 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); @@ -41,7 +48,23 @@ public virtual Beatmap Process(Beatmap beatmap) return beatmap; } - protected abstract Beatmap ParseFile(TextReader stream); + protected virtual Beatmap ParseFile(TextReader stream) + { + var beatmap = new Beatmap + { + HitObjects = new List(), + ControlPoints = new List(), + ComboColors = new List(), + 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) { diff --git a/osu.Game/Beatmaps/Formats/ConstructableBeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/ConstructableBeatmapDecoder.cs index 6f81cae00f4f..f80c673e8950 100644 --- a/osu.Game/Beatmaps/Formats/ConstructableBeatmapDecoder.cs +++ b/osu.Game/Beatmaps/Formats/ConstructableBeatmapDecoder.cs @@ -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(); } diff --git a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs index 29bb7ca32b3e..c32c3fe2f18b 100644 --- a/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs @@ -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(), - ControlPoints = new List(), - ComboColors = new List(), - BeatmapInfo = new BeatmapInfo - { - Metadata = new BeatmapMetadata(), - BaseDifficulty = new BaseDifficulty(), - }, - }; - HitObjectParser parser = null; var section = Section.None; @@ -309,8 +297,6 @@ protected override Beatmap ParseFile(TextReader stream) break; } } - - return beatmap; } } } diff --git a/osu.Game/Beatmaps/IO/ArchiveReader.cs b/osu.Game/Beatmaps/IO/ArchiveReader.cs index 8ae495a1fe98..a944e7589a52 100644 --- a/osu.Game/Beatmaps/IO/ArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/ArchiveReader.cs @@ -44,6 +44,10 @@ protected static void AddReader(Func test) where /// public abstract string[] ReadBeatmaps(); /// + /// Gets the storyboard file name. + /// + public abstract string ReadStoryboard(); + /// /// Opens a stream for reading a specific file from this archive. /// public abstract Stream GetStream(string name); diff --git a/osu.Game/Beatmaps/IO/OszArchiveReader.cs b/osu.Game/Beatmaps/IO/OszArchiveReader.cs index 4b3580537374..f9b7fb2cf8e7 100644 --- a/osu.Game/Beatmaps/IO/OszArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/OszArchiveReader.cs @@ -26,8 +26,9 @@ public static void Register() 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 @@ public OszArchiveReader(Stream archiveStream) .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 @@ public override string[] ReadBeatmaps() return beatmaps; } + public override string ReadStoryboard() + { + return storyboard; + } + public override Stream GetStream(string name) { ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name); diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 019674c3adc5..674ab76802f6 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -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; @@ -58,8 +60,18 @@ 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 { } @@ -103,11 +115,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; diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index dff76479e80a..1b678b21483f 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -137,6 +137,7 @@ public void Import(params string[] paths) beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } } + beatmapSet.StoryboardFile = reader.ReadStoryboard(); } Import(new[] { beatmapSet }); @@ -169,7 +170,7 @@ public BeatmapSetInfo GetBeatmapSet(int id) return Query().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().FirstOrDefault(s => s.ID == beatmapInfo.BeatmapSetInfoID); @@ -182,7 +183,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); diff --git a/osu.Game/Database/BeatmapSetInfo.cs b/osu.Game/Database/BeatmapSetInfo.cs index f556c3546a6e..a9c3f03a499f 100644 --- a/osu.Game/Database/BeatmapSetInfo.cs +++ b/osu.Game/Database/BeatmapSetInfo.cs @@ -27,6 +27,8 @@ public class BeatmapSetInfo public string Hash { get; set; } public string Path { get; set; } + + public string StoryboardFile { get; set; } } } diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs index a9584a1f1074..36ea7c5b4990 100644 --- a/osu.Game/Screens/Play/Player.cs +++ b/osu.Game/Screens/Play/Player.cs @@ -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 { From 11643d2e0954d25fed7881a22a08ee41416decbe Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 13 Feb 2017 18:30:47 +0900 Subject: [PATCH 2/2] Remove unnecessary methods and local variables. --- .../Beatmaps/IO/LegacyFilesystemReader.cs | 20 ++++--------------- .../Beatmaps/IO/OszArchiveReaderTest.cs | 2 +- osu.Game/Beatmaps/IO/ArchiveReader.cs | 15 ++++++++------ osu.Game/Beatmaps/IO/OszArchiveReader.cs | 20 ++++--------------- osu.Game/Beatmaps/WorkingBeatmap.cs | 1 + osu.Game/Database/BeatmapDatabase.cs | 4 ++-- 6 files changed, 21 insertions(+), 41 deletions(-) diff --git a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs index 420141e9a196..132f14ddb5e3 100644 --- a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs +++ b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs @@ -20,34 +20,22 @@ public class LegacyFilesystemReader : ArchiveReader public static void Register() => AddReader((storage, path) => Directory.Exists(path)); private string basePath { get; set; } - private string[] beatmaps { get; set; } - private string storyboard { 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"); - storyboard = Directory.GetFiles(basePath, @"*.osb").Select(f => Path.GetFileName(f)).FirstOrDefault(); - 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 string ReadStoryboard() - { - return storyboard; - } - public override Stream GetStream(string name) { return File.OpenRead(Path.Combine(basePath, name)); diff --git a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs index a09d0c2f86ec..037c0185b81d 100644 --- a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs @@ -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); } diff --git a/osu.Game/Beatmaps/IO/ArchiveReader.cs b/osu.Game/Beatmaps/IO/ArchiveReader.cs index a944e7589a52..cb5f93693933 100644 --- a/osu.Game/Beatmaps/IO/ArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/ArchiveReader.cs @@ -19,7 +19,7 @@ private class Reader } private static List readers { get; } = new List(); - + public static ArchiveReader GetReader(BasicStorage storage, string path) { foreach (var reader in readers) @@ -29,24 +29,27 @@ public static ArchiveReader GetReader(BasicStorage storage, string path) } throw new IOException(@"Unknown file format"); } - + protected static void AddReader(Func test) where T : ArchiveReader { readers.Add(new Reader { Test = test, Type = typeof(T) }); } - + /// /// Reads the beatmap metadata from this archive. /// public abstract BeatmapMetadata ReadMetadata(); + /// /// Gets a list of beatmap file names. /// - public abstract string[] ReadBeatmaps(); + public string[] BeatmapFilenames { get; protected set; } + /// - /// Gets the storyboard file name. + /// The storyboard filename. Null if no storyboard is present. /// - public abstract string ReadStoryboard(); + public string StoryboardFilename { get; protected set; } + /// /// Opens a stream for reading a specific file from this archive. /// diff --git a/osu.Game/Beatmaps/IO/OszArchiveReader.cs b/osu.Game/Beatmaps/IO/OszArchiveReader.cs index f9b7fb2cf8e7..273e0c1fb9d6 100644 --- a/osu.Game/Beatmaps/IO/OszArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/OszArchiveReader.cs @@ -25,37 +25,25 @@ public static void Register() private Stream archiveStream; private ZipFile archive; - private string[] beatmaps; - private string storyboard; 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"); - storyboard = archive.Entries.Where(e => e.FileName.EndsWith(@".osb")) + StoryboardFilename = archive.Entries.Where(e => e.FileName.EndsWith(@".osb")) .Select(e => e.FileName).FirstOrDefault(); - using (var stream = new StreamReader(GetStream(beatmaps[0]))) + 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 string ReadStoryboard() - { - return storyboard; - } - public override Stream GetStream(string name) { ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name); diff --git a/osu.Game/Beatmaps/WorkingBeatmap.cs b/osu.Game/Beatmaps/WorkingBeatmap.cs index 7f1cf236ac9b..62223ec87925 100644 --- a/osu.Game/Beatmaps/WorkingBeatmap.cs +++ b/osu.Game/Beatmaps/WorkingBeatmap.cs @@ -68,6 +68,7 @@ public Beatmap Beatmap beatmap = decoder?.Decode(stream); } + if (WithStoryboard && beatmap != null && BeatmapSetInfo.StoryboardFile != null) using (var stream = new StreamReader(reader.GetStream(BeatmapSetInfo.StoryboardFile))) decoder?.Decode(stream, beatmap); diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index a81c1fe55235..9c7e70f8ebf2 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -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))) @@ -139,7 +139,7 @@ public void Import(string path) beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo); } - beatmapSet.StoryboardFile = reader.ReadStoryboard(); + beatmapSet.StoryboardFile = reader.StoryboardFilename; } }