1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 04:07:25 +08:00

Merge pull request #6396 from bdach/allow-fallback-decoder-overwrite

Allow fallback decoder overwrite
This commit is contained in:
Dean Herbert 2019-10-05 11:08:41 +08:00 committed by GitHub
commit 8c3caed2e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -591,5 +591,27 @@ namespace osu.Game.Tests.Beatmaps.Formats
Assert.Throws<IOException>(() => Decoder.GetDecoder<Beatmap>(stream)); Assert.Throws<IOException>(() => Decoder.GetDecoder<Beatmap>(stream));
} }
} }
[Test]
public void TestAllowFallbackDecoderOverwrite()
{
Decoder<Beatmap> decoder = 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(LegacyDifficultyCalculatorBeatmapDecoder.Register);
using (var resStream = TestResources.OpenResource("corrupted-header.osu"))
using (var stream = new LineBufferedReader(resStream))
{
Assert.DoesNotThrow(() => decoder = Decoder.GetDecoder<Beatmap>(stream));
Assert.IsInstanceOf<LegacyDifficultyCalculatorBeatmapDecoder>(decoder);
}
}
} }
} }

View File

@ -93,14 +93,12 @@ namespace osu.Game.Beatmaps.Formats
/// <summary> /// <summary>
/// Registers a fallback decoder instantiation function. /// Registers a fallback decoder instantiation function.
/// The fallback will be returned if the first non-empty line of the decoded stream does not match any known magic. /// The fallback will be returned if the first non-empty line of the decoded stream does not match any known magic.
/// Calling this method will overwrite any existing global fallback registration for type <see cref="T"/> - use with caution.
/// </summary> /// </summary>
/// <typeparam name="T">Type of object being decoded.</typeparam> /// <typeparam name="T">Type of object being decoded.</typeparam>
/// <param name="constructor">A function that constructs the fallback<see cref="Decoder"/>.</param> /// <param name="constructor">A function that constructs the fallback<see cref="Decoder"/>.</param>
protected static void SetFallbackDecoder<T>(Func<Decoder> constructor) protected static void SetFallbackDecoder<T>(Func<Decoder> constructor)
{ {
if (fallback_decoders.ContainsKey(typeof(T)))
throw new InvalidOperationException($"A fallback decoder was already added for type {typeof(T)}.");
fallback_decoders[typeof(T)] = constructor; fallback_decoders[typeof(T)] = constructor;
} }
} }