This commit is contained in:
2026-07-23 08:59:10 -04:00
commit fc970fa3d8
30 changed files with 1376 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
namespace RhythiaLib.Rhr.Internal
{
internal static class RhrFrameCodec
{
public const int WireSize = 17; // total size of frame in bytes
public static RhrReplayFrame Read(BinaryReader reader, int version)
{
ArgumentNullException.ThrowIfNull(reader);
bool usesInt32Time = version >= RhrVersions.Int32Time;
float? legacyWireTime = usesInt32Time ? null : reader.ReadSingle();
int time = usesInt32Time
? reader.ReadInt32()
: ConvertLegacyTime(legacyWireTime!.Value);
float positionX = reader.ReadSingle();
float positionY = reader.ReadSingle();
float health = reader.ReadSingle();
byte isImportantFrame = reader.ReadByte();
if (version < RhrVersions.NegateY)
positionY = -positionY;
return new RhrReplayFrame
{
Time = time,
PositionX = positionX,
PositionY = positionY,
Health = health,
IsImportantFrame = isImportantFrame,
LegacyWireTime = legacyWireTime,
DecodedLegacyTime = legacyWireTime.HasValue ? time : null,
};
}
public static void Write(BinaryWriter writer, RhrReplayFrame frame, int version)
{
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(frame);
bool usesInt32Time = version >= RhrVersions.Int32Time;
bool negateY = version < RhrVersions.NegateY;
if (usesInt32Time)
writer.Write(frame.Time);
else
writer.Write(
frame.LegacyWireTime.HasValue && frame.DecodedLegacyTime == frame.Time
? frame.LegacyWireTime.Value
: (float)frame.Time
);
writer.Write(frame.PositionX);
writer.Write(negateY ? -frame.PositionY : frame.PositionY);
writer.Write(frame.Health);
writer.Write(frame.IsImportantFrame);
}
private static int ConvertLegacyTime(float value)
{
if (!float.IsFinite(value))
{
throw new InvalidRhrFormatException(
"A legacy replay frame contains a non finite time."
);
}
if (value > int.MaxValue || value < int.MinValue)
{
throw new InvalidRhrFormatException(
$"Legacy frame time {value} is outside the Int32 range."
);
}
return (int)value;
}
}
}
+54
View File
@@ -0,0 +1,54 @@
using System.Text;
namespace RhythiaLib.Rhr.Internal
{
internal static class RhrReader
{
public static RhrReplay Read(Stream stream, RhrReadOptions options)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(options);
if (!stream.CanRead)
throw new ArgumentException("The stream must be readable.", nameof(stream));
using var reader = new BinaryReader(stream, Encoding.UTF8, leaveOpen: true);
try
{
int version = reader.ReadInt32();
RhrScoreData scoreData = RhrScoreDataCodec.Read(reader, version);
int frameCount = reader.ReadInt32();
RhrValidation.ValidateFrameCount(stream, frameCount, options);
var frames = new List<RhrReplayFrame>(frameCount);
for (int index = 0; index < frameCount; index++)
{
RhrReplayFrame frame = RhrFrameCodec.Read(reader, version);
frames.Add(frame);
}
RhrValidation.ValidateEndOfStream(stream);
return new RhrReplay
{
Version = version,
ScoreData = scoreData,
Frames = frames,
};
}
catch (EndOfStreamException exception)
{
throw new InvalidRhrFormatException("The replay is truncated.", exception);
}
catch (IOException exception)
{
throw new InvalidRhrFormatException("The replay could not be read.", exception);
}
}
}
}
@@ -0,0 +1,95 @@
namespace RhythiaLib.Rhr.Internal
{
internal static class RhrScoreDataCodec
{
public static RhrScoreData Read(BinaryReader reader, int version)
{
ArgumentNullException.ThrowIfNull(reader);
var scoreData = new RhrScoreData
{
TimestampTicks = reader.ReadInt64(),
PlayerName = reader.ReadString(),
LegacyMapId = reader.ReadString(),
MapId = reader.ReadInt32(),
StartFrom = reader.ReadInt32(),
Mode = reader.ReadString(),
};
if (version >= RhrVersions.ExtendedFields)
{
scoreData.Passed = reader.ReadBoolean();
scoreData.ModsJson = reader.ReadString();
scoreData.Spin = reader.ReadBoolean();
scoreData.Speed = reader.ReadSingle();
scoreData.TotalScore = reader.ReadInt64();
}
else
{
scoreData.Passed = true;
scoreData.ModsJson = "[]";
scoreData.Spin = false;
scoreData.Speed = 0f;
scoreData.TotalScore = 0;
}
scoreData.Accuracy = reader.ReadSingle();
scoreData.Hits = reader.ReadInt32();
scoreData.Misses = reader.ReadInt32();
scoreData.Points = reader.ReadSingle();
if (version >= RhrVersions.FailTime)
{
scoreData.FailTime = reader.ReadInt32();
scoreData.Failed = scoreData.FailTime >= 0;
}
else
{
scoreData.FailTime = -1;
scoreData.Failed = false;
}
scoreData.BeatmapHash =
version >= RhrVersions.BeatmapHash ? reader.ReadString() : string.Empty;
return scoreData;
}
public static void Write(BinaryWriter writer, RhrScoreData scoreData, int version)
{
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(scoreData);
writer.Write(scoreData.TimestampTicks);
writer.Write(scoreData.PlayerName);
writer.Write(scoreData.LegacyMapId);
writer.Write(scoreData.MapId);
writer.Write(scoreData.StartFrom);
writer.Write(scoreData.Mode);
if (version >= RhrVersions.ExtendedFields)
{
writer.Write(scoreData.Passed);
writer.Write(scoreData.ModsJson);
writer.Write(scoreData.Spin);
writer.Write(scoreData.Speed);
writer.Write(scoreData.TotalScore);
}
writer.Write(scoreData.Accuracy);
writer.Write(scoreData.Hits);
writer.Write(scoreData.Misses);
writer.Write(scoreData.Points);
if (version >= RhrVersions.FailTime)
{
writer.Write(scoreData.Failed ? scoreData.FailTime : -1);
}
if (version >= RhrVersions.BeatmapHash)
{
writer.Write(scoreData.BeatmapHash);
}
}
}
}
+104
View File
@@ -0,0 +1,104 @@
namespace RhythiaLib.Rhr.Internal
{
internal static class RhrValidation
{
public static void ValidateFrameCount(
Stream streamm,
int frameCount,
RhrReadOptions options
)
{
ArgumentNullException.ThrowIfNull(streamm);
ArgumentNullException.ThrowIfNull(options);
if (frameCount < 0)
throw new InvalidRhrFormatException($"Negative frame count: {frameCount}.");
if (frameCount > options.MaximumFrameCount)
throw new InvalidRhrFormatException(
$"Frame count {frameCount} exceeded the configured maximum of {options.MaximumFrameCount}"
);
if (!streamm.CanSeek)
return;
long requiredBytes = checked((long)frameCount * RhrFrameCodec.WireSize);
long remainingBytes = streamm.Length - streamm.Position;
if (remainingBytes < requiredBytes)
{
throw new InvalidRhrFormatException(
$"The replay declares {frameCount} frames, "
+ $"requiring {requiredBytes} bytes, "
+ $"but only {remainingBytes} bytes remain"
);
}
}
public static void ValidateEndOfStream(Stream stream)
{
if (!stream.CanSeek)
return;
if (stream.Position != stream.Length)
{
long trailingByteCount = stream.Length - stream.Position;
throw new InvalidRhrFormatException(
$"The replay contains {trailingByteCount} unexpected trailing bytes."
);
}
}
public static void ValidateReplay(RhrReplay replay)
{
ArgumentNullException.ThrowIfNull(replay);
ArgumentNullException.ThrowIfNull(replay.ScoreData);
ArgumentNullException.ThrowIfNull(replay.Frames);
ValidateScoreData(replay.ScoreData);
for (int index = 0; index < replay.Frames.Count; index++)
{
RhrReplayFrame frame =
replay.Frames[index]
?? throw new ArgumentException($"Frame {index} is null", nameof(replay));
ValidateFrame(frame, index);
}
}
private static void ValidateScoreData(RhrScoreData scoreData)
{
ArgumentNullException.ThrowIfNull(scoreData.PlayerName);
ArgumentNullException.ThrowIfNull(scoreData.LegacyMapId);
ArgumentNullException.ThrowIfNull(scoreData.Mode);
ArgumentNullException.ThrowIfNull(scoreData.ModsJson);
ArgumentNullException.ThrowIfNull(scoreData.BeatmapHash);
if (!float.IsFinite(scoreData.Speed))
throw new ArgumentException("Replay speed must be finite.");
if (!float.IsFinite(scoreData.Accuracy))
throw new ArgumentException("Replay accuracy must be finite.");
if (!float.IsFinite(scoreData.Points))
throw new ArgumentException("Replay points must be finite.");
if (scoreData.Failed && scoreData.FailTime < 0)
throw new ArgumentException("A failed replay must have a non-negative fail time.");
}
private static void ValidateFrame(RhrReplayFrame frame, int index)
{
if (
!float.IsFinite(frame.PositionX)
|| !float.IsFinite(frame.PositionY)
|| !float.IsFinite(frame.Health)
)
{
throw new ArgumentException($"Frame {index} contains a non-finite value.");
}
}
}
}
+31
View File
@@ -0,0 +1,31 @@
using System.Text;
namespace RhythiaLib.Rhr.Internal
{
internal static class RhrWriter
{
public static void Write(Stream stream, RhrReplay replay)
{
ArgumentNullException.ThrowIfNull(stream);
ArgumentNullException.ThrowIfNull(replay);
if (!stream.CanWrite)
throw new ArgumentException("The stream must be writable.", nameof(stream));
RhrValidation.ValidateReplay(replay);
using var writer = new BinaryWriter(stream, Encoding.UTF8, leaveOpen: true);
writer.Write(replay.Version);
RhrScoreDataCodec.Write(writer, replay.ScoreData, replay.Version);
writer.Write(replay.Frames.Count);
foreach (RhrReplayFrame frame in replay.Frames)
{
RhrFrameCodec.Write(writer, frame, replay.Version);
}
}
}
}