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

379 lines
14 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;
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.Timing;
2016-11-14 17:03:20 +08:00
using osu.Game.Modes.Objects;
2017-03-17 13:24:46 +08:00
using osu.Game.Beatmaps.Legacy;
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");
AddDecoder<OsuLegacyDecoder>(@"osu file format v9");
2016-12-21 16:29:57 +08:00
AddDecoder<OsuLegacyDecoder>(@"osu file format v8");
AddDecoder<OsuLegacyDecoder>(@"osu file format v7");
AddDecoder<OsuLegacyDecoder>(@"osu file format v6");
AddDecoder<OsuLegacyDecoder>(@"osu file format v5");
// TODO: Not sure how far back to go, or differences between versions
}
2017-04-05 20:59:40 +08:00
private LegacySampleBank defaultSampleBank;
2017-04-04 13:31:50 +08:00
private int defaultSampleVolume = 100;
2017-04-04 12:11:04 +08:00
private readonly int beatmapVersion;
public OsuLegacyDecoder()
{
}
public OsuLegacyDecoder(string header)
{
beatmapVersion = int.Parse(header.Substring(17));
}
private enum Section
{
None,
General,
Editor,
Metadata,
Difficulty,
Events,
TimingPoints,
Colours,
HitObjects,
}
private void handleGeneral(Beatmap beatmap, string key, string val)
{
2016-10-19 01:35:01 +08:00
var metadata = beatmap.BeatmapInfo.Metadata;
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"AudioFilename":
2016-10-19 01:35:01 +08:00
metadata.AudioFile = val;
break;
2016-10-13 01:36:10 +08:00
case @"AudioLeadIn":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.AudioLeadIn = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"PreviewTime":
2016-10-19 01:35:01 +08:00
metadata.PreviewTime = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"Countdown":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.Countdown = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"SampleSet":
2017-04-05 20:59:40 +08:00
defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), val);
2017-04-04 12:11:04 +08:00
break;
case @"SampleVolume":
defaultSampleVolume = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"StackLeniency":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.StackLeniency = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"Mode":
beatmap.BeatmapInfo.Mode = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"LetterboxInBreaks":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"SpecialStyle":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.SpecialStyle = int.Parse(val) == 1;
break;
2016-10-13 01:36:10 +08:00
case @"WidescreenStoryboard":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.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":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.StoredBookmarks = val;
break;
2016-10-13 01:36:10 +08:00
case @"DistanceSpacing":
2016-11-28 21:48:28 +08:00
beatmap.BeatmapInfo.DistanceSpacing = double.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"BeatDivisor":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.BeatDivisor = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"GridSize":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.GridSize = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"TimelineZoom":
2016-11-28 21:48:28 +08:00
beatmap.BeatmapInfo.TimelineZoom = double.Parse(val, NumberFormatInfo.InvariantInfo);
break;
}
}
private void handleMetadata(Beatmap beatmap, string key, string val)
{
2016-10-19 01:35:01 +08:00
var metadata = beatmap.BeatmapInfo.Metadata;
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"Title":
2016-10-19 01:35:01 +08:00
metadata.Title = val;
break;
2016-10-13 01:36:10 +08:00
case @"TitleUnicode":
2016-10-19 01:35:01 +08:00
metadata.TitleUnicode = val;
break;
2016-10-13 01:36:10 +08:00
case @"Artist":
2016-10-19 01:35:01 +08:00
metadata.Artist = val;
break;
2016-10-13 01:36:10 +08:00
case @"ArtistUnicode":
2016-10-19 01:35:01 +08:00
metadata.ArtistUnicode = val;
break;
2016-10-13 01:36:10 +08:00
case @"Creator":
2016-10-19 01:35:01 +08:00
metadata.Author = val;
break;
2016-10-13 01:36:10 +08:00
case @"Version":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.Version = val;
break;
2016-10-13 01:36:10 +08:00
case @"Source":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.Metadata.Source = val;
break;
2016-10-13 01:36:10 +08:00
case @"Tags":
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.Metadata.Tags = val;
break;
2016-10-13 01:36:10 +08:00
case @"BeatmapID":
beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(val);
break;
2016-10-13 01:36:10 +08:00
case @"BeatmapSetID":
beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(val);
metadata.OnlineBeatmapSetID = int.Parse(val);
break;
}
}
private void handleDifficulty(Beatmap beatmap, string key, string val)
{
var difficulty = beatmap.BeatmapInfo.Difficulty;
switch (key)
{
2016-10-13 01:36:10 +08:00
case @"HPDrainRate":
2016-10-19 01:35:01 +08:00
difficulty.DrainRate = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"CircleSize":
2016-10-19 01:35:01 +08:00
difficulty.CircleSize = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"OverallDifficulty":
2016-10-19 01:35:01 +08:00
difficulty.OverallDifficulty = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"ApproachRate":
2016-10-19 01:35:01 +08:00
difficulty.ApproachRate = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"SliderMultiplier":
2016-10-19 01:35:01 +08:00
difficulty.SliderMultiplier = float.Parse(val, NumberFormatInfo.InvariantInfo);
break;
2016-10-13 01:36:10 +08:00
case @"SliderTickRate":
2016-10-19 01:35:01 +08:00
difficulty.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;
2017-02-07 15:15:45 +08:00
int intType;
if (!int.TryParse(split[0], out intType))
{
if (!Enum.TryParse(split[0], out type))
throw new InvalidDataException($@"Unknown event type {split[0]}");
}
else
2017-02-07 15:15:45 +08:00
type = (EventType)intType;
// TODO: Parse and store the rest of the event
if (type == EventType.Background)
2016-10-19 01:35:01 +08:00
beatmap.BeatmapInfo.Metadata.BackgroundFile = split[2].Trim('"');
2016-10-08 03:09:52 +08:00
}
private void handleTimingPoints(Beatmap beatmap, string val)
{
2016-11-28 14:12:11 +08:00
string[] split = val.Split(',');
2017-04-04 12:11:04 +08:00
double time = double.Parse(split[0].Trim(), NumberFormatInfo.InvariantInfo);
double beatLength = double.Parse(split[1].Trim(), NumberFormatInfo.InvariantInfo);
TimeSignatures timeSignature = TimeSignatures.SimpleQuadruple;
if (split.Length >= 3)
timeSignature = split[2][0] == '0' ? TimeSignatures.SimpleQuadruple : (TimeSignatures)int.Parse(split[2]);
2017-04-05 20:59:40 +08:00
LegacySampleBank sampleSet = defaultSampleBank;
2017-04-04 12:11:04 +08:00
if (split.Length >= 4)
2017-04-05 20:59:40 +08:00
sampleSet = (LegacySampleBank)int.Parse(split[3]);
2017-04-04 12:11:04 +08:00
2017-04-05 20:59:40 +08:00
//SampleBank sampleBank = SampleBank.Default;
//if (split.Length >= 5)
// sampleBank = (SampleBank)int.Parse(split[4]);
2017-04-04 12:11:04 +08:00
int sampleVolume = defaultSampleVolume;
if (split.Length >= 6)
sampleVolume = int.Parse(split[5]);
bool timingChange = true;
if (split.Length >= 7)
timingChange = split[6][0] == '1';
bool kiaiMode = false;
bool omitFirstBarSignature = false;
if (split.Length >= 8)
2016-11-28 14:12:11 +08:00
{
2017-04-04 12:11:04 +08:00
int effectFlags = int.Parse(split[7]);
kiaiMode = (effectFlags & 1) > 0;
omitFirstBarSignature = (effectFlags & 8) > 0;
2016-11-28 14:12:11 +08:00
}
string stringSampleSet = sampleSet.ToString().ToLower();
2017-04-06 10:54:11 +08:00
if (stringSampleSet == @"none")
stringSampleSet = @"normal";
2017-04-04 12:11:04 +08:00
beatmap.TimingInfo.ControlPoints.Add(new ControlPoint
{
Time = time,
BeatLength = beatLength,
2017-04-06 15:06:02 +08:00
SpeedMultiplier = beatLength < 0 ? -beatLength / 100.0 : 1,
2017-04-04 12:11:04 +08:00
TimingChange = timingChange,
TimeSignature = timeSignature,
SampleBank = stringSampleSet,
SampleVolume = sampleVolume,
2017-04-04 12:11:04 +08:00
KiaiMode = kiaiMode,
OmitFirstBarLine = omitFirstBarSignature
});
}
2017-03-14 13:48:32 +08:00
private void handleColours(Beatmap beatmap, string key, string val, ref bool hasCustomColours)
2016-10-11 01:00:16 +08:00
{
string[] split = val.Split(',');
2017-03-14 13:48:32 +08:00
2016-10-11 01:00:16 +08:00
if (split.Length != 3)
throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B): {val}");
2017-03-14 13:48:32 +08:00
2016-10-11 01:00:16 +08:00
byte r, g, b;
if (!byte.TryParse(split[0], out r) || !byte.TryParse(split[1], out g) || !byte.TryParse(split[2], out b))
2017-03-07 09:59:19 +08:00
throw new InvalidOperationException(@"Color must be specified with 8-bit integer components");
2017-03-14 13:48:32 +08:00
if (!hasCustomColours)
{
beatmap.ComboColors.Clear();
hasCustomColours = true;
}
2016-10-11 01:00:16 +08:00
// Note: the combo index specified in the beatmap is discarded
2017-03-07 09:59:19 +08:00
if (key.StartsWith(@"Combo"))
2016-10-11 01:00:16 +08:00
{
2017-03-07 09:59:19 +08:00
beatmap.ComboColors.Add(new Color4
{
R = r / 255f,
G = g / 255f,
B = b / 255f,
A = 1f,
});
}
2016-10-11 01:00:16 +08:00
}
protected override Beatmap ParseFile(StreamReader stream)
2017-03-17 13:24:46 +08:00
{
return new LegacyBeatmap(base.ParseFile(stream));
}
public override Beatmap Decode(StreamReader stream)
2017-03-17 13:24:46 +08:00
{
return new LegacyBeatmap(base.Decode(stream));
}
protected override void ParseFile(StreamReader stream, Beatmap beatmap)
{
beatmap.BeatmapInfo.BeatmapVersion = beatmapVersion;
2017-04-04 13:31:50 +08:00
HitObjectParser parser = new LegacyHitObjectParser();
2017-04-03 19:33:10 +08:00
Section section = Section.None;
2017-03-14 13:48:32 +08:00
bool hasCustomColours = false;
2017-04-03 19:33:10 +08:00
string line;
while ((line = stream.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line))
continue;
2016-10-13 01:36:10 +08:00
if (line.StartsWith(@"osu file format v"))
{
beatmap.BeatmapInfo.BeatmapVersion = int.Parse(line.Substring(17));
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:
2017-03-14 13:48:32 +08:00
handleColours(beatmap, key, val, ref hasCustomColours);
2016-10-11 01:00:16 +08:00
break;
case Section.HitObjects:
2017-04-06 11:27:35 +08:00
var obj = parser.Parse(val);
if (obj != null)
beatmap.HitObjects.Add(obj);
2017-03-14 13:48:32 +08:00
break;
}
}
}
2017-04-05 20:59:40 +08:00
internal enum LegacySampleBank
2017-04-05 20:59:40 +08:00
{
None = 0,
Normal = 1,
Soft = 2,
Drum = 3
}
}
}