1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 09:02:58 +08:00

Rework registration/instantiation of decoders to not rely on reflection

This commit is contained in:
smoogipoo 2017-12-21 15:34:00 +09:00
parent 22f8853f49
commit 58859f2ffb
3 changed files with 22 additions and 20 deletions

View File

@ -10,11 +10,12 @@ namespace osu.Game.Beatmaps.Formats
{
public abstract class Decoder
{
private static readonly Dictionary<string, Type> decoders = new Dictionary<string, Type>();
private static readonly Dictionary<string, Func<string, Decoder>> decoders = new Dictionary<string, Func<string, Decoder>>();
static Decoder()
{
LegacyDecoder.Register();
JsonBeatmapDecoder.Register();
}
/// <summary>
@ -33,17 +34,18 @@ namespace osu.Game.Beatmaps.Formats
if (line == null || !decoders.ContainsKey(line))
throw new IOException(@"Unknown file format");
return (Decoder)Activator.CreateInstance(decoders[line], line);
return decoders[line](line);
}
/// <summary>
/// Adds the <see cref="Decoder"/> to the list of <see cref="Beatmap"/> and <see cref="Storyboard"/> decoder.
/// Registers an instantiation function for a <see cref="Decoder"/>.
/// </summary>
/// <typeparam name="T">Type to decode a <see cref="Beatmap"/> with.</typeparam>
/// <param name="version">A string representation of the version.</param>
protected static void AddDecoder<T>(string version) where T : Decoder
/// <param name="magic">A string in the file which triggers this decoder to be used.</param>
/// <param name="constructor">A function which constructs the <see cref="Decoder"/> given <paramref name="magic"/>.</param>
protected static void AddDecoder(string magic, Func<string, Decoder> constructor)
{
decoders[version] = typeof(T);
decoders[magic] = constructor;
}
/// <summary>

View File

@ -11,7 +11,7 @@ namespace osu.Game.Beatmaps.Formats
{
public static void Register()
{
AddDecoder<JsonBeatmapDecoder>("{");
AddDecoder("{", m => new JsonBeatmapDecoder());
}
public override Decoder GetStoryboardDecoder() => this;

View File

@ -13,18 +13,18 @@ namespace osu.Game.Beatmaps.Formats
{
public static void Register()
{
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v14");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v13");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v12");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v11");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v10");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v9");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v8");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v7");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v6");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v5");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v4");
AddDecoder<LegacyBeatmapDecoder>(@"osu file format v3");
AddDecoder(@"osu file format v14", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v13", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v12", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v11", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v10", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v9", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v8", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v7", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v6", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v5", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v4", m => new LegacyBeatmapDecoder(m));
AddDecoder(@"osu file format v3", m => new LegacyBeatmapDecoder(m));
// TODO: differences between versions
}