// Copyright (c) 2007-2018 ppy Pty Ltd . // 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 { private static readonly Dictionary> decoders = new Dictionary>(); static Decoder() { LegacyDecoder.Register(); JsonBeatmapDecoder.Register(); } /// /// Retrieves a to parse a . /// /// A stream pointing to the . public static Decoder GetDecoder(StreamReader stream) { if (stream == null) throw new ArgumentNullException(nameof(stream)); string line; do { line = stream.ReadLine()?.Trim(); } while (line != null && line.Length == 0); if (line == null || !decoders.ContainsKey(line)) throw new IOException(@"Unknown file format"); return decoders[line](line); } /// /// Registers an instantiation function for a . /// /// A string in the file which triggers this decoder to be used. /// A function which constructs the given . protected static void AddDecoder(string magic, Func constructor) { decoders[magic] = constructor; } /// /// Retrieves a to parse a /// public abstract Decoder GetStoryboardDecoder(); public virtual Beatmap DecodeBeatmap(StreamReader stream) { 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); } }