1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 02:22:56 +08:00
osu-lazer/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs

390 lines
16 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Globalization;
using System.IO;
2018-03-09 20:23:03 +08:00
using System.Linq;
using osu.Game.Beatmaps.Timing;
using osu.Game.Rulesets.Objects.Legacy;
using osu.Game.Beatmaps.ControlPoints;
using osu.Framework;
namespace osu.Game.Beatmaps.Formats
{
2018-03-09 20:23:03 +08:00
public class LegacyBeatmapDecoder : LegacyDecoder<Beatmap>
{
2018-03-09 20:23:03 +08:00
public const int LATEST_VERSION = 14;
private Beatmap beatmap;
private ConvertHitObjectParser parser;
private LegacySampleBank defaultSampleBank;
private int defaultSampleVolume = 100;
2018-03-09 20:23:03 +08:00
public static void Register()
{
AddDecoder<Beatmap>(@"osu file format v", m => new LegacyBeatmapDecoder(int.Parse(m.Split('v').Last())));
}
2018-03-04 21:13:43 +08:00
/// <summary>
/// lazer's audio timings in general doesn't match stable. this is the result of user testing, albeit limited.
/// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
/// </summary>
public static int UniversalOffset => RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? -22 : 0;
/// <summary>
/// Whether or not beatmap or runtime offsets should be applied. Defaults on; only disable for testing purposes.
/// </summary>
public bool ApplyOffsets = true;
private readonly int offset = UniversalOffset;
2018-03-09 20:23:03 +08:00
public LegacyBeatmapDecoder(int version = LATEST_VERSION) : base(version)
{
// BeatmapVersion 4 and lower had an incorrect offset (stable has this set as 24ms off)
2018-03-09 20:23:03 +08:00
offset += FormatVersion < 5 ? 24 : 0;
}
2018-03-09 20:23:03 +08:00
protected override void ParseStreamInto(StreamReader stream, Beatmap beatmap)
{
this.beatmap = beatmap;
2018-03-12 10:33:12 +08:00
this.beatmap.BeatmapInfo.BeatmapVersion = FormatVersion;
2018-03-09 20:23:03 +08:00
base.ParseStreamInto(stream, beatmap);
// objects may be out of order *only* if a user has manually edited an .osu file.
// unfortunately there are ranked maps in this state (example: https://osu.ppy.sh/s/594828).
this.beatmap.HitObjects.Sort((x, y) => x.StartTime.CompareTo(y.StartTime));
foreach (var hitObject in this.beatmap.HitObjects)
hitObject.ApplyDefaults(this.beatmap.ControlPointInfo, this.beatmap.BeatmapInfo.BaseDifficulty);
}
2018-03-09 20:23:03 +08:00
protected override bool ShouldSkipLine(string line) => base.ShouldSkipLine(line) || line.StartsWith(" ") || line.StartsWith("_");
2018-03-09 20:23:03 +08:00
protected override void ParseLine(Beatmap beatmap, Section section, string line)
{
switch (section)
{
case Section.General:
handleGeneral(line);
2018-03-13 18:13:50 +08:00
return;
case Section.Editor:
handleEditor(line);
2018-03-13 18:13:50 +08:00
return;
case Section.Metadata:
handleMetadata(line);
2018-03-13 18:13:50 +08:00
return;
case Section.Difficulty:
handleDifficulty(line);
2018-03-13 18:13:50 +08:00
return;
case Section.Events:
2018-04-02 19:07:18 +08:00
handleEvent(line);
2018-03-13 18:13:50 +08:00
return;
case Section.TimingPoints:
2018-04-02 19:07:18 +08:00
handleTimingPoint(line);
2018-03-13 18:13:50 +08:00
return;
case Section.HitObjects:
2018-04-02 19:07:18 +08:00
handleHitObject(line);
2018-03-13 18:13:50 +08:00
return;
}
2018-03-13 18:13:50 +08:00
base.ParseLine(beatmap, section, line);
}
private void handleGeneral(string line)
{
2018-03-14 17:41:48 +08:00
var pair = SplitKeyVal(line);
var metadata = beatmap.BeatmapInfo.Metadata;
switch (pair.Key)
{
case @"AudioFilename":
metadata.AudioFile = pair.Value;
break;
case @"AudioLeadIn":
beatmap.BeatmapInfo.AudioLeadIn = int.Parse(pair.Value);
break;
case @"PreviewTime":
metadata.PreviewTime = getOffsetTime(int.Parse(pair.Value));
break;
case @"Countdown":
beatmap.BeatmapInfo.Countdown = int.Parse(pair.Value) == 1;
break;
case @"SampleSet":
defaultSampleBank = (LegacySampleBank)Enum.Parse(typeof(LegacySampleBank), pair.Value);
break;
case @"SampleVolume":
defaultSampleVolume = int.Parse(pair.Value);
break;
case @"StackLeniency":
beatmap.BeatmapInfo.StackLeniency = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"Mode":
beatmap.BeatmapInfo.RulesetID = int.Parse(pair.Value);
switch (beatmap.BeatmapInfo.RulesetID)
{
case 0:
parser = new Rulesets.Objects.Legacy.Osu.ConvertHitObjectParser();
break;
case 1:
parser = new Rulesets.Objects.Legacy.Taiko.ConvertHitObjectParser();
break;
case 2:
parser = new Rulesets.Objects.Legacy.Catch.ConvertHitObjectParser();
break;
case 3:
parser = new Rulesets.Objects.Legacy.Mania.ConvertHitObjectParser();
break;
}
break;
case @"LetterboxInBreaks":
beatmap.BeatmapInfo.LetterboxInBreaks = int.Parse(pair.Value) == 1;
break;
case @"SpecialStyle":
beatmap.BeatmapInfo.SpecialStyle = int.Parse(pair.Value) == 1;
break;
case @"WidescreenStoryboard":
beatmap.BeatmapInfo.WidescreenStoryboard = int.Parse(pair.Value) == 1;
break;
}
}
private void handleEditor(string line)
{
2018-03-14 17:41:48 +08:00
var pair = SplitKeyVal(line);
switch (pair.Key)
{
case @"Bookmarks":
beatmap.BeatmapInfo.StoredBookmarks = pair.Value;
break;
case @"DistanceSpacing":
beatmap.BeatmapInfo.DistanceSpacing = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"BeatDivisor":
beatmap.BeatmapInfo.BeatDivisor = int.Parse(pair.Value);
break;
case @"GridSize":
beatmap.BeatmapInfo.GridSize = int.Parse(pair.Value);
break;
case @"TimelineZoom":
beatmap.BeatmapInfo.TimelineZoom = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
}
}
private void handleMetadata(string line)
{
2018-03-14 17:41:48 +08:00
var pair = SplitKeyVal(line);
var metadata = beatmap.BeatmapInfo.Metadata;
switch (pair.Key)
{
case @"Title":
metadata.Title = pair.Value;
break;
case @"TitleUnicode":
metadata.TitleUnicode = pair.Value;
break;
case @"Artist":
metadata.Artist = pair.Value;
break;
case @"ArtistUnicode":
metadata.ArtistUnicode = pair.Value;
break;
case @"Creator":
metadata.AuthorString = pair.Value;
break;
case @"Version":
beatmap.BeatmapInfo.Version = pair.Value;
break;
case @"Source":
beatmap.BeatmapInfo.Metadata.Source = pair.Value;
break;
case @"Tags":
beatmap.BeatmapInfo.Metadata.Tags = pair.Value;
break;
case @"BeatmapID":
beatmap.BeatmapInfo.OnlineBeatmapID = int.Parse(pair.Value);
break;
case @"BeatmapSetID":
beatmap.BeatmapInfo.OnlineBeatmapSetID = int.Parse(pair.Value);
metadata.OnlineBeatmapSetID = int.Parse(pair.Value);
break;
}
}
private void handleDifficulty(string line)
{
2018-03-14 17:41:48 +08:00
var pair = SplitKeyVal(line);
var difficulty = beatmap.BeatmapInfo.BaseDifficulty;
switch (pair.Key)
{
case @"HPDrainRate":
difficulty.DrainRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"CircleSize":
difficulty.CircleSize = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"OverallDifficulty":
difficulty.OverallDifficulty = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"ApproachRate":
difficulty.ApproachRate = float.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"SliderMultiplier":
difficulty.SliderMultiplier = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
case @"SliderTickRate":
difficulty.SliderTickRate = double.Parse(pair.Value, NumberFormatInfo.InvariantInfo);
break;
}
}
2018-04-02 19:07:18 +08:00
private void handleEvent(string line)
{
string[] split = line.Split(',');
EventType type;
if (!Enum.TryParse(split[0], out type))
throw new InvalidDataException($@"Unknown event type {split[0]}");
switch (type)
{
case EventType.Background:
string filename = split[2].Trim('"');
beatmap.BeatmapInfo.Metadata.BackgroundFile = filename;
break;
case EventType.Break:
var breakEvent = new BreakPeriod
{
StartTime = getOffsetTime(double.Parse(split[1], NumberFormatInfo.InvariantInfo)),
EndTime = getOffsetTime(double.Parse(split[2], NumberFormatInfo.InvariantInfo))
};
if (!breakEvent.HasEffect)
return;
beatmap.Breaks.Add(breakEvent);
break;
}
}
2018-04-02 19:07:18 +08:00
private void handleTimingPoint(string line)
{
2018-04-02 19:08:40 +08:00
try
{
string[] split = line.Split(',');
2018-04-02 19:08:40 +08:00
double time = getOffsetTime(double.Parse(split[0].Trim(), NumberFormatInfo.InvariantInfo));
double beatLength = double.Parse(split[1].Trim(), NumberFormatInfo.InvariantInfo);
double speedMultiplier = beatLength < 0 ? 100.0 / -beatLength : 1;
2018-04-02 19:08:40 +08:00
TimeSignatures timeSignature = TimeSignatures.SimpleQuadruple;
if (split.Length >= 3)
timeSignature = split[2][0] == '0' ? TimeSignatures.SimpleQuadruple : (TimeSignatures)int.Parse(split[2]);
2018-04-02 19:08:40 +08:00
LegacySampleBank sampleSet = defaultSampleBank;
if (split.Length >= 4)
sampleSet = (LegacySampleBank)int.Parse(split[3]);
2018-04-02 19:08:40 +08:00
//SampleBank sampleBank = SampleBank.Default;
//if (split.Length >= 5)
// sampleBank = (SampleBank)int.Parse(split[4]);
2018-04-02 19:08:40 +08:00
int sampleVolume = defaultSampleVolume;
if (split.Length >= 6)
sampleVolume = int.Parse(split[5]);
2018-04-02 19:08:40 +08:00
bool timingChange = true;
if (split.Length >= 7)
timingChange = split[6][0] == '1';
2018-04-02 19:08:40 +08:00
bool kiaiMode = false;
bool omitFirstBarSignature = false;
if (split.Length >= 8)
{
int effectFlags = int.Parse(split[7]);
kiaiMode = (effectFlags & 1) > 0;
omitFirstBarSignature = (effectFlags & 8) > 0;
}
2018-04-02 19:08:40 +08:00
string stringSampleSet = sampleSet.ToString().ToLower();
if (stringSampleSet == @"none")
stringSampleSet = @"normal";
2018-04-02 19:08:40 +08:00
DifficultyControlPoint difficultyPoint = beatmap.ControlPointInfo.DifficultyPointAt(time);
SampleControlPoint samplePoint = beatmap.ControlPointInfo.SamplePointAt(time);
EffectControlPoint effectPoint = beatmap.ControlPointInfo.EffectPointAt(time);
2018-04-02 19:08:40 +08:00
if (timingChange)
{
2018-04-02 19:08:40 +08:00
beatmap.ControlPointInfo.TimingPoints.Add(new TimingControlPoint
{
Time = time,
BeatLength = beatLength,
TimeSignature = timeSignature
});
}
2018-04-02 19:08:40 +08:00
if (speedMultiplier != difficultyPoint.SpeedMultiplier)
{
2018-04-02 19:08:40 +08:00
beatmap.ControlPointInfo.DifficultyPoints.RemoveAll(x => x.Time == time);
beatmap.ControlPointInfo.DifficultyPoints.Add(new DifficultyControlPoint
{
Time = time,
SpeedMultiplier = speedMultiplier
});
}
2018-04-02 19:08:40 +08:00
if (stringSampleSet != samplePoint.SampleBank || sampleVolume != samplePoint.SampleVolume)
{
2018-04-02 19:08:40 +08:00
beatmap.ControlPointInfo.SamplePoints.Add(new SampleControlPoint
{
Time = time,
SampleBank = stringSampleSet,
SampleVolume = sampleVolume
});
}
2018-04-02 19:08:40 +08:00
if (kiaiMode != effectPoint.KiaiMode || omitFirstBarSignature != effectPoint.OmitFirstBarLine)
{
2018-04-02 19:08:40 +08:00
beatmap.ControlPointInfo.EffectPoints.Add(new EffectControlPoint
{
Time = time,
KiaiMode = kiaiMode,
OmitFirstBarLine = omitFirstBarSignature
});
}
}
catch (FormatException e)
{
}
}
2018-04-02 19:07:18 +08:00
private void handleHitObject(string line)
{
// If the ruleset wasn't specified, assume the osu!standard ruleset.
if (parser == null)
parser = new Rulesets.Objects.Legacy.Osu.ConvertHitObjectParser();
var obj = parser.Parse(line);
if (obj != null)
{
obj.StartTime = getOffsetTime(obj.StartTime);
beatmap.HitObjects.Add(obj);
}
}
2018-03-04 21:13:43 +08:00
private int getOffsetTime(int time) => time + (ApplyOffsets ? offset : 0);
2018-03-04 21:13:43 +08:00
private double getOffsetTime(double time) => time + (ApplyOffsets ? offset : 0);
}
}