2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2019-09-10 06:43:30 +08:00
|
|
|
|
using osu.Game.IO;
|
2022-02-16 16:12:57 +08:00
|
|
|
|
using osu.Game.Rulesets;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
|
|
|
|
public abstract class Decoder<TOutput> : Decoder
|
|
|
|
|
where TOutput : new()
|
|
|
|
|
{
|
|
|
|
|
protected virtual TOutput CreateTemplateObject() => new TOutput();
|
|
|
|
|
|
2019-09-10 06:43:30 +08:00
|
|
|
|
public TOutput Decode(LineBufferedReader primaryStream, params LineBufferedReader[] otherStreams)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
var output = CreateTemplateObject();
|
2019-09-10 06:43:30 +08:00
|
|
|
|
foreach (LineBufferedReader stream in otherStreams.Prepend(primaryStream))
|
2018-04-13 17:19:50 +08:00
|
|
|
|
ParseStreamInto(stream, output);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 06:43:30 +08:00
|
|
|
|
protected abstract void ParseStreamInto(LineBufferedReader stream, TOutput output);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class Decoder
|
|
|
|
|
{
|
|
|
|
|
private static readonly Dictionary<Type, Dictionary<string, Func<string, Decoder>>> decoders = new Dictionary<Type, Dictionary<string, Func<string, Decoder>>>();
|
2019-09-11 04:06:10 +08:00
|
|
|
|
private static readonly Dictionary<Type, Func<Decoder>> fallback_decoders = new Dictionary<Type, Func<Decoder>>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
static Decoder()
|
|
|
|
|
{
|
|
|
|
|
LegacyBeatmapDecoder.Register();
|
|
|
|
|
JsonBeatmapDecoder.Register();
|
|
|
|
|
LegacyStoryboardDecoder.Register();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 16:12:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Register dependencies for use with static decoder classes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="rulesets">A store containing all available rulesets (used by <see cref="LegacyBeatmapDecoder"/>).</param>
|
|
|
|
|
public static void RegisterDependencies(RulesetStore rulesets)
|
|
|
|
|
{
|
|
|
|
|
LegacyBeatmapDecoder.RulesetStore = rulesets;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a <see cref="Decoder"/> to parse a <see cref="Beatmap"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">A stream pointing to the <see cref="Beatmap"/>.</param>
|
2019-09-10 06:43:30 +08:00
|
|
|
|
public static Decoder<T> GetDecoder<T>(LineBufferedReader stream)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
where T : new()
|
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
|
|
|
|
|
|
if (!decoders.TryGetValue(typeof(T), out var typedDecoders))
|
|
|
|
|
throw new IOException(@"Unknown decoder type");
|
|
|
|
|
|
2019-09-11 04:06:10 +08:00
|
|
|
|
// start off with the first line of the file
|
|
|
|
|
string line = stream.PeekLine()?.Trim();
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2019-09-11 04:06:10 +08:00
|
|
|
|
while (line != null && line.Length == 0)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-09-11 04:06:10 +08:00
|
|
|
|
// consume the previously peeked empty line and advance to the next one
|
|
|
|
|
stream.ReadLine();
|
|
|
|
|
line = stream.PeekLine()?.Trim();
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (line == null)
|
2019-09-11 04:06:10 +08:00
|
|
|
|
throw new IOException("Unknown file format (null)");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-04-03 09:36:31 +08:00
|
|
|
|
var decoder = typedDecoders.Where(d => line.StartsWith(d.Key, StringComparison.InvariantCulture)).Select(d => d.Value).FirstOrDefault();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-09-11 04:06:10 +08:00
|
|
|
|
// it's important the magic does NOT get consumed here, since sometimes it's part of the structure
|
|
|
|
|
// (see JsonBeatmapDecoder - the magic string is the opening brace)
|
|
|
|
|
// decoder implementations should therefore not die on receiving their own magic
|
|
|
|
|
if (decoder != null)
|
|
|
|
|
return (Decoder<T>)decoder.Invoke(line);
|
|
|
|
|
|
|
|
|
|
if (!fallback_decoders.TryGetValue(typeof(T), out var fallbackDecoder))
|
|
|
|
|
throw new IOException($"Unknown file format ({line})");
|
|
|
|
|
|
|
|
|
|
return (Decoder<T>)fallbackDecoder.Invoke();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers an instantiation function for a <see cref="Decoder"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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<T>(string magic, Func<string, Decoder> constructor)
|
|
|
|
|
{
|
|
|
|
|
if (!decoders.TryGetValue(typeof(T), out var typedDecoders))
|
|
|
|
|
decoders.Add(typeof(T), typedDecoders = new Dictionary<string, Func<string, Decoder>>());
|
|
|
|
|
|
|
|
|
|
typedDecoders[magic] = constructor;
|
|
|
|
|
}
|
2019-09-11 04:06:10 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registers a fallback decoder instantiation function.
|
2019-09-11 04:45:34 +08:00
|
|
|
|
/// The fallback will be returned if the first non-empty line of the decoded stream does not match any known magic.
|
2019-11-17 20:48:23 +08:00
|
|
|
|
/// Calling this method will overwrite any existing global fallback registration for type <typeparamref name="T"/> - use with caution.
|
2019-09-11 04:06:10 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Type of object being decoded.</typeparam>
|
|
|
|
|
/// <param name="constructor">A function that constructs the fallback<see cref="Decoder"/>.</param>
|
|
|
|
|
protected static void SetFallbackDecoder<T>(Func<Decoder> constructor)
|
|
|
|
|
{
|
|
|
|
|
fallback_decoders[typeof(T)] = constructor;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|