diff --git a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs index b4aaa2fa03..8282d8556a 100644 --- a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs +++ b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs @@ -1,39 +1,39 @@ -using System; -using System.IO; +using System; +using System.IO; using System.Collections.Generic; using System.Linq; using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.IO; using osu.Game.Beatmaps; -namespace osu.Desktop.Beatmaps.IO -{ - /// - /// Reads an extracted legacy beatmap from disk. - /// - public class LegacyFilesystemReader : ArchiveReader - { - static LegacyFilesystemReader() - { - AddReader((storage, path) => Directory.Exists(path)); - } - - private string basePath { get; set; } - private string[] beatmaps { get; set; } +namespace osu.Desktop.Beatmaps.IO +{ + /// + /// Reads an extracted legacy beatmap from disk. + /// + public class LegacyFilesystemReader : ArchiveReader + { + static LegacyFilesystemReader() + { + AddReader((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) - throw new FileNotFoundException(@"This directory contains no beatmaps"); - using (var stream = new StreamReader(ReadFile(beatmaps[0]))) - { - var decoder = BeatmapDecoder.GetDecoder(stream); - firstMap = new Beatmap(); - decoder.Decode(stream, firstMap); - } + + public LegacyFilesystemReader(string path) + { + basePath = path; + beatmaps = Directory.GetFiles(basePath, @"*.osu").Select(f => Path.GetFileName(f)).ToArray(); + if (beatmaps.Length == 0) + throw new FileNotFoundException(@"This directory contains no beatmaps"); + using (var stream = new StreamReader(ReadFile(beatmaps[0]))) + { + var decoder = BeatmapDecoder.GetDecoder(stream); + firstMap = new Beatmap(); + decoder.Decode(stream, firstMap); + } } public override string[] ReadBeatmaps() @@ -49,10 +49,10 @@ public override Stream ReadFile(string name) public override BeatmapMetadata ReadMetadata() { return firstMap.Metadata; - } - + } + public override void Dispose() - { + { // no-op - } } + } } } \ No newline at end of file diff --git a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs index adc78a55bf..adb203b483 100644 --- a/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs +++ b/osu.Game.Tests/Beatmaps/Formats/OsuLegacyDecoderTest.cs @@ -1,6 +1,6 @@ -using System; +using System; using System.IO; -using NUnit.Framework; +using NUnit.Framework; using OpenTK; using OpenTK.Graphics; using osu.Game.Beatmaps; @@ -10,141 +10,141 @@ using osu.Game.GameModes.Play; using osu.Game.Tests.Resources; -namespace osu.Game.Tests.Beatmaps.Formats -{ - [TestFixture] - public class OsuLegacyDecoderTest - { - [TestFixtureSetUp] - public void SetUp() - { - OsuLegacyDecoder.Register(); +namespace osu.Game.Tests.Beatmaps.Formats +{ + [TestFixture] + public class OsuLegacyDecoderTest + { + [TestFixtureSetUp] + public void SetUp() + { + OsuLegacyDecoder.Register(); } - [Test] - public void TestDecodeMetadata() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); - var meta = beatmap.Metadata; - Assert.AreEqual(241526, meta.BeatmapSetID); - Assert.AreEqual("Soleily", meta.Artist); - Assert.AreEqual("Soleily", meta.ArtistUnicode); - Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile); - Assert.AreEqual("Gamu", meta.Author); - Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile); - Assert.AreEqual(164471, meta.PreviewTime); - Assert.AreEqual(string.Empty, meta.Source); - Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags); - Assert.AreEqual("Renatus", meta.Title); - Assert.AreEqual("Renatus", meta.TitleUnicode); - } + [Test] + public void TestDecodeMetadata() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); + var meta = beatmap.Metadata; + Assert.AreEqual(241526, meta.BeatmapSetID); + Assert.AreEqual("Soleily", meta.Artist); + Assert.AreEqual("Soleily", meta.ArtistUnicode); + Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile); + Assert.AreEqual("Gamu", meta.Author); + Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile); + Assert.AreEqual(164471, meta.PreviewTime); + Assert.AreEqual(string.Empty, meta.Source); + Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags); + Assert.AreEqual("Renatus", meta.Title); + Assert.AreEqual("Renatus", meta.TitleUnicode); + } } - [Test] - public void TestDecodeGeneral() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); - Assert.AreEqual(0, beatmap.AudioLeadIn); - Assert.AreEqual(false, beatmap.Countdown); - Assert.AreEqual(SampleSet.Soft, beatmap.SampleSet); - Assert.AreEqual(0.7f, beatmap.StackLeniency); - Assert.AreEqual(false, beatmap.SpecialStyle); - Assert.AreEqual(PlayMode.Osu, beatmap.Mode); - Assert.AreEqual(false, beatmap.LetterboxInBreaks); - Assert.AreEqual(false, beatmap.WidescreenStoryboard); - } + [Test] + public void TestDecodeGeneral() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); + Assert.AreEqual(0, beatmap.AudioLeadIn); + Assert.AreEqual(false, beatmap.Countdown); + Assert.AreEqual(SampleSet.Soft, beatmap.SampleSet); + Assert.AreEqual(0.7f, beatmap.StackLeniency); + Assert.AreEqual(false, beatmap.SpecialStyle); + Assert.AreEqual(PlayMode.Osu, beatmap.Mode); + Assert.AreEqual(false, beatmap.LetterboxInBreaks); + Assert.AreEqual(false, beatmap.WidescreenStoryboard); + } } - [Test] - public void TestDecodeEditor() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); - int[] expectedBookmarks = - { - 11505, 22054, 32604, 43153, 53703, 64252, 74802, 85351, - 95901, 106450, 116999, 119637, 130186, 140735, 151285, - 161834, 164471, 175020, 185570, 196119, 206669, 209306 - }; - Assert.AreEqual(expectedBookmarks.Length, beatmap.Bookmarks.Length); - for (int i = 0; i < expectedBookmarks.Length; i++) - Assert.AreEqual(expectedBookmarks[i], beatmap.Bookmarks[i]); - Assert.AreEqual(1.8, beatmap.DistanceSpacing); - Assert.AreEqual(4, beatmap.BeatDivisor); - Assert.AreEqual(4, beatmap.GridSize); - Assert.AreEqual(2, beatmap.TimelineZoom); - } + [Test] + public void TestDecodeEditor() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); + int[] expectedBookmarks = + { + 11505, 22054, 32604, 43153, 53703, 64252, 74802, 85351, + 95901, 106450, 116999, 119637, 130186, 140735, 151285, + 161834, 164471, 175020, 185570, 196119, 206669, 209306 + }; + Assert.AreEqual(expectedBookmarks.Length, beatmap.Bookmarks.Length); + for (int i = 0; i < expectedBookmarks.Length; i++) + Assert.AreEqual(expectedBookmarks[i], beatmap.Bookmarks[i]); + Assert.AreEqual(1.8, beatmap.DistanceSpacing); + Assert.AreEqual(4, beatmap.BeatDivisor); + Assert.AreEqual(4, beatmap.GridSize); + Assert.AreEqual(2, beatmap.TimelineZoom); + } } - [Test] - public void TestDecodeDifficulty() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); - var difficulty = beatmap.BaseDifficulty; - Assert.AreEqual(6.5f, difficulty.DrainRate); - Assert.AreEqual(4, difficulty.CircleSize); - Assert.AreEqual(8, difficulty.OverallDifficulty); - Assert.AreEqual(9, difficulty.ApproachRate); - Assert.AreEqual(1.8f, difficulty.SliderMultiplier); - Assert.AreEqual(2, difficulty.SliderTickRate); - } + [Test] + public void TestDecodeDifficulty() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); + var difficulty = beatmap.BaseDifficulty; + Assert.AreEqual(6.5f, difficulty.DrainRate); + Assert.AreEqual(4, difficulty.CircleSize); + Assert.AreEqual(8, difficulty.OverallDifficulty); + Assert.AreEqual(9, difficulty.ApproachRate); + Assert.AreEqual(1.8f, difficulty.SliderMultiplier); + Assert.AreEqual(2, difficulty.SliderTickRate); + } } - [Test] - public void TestDecodeColors() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); + [Test] + public void TestDecodeColors() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); Color4[] expected = - { - new Color4(142, 199, 255, 255), - new Color4(255, 128, 128, 255), - new Color4(128, 255, 255, 255), - new Color4(128, 255, 128, 255), - new Color4(255, 187, 255, 255), - new Color4(255, 177, 140, 255), - }; - Assert.AreEqual(expected.Length, beatmap.ComboColors.Count); - for (int i = 0; i < expected.Length; i++) - Assert.AreEqual(expected[i], beatmap.ComboColors[i]); - } + { + new Color4(142, 199, 255, 255), + new Color4(255, 128, 128, 255), + new Color4(128, 255, 255, 255), + new Color4(128, 255, 128, 255), + new Color4(255, 187, 255, 255), + new Color4(255, 177, 140, 255), + }; + Assert.AreEqual(expected.Length, beatmap.ComboColors.Count); + for (int i = 0; i < expected.Length; i++) + Assert.AreEqual(expected[i], beatmap.ComboColors[i]); + } } - - [Test] public void TestDecodeHitObjects() - { - var decoder = new OsuLegacyDecoder(); - using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) - { - Beatmap beatmap = new Beatmap(); - decoder.Decode(new StreamReader(stream), beatmap); - var slider = beatmap.HitObjects[0] as Slider; - Assert.IsNotNull(slider); - Assert.AreEqual(new Vector2(192, 168), slider.Position); - Assert.AreEqual(956, slider.StartTime); - Assert.AreEqual(SampleType.None, slider.Sample.Type); - var circle = beatmap.HitObjects[1] as Circle; - Assert.IsNotNull(circle); - Assert.AreEqual(new Vector2(304, 56), circle.Position); - Assert.AreEqual(1285, circle.StartTime); + + [Test] public void TestDecodeHitObjects() + { + var decoder = new OsuLegacyDecoder(); + using (var stream = Resource.OpenResource("Soleily - Renatus (Gamu) [Insane].osu")) + { + Beatmap beatmap = new Beatmap(); + decoder.Decode(new StreamReader(stream), beatmap); + var slider = beatmap.HitObjects[0] as Slider; + Assert.IsNotNull(slider); + Assert.AreEqual(new Vector2(192, 168), slider.Position); + Assert.AreEqual(956, slider.StartTime); + Assert.AreEqual(SampleType.None, slider.Sample.Type); + var circle = beatmap.HitObjects[1] as Circle; + Assert.IsNotNull(circle); + Assert.AreEqual(new Vector2(304, 56), circle.Position); + Assert.AreEqual(1285, circle.StartTime); Assert.AreEqual(SampleType.Clap, circle.Sample.Type); - } - } - } + } + } + } } \ No newline at end of file diff --git a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs index e224bf3db4..caa56c26c2 100644 --- a/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs +++ b/osu.Game.Tests/Beatmaps/IO/OszArchiveReaderTest.cs @@ -1,81 +1,81 @@ -using System; +using System; using System.IO; using NUnit.Framework; using osu.Game.Beatmaps.IO; using osu.Game.GameModes.Play; using osu.Game.Tests.Resources; -namespace osu.Game.Tests.Beatmaps.IO -{ - [TestFixture] - public class OszArchiveReaderTest - { - [TestFixtureSetUp] - public void SetUp() - { - OszArchiveReader.Register(); - } - - [Test] - public void TestReadBeatmaps() - { - using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) - { - var reader = new OszArchiveReader(osz); - string[] expected = - { - "Soleily - Renatus (Deif) [Platter].osu", - "Soleily - Renatus (Deif) [Rain].osu", - "Soleily - Renatus (Deif) [Salad].osu", - "Soleily - Renatus (ExPew) [Another].osu", - "Soleily - Renatus (ExPew) [Hyper].osu", - "Soleily - Renatus (ExPew) [Normal].osu", - "Soleily - Renatus (Gamu) [Hard].osu", - "Soleily - Renatus (Gamu) [Insane].osu", - "Soleily - Renatus (Gamu) [Normal].osu", - "Soleily - Renatus (MMzz) [Futsuu].osu", - "Soleily - Renatus (MMzz) [Muzukashii].osu", - "Soleily - Renatus (MMzz) [Oni].osu" - }; - var maps = reader.ReadBeatmaps(); - foreach (var map in expected) - Assert.Contains(map, maps); - } +namespace osu.Game.Tests.Beatmaps.IO +{ + [TestFixture] + public class OszArchiveReaderTest + { + [TestFixtureSetUp] + public void SetUp() + { + OszArchiveReader.Register(); + } + + [Test] + public void TestReadBeatmaps() + { + using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) + { + var reader = new OszArchiveReader(osz); + string[] expected = + { + "Soleily - Renatus (Deif) [Platter].osu", + "Soleily - Renatus (Deif) [Rain].osu", + "Soleily - Renatus (Deif) [Salad].osu", + "Soleily - Renatus (ExPew) [Another].osu", + "Soleily - Renatus (ExPew) [Hyper].osu", + "Soleily - Renatus (ExPew) [Normal].osu", + "Soleily - Renatus (Gamu) [Hard].osu", + "Soleily - Renatus (Gamu) [Insane].osu", + "Soleily - Renatus (Gamu) [Normal].osu", + "Soleily - Renatus (MMzz) [Futsuu].osu", + "Soleily - Renatus (MMzz) [Muzukashii].osu", + "Soleily - Renatus (MMzz) [Oni].osu" + }; + var maps = reader.ReadBeatmaps(); + foreach (var map in expected) + Assert.Contains(map, maps); + } } - [Test] - public void TestReadMetadata() - { - using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) - { - var reader = new OszArchiveReader(osz); - var meta = reader.ReadMetadata(); - Assert.AreEqual(241526, meta.BeatmapSetID); - Assert.AreEqual("Soleily", meta.Artist); - Assert.AreEqual("Soleily", meta.ArtistUnicode); - Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile); - Assert.AreEqual("Deif", meta.Author); - Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile); - Assert.AreEqual(164471, meta.PreviewTime); - Assert.AreEqual(string.Empty, meta.Source); - Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags); - Assert.AreEqual("Renatus", meta.Title); - Assert.AreEqual("Renatus", meta.TitleUnicode); - } + [Test] + public void TestReadMetadata() + { + using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) + { + var reader = new OszArchiveReader(osz); + var meta = reader.ReadMetadata(); + Assert.AreEqual(241526, meta.BeatmapSetID); + Assert.AreEqual("Soleily", meta.Artist); + Assert.AreEqual("Soleily", meta.ArtistUnicode); + Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile); + Assert.AreEqual("Deif", meta.Author); + Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile); + Assert.AreEqual(164471, meta.PreviewTime); + Assert.AreEqual(string.Empty, meta.Source); + Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags); + Assert.AreEqual("Renatus", meta.Title); + Assert.AreEqual("Renatus", meta.TitleUnicode); + } } - [Test] - public void TestReadFile() - { - using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) - { - var reader = new OszArchiveReader(osz); - using (var stream = new StreamReader( - reader.ReadFile("Soleily - Renatus (Deif) [Platter].osu"))) - { - Assert.AreEqual("osu file format v13", stream.ReadLine().Trim()); - } - } - } - } -} - + [Test] + public void TestReadFile() + { + using (var osz = Resource.OpenResource("Beatmaps.241526 Soleily - Renatus.osz")) + { + var reader = new OszArchiveReader(osz); + using (var stream = new StreamReader( + reader.ReadFile("Soleily - Renatus (Deif) [Platter].osu"))) + { + Assert.AreEqual("osu file format v13", stream.ReadLine().Trim()); + } + } + } + } +} + diff --git a/osu.Game.Tests/Resources/Resource.cs b/osu.Game.Tests/Resources/Resource.cs index 46c51da5ef..910268adbf 100644 --- a/osu.Game.Tests/Resources/Resource.cs +++ b/osu.Game.Tests/Resources/Resource.cs @@ -1,17 +1,17 @@ -using System; +using System; using System.IO; using System.Reflection; -namespace osu.Game.Tests.Resources -{ - public static class Resource - { - public static Stream OpenResource(string name) - { - return Assembly.GetExecutingAssembly().GetManifestResourceStream( - $@"osu.Game.Tests.Resources.{name}") ?? - Assembly.LoadFrom("osu.Game.Resources.dll").GetManifestResourceStream( - $@"osu.Game.Resources.{name}"); - } - } +namespace osu.Game.Tests.Resources +{ + public static class Resource + { + public static Stream OpenResource(string name) + { + return Assembly.GetExecutingAssembly().GetManifestResourceStream( + $@"osu.Game.Tests.Resources.{name}") ?? + Assembly.LoadFrom("osu.Game.Resources.dll").GetManifestResourceStream( + $@"osu.Game.Resources.{name}"); + } + } } \ No newline at end of file diff --git a/osu.Game/Beatmaps/BaseDifficulty.cs b/osu.Game/Beatmaps/BaseDifficulty.cs index 62acf07695..ce9e3bc624 100644 --- a/osu.Game/Beatmaps/BaseDifficulty.cs +++ b/osu.Game/Beatmaps/BaseDifficulty.cs @@ -1,18 +1,18 @@ -using System; +using System; using SQLite; -namespace osu.Game.Beatmaps -{ - public class BaseDifficulty - { - [PrimaryKey, AutoIncrement] - public int ID { get; set; } - public float DrainRate { get; set; } - public float CircleSize { get; set; } - public float OverallDifficulty { get; set; } - public float ApproachRate { get; set; } - public float SliderMultiplier { get; set; } - public float SliderTickRate { get; set; } - } -} - +namespace osu.Game.Beatmaps +{ + public class BaseDifficulty + { + [PrimaryKey, AutoIncrement] + public int ID { get; set; } + public float DrainRate { get; set; } + public float CircleSize { get; set; } + public float OverallDifficulty { get; set; } + public float ApproachRate { get; set; } + public float SliderMultiplier { get; set; } + public float SliderTickRate { get; set; } + } +} + diff --git a/osu.Game/Beatmaps/Events/EventType.cs b/osu.Game/Beatmaps/Events/EventType.cs index cb66a42f2d..1f7e8d2aa5 100644 --- a/osu.Game/Beatmaps/Events/EventType.cs +++ b/osu.Game/Beatmaps/Events/EventType.cs @@ -1,14 +1,14 @@ -using System; -namespace osu.Game.Beatmaps.Events -{ - public enum EventType - { - Background = 0, - Video = 1, - Break = 2, - Colour = 3, - Sprite = 4, - Sample = 5, - Animation = 6 - } +using System; +namespace osu.Game.Beatmaps.Events +{ + public enum EventType + { + Background = 0, + Video = 1, + Break = 2, + Colour = 3, + Sprite = 4, + Sample = 5, + Animation = 6 + } } \ No newline at end of file diff --git a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs index d643433b3c..0c6bbb02e1 100644 --- a/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs +++ b/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs @@ -1,25 +1,25 @@ -using System; +using System; using System.Collections.Generic; -using System.IO; - -namespace osu.Game.Beatmaps.Formats -{ - public abstract class BeatmapDecoder - { - private static Dictionary decoders { get; } = new Dictionary(); - - public static BeatmapDecoder GetDecoder(TextReader stream) - { - var line = stream.ReadLine().Trim(); - if (!decoders.ContainsKey(line)) - throw new IOException(@"Unknown file format"); - return (BeatmapDecoder)Activator.CreateInstance(decoders[line]); +using System.IO; + +namespace osu.Game.Beatmaps.Formats +{ + public abstract class BeatmapDecoder + { + private static Dictionary decoders { get; } = new Dictionary(); + + public static BeatmapDecoder GetDecoder(TextReader stream) + { + var line = stream.ReadLine().Trim(); + if (!decoders.ContainsKey(line)) + throw new IOException(@"Unknown file format"); + return (BeatmapDecoder)Activator.CreateInstance(decoders[line]); } - protected static void AddDecoder(string magic) where T : BeatmapDecoder - { - decoders[magic] = typeof(T); - } - - public abstract void Decode(TextReader stream, Beatmap beatmap); - } + protected static void AddDecoder(string magic) where T : BeatmapDecoder + { + decoders[magic] = typeof(T); + } + + public abstract void Decode(TextReader stream, Beatmap beatmap); + } } \ No newline at end of file diff --git a/osu.Game/Beatmaps/IO/ArchiveReader.cs b/osu.Game/Beatmaps/IO/ArchiveReader.cs index 77315d4a21..89adf26989 100644 --- a/osu.Game/Beatmaps/IO/ArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/ArchiveReader.cs @@ -1,48 +1,48 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using osu.Framework.Platform; -namespace osu.Game.Beatmaps.IO -{ - public abstract class ArchiveReader : IDisposable - { - private class Reader - { - public Func Test { get; set; } - public Type Type { get; set; } - } - - private static List readers { get; } = new List(); - - public static ArchiveReader GetReader(BasicStorage storage, string path) - { - foreach (var reader in readers) - { - if (reader.Test(storage, path)) - return (ArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(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(); - /// - /// Opens a stream for reading a specific file from this archive. - /// +namespace osu.Game.Beatmaps.IO +{ + public abstract class ArchiveReader : IDisposable + { + private class Reader + { + public Func Test { get; set; } + public Type Type { get; set; } + } + + private static List readers { get; } = new List(); + + public static ArchiveReader GetReader(BasicStorage storage, string path) + { + foreach (var reader in readers) + { + if (reader.Test(storage, path)) + return (ArchiveReader)Activator.CreateInstance(reader.Type, storage.GetStream(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(); + /// + /// Opens a stream for reading a specific file from this archive. + /// public abstract Stream ReadFile(string name); - public abstract void Dispose(); - } + public abstract void Dispose(); + } } \ No newline at end of file diff --git a/osu.Game/Beatmaps/IO/OszArchiveReader.cs b/osu.Game/Beatmaps/IO/OszArchiveReader.cs index 0aa706933e..69776d0095 100644 --- a/osu.Game/Beatmaps/IO/OszArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/OszArchiveReader.cs @@ -1,41 +1,41 @@ -using System; +using System; using System.IO; using System.Linq; using System.Security.Cryptography; using Ionic.Zip; using osu.Game.Beatmaps.Formats; -namespace osu.Game.Beatmaps.IO -{ - public sealed class OszArchiveReader : ArchiveReader - { - public static void Register() - { +namespace osu.Game.Beatmaps.IO +{ + public sealed class OszArchiveReader : ArchiveReader + { + public static void Register() + { AddReader((storage, path) => - { - using (var stream = storage.GetStream(path)) + { + using (var stream = storage.GetStream(path)) return ZipFile.IsZipFile(stream, false); - }); - OsuLegacyDecoder.Register(); - } - - private ZipFile archive { get; set; } - private string[] beatmaps { get; set; } + }); + OsuLegacyDecoder.Register(); + } + + private ZipFile archive { get; set; } + private string[] beatmaps { get; set; } private Beatmap firstMap { get; set; } - - public OszArchiveReader(Stream archiveStream) - { - archive = ZipFile.Read(archiveStream); - beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu")) - .Select(e => e.FileName).ToArray(); - if (beatmaps.Length == 0) - throw new FileNotFoundException(@"This directory contains no beatmaps"); - using (var stream = new StreamReader(ReadFile(beatmaps[0]))) - { - var decoder = BeatmapDecoder.GetDecoder(stream); - firstMap = new Beatmap(); - decoder.Decode(stream, firstMap); - } + + public OszArchiveReader(Stream archiveStream) + { + archive = ZipFile.Read(archiveStream); + beatmaps = archive.Entries.Where(e => e.FileName.EndsWith(@".osu")) + .Select(e => e.FileName).ToArray(); + if (beatmaps.Length == 0) + throw new FileNotFoundException(@"This directory contains no beatmaps"); + using (var stream = new StreamReader(ReadFile(beatmaps[0]))) + { + var decoder = BeatmapDecoder.GetDecoder(stream); + firstMap = new Beatmap(); + decoder.Decode(stream, firstMap); + } } public override string[] ReadBeatmaps() @@ -45,9 +45,9 @@ public override string[] ReadBeatmaps() public override Stream ReadFile(string name) { - ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name); - if (entry == null) - throw new FileNotFoundException(); + ZipEntry entry = archive.Entries.SingleOrDefault(e => e.FileName == name); + if (entry == null) + throw new FileNotFoundException(); return entry.OpenReader(); } @@ -55,9 +55,9 @@ public override BeatmapMetadata ReadMetadata() { return firstMap.Metadata; } - public override void Dispose() - { - archive.Dispose(); + public override void Dispose() + { + archive.Dispose(); } - } + } } \ No newline at end of file diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 4913a51af3..7f8cf86c14 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography; @@ -8,82 +8,82 @@ using osu.Game.Beatmaps.IO; using SQLite; -namespace osu.Game.Database -{ - public class BeatmapDatabase - { - private static SQLiteConnection connection { get; set; } - private BasicStorage storage; - - public BeatmapDatabase(BasicStorage storage) - { - this.storage = storage; - if (connection == null) - { - connection = storage.GetDatabase(@"beatmaps"); - connection.CreateTable(); - connection.CreateTable(); - connection.CreateTable(); - connection.CreateTable(); - } - } - public void AddBeatmap(string path) - { - string hash = null; - ArchiveReader reader; - if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader - { - using (var md5 = MD5.Create()) - using (var input = storage.GetStream(path)) - { - hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant(); - input.Seek(0, SeekOrigin.Begin); - var outputPath = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); - using (var output = storage.GetStream(outputPath, FileAccess.Write)) - input.CopyTo(output); - reader = ArchiveReader.GetReader(storage, path = outputPath); - } - } - else - reader = ArchiveReader.GetReader(storage, path); - var metadata = reader.ReadMetadata(); - if (connection.Table().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) - return; // TODO: Update this beatmap instead - string[] mapNames = reader.ReadBeatmaps(); - var beatmapSet = new BeatmapSet - { - BeatmapSetID = metadata.BeatmapSetID, - Path = path, - Hash = hash, - }; - var maps = new List(); - foreach (var name in mapNames) - { - using (var stream = new StreamReader(reader.ReadFile(name))) - { - var decoder = BeatmapDecoder.GetDecoder(stream); - Beatmap beatmap = new Beatmap(); - decoder.Decode(stream, beatmap); - maps.Add(beatmap); - beatmap.BaseDifficultyID = connection.Insert(beatmap.BaseDifficulty); - } - } - beatmapSet.BeatmapMetadataID = connection.Insert(metadata); - connection.Insert(beatmapSet); - connection.InsertAll(maps); - } - public ArchiveReader GetReader(BeatmapSet beatmapSet) - { - return ArchiveReader.GetReader(storage, beatmapSet.Path); - } - - /// - /// Given a BeatmapSet pulled from the database, loads the rest of its data from disk. - /// public void PopulateBeatmap(BeatmapSet beatmapSet) - { - using (var reader = GetReader(beatmapSet)) +namespace osu.Game.Database +{ + public class BeatmapDatabase + { + private static SQLiteConnection connection { get; set; } + private BasicStorage storage; + + public BeatmapDatabase(BasicStorage storage) + { + this.storage = storage; + if (connection == null) { - string[] mapNames = reader.ReadBeatmaps(); + connection = storage.GetDatabase(@"beatmaps"); + connection.CreateTable(); + connection.CreateTable(); + connection.CreateTable(); + connection.CreateTable(); + } + } + public void AddBeatmap(string path) + { + string hash = null; + ArchiveReader reader; + if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader + { + using (var md5 = MD5.Create()) + using (var input = storage.GetStream(path)) + { + hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant(); + input.Seek(0, SeekOrigin.Begin); + var outputPath = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); + using (var output = storage.GetStream(outputPath, FileAccess.Write)) + input.CopyTo(output); + reader = ArchiveReader.GetReader(storage, path = outputPath); + } + } + else + reader = ArchiveReader.GetReader(storage, path); + var metadata = reader.ReadMetadata(); + if (connection.Table().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) + return; // TODO: Update this beatmap instead + string[] mapNames = reader.ReadBeatmaps(); + var beatmapSet = new BeatmapSet + { + BeatmapSetID = metadata.BeatmapSetID, + Path = path, + Hash = hash, + }; + var maps = new List(); + foreach (var name in mapNames) + { + using (var stream = new StreamReader(reader.ReadFile(name))) + { + var decoder = BeatmapDecoder.GetDecoder(stream); + Beatmap beatmap = new Beatmap(); + decoder.Decode(stream, beatmap); + maps.Add(beatmap); + beatmap.BaseDifficultyID = connection.Insert(beatmap.BaseDifficulty); + } + } + beatmapSet.BeatmapMetadataID = connection.Insert(metadata); + connection.Insert(beatmapSet); + connection.InsertAll(maps); + } + public ArchiveReader GetReader(BeatmapSet beatmapSet) + { + return ArchiveReader.GetReader(storage, beatmapSet.Path); + } + + /// + /// Given a BeatmapSet pulled from the database, loads the rest of its data from disk. + /// public void PopulateBeatmap(BeatmapSet beatmapSet) + { + using (var reader = GetReader(beatmapSet)) + { + string[] mapNames = reader.ReadBeatmaps(); foreach (var name in mapNames) { using (var stream = new StreamReader(reader.ReadFile(name))) @@ -92,9 +92,9 @@ public BeatmapDatabase(BasicStorage storage) Beatmap beatmap = new Beatmap(); decoder.Decode(stream, beatmap); beatmapSet.Beatmaps.Add(beatmap); - } - } - } - } - } + } + } + } + } + } } \ No newline at end of file