2018-01-05 11:21:19 +00:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-12-01 18:11:52 +00:00
|
|
|
|
// 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-21 06:34:00 +00:00
|
|
|
|
private static readonly Dictionary<string, Func<string, Decoder>> decoders = new Dictionary<string, Func<string, Decoder>>();
|
2017-12-01 18:11:52 +00:00
|
|
|
|
|
|
|
|
|
static Decoder()
|
|
|
|
|
{
|
|
|
|
|
LegacyDecoder.Register();
|
2017-12-21 06:34:00 +00:00
|
|
|
|
JsonBeatmapDecoder.Register();
|
2017-12-01 18:11:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-01 21:05:01 +00:00
|
|
|
|
/// Retrieves a <see cref="Decoder"/> to parse a <see cref="Beatmap"/>.
|
2017-12-01 18:11:52 +00:00
|
|
|
|
/// </summary>
|
2017-12-01 21:05:01 +00:00
|
|
|
|
/// <param name="stream">A stream pointing to the <see cref="Beatmap"/>.</param>
|
|
|
|
|
public static Decoder GetDecoder(StreamReader stream)
|
2017-12-01 18:11:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
|
|
|
|
string line;
|
|
|
|
|
do
|
|
|
|
|
{ line = stream.ReadLine()?.Trim(); }
|
|
|
|
|
while (line != null && line.Length == 0);
|
|
|
|
|
|
2017-12-01 21:05:01 +00:00
|
|
|
|
if (line == null || !decoders.ContainsKey(line))
|
|
|
|
|
throw new IOException(@"Unknown file format");
|
2017-12-21 06:34:00 +00:00
|
|
|
|
|
|
|
|
|
return decoders[line](line);
|
2017-12-01 18:11:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-21 06:34:00 +00:00
|
|
|
|
/// Registers an instantiation function for a <see cref="Decoder"/>.
|
2017-12-01 18:11:52 +00:00
|
|
|
|
/// </summary>
|
2017-12-21 06:34:00 +00: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>
|
|
|
|
|
protected static void AddDecoder(string magic, Func<string, Decoder> constructor)
|
2017-12-01 18:11:52 +00:00
|
|
|
|
{
|
2017-12-21 06:34:00 +00:00
|
|
|
|
decoders[magic] = constructor;
|
2017-12-01 18:11:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-01 21:05:01 +00:00
|
|
|
|
/// Retrieves a <see cref="Decoder"/> to parse a <see cref="Storyboard"/>
|
2017-12-01 18:11:52 +00:00
|
|
|
|
/// </summary>
|
2017-12-01 21:05:01 +00:00
|
|
|
|
public abstract Decoder GetStoryboardDecoder();
|
2017-12-01 18:11:52 +00:00
|
|
|
|
|
2017-12-01 21:05:01 +00:00
|
|
|
|
public virtual Beatmap DecodeBeatmap(StreamReader stream)
|
2017-12-01 18:11:52 +00: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);
|
|
|
|
|
|
2018-01-10 10:55:04 +00:00
|
|
|
|
public virtual Storyboard DecodeStoryboard(params StreamReader[] streams)
|
2017-12-01 18:11:52 +00:00
|
|
|
|
{
|
|
|
|
|
var storyboard = new Storyboard();
|
2018-01-10 10:55:04 +00:00
|
|
|
|
foreach (StreamReader stream in streams)
|
|
|
|
|
ParseStoryboard(stream, storyboard);
|
2017-12-01 18:11:52 +00:00
|
|
|
|
return storyboard;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void ParseStoryboard(StreamReader stream, Storyboard storyboard);
|
|
|
|
|
}
|
|
|
|
|
}
|