mirror of
https://github.com/osukey/osukey.git
synced 2025-07-01 16:29:58 +09:00
Implement fallback decoder registration
After the preparatory introduction of LineBufferedReader, it is now possible to introduce registration of fallback decoders that won't drop input supplied in the first line of the file. A fallback decoder is used when the magic in the first line of the file does not match any of the other known decoders. In such a case, the fallback decoder is constructed and provided a LineBufferedReader instance. The process of matching magic only peeks the first non-empty line, so it is available for re-reading in Decode() using ReadLine(). There can be only one fallback decoder per type; a second attempt of registering a fallback will result in an exception to avoid bugs. To address the issue of parsing failing on badly or non-headered files, set the legacy decoders for Beatmaps and Storyboards as the fallbacks. Due to non-trivial logic, several new, passing unit tests with possible edge cases also included.
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using osuTK;
|
||||
using osuTK.Graphics;
|
||||
@ -490,5 +491,105 @@ namespace osu.Game.Tests.Beatmaps.Formats
|
||||
Assert.DoesNotThrow(() => decoder.Decode(badStream));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFallbackDecoderForCorruptedHeader()
|
||||
{
|
||||
Decoder<Beatmap> decoder = null;
|
||||
Beatmap beatmap = null;
|
||||
|
||||
using (var resStream = TestResources.OpenResource("corrupted-header.osu"))
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
|
||||
Assert.IsInstanceOf<LegacyBeatmapDecoder>(decoder);
|
||||
Assert.DoesNotThrow(() => beatmap = decoder.Decode(stream));
|
||||
Assert.IsNotNull(beatmap);
|
||||
Assert.AreEqual("Beatmap with corrupted header", beatmap.Metadata.Title);
|
||||
Assert.AreEqual("Evil Hacker", beatmap.Metadata.AuthorString);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFallbackDecoderForMissingHeader()
|
||||
{
|
||||
Decoder<Beatmap> decoder = null;
|
||||
Beatmap beatmap = null;
|
||||
|
||||
using (var resStream = TestResources.OpenResource("missing-header.osu"))
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
|
||||
Assert.IsInstanceOf<LegacyBeatmapDecoder>(decoder);
|
||||
Assert.DoesNotThrow(() => beatmap = decoder.Decode(stream));
|
||||
Assert.IsNotNull(beatmap);
|
||||
Assert.AreEqual("Beatmap with no header", beatmap.Metadata.Title);
|
||||
Assert.AreEqual("Incredibly Evil Hacker", beatmap.Metadata.AuthorString);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDecodeFileWithEmptyLinesAtStart()
|
||||
{
|
||||
Decoder<Beatmap> decoder = null;
|
||||
Beatmap beatmap = null;
|
||||
|
||||
using (var resStream = TestResources.OpenResource("empty-lines-at-start.osu"))
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
|
||||
Assert.IsInstanceOf<LegacyBeatmapDecoder>(decoder);
|
||||
Assert.DoesNotThrow(() => beatmap = decoder.Decode(stream));
|
||||
Assert.IsNotNull(beatmap);
|
||||
Assert.AreEqual("Empty lines at start", beatmap.Metadata.Title);
|
||||
Assert.AreEqual("Edge Case Hunter", beatmap.Metadata.AuthorString);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDecodeFileWithEmptyLinesAndNoHeader()
|
||||
{
|
||||
Decoder<Beatmap> decoder = null;
|
||||
Beatmap beatmap = null;
|
||||
|
||||
using (var resStream = TestResources.OpenResource("empty-line-instead-of-header.osu"))
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
|
||||
Assert.IsInstanceOf<LegacyBeatmapDecoder>(decoder);
|
||||
Assert.DoesNotThrow(() => beatmap = decoder.Decode(stream));
|
||||
Assert.IsNotNull(beatmap);
|
||||
Assert.AreEqual("The dog ate the file header", beatmap.Metadata.Title);
|
||||
Assert.AreEqual("Why does this keep happening", beatmap.Metadata.AuthorString);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDecodeFileWithContentImmediatelyAfterHeader()
|
||||
{
|
||||
Decoder<Beatmap> decoder = null;
|
||||
Beatmap beatmap = null;
|
||||
|
||||
using (var resStream = TestResources.OpenResource("no-empty-line-after-header.osu"))
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
|
||||
Assert.IsInstanceOf<LegacyBeatmapDecoder>(decoder);
|
||||
Assert.DoesNotThrow(() => beatmap = decoder.Decode(stream));
|
||||
Assert.IsNotNull(beatmap);
|
||||
Assert.AreEqual("No empty line delimiting header from contents", beatmap.Metadata.Title);
|
||||
Assert.AreEqual("Edge Case Hunter", beatmap.Metadata.AuthorString);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDecodeEmptyFile()
|
||||
{
|
||||
using (var resStream = new MemoryStream())
|
||||
using (var stream = new LineBufferedReader(resStream))
|
||||
{
|
||||
Assert.Throws<IOException>(() => Decoder.GetDecoder<Beatmap>(stream));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user