mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 21:43:04 +08:00
Merge pull request #6396 from bdach/allow-fallback-decoder-overwrite
Allow fallback decoder overwrite
This commit is contained in:
commit
8c3caed2e0
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user