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