2017-02-07 12:59:30 +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
|
2016-12-06 17:56:20 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-14 11:33:58 +08:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
|
|
|
|
public abstract class BeatmapDecoder
|
|
|
|
|
{
|
2017-05-08 18:56:04 +08:00
|
|
|
|
private static readonly Dictionary<string, Type> decoders = new Dictionary<string, Type>();
|
2016-11-02 17:08:08 +08:00
|
|
|
|
|
2017-07-26 19:22:02 +08:00
|
|
|
|
static BeatmapDecoder()
|
|
|
|
|
{
|
|
|
|
|
OsuLegacyDecoder.Register();
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 19:26:46 +08:00
|
|
|
|
public static BeatmapDecoder GetDecoder(StreamReader stream)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-11-08 06:12:30 +08:00
|
|
|
|
if (stream == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
2017-08-10 13:08:39 +08:00
|
|
|
|
string line;
|
|
|
|
|
do { line = stream.ReadLine()?.Trim(); }
|
2017-09-08 18:04:24 +08:00
|
|
|
|
while (line != null && line.Length == 0);
|
2017-03-07 09:59:19 +08:00
|
|
|
|
|
|
|
|
|
if (line == null || !decoders.ContainsKey(line))
|
2016-10-14 11:33:58 +08:00
|
|
|
|
throw new IOException(@"Unknown file format");
|
2017-04-03 19:26:46 +08:00
|
|
|
|
return (BeatmapDecoder)Activator.CreateInstance(decoders[line], line);
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
2016-10-19 01:35:01 +08:00
|
|
|
|
|
|
|
|
|
protected static void AddDecoder<T>(string magic) where T : BeatmapDecoder
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
decoders[magic] = typeof(T);
|
|
|
|
|
}
|
2016-11-02 17:08:08 +08:00
|
|
|
|
|
2017-04-03 19:26:46 +08:00
|
|
|
|
public virtual Beatmap Decode(StreamReader stream)
|
2016-11-02 17:08:08 +08:00
|
|
|
|
{
|
2017-03-14 16:01:21 +08:00
|
|
|
|
return ParseFile(stream);
|
2016-11-02 17:08:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 19:26:46 +08:00
|
|
|
|
public virtual void Decode(StreamReader stream, Beatmap beatmap)
|
2017-02-09 22:09:48 +08:00
|
|
|
|
{
|
|
|
|
|
ParseFile(stream, beatmap);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 19:26:46 +08:00
|
|
|
|
protected virtual Beatmap ParseFile(StreamReader stream)
|
2017-02-09 22:09:48 +08:00
|
|
|
|
{
|
|
|
|
|
var beatmap = new Beatmap
|
|
|
|
|
{
|
|
|
|
|
BeatmapInfo = new BeatmapInfo
|
|
|
|
|
{
|
2017-10-06 05:23:26 +08:00
|
|
|
|
Metadata = new BeatmapMetadata(),
|
2017-10-19 13:05:11 +08:00
|
|
|
|
BaseDifficulty = new BeatmapDifficulty(),
|
2017-02-09 22:09:48 +08:00
|
|
|
|
},
|
|
|
|
|
};
|
2017-04-03 19:26:46 +08:00
|
|
|
|
|
2017-02-09 22:09:48 +08:00
|
|
|
|
ParseFile(stream, beatmap);
|
|
|
|
|
return beatmap;
|
|
|
|
|
}
|
2017-04-03 19:26:46 +08:00
|
|
|
|
|
|
|
|
|
protected abstract void ParseFile(StreamReader stream, Beatmap beatmap);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2017-02-07 12:52:19 +08:00
|
|
|
|
}
|