2020-03-24 03:06:24 +00: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.
|
|
|
|
|
|
|
|
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.IO.Legacy;
|
|
|
|
using osu.Game.Rulesets.Replays.Types;
|
|
|
|
using SharpCompress.Compressors.LZMA;
|
2020-03-24 03:06:24 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Scoring.Legacy
|
|
|
|
{
|
|
|
|
public class LegacyScoreEncoder
|
|
|
|
{
|
2021-06-08 08:58:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Database version in stable-compatible YYYYMMDD format.
|
|
|
|
/// </summary>
|
|
|
|
public const int LATEST_VERSION = 30000000;
|
2020-03-24 03:06:24 +00:00
|
|
|
|
|
|
|
private readonly Score score;
|
2020-03-24 05:13:46 +00:00
|
|
|
private readonly IBeatmap beatmap;
|
2020-03-24 03:06:24 +00:00
|
|
|
|
2020-03-24 05:13:46 +00:00
|
|
|
public LegacyScoreEncoder(Score score, IBeatmap beatmap)
|
2020-03-24 03:06:24 +00:00
|
|
|
{
|
|
|
|
this.score = score;
|
2020-03-24 05:13:46 +00:00
|
|
|
this.beatmap = beatmap;
|
2020-03-24 03:06:24 +00:00
|
|
|
|
|
|
|
if (score.ScoreInfo.Beatmap.RulesetID < 0 || score.ScoreInfo.Beatmap.RulesetID > 3)
|
|
|
|
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 03:06:24 +00:00
|
|
|
{
|
2020-03-24 05:13:46 +00:00
|
|
|
using (SerializationWriter sw = new SerializationWriter(stream))
|
|
|
|
{
|
2020-03-24 06:43:22 +00:00
|
|
|
sw.Write((byte)(score.ScoreInfo.Ruleset.ID ?? 0));
|
2020-03-24 05:13:46 +00:00
|
|
|
sw.Write(LATEST_VERSION);
|
|
|
|
sw.Write(score.ScoreInfo.Beatmap.MD5Hash);
|
|
|
|
sw.Write(score.ScoreInfo.UserString);
|
|
|
|
sw.Write($"lazer-{score.ScoreInfo.UserString}-{score.ScoreInfo.Date}".ComputeMD5Hash());
|
|
|
|
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 03:06:24 +00:00
|
|
|
|
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 03:06:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 05:13:46 +00:00
|
|
|
private void writeModSpecificData(ScoreInfo score, SerializationWriter sw)
|
2020-03-24 03:06:24 +00:00
|
|
|
{
|
|
|
|
}
|
2020-03-24 05:13:46 +00:00
|
|
|
|
|
|
|
private byte[] createReplayData()
|
|
|
|
{
|
|
|
|
var content = new ASCIIEncoding().GetBytes(replayStringContent);
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (score.Replay != null)
|
|
|
|
{
|
2021-04-28 12:55:20 +00:00
|
|
|
int lastTime = 0;
|
|
|
|
|
2020-03-25 11:21:34 +00:00
|
|
|
foreach (var f in score.Replay.Frames.OfType<IConvertibleReplayFrame>().Select(f => f.ToLegacy(beatmap)))
|
2020-03-24 05:13:46 +00:00
|
|
|
{
|
2021-04-28 12:23:56 +00:00
|
|
|
// Rounding because stable could only parse integral values
|
2021-04-28 12:55:20 +00:00
|
|
|
int time = (int)Math.Round(f.Time);
|
|
|
|
replayData.Append(FormattableString.Invariant($"{time - lastTime}|{f.MouseX ?? 0}|{f.MouseY ?? 0}|{(int)f.ButtonState},"));
|
|
|
|
lastTime = time;
|
2020-03-24 05:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
replayData.AppendFormat(@"{0}|{1}|{2}|{3},", -12345, 0, 0, 0);
|
|
|
|
return replayData.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string getHpGraphFormatted()
|
|
|
|
{
|
|
|
|
// todo: implement, maybe?
|
|
|
|
return string.Empty;
|
|
|
|
}
|
2020-03-24 03:06:24 +00:00
|
|
|
}
|
|
|
|
}
|