From a49f3479a2dcc111a821a6891814553443526ec7 Mon Sep 17 00:00:00 2001 From: Aergwyn Date: Fri, 1 Dec 2017 19:11:52 +0100 Subject: [PATCH] Split retrieving of beatmap and storyboard decoder Storyboard works again. Not satisfied with the solution though. --- .../Beatmaps/IO/OszArchiveReaderTest.cs | 2 +- osu.Game/Beatmaps/BeatmapManager.cs | 10 +- osu.Game/Beatmaps/Formats/BeatmapDecoder.cs | 67 ---------- osu.Game/Beatmaps/Formats/Decoder.cs | 120 ++++++++++++++++++ .../Beatmaps/Formats/LegacyBeatmapDecoder.cs | 9 ++ osu.Game/Beatmaps/Formats/LegacyDecoder.cs | 40 ++---- .../Formats/LegacyStoryboardDecoder.cs | 3 - osu.Game/Tests/Visual/TestCasePlayer.cs | 2 +- osu.Game/osu.Game.csproj | 2 +- 9 files changed, 151 insertions(+), 104 deletions(-) delete mode 100644 osu.Game/Beatmaps/Formats/BeatmapDecoder.cs create mode 100644 osu.Game/Beatmaps/Formats/Decoder.cs diff --git a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs index 7b30a4f1fe..22b1d16f7d 100644 --- a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs @@ -50,7 +50,7 @@ namespace osu.Game.Tests.Beatmaps.IO BeatmapMetadata meta; using (var stream = new StreamReader(reader.GetStream("Soleily - Renatus (Deif) [Platter].osu"))) - meta = BeatmapDecoder.GetDecoder(stream).DecodeBeatmap(stream).Metadata; + meta = Decoder.GetBeatmapDecoder(stream).DecodeBeatmap(stream).Metadata; Assert.AreEqual(241526, meta.OnlineBeatmapSetID); Assert.AreEqual("Soleily", meta.Artist); diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index e5f2064ee3..e0d6ac214e 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -495,7 +495,7 @@ namespace osu.Game.Beatmaps BeatmapMetadata metadata; using (var stream = new StreamReader(reader.GetStream(mapName))) - metadata = BeatmapDecoder.GetDecoder(stream).DecodeBeatmap(stream).Metadata; + metadata = Decoder.GetBeatmapDecoder(stream).DecodeBeatmap(stream).Metadata; // check if a set already exists with the same online id. beatmapSet = beatmaps.BeatmapSets.FirstOrDefault(b => b.OnlineBeatmapSetID == metadata.OnlineBeatmapSetID) ?? new BeatmapSetInfo @@ -518,7 +518,7 @@ namespace osu.Game.Beatmaps raw.CopyTo(ms); ms.Position = 0; - var decoder = BeatmapDecoder.GetDecoder(sr); + var decoder = Decoder.GetBeatmapDecoder(sr); Beatmap beatmap = decoder.DecodeBeatmap(sr); beatmap.BeatmapInfo.Path = name; @@ -571,7 +571,7 @@ namespace osu.Game.Beatmaps { using (var stream = new StreamReader(store.GetStream(getPathForFile(BeatmapInfo.Path)))) { - BeatmapDecoder decoder = BeatmapDecoder.GetDecoder(stream); + Decoder decoder = Decoder.GetBeatmapDecoder(stream); return decoder.DecodeBeatmap(stream); } } @@ -620,9 +620,9 @@ namespace osu.Game.Beatmaps try { - BeatmapDecoder decoder; + Decoder decoder; using (var stream = new StreamReader(store.GetStream(getPathForFile(BeatmapInfo.Path)))) - decoder = BeatmapDecoder.GetDecoder(stream); + decoder = Decoder.GetStoryboardDecoder(stream); using (var stream = new StreamReader(store.GetStream(getPathForFile(BeatmapSetInfo.StoryboardFile)))) return decoder.DecodeStoryboard(stream); diff --git a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs deleted file mode 100644 index 1ecc6c0ee0..0000000000 --- a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using System; -using System.Collections.Generic; -using System.IO; -using osu.Game.Storyboards; - -namespace osu.Game.Beatmaps.Formats -{ - public abstract class BeatmapDecoder - { - private static readonly Dictionary decoders = new Dictionary(); - - static BeatmapDecoder() - { - LegacyDecoder.Register(); - } - - public static BeatmapDecoder GetDecoder(StreamReader stream) - { - if (stream == null) - throw new ArgumentNullException(nameof(stream)); - - string line; - do { line = stream.ReadLine()?.Trim(); } - while (line != null && line.Length == 0); - - if (line == null || !decoders.ContainsKey(line)) - throw new IOException(@"Unknown file format"); - return (BeatmapDecoder)Activator.CreateInstance(decoders[line], line); - } - - protected static void AddDecoder(string magic) where T : BeatmapDecoder - { - decoders[magic] = typeof(T); - } - - public virtual Beatmap DecodeBeatmap(StreamReader stream) => ParseBeatmap(stream); - - protected virtual Beatmap ParseBeatmap(StreamReader stream) - { - var beatmap = new Beatmap - { - BeatmapInfo = new BeatmapInfo - { - Metadata = new BeatmapMetadata(), - BaseDifficulty = new BeatmapDifficulty(), - }, - }; - - ParseBeatmap(stream, beatmap); - return beatmap; - } - - protected abstract void ParseBeatmap(StreamReader stream, Beatmap beatmap); - - public virtual Storyboard DecodeStoryboard(StreamReader stream) - { - var storyboard = new Storyboard(); - ParseStoryboard(stream, storyboard); - return storyboard; - } - - protected abstract void ParseStoryboard(StreamReader stream, Storyboard storyboard); - } -} diff --git a/osu.Game/Beatmaps/Formats/Decoder.cs b/osu.Game/Beatmaps/Formats/Decoder.cs new file mode 100644 index 0000000000..75e660bc8d --- /dev/null +++ b/osu.Game/Beatmaps/Formats/Decoder.cs @@ -0,0 +1,120 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Generic; +using System.IO; +using osu.Game.Storyboards; + +namespace osu.Game.Beatmaps.Formats +{ + public abstract class Decoder + { + private static readonly Dictionary beatmapDecoders = new Dictionary(); + private static readonly Dictionary storyboardDecoders = new Dictionary(); + + static Decoder() + { + LegacyDecoder.Register(); + } + + /// + /// Retrieves a to parse s. + /// + /// A stream pointing to the to retrieve the version from. + public static Decoder GetBeatmapDecoder(StreamReader stream) + { + string line = readFirstLine(stream); + + if (line == null || !beatmapDecoders.ContainsKey(line)) + throw new IOException(@"Unknown file format"); + return (Decoder)Activator.CreateInstance(beatmapDecoders[line], line); + } + + /// + /// Retrieves a to parse s. + /// + /// A stream pointing to the to retrieve the version from. + public static Decoder GetStoryboardDecoder(StreamReader stream) + { + string line = readFirstLine(stream); + + if (line == null || !storyboardDecoders.ContainsKey(line)) + throw new IOException(@"Unknown file format"); + return (Decoder)Activator.CreateInstance(storyboardDecoders[line], line); + } + + private static string readFirstLine(StreamReader stream) + { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + + string line; + do + { line = stream.ReadLine()?.Trim(); } + while (line != null && line.Length == 0); + + return line; + } + + /// + /// Adds the to the list of and decoder. + /// + /// Type to decode a with. + /// /// Type to decode a with. + /// A string representation of the version. + protected static void AddDecoder(string version) where A : Decoder where B : Decoder + { + beatmapDecoders[version] = typeof(A); + storyboardDecoders[version] = typeof(B); + } + + /// + /// Adds the to the list of decoder. + /// + /// Type to decode a with. + /// A string representation of the version. + protected static void AddBeatmapDecoder(string version) where T : Decoder + { + beatmapDecoders[version] = typeof(T); + } + + /// + /// Adds the to the list of decoder. + /// + /// Type to decode a with. + /// A string representation of the version. + protected static void AddStoryboardDecoder(string version) where T : Decoder + { + storyboardDecoders[version] = typeof(T); + } + + public virtual Beatmap DecodeBeatmap(StreamReader stream) => ParseBeatmap(stream); + + protected virtual Beatmap ParseBeatmap(StreamReader stream) + { + var beatmap = new Beatmap + { + BeatmapInfo = new BeatmapInfo + { + Metadata = new BeatmapMetadata(), + BaseDifficulty = new BeatmapDifficulty(), + }, + }; + + ParseBeatmap(stream, beatmap); + return beatmap; + } + + protected abstract void ParseBeatmap(StreamReader stream, Beatmap beatmap); + + public virtual Storyboard DecodeStoryboard(StreamReader stream) + { + var storyboard = new Storyboard(); + ParseStoryboard(stream, storyboard); + return storyboard; + } + + protected abstract void ParseStoryboard(StreamReader stream, Storyboard storyboard); + } +} diff --git a/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs index 61e9bbc11d..17215a935c 100644 --- a/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs +++ b/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs @@ -20,6 +20,15 @@ namespace osu.Game.Beatmaps.Formats private LegacySampleBank defaultSampleBank; private int defaultSampleVolume = 100; + public LegacyBeatmapDecoder() + { + } + + public LegacyBeatmapDecoder(string header) + { + BeatmapVersion = int.Parse(header.Substring(17)); + } + protected override void ProcessSection(Section section, string line) { switch (section) diff --git a/osu.Game/Beatmaps/Formats/LegacyDecoder.cs b/osu.Game/Beatmaps/Formats/LegacyDecoder.cs index 249b106962..b060de5120 100644 --- a/osu.Game/Beatmaps/Formats/LegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/LegacyDecoder.cs @@ -9,34 +9,25 @@ using osu.Game.Storyboards; namespace osu.Game.Beatmaps.Formats { - public class LegacyDecoder : BeatmapDecoder + public abstract class LegacyDecoder : Decoder { public static void Register() { - AddDecoder(@"osu file format v14"); - AddDecoder(@"osu file format v13"); - AddDecoder(@"osu file format v12"); - AddDecoder(@"osu file format v11"); - AddDecoder(@"osu file format v10"); - AddDecoder(@"osu file format v9"); - AddDecoder(@"osu file format v8"); - AddDecoder(@"osu file format v7"); - AddDecoder(@"osu file format v6"); - AddDecoder(@"osu file format v5"); - AddDecoder(@"osu file format v4"); - AddDecoder(@"osu file format v3"); + AddDecoder(@"osu file format v14"); + AddDecoder(@"osu file format v13"); + AddDecoder(@"osu file format v12"); + AddDecoder(@"osu file format v11"); + AddDecoder(@"osu file format v10"); + AddDecoder(@"osu file format v9"); + AddDecoder(@"osu file format v8"); + AddDecoder(@"osu file format v7"); + AddDecoder(@"osu file format v6"); + AddDecoder(@"osu file format v5"); + AddDecoder(@"osu file format v4"); + AddDecoder(@"osu file format v3"); // TODO: differences between versions } - public LegacyDecoder() - { - } - - public LegacyDecoder(string header) - { - BeatmapVersion = int.Parse(header.Substring(17)); - } - protected Beatmap Beatmap; protected Storyboard Storyboard; @@ -105,10 +96,7 @@ namespace osu.Game.Beatmaps.Formats } } - protected virtual void ProcessSection(Section section, string line) - { - - } + protected abstract void ProcessSection(Section section, string line); /// /// Decodes any beatmap variables present in a line into their real values. diff --git a/osu.Game/Beatmaps/Formats/LegacyStoryboardDecoder.cs b/osu.Game/Beatmaps/Formats/LegacyStoryboardDecoder.cs index 576eaead11..317f99b9d5 100644 --- a/osu.Game/Beatmaps/Formats/LegacyStoryboardDecoder.cs +++ b/osu.Game/Beatmaps/Formats/LegacyStoryboardDecoder.cs @@ -4,9 +4,6 @@ using System; using System.Globalization; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using OpenTK; using OpenTK.Graphics; using osu.Framework.Graphics; diff --git a/osu.Game/Tests/Visual/TestCasePlayer.cs b/osu.Game/Tests/Visual/TestCasePlayer.cs index d17f20ff2f..bca2cc02d0 100644 --- a/osu.Game/Tests/Visual/TestCasePlayer.cs +++ b/osu.Game/Tests/Visual/TestCasePlayer.cs @@ -63,7 +63,7 @@ namespace osu.Game.Tests.Visual using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(test_beatmap_data))) using (var reader = new StreamReader(stream)) - beatmap = BeatmapDecoder.GetDecoder(reader).DecodeBeatmap(reader); + beatmap = Game.Beatmaps.Formats.Decoder.GetBeatmapDecoder(reader).DecodeBeatmap(reader); return beatmap; } diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 4c33b2d266..1b1576b4c3 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -313,7 +313,7 @@ - +