1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 22:27:25 +08:00
osu-lazer/osu.Game/Beatmaps/Formats/OsuLegacyDecoder.cs

271 lines
10 KiB
C#
Raw Normal View History

using System;
2016-10-08 03:09:52 +08:00
using System.Collections.Generic;
2016-10-13 01:26:09 +08:00
using System.Globalization;
using System.IO;
2016-10-11 01:00:16 +08:00
using OpenTK.Graphics;
using osu.Game.Beatmaps.Events;
2016-10-08 03:09:52 +08:00
using osu.Game.Beatmaps.Objects;
using osu.Game.Beatmaps.Samples;
2016-10-08 03:09:52 +08:00
using osu.Game.Beatmaps.Timing;
using osu.Game.GameModes.Play;
namespace osu.Game.Beatmaps.Formats
{
public class OsuLegacyDecoder : BeatmapDecoder
{
public static void Register()
{
2016-10-13 01:36:10 +08:00
AddDecoder<OsuLegacyDecoder>(@"osu file format v14");
AddDecoder<OsuLegacyDecoder>(@"osu file format v13");
AddDecoder<OsuLegacyDecoder>(@"osu file format v12");
AddDecoder<OsuLegacyDecoder>(@"osu file format v11");
AddDecoder<OsuLegacyDecoder>(@"osu file format v10");
// TODO: Not sure how far back to go, or differences between versions
}
private enum Section
{
None,
General,
Editor,
Metadata,
Difficulty,
Events,
TimingPoints,
Colours,
HitObjects,
}
private void handleGeneral(Beatmap beatmap, string key, string val)
{
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"AudioFilename":
2016-10-08 03:09:52 +08:00
beatmap.Metadata.AudioFile = val;
break;
2016-10-13 01:36:10 +08:00
case @"AudioLeadIn":
beatmap.AudioLeadIn = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"PreviewTime":
beatmap.Metadata.PreviewTime = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"Countdown":
beatmap.Countdown = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"SampleSet":
beatmap.SampleSet = (SampleSet)Enum.Parse(typeof(SampleSet), val);
break;
2016-10-13 01:36:10 +08:00
case @"StackLeniency":
2016-10-13 01:26:09 +08:00
beatmap.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"Mode":
beatmap.Mode = (PlayMode)int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"LetterboxInBreaks":
beatmap.LetterboxInBreaks = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"SpecialStyle":
beatmap.SpecialStyle = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"WidescreenStoryboard":
beatmap.WidescreenStoryboard = int.Parse(val) == 1;
break;
}
}
private void handleEditor(Beatmap beatmap, string key, string val)
{
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"Bookmarks":
beatmap.StoredBookmarks = val;
break;
2016-10-13 01:36:10 +08:00
case @"DistanceSpacing":
2016-10-13 01:26:09 +08:00
beatmap.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"BeatDivisor":
beatmap.BeatDivisor = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"GridSize":
beatmap.GridSize = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"TimelineZoom":
2016-10-13 01:26:09 +08:00
beatmap.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo);
break;
}
}
private void handleMetadata(Beatmap beatmap, string key, string val)
{
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"Title":
beatmap.Metadata.Title = val;
break;
2016-10-13 01:36:10 +08:00
case @"TitleUnicode":
beatmap.Metadata.TitleUnicode = val;
break;
2016-10-13 01:36:10 +08:00
case @"Artist":
beatmap.Metadata.Artist = val;
break;
2016-10-13 01:36:10 +08:00
case @"ArtistUnicode":
beatmap.Metadata.ArtistUnicode = val;
break;
2016-10-13 01:36:10 +08:00
case @"Creator":
beatmap.Metadata.Author = val;
break;
2016-10-13 01:36:10 +08:00
case @"Version":
beatmap.Version = val;
break;
2016-10-13 01:36:10 +08:00
case @"Source":
beatmap.Metadata.Source = val;
break;
2016-10-13 01:36:10 +08:00
case @"Tags":
beatmap.Metadata.Tags = val;
break;
2016-10-13 01:36:10 +08:00
case @"BeatmapID":
beatmap.BeatmapID = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"BeatmapSetID":
beatmap.BeatmapSetID = int.Parse(val);
beatmap.Metadata.BeatmapSetID = int.Parse(val);
break;
}
}
private void handleDifficulty(Beatmap beatmap, string key, string val)
{
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"HPDrainRate":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"CircleSize":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"OverallDifficulty":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"ApproachRate":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"SliderMultiplier":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"SliderTickRate":
2016-10-13 01:26:09 +08:00
beatmap.BaseDifficulty.SliderTickRate = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
}
}
private void handleEvents(Beatmap beatmap, string val)
{
2016-10-13 01:36:10 +08:00
if (val.StartsWith(@"//"))
return;
2016-10-13 01:36:10 +08:00
if (val.StartsWith(@" "))
2016-10-13 01:26:09 +08:00
return; // TODO
string[] split = val.Split(',');
EventType type;
int _type;
if (!int.TryParse(split[0], out _type))
{
if (!Enum.TryParse(split[0], out type))
throw new InvalidDataException($@"Unknown event type {split[0]}");
}
else
type = (EventType)_type;
// TODO: Parse and store the rest of the event
if (type == EventType.Background)
beatmap.Metadata.BackgroundFile = split[2].Trim('"');
2016-10-08 03:09:52 +08:00
}
private void handleTimingPoints(Beatmap beatmap, string val)
{
// TODO
}
private void handleColours(Beatmap beatmap, string key, string val)
2016-10-11 01:00:16 +08:00
{
string[] split = val.Split(',');
if (split.Length != 3)
throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {val}");
byte r, g, b;
if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out b))
throw new InvalidOperationException($@"Color must be specified with 8-bit integer components");
// Note: the combo index specified in the beatmap is discarded
beatmap.ComboColors.Add(new Color4
{
R = r / 255f,
G = g / 255f,
B = b / 255f,
A = 1f,
});
}
2016-10-13 22:29:30 +08:00
public override void Decode(TextReader stream, Beatmap beatmap)
{
2016-10-13 22:29:30 +08:00
// We don't overwrite these two because they're DB bound
if (beatmap.Metadata == null) beatmap.Metadata = new BeatmapMetadata();
if (beatmap.BaseDifficulty == null) beatmap.BaseDifficulty = new BaseDifficulty();
// These are fine though
beatmap.HitObjects = new List<HitObject>();
beatmap.ControlPoints = new List<ControlPoint>();
beatmap.ComboColors = new List<Color4>();
var section = Section.None;
string line;
2016-10-08 03:09:52 +08:00
while (true)
{
line = stream.ReadLine();
2016-10-08 03:09:52 +08:00
if (line == null)
break;
line = line.Trim();
if (string.IsNullOrEmpty(line))
continue;
2016-10-13 01:36:10 +08:00
if (line.StartsWith(@"osu file format v"))
2016-10-10 22:17:18 +08:00
continue;
2016-10-13 01:36:10 +08:00
if (line.StartsWith(@"[") && line.EndsWith(@"]"))
{
if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section))
throw new InvalidDataException($@"Unknown osu section {line}");
continue;
}
string val = line, key = null;
2016-10-11 01:00:16 +08:00
if (section != Section.Events && section != Section.TimingPoints && section != Section.HitObjects)
{
key = val.Remove(val.IndexOf(':')).Trim();
2016-10-08 03:09:52 +08:00
val = val.Substring(val.IndexOf(':') + 1).Trim();
}
switch (section)
{
case Section.General:
handleGeneral(beatmap, key, val);
break;
case Section.Editor:
handleEditor(beatmap, key, val);
break;
case Section.Metadata:
handleMetadata(beatmap, key, val);
break;
case Section.Difficulty:
handleDifficulty(beatmap, key, val);
break;
case Section.Events:
handleEvents(beatmap, val);
break;
case Section.TimingPoints:
handleTimingPoints(beatmap, val);
break;
2016-10-11 01:00:16 +08:00
case Section.Colours:
handleColours(beatmap, key, val);
2016-10-11 01:00:16 +08:00
break;
case Section.HitObjects:
beatmap.HitObjects.Add(HitObject.Parse(beatmap.Mode, val));
break;
}
}
}
}
}