osu/osu.Game/Scoring/Legacy/LegacyScoreEncoder.cs

162 lines
6.5 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.
using System;
using System.IO;
2020-03-24 05:13:46 +00:00
using System.Linq;
using System.Text;
using osu.Framework.Extensions;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
2020-03-24 05:13:46 +00:00
using osu.Game.IO.Legacy;
using osu.Game.Replays.Legacy;
using osu.Game.Rulesets.Replays;
2020-03-24 05:13:46 +00:00
using osu.Game.Rulesets.Replays.Types;
using SharpCompress.Compressors.LZMA;
#nullable enable
namespace osu.Game.Scoring.Legacy
{
public class LegacyScoreEncoder
{
/// <summary>
/// Database version in stable-compatible YYYYMMDD format.
2021-06-08 09:38:47 +00:00
/// Should be incremented if any changes are made to the format/usage.
/// </summary>
2021-06-08 09:38:47 +00:00
public const int LATEST_VERSION = FIRST_LAZER_VERSION;
/// <summary>
/// The first stable-compatible YYYYMMDD format version given to lazer usage of replays.
/// </summary>
public const int FIRST_LAZER_VERSION = 30000000;
private readonly Score score;
private readonly IBeatmap? beatmap;
/// <summary>
/// Create a new score encoder for a specific score.
/// </summary>
/// <param name="score">The score to be encoded.</param>
/// <param name="beatmap">The beatmap used to convert frames for the score. May be null if the frames are already <see cref="LegacyReplayFrame"/>s.</param>
/// <exception cref="ArgumentException"></exception>
public LegacyScoreEncoder(Score score, IBeatmap? beatmap)
{
this.score = score;
2020-03-24 05:13:46 +00:00
this.beatmap = beatmap;
if (beatmap == null && !score.Replay.Frames.All(f => f is LegacyReplayFrame))
throw new ArgumentException(@"Beatmap must be provided if frames are not already legacy frames.", nameof(beatmap));
if (!score.ScoreInfo.Ruleset.IsLegacyRuleset())
throw new ArgumentException(@"Only scores in the osu, taiko, catch, or mania rulesets can be encoded to the legacy score format.", nameof(score));
}
2020-03-24 05:13:46 +00:00
public void Encode(Stream stream)
{
2020-03-24 05:13:46 +00:00
using (SerializationWriter sw = new SerializationWriter(stream))
{
sw.Write((byte)(score.ScoreInfo.Ruleset.OnlineID));
2020-03-24 05:13:46 +00:00
sw.Write(LATEST_VERSION);
sw.Write(score.ScoreInfo.BeatmapInfo.MD5Hash);
sw.Write(score.ScoreInfo.User.Username);
sw.Write(FormattableString.Invariant($"lazer-{score.ScoreInfo.User.Username}-{score.ScoreInfo.Date}").ComputeMD5Hash());
2020-03-24 05:13:46 +00:00
sw.Write((ushort)(score.ScoreInfo.GetCount300() ?? 0));
sw.Write((ushort)(score.ScoreInfo.GetCount100() ?? 0));
sw.Write((ushort)(score.ScoreInfo.GetCount50() ?? 0));
sw.Write((ushort)(score.ScoreInfo.GetCountGeki() ?? 0));
sw.Write((ushort)(score.ScoreInfo.GetCountKatu() ?? 0));
sw.Write((ushort)(score.ScoreInfo.GetCountMiss() ?? 0));
sw.Write((int)(score.ScoreInfo.TotalScore));
sw.Write((ushort)score.ScoreInfo.MaxCombo);
sw.Write(score.ScoreInfo.Combo == score.ScoreInfo.MaxCombo);
sw.Write((int)score.ScoreInfo.Ruleset.CreateInstance().ConvertToLegacyMods(score.ScoreInfo.Mods));
2020-03-24 05:13:46 +00:00
sw.Write(getHpGraphFormatted());
sw.Write(score.ScoreInfo.Date.DateTime);
sw.WriteByteArray(createReplayData());
sw.Write((long)0);
writeModSpecificData(score.ScoreInfo, sw);
}
}
2020-03-24 05:13:46 +00:00
private void writeModSpecificData(ScoreInfo score, SerializationWriter sw)
{
}
2020-03-24 05:13:46 +00:00
private byte[] createReplayData()
{
byte[] content = new ASCIIEncoding().GetBytes(replayStringContent);
2020-03-24 05:13:46 +00:00
using (var outStream = new MemoryStream())
{
using (var lzma = new LzmaStream(new LzmaEncoderProperties(false, 1 << 21, 255), false, outStream))
{
outStream.Write(lzma.Properties);
long fileSize = content.Length;
for (int i = 0; i < 8; i++)
outStream.WriteByte((byte)(fileSize >> (8 * i)));
lzma.Write(content);
}
return outStream.ToArray();
}
}
private string replayStringContent
{
get
{
StringBuilder replayData = new StringBuilder();
// BeatmapVersion 4 and lower had an incorrect offset (stable has this set as 24ms off)
// As this is baked into hitobject timing (see `LegacyBeatmapDecoder`) we also need to apply this to replay frame timing.
double offset = beatmap?.BeatmapInfo.BeatmapVersion < 5 ? -24 : 0;
2020-03-24 05:13:46 +00:00
if (score.Replay != null)
{
2021-04-28 12:55:20 +00:00
int lastTime = 0;
foreach (var f in score.Replay.Frames)
2020-03-24 05:13:46 +00:00
{
var legacyFrame = getLegacyFrame(f);
2021-04-28 12:23:56 +00:00
// Rounding because stable could only parse integral values
int time = (int)Math.Round(legacyFrame.Time + offset);
replayData.Append(FormattableString.Invariant($"{time - lastTime}|{legacyFrame.MouseX ?? 0}|{legacyFrame.MouseY ?? 0}|{(int)legacyFrame.ButtonState},"));
2021-04-28 12:55:20 +00:00
lastTime = time;
2020-03-24 05:13:46 +00:00
}
}
// Warning: this is purposefully hardcoded as a string rather than interpolating, as in some cultures the minus sign is not encoded as the standard ASCII U+00C2 codepoint,
// which then would break decoding.
replayData.Append(@"-12345|0|0|0");
2020-03-24 05:13:46 +00:00
return replayData.ToString();
}
}
private LegacyReplayFrame getLegacyFrame(ReplayFrame replayFrame)
{
switch (replayFrame)
{
case LegacyReplayFrame legacyFrame:
return legacyFrame;
case IConvertibleReplayFrame convertibleFrame:
return convertibleFrame.ToLegacy(beatmap);
default:
throw new ArgumentException(@"Frame could not be converted to legacy frames", nameof(replayFrame));
}
}
2020-03-24 05:13:46 +00:00
private string getHpGraphFormatted()
{
// todo: implement, maybe?
return string.Empty;
}
}
}