2019-01-24 16:43:03 +08:00
|
|
|
|
// 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 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using osu.Framework.Logging;
|
2018-06-28 17:20:43 +08:00
|
|
|
|
using osu.Game.Audio;
|
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps.Formats
|
|
|
|
|
{
|
|
|
|
|
public abstract class LegacyDecoder<T> : Decoder<T>
|
|
|
|
|
where T : new()
|
|
|
|
|
{
|
|
|
|
|
protected readonly int FormatVersion;
|
|
|
|
|
|
|
|
|
|
protected LegacyDecoder(int version)
|
|
|
|
|
{
|
|
|
|
|
FormatVersion = version;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 17:32:24 +08:00
|
|
|
|
protected override void ParseStreamInto(StreamReader stream, T output)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
Section section = Section.None;
|
|
|
|
|
|
|
|
|
|
string line;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
while ((line = stream.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (ShouldSkipLine(line))
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-08-21 10:10:54 +08:00
|
|
|
|
if (line.StartsWith(@"[", StringComparison.Ordinal) && line.EndsWith(@"]", StringComparison.Ordinal))
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
if (!Enum.TryParse(line.Substring(1, line.Length - 2), out section))
|
|
|
|
|
{
|
2018-04-20 17:32:24 +08:00
|
|
|
|
Logger.Log($"Unknown section \"{line}\" in {output}");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
section = Section.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 17:32:24 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ParseLine(output, section, line);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error(e, $"Failed to process line \"{line}\" into {output}");
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 15:16:55 +08:00
|
|
|
|
protected virtual bool ShouldSkipLine(string line) => string.IsNullOrWhiteSpace(line) || line.AsSpan().TrimStart().StartsWith("//".AsSpan(), StringComparison.Ordinal);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
protected virtual void ParseLine(T output, Section section, string line)
|
|
|
|
|
{
|
2018-07-16 07:04:41 +08:00
|
|
|
|
line = StripComments(line);
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
switch (section)
|
|
|
|
|
{
|
|
|
|
|
case Section.Colours:
|
|
|
|
|
handleColours(output, line);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-16 22:35:55 +08:00
|
|
|
|
|
|
|
|
|
protected string StripComments(string line)
|
2018-07-16 07:04:41 +08:00
|
|
|
|
{
|
2018-10-17 09:53:21 +08:00
|
|
|
|
var index = line.AsSpan().IndexOf("//".AsSpan());
|
2018-07-16 07:04:41 +08:00
|
|
|
|
if (index > 0)
|
|
|
|
|
return line.Substring(0, index);
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-07-16 07:04:41 +08:00
|
|
|
|
return line;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
private bool hasComboColours;
|
|
|
|
|
|
|
|
|
|
private void handleColours(T output, string line)
|
|
|
|
|
{
|
|
|
|
|
var pair = SplitKeyVal(line);
|
|
|
|
|
|
|
|
|
|
bool isCombo = pair.Key.StartsWith(@"Combo");
|
|
|
|
|
|
2018-07-16 07:04:41 +08:00
|
|
|
|
string[] split = pair.Value.Split(',');
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-05 10:19:01 +08: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 17:19:50 +08:00
|
|
|
|
|
2018-10-05 10:55:59 +08:00
|
|
|
|
Color4 colour;
|
2018-10-05 10:19:01 +08:00
|
|
|
|
|
2018-10-05 10:55:59 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-05 11:06:24 +08:00
|
|
|
|
colour = new Color4(byte.Parse(split[0]), byte.Parse(split[1]), byte.Parse(split[2]), split.Length == 4 ? byte.Parse(split[3]) : (byte)255);
|
2018-10-05 10:55:59 +08:00
|
|
|
|
}
|
2019-04-25 16:36:17 +08:00
|
|
|
|
catch
|
2018-10-05 10:19:01 +08:00
|
|
|
|
{
|
2018-04-13 17:19:50 +08:00
|
|
|
|
throw new InvalidOperationException(@"Color must be specified with 8-bit integer components");
|
2018-10-05 10:19:01 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
if (isCombo)
|
|
|
|
|
{
|
|
|
|
|
if (!(output is IHasComboColours tHasComboColours)) return;
|
|
|
|
|
|
|
|
|
|
if (!hasComboColours)
|
|
|
|
|
{
|
|
|
|
|
// remove default colours.
|
|
|
|
|
tHasComboColours.ComboColours.Clear();
|
|
|
|
|
hasComboColours = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tHasComboColours.ComboColours.Add(colour);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!(output is IHasCustomColours tHasCustomColours)) return;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
tHasCustomColours.CustomColours[pair.Key] = colour;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected KeyValuePair<string, string> SplitKeyVal(string line, char separator = ':')
|
|
|
|
|
{
|
|
|
|
|
var split = line.Trim().Split(new[] { separator }, 2);
|
|
|
|
|
|
|
|
|
|
return new KeyValuePair<string, string>
|
|
|
|
|
(
|
|
|
|
|
split[0].Trim(),
|
|
|
|
|
split.Length > 1 ? split[1].Trim() : string.Empty
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected enum Section
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
General,
|
|
|
|
|
Editor,
|
|
|
|
|
Metadata,
|
|
|
|
|
Difficulty,
|
|
|
|
|
Events,
|
|
|
|
|
TimingPoints,
|
|
|
|
|
Colours,
|
|
|
|
|
HitObjects,
|
|
|
|
|
Variables,
|
|
|
|
|
Fonts
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum LegacySampleBank
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
|
|
|
|
Normal = 1,
|
|
|
|
|
Soft = 2,
|
|
|
|
|
Drum = 3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum EventType
|
|
|
|
|
{
|
|
|
|
|
Background = 0,
|
|
|
|
|
Video = 1,
|
|
|
|
|
Break = 2,
|
|
|
|
|
Colour = 3,
|
|
|
|
|
Sprite = 4,
|
|
|
|
|
Sample = 5,
|
|
|
|
|
Animation = 6
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum LegacyOrigins
|
|
|
|
|
{
|
|
|
|
|
TopLeft,
|
|
|
|
|
Centre,
|
|
|
|
|
CentreLeft,
|
|
|
|
|
TopRight,
|
|
|
|
|
BottomCentre,
|
|
|
|
|
TopCentre,
|
|
|
|
|
Custom,
|
|
|
|
|
CentreRight,
|
|
|
|
|
BottomLeft,
|
|
|
|
|
BottomRight
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum StoryLayer
|
|
|
|
|
{
|
|
|
|
|
Background = 0,
|
|
|
|
|
Fail = 1,
|
|
|
|
|
Pass = 2,
|
|
|
|
|
Foreground = 3
|
|
|
|
|
}
|
2018-06-28 17:20:43 +08:00
|
|
|
|
|
2019-05-21 13:27:57 +08:00
|
|
|
|
internal class LegacySampleControlPoint : SampleControlPoint, IEquatable<LegacySampleControlPoint>
|
2018-06-28 17:20:43 +08:00
|
|
|
|
{
|
|
|
|
|
public int CustomSampleBank;
|
|
|
|
|
|
|
|
|
|
public override SampleInfo ApplyTo(SampleInfo sampleInfo)
|
|
|
|
|
{
|
|
|
|
|
var baseInfo = base.ApplyTo(sampleInfo);
|
|
|
|
|
|
2018-07-20 14:12:44 +08:00
|
|
|
|
if (string.IsNullOrEmpty(baseInfo.Suffix) && CustomSampleBank > 1)
|
2018-07-10 01:56:00 +08:00
|
|
|
|
baseInfo.Suffix = CustomSampleBank.ToString();
|
2018-06-28 17:20:43 +08:00
|
|
|
|
|
|
|
|
|
return baseInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-21 13:27:57 +08:00
|
|
|
|
public bool Equals(LegacySampleControlPoint other)
|
|
|
|
|
=> base.Equals(other)
|
|
|
|
|
&& CustomSampleBank == other?.CustomSampleBank;
|
2018-06-28 17:20:43 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|