2017-12-02 02:11:52 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using osu.Game.Storyboards;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
|
|
|
|
public abstract class Decoder
|
|
|
|
|
{
|
2017-12-02 05:05:01 +08:00
|
|
|
|
private static readonly Dictionary<string, Type> decoders = new Dictionary<string, Type>();
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
|
|
|
|
static Decoder()
|
|
|
|
|
{
|
|
|
|
|
LegacyDecoder.Register();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public static Decoder GetDecoder(StreamReader stream)
|
2017-12-02 02:11:52 +08:00
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
|
|
|
|
string line;
|
|
|
|
|
do
|
|
|
|
|
{ line = stream.ReadLine()?.Trim(); }
|
|
|
|
|
while (line != null && line.Length == 0);
|
|
|
|
|
|
2017-12-02 05:05:01 +08:00
|
|
|
|
if (line == null || !decoders.ContainsKey(line))
|
|
|
|
|
throw new IOException(@"Unknown file format");
|
|
|
|
|
return (Decoder)Activator.CreateInstance(decoders[line], line);
|
2017-12-02 02:11:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the <see cref="Decoder"/> to the list of <see cref="Beatmap"/> and <see cref="Storyboard"/> decoder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Type to decode a <see cref="Beatmap"/> with.</typeparam>
|
|
|
|
|
/// <param name="version">A string representation of the version.</param>
|
2017-12-02 05:05:01 +08:00
|
|
|
|
protected static void AddDecoder<T>(string version) where T : Decoder
|
2017-12-02 02:11:52 +08:00
|
|
|
|
{
|
2017-12-02 05:05:01 +08:00
|
|
|
|
decoders[version] = typeof(T);
|
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="Storyboard"/>
|
2017-12-02 02:11:52 +08:00
|
|
|
|
/// </summary>
|
2017-12-02 05:05:01 +08:00
|
|
|
|
public abstract Decoder GetStoryboardDecoder();
|
2017-12-02 02:11:52 +08:00
|
|
|
|
|
2017-12-02 05:05:01 +08:00
|
|
|
|
public virtual Beatmap DecodeBeatmap(StreamReader stream)
|
2017-12-02 02:11:52 +08:00
|
|
|
|
{
|
|
|
|
|
var beatmap = new Beatmap
|
|
|
|
|
{
|
|
|
|
|
BeatmapInfo = new BeatmapInfo
|
|
|
|
|
{
|
|
|
|
|
Metadata = new BeatmapMetadata(),
|
|
|
|
|
BaseDifficulty = new BeatmapDifficulty(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParseBeatmap(stream, beatmap);
|
|
|
|
|
return beatmap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void ParseBeatmap(StreamReader stream, Beatmap beatmap);
|
|
|
|
|
|
|
|
|
|
public virtual Storyboard DecodeStoryboard(StreamReader stream)
|
|
|
|
|
{
|
|
|
|
|
var storyboard = new Storyboard();
|
|
|
|
|
ParseStoryboard(stream, storyboard);
|
|
|
|
|
return storyboard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void ParseStoryboard(StreamReader stream, Storyboard storyboard);
|
|
|
|
|
}
|
|
|
|
|
}
|