2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-12-02 02:11:52 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2018-03-09 20:23:03 +08:00
|
|
|
|
using System.Linq;
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
|
public abstract class Decoder<TOutput> : Decoder
|
|
|
|
|
where TOutput : new()
|
|
|
|
|
{
|
|
|
|
|
protected virtual TOutput CreateTemplateObject() => new TOutput();
|
|
|
|
|
|
|
|
|
|
public TOutput Decode(StreamReader primaryStream, params StreamReader[] otherStreams)
|
|
|
|
|
{
|
|
|
|
|
var output = CreateTemplateObject();
|
2018-04-02 12:06:34 +08:00
|
|
|
|
foreach (StreamReader stream in otherStreams.Prepend(primaryStream))
|
2018-03-09 20:23:03 +08:00
|
|
|
|
ParseStreamInto(stream, output);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void ParseStreamInto(StreamReader stream, TOutput beatmap);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-02 02:11:52 +08:00
|
|
|
|
public abstract class Decoder
|
|
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
|
private static readonly Dictionary<Type, Dictionary<string, Func<string, Decoder>>> decoders = new Dictionary<Type, Dictionary<string, Func<string, Decoder>>>();
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
|
|
|
|
static Decoder()
|
|
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
|
LegacyBeatmapDecoder.Register();
|
2017-12-21 14:34:00 +08:00
|
|
|
|
JsonBeatmapDecoder.Register();
|
2018-03-09 20:23:03 +08:00
|
|
|
|
LegacyStoryboardDecoder.Register();
|
2017-12-02 02:11:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-02 05:05:01 +08:00
|
|
|
|
/// Retrieves a <see cref="Decoder"/> to parse a <see cref="Beatmap"/>.
|
2017-12-02 02:11:52 +08:00
|
|
|
|
/// </summary>
|
2017-12-02 05:05:01 +08:00
|
|
|
|
/// <param name="stream">A stream pointing to the <see cref="Beatmap"/>.</param>
|
2018-03-09 20:23:03 +08:00
|
|
|
|
public static Decoder<T> GetDecoder<T>(StreamReader stream)
|
|
|
|
|
where T : new()
|
2017-12-02 02:11:52 +08:00
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
2018-03-09 20:23:03 +08:00
|
|
|
|
if (!decoders.TryGetValue(typeof(T), out var typedDecoders))
|
|
|
|
|
throw new IOException(@"Unknown decoder type");
|
|
|
|
|
|
2017-12-02 02:11:52 +08:00
|
|
|
|
string line;
|
|
|
|
|
do
|
2018-03-09 20:23:03 +08:00
|
|
|
|
{
|
|
|
|
|
line = stream.ReadLine()?.Trim();
|
|
|
|
|
} while (line != null && line.Length == 0);
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
2018-03-09 20:23:03 +08:00
|
|
|
|
if (line == null)
|
2017-12-02 05:05:01 +08:00
|
|
|
|
throw new IOException(@"Unknown file format");
|
2017-12-21 14:34:00 +08:00
|
|
|
|
|
2018-03-09 20:23:03 +08:00
|
|
|
|
var decoder = typedDecoders.Select(d => line.StartsWith(d.Key) ? d.Value : null).FirstOrDefault();
|
|
|
|
|
if (decoder == null)
|
|
|
|
|
throw new IOException(@"Unknown file format");
|
|
|
|
|
|
|
|
|
|
return (Decoder<T>)decoder.Invoke(line);
|
2017-12-02 02:11:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-21 14:34:00 +08:00
|
|
|
|
/// Registers an instantiation function for a <see cref="Decoder"/>.
|
2017-12-02 02:11:52 +08:00
|
|
|
|
/// </summary>
|
2017-12-21 14:34:00 +08:00
|
|
|
|
/// <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>
|
2018-03-09 20:23:03 +08:00
|
|
|
|
protected static void AddDecoder<T>(string magic, Func<string, Decoder> constructor)
|
2017-12-02 02:11:52 +08:00
|
|
|
|
{
|
2018-03-09 20:23:03 +08:00
|
|
|
|
if (!decoders.TryGetValue(typeof(T), out var typedDecoders))
|
|
|
|
|
decoders.Add(typeof(T), typedDecoders = new Dictionary<string, Func<string, Decoder>>());
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
2018-03-09 20:23:03 +08:00
|
|
|
|
typedDecoders[magic] = constructor;
|
2017-12-02 02:11:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|