osu/osu.Game/Beatmaps/Formats/LegacyDecoder.cs

219 lines
7.1 KiB
C#
Raw Normal View History

// 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 09:19:50 +00:00
using System;
using System.Collections.Generic;
using osu.Framework.Extensions;
2018-04-13 09:19:50 +00:00
using osu.Framework.Logging;
2018-06-28 09:20:43 +00:00
using osu.Game.Audio;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.IO;
2020-04-14 12:05:07 +00:00
using osu.Game.Rulesets.Objects.Legacy;
2018-11-20 07:51:59 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Beatmaps.Formats
{
public abstract class LegacyDecoder<T> : Decoder<T>
where T : new()
{
public const int LATEST_VERSION = 14;
2018-04-13 09:19:50 +00:00
protected readonly int FormatVersion;
protected LegacyDecoder(int version)
{
FormatVersion = version;
}
protected override void ParseStreamInto(LineBufferedReader stream, T output)
2018-04-13 09:19:50 +00:00
{
Section section = Section.General;
2018-04-13 09:19:50 +00:00
string line;
2019-04-01 03:16:05 +00:00
2018-04-13 09:19:50 +00:00
while ((line = stream.ReadLine()) != null)
{
if (ShouldSkipLine(line))
continue;
if (section != Section.Metadata)
{
// comments should not be stripped from metadata lines, as the song metadata may contain "//" as valid data.
line = StripComments(line);
}
line = line.TrimEnd();
if (line.StartsWith('[') && line.EndsWith(']'))
2018-04-13 09:19:50 +00:00
{
2019-12-14 12:54:22 +00:00
if (!Enum.TryParse(line[1..^1], out section))
2019-08-11 16:42:05 +00:00
Logger.Log($"Unknown section \"{line}\" in \"{output}\"");
2018-04-13 09:19:50 +00:00
2020-03-30 08:18:09 +00:00
OnBeginNewSection(section);
2018-04-13 09:19:50 +00:00
continue;
}
2019-08-07 10:33:54 +00:00
try
{
ParseLine(output, section, line);
}
catch (Exception e)
{
2019-08-11 16:42:05 +00:00
Logger.Log($"Failed to process line \"{line}\" into \"{output}\": {e.Message}", LoggingTarget.Runtime, LogLevel.Important);
2019-08-07 10:33:54 +00:00
}
2018-04-13 09:19:50 +00:00
}
}
protected virtual bool ShouldSkipLine(string line) => string.IsNullOrWhiteSpace(line) || line.AsSpan().TrimStart().StartsWith("//".AsSpan(), StringComparison.Ordinal);
2018-04-13 09:19:50 +00:00
2020-03-30 08:18:09 +00:00
/// <summary>
/// Invoked when a new <see cref="Section"/> has been entered.
/// </summary>
/// <param name="section">The entered <see cref="Section"/>.</param>
protected virtual void OnBeginNewSection(Section section)
{
}
2018-04-13 09:19:50 +00:00
protected virtual void ParseLine(T output, Section section, string line)
{
switch (section)
{
case Section.Colours:
HandleColours(output, line);
2018-04-13 09:19:50 +00:00
return;
}
}
protected string StripComments(string line)
{
int index = line.AsSpan().IndexOf("//".AsSpan());
if (index > 0)
return line.Substring(0, index);
2019-02-28 04:31:40 +00:00
return line;
}
2018-04-13 09:19:50 +00:00
protected void HandleColours<TModel>(TModel output, string line)
2018-04-13 09:19:50 +00:00
{
var pair = SplitKeyVal(line);
bool isCombo = pair.Key.StartsWith(@"Combo", StringComparison.Ordinal);
2018-04-13 09:19:50 +00:00
string[] split = pair.Value.Split(',');
2018-04-13 09:19:50 +00:00
if (split.Length != 3 && split.Length != 4)
throw new InvalidOperationException($@"Color specified in incorrect format (should be R,G,B or R,G,B,A): {pair.Value}");
2018-04-13 09:19:50 +00:00
2018-10-05 02:55:59 +00:00
Color4 colour;
2018-10-05 02:55:59 +00:00
try
{
2020-06-25 05:15:26 +00:00
byte alpha = split.Length == 4 ? byte.Parse(split[3]) : (byte)255;
colour = new Color4(byte.Parse(split[0]), byte.Parse(split[1]), byte.Parse(split[2]), alpha);
2018-10-05 02:55:59 +00:00
}
2019-04-25 08:36:17 +00:00
catch
{
2018-04-13 09:19:50 +00:00
throw new InvalidOperationException(@"Color must be specified with 8-bit integer components");
}
2018-04-13 09:19:50 +00:00
if (isCombo)
{
if (!(output is IHasComboColours tHasComboColours)) return;
tHasComboColours.CustomComboColours.Add(colour);
2018-04-13 09:19:50 +00:00
}
else
{
if (!(output is IHasCustomColours tHasCustomColours)) return;
2019-02-28 04:31:40 +00:00
2018-04-13 09:19:50 +00:00
tHasCustomColours.CustomColours[pair.Key] = colour;
}
}
protected KeyValuePair<string, string> SplitKeyVal(string line, char separator = ':')
{
string[] split = line.Split(separator, 2);
2018-04-13 09:19:50 +00:00
return new KeyValuePair<string, string>
(
split[0].Trim(),
split.Length > 1 ? split[1].Trim() : string.Empty
);
}
protected string CleanFilename(string path) => path.Trim('"').ToStandardisedPath();
2018-04-13 09:19:50 +00:00
protected enum Section
{
General,
Editor,
Metadata,
Difficulty,
Events,
TimingPoints,
Colours,
HitObjects,
Variables,
2020-03-30 08:18:09 +00:00
Fonts,
CatchTheBeat,
Mania,
2018-04-13 09:19:50 +00:00
}
[Obsolete("Do not use unless you're a legacy ruleset and 100% sure.")]
public class LegacyDifficultyControlPoint : DifficultyControlPoint
{
/// <summary>
/// Legacy BPM multiplier that introduces floating-point errors for rulesets that depend on it.
/// DO NOT USE THIS UNLESS 100% SURE.
/// </summary>
public double BpmMultiplier { get; private set; }
public LegacyDifficultyControlPoint(double beatLength)
: this()
{
// Note: In stable, the division occurs on floats, but with compiler optimisations turned on actually seems to occur on doubles via some .NET black magic (possibly inlining?).
BpmMultiplier = beatLength < 0 ? Math.Clamp((float)-beatLength, 10, 10000) / 100.0 : 1;
}
public LegacyDifficultyControlPoint()
{
SliderVelocityBindable.Precision = double.Epsilon;
}
public override void CopyFrom(ControlPoint other)
{
base.CopyFrom(other);
BpmMultiplier = ((LegacyDifficultyControlPoint)other).BpmMultiplier;
}
}
internal class LegacySampleControlPoint : SampleControlPoint
2018-06-28 09:20:43 +00:00
{
public int CustomSampleBank;
2019-06-30 12:58:30 +00:00
public override HitSampleInfo ApplyTo(HitSampleInfo hitSampleInfo)
2018-06-28 09:20:43 +00:00
{
2019-06-30 12:58:30 +00:00
var baseInfo = base.ApplyTo(hitSampleInfo);
2018-06-28 09:20:43 +00:00
2020-12-01 06:37:51 +00:00
if (baseInfo is ConvertHitObjectParser.LegacyHitSampleInfo legacy && legacy.CustomSampleBank == 0)
2020-12-02 01:55:48 +00:00
return legacy.With(newCustomSampleBank: CustomSampleBank);
2018-06-28 09:20:43 +00:00
return baseInfo;
}
2020-04-17 08:06:12 +00:00
public override bool IsRedundant(ControlPoint existing)
=> base.IsRedundant(existing)
2020-04-17 08:04:09 +00:00
&& existing is LegacySampleControlPoint existingSample
&& CustomSampleBank == existingSample.CustomSampleBank;
public override void CopyFrom(ControlPoint other)
{
base.CopyFrom(other);
CustomSampleBank = ((LegacySampleControlPoint)other).CustomSampleBank;
}
2018-06-28 09:20:43 +00:00
}
2018-04-13 09:19:50 +00:00
}
}