1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 08:07:40 +08:00
osu-lazer/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs

59 lines
1.8 KiB
C#
Raw Normal View History

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