osu/osu.Game/Beatmaps/Formats/BeatmapDecoder.cs

93 lines
3.0 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 09:56:20 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2016-11-14 09:03:20 +00:00
using osu.Game.Modes.Objects;
2016-11-02 09:08:08 +00:00
using OpenTK.Graphics;
using osu.Game.Beatmaps.Timing;
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 09:08:08 +00:00
public static BeatmapDecoder GetDecoder(TextReader stream)
{
2017-03-07 01:59:19 +00:00
var line = stream.ReadLine()?.Trim();
if (line == null || !decoders.ContainsKey(line))
throw new IOException(@"Unknown file format");
return (BeatmapDecoder)Activator.CreateInstance(decoders[line]);
}
2016-10-18 17:35:01 +00:00
protected static void AddDecoder<T>(string magic) where T : BeatmapDecoder
{
decoders[magic] = typeof(T);
}
2016-11-02 09:08:08 +00:00
public virtual Beatmap Decode(TextReader stream)
{
Beatmap b = ParseFile(stream);
Process(b);
return b;
}
public virtual void Decode(TextReader stream, Beatmap beatmap)
{
ParseFile(stream, beatmap);
}
2016-11-02 09:08:08 +00:00
public virtual Beatmap Process(Beatmap beatmap)
{
2017-03-14 05:48:32 +00:00
int comboIndex = 0;
int colourIndex = 0;
foreach (var obj in beatmap.HitObjects)
{
HitObjectWithCombo comboObject = obj as HitObjectWithCombo;
if (comboObject == null || comboObject.NewCombo)
{
comboIndex = 0;
colourIndex = (colourIndex + 1) % beatmap.ComboColors.Count;
}
if (comboObject != null)
{
comboObject.ComboIndex = comboIndex++;
comboObject.ComboColour = beatmap.ComboColors[colourIndex];
}
}
2016-11-02 09:08:08 +00:00
return beatmap;
}
protected virtual Beatmap ParseFile(TextReader stream)
{
var beatmap = new Beatmap
{
HitObjects = new List<HitObject>(),
ControlPoints = new List<ControlPoint>(),
2017-03-14 05:48:32 +00:00
ComboColors = new List<Color4> {
new Color4(17, 136, 170, 255),
new Color4(102, 136, 0, 255),
new Color4(204, 102, 0, 255),
new Color4(121, 9, 13, 255),
},
BeatmapInfo = new BeatmapInfo
{
Metadata = new BeatmapMetadata(),
BaseDifficulty = new BaseDifficulty(),
},
};
ParseFile(stream, beatmap);
return beatmap;
}
protected abstract void ParseFile(TextReader stream, Beatmap beatmap);
}
2017-02-07 04:52:19 +00:00
}