feat: rhm

This commit is contained in:
2026-07-23 22:19:33 -04:00
parent 569cdbd14a
commit c92b395a3e
21 changed files with 1090 additions and 7 deletions
Binary file not shown.
Binary file not shown.
+165
View File
@@ -0,0 +1,165 @@
using System.Text.Json;
using RhythiaLib.Maps;
using RhythiaLib.Rhm;
namespace RhythiaLib.Tests.Rhm
{
public sealed class RhmCorruptionTests
{
[Fact]
public void Decode_NonZip_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() => RhmFile.Decode("not a zip"u8));
}
[Fact]
public void Decode_MissingMap_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive("", includeMap: false))
);
}
[Fact]
public void Decode_InvalidMapJson_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive("{"))
);
}
[Fact]
public void Decode_MissingAudio_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(
RhmFixtureData.CreateArchive(RhmFixtureData.ValidMapJson, includeAudio: false)
)
);
}
[Fact]
public void Decode_MissingCover_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(
RhmFixtureData.CreateArchive(RhmFixtureData.ValidMapJson, includeCover: false)
)
);
}
[Fact]
public void Decode_MapOverLimit_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(
RhmFixtureData.CreateArchive(RhmFixtureData.ValidMapJson),
new RhmReadOptions { MaximumMapSize = 1 }
)
);
}
[Theory]
[InlineData(0)]
[InlineData(6)]
public void Decode_InvalidDifficulty_Throws(int difficulty)
{
string json = Replace("Difficulty", difficulty);
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive(json))
);
}
[Fact]
public void Decode_NegativeDuration_Throws()
{
string json = Replace("Duration", -1);
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive(json))
);
}
[Fact]
public void Decode_ExcessiveNoteCount_Throws()
{
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(
RhmFixtureData.CreateArchive(RhmFixtureData.ValidMapJson),
new RhmReadOptions { MaximumNoteCount = 0 }
)
);
}
[Fact]
public void Decode_NegativeNoteTime_Throws()
{
string json = RhmFixtureData.ValidMapJson.Replace(
"\"Time\":10",
"\"Time\":-1",
StringComparison.Ordinal
);
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive(json))
);
}
[Fact]
public void Decode_NonFiniteNoteCoordinate_Throws()
{
string json = RhmFixtureData.ValidMapJson.Replace(
"\"X\":1",
"\"X\":1e100",
StringComparison.Ordinal
);
Assert.Throws<InvalidRhmFormatException>(() =>
RhmFile.Decode(RhmFixtureData.CreateArchive(json))
);
}
[Fact]
public void Read_UnreadableStream_Throws()
{
using var stream = new UnreadableStream();
Assert.Throws<ArgumentException>(() => RhmFile.Read(stream));
}
[Fact]
public void Write_UnwritableStream_Throws()
{
using var stream = new MemoryStream([], writable: false);
Assert.Throws<ArgumentException>(() =>
RhmFile.Write(stream, RhmFixtureData.CreateMap())
);
}
[Fact]
public void Encode_MissingRequiredMedia_Throws()
{
RhythiaMap map = RhmFixtureData.CreateMap();
map.Audio = null;
Assert.Throws<ArgumentException>(() => RhmFile.Encode(map));
map.Audio = [];
map.Cover = null;
Assert.Throws<ArgumentException>(() => RhmFile.Encode(map));
}
private static string Replace(string propertyName, int value)
{
using JsonDocument document = JsonDocument.Parse(RhmFixtureData.ValidMapJson);
var values = new Dictionary<string, object?>();
foreach (JsonProperty property in document.RootElement.EnumerateObject())
{
values[property.Name] =
property.Name == propertyName ? value : property.Value.Clone();
}
return JsonSerializer.Serialize(values);
}
private sealed class UnreadableStream : MemoryStream
{
public override bool CanRead => false;
}
}
}
+59
View File
@@ -0,0 +1,59 @@
using RhythiaLib.Maps;
using RhythiaLib.Rhm;
namespace RhythiaLib.Tests.Rhm
{
public sealed class RhmFileReadTests
{
[Theory]
[MemberData(nameof(RhmFixtureData.FixturePaths), MemberType = typeof(RhmFixtureData))]
public void Read_GameFixture_ReturnsValidMap(string path)
{
RhythiaMap map = RhmFile.Read(path);
Assert.False(string.IsNullOrWhiteSpace(map.Title));
Assert.InRange(map.Difficulty, 1, 5);
Assert.NotEmpty(map.Notes);
Assert.NotNull(map.Audio);
Assert.NotEmpty(map.Audio);
Assert.NotNull(map.Cover);
Assert.NotEmpty(map.Cover);
for (int index = 0; index < map.Notes.Count; index++)
{
RhythiaNote note = map.Notes[index];
Assert.Equal(index, note.Index);
Assert.True(float.IsFinite(note.X));
Assert.True(float.IsFinite(note.Y));
if (index > 0)
Assert.True(map.Notes[index - 1].TimeMilliseconds <= note.TimeMilliseconds);
}
}
[Fact]
public void Read_KnownFixture_ReturnsKnownMetadata()
{
string path = Path.Combine(AppContext.BaseDirectory, "Rhm", "Fixtures", "quantum.rhm");
RhythiaMap map = RhmFile.Read(path);
Assert.Equal("5860246058480dfb", map.Id);
Assert.Equal("nature", map.Artist);
Assert.Equal("chirp", map.Title);
Assert.Equal(465, map.LengthMilliseconds);
Assert.Equal(1, map.Difficulty);
Assert.Equal(["whip"], map.Mappers);
}
[Fact]
public void Read_LeavesCallerStreamOpen()
{
byte[] data = RhmFixtureData.CreateArchive(RhmFixtureData.ValidMapJson);
using var stream = new MemoryStream(data);
_ = RhmFile.Read(stream);
Assert.True(stream.CanRead);
}
}
}
+92
View File
@@ -0,0 +1,92 @@
using System.IO.Compression;
using System.Text.Json;
using RhythiaLib.Maps;
using RhythiaLib.Rhm;
namespace RhythiaLib.Tests.Rhm
{
public sealed class RhmFileWriteTests
{
[Fact]
public void Encode_WritesExpectedArchiveLayout()
{
byte[] data = RhmFile.Encode(RhmFixtureData.CreateMap());
using var stream = new MemoryStream(data);
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
ZipArchiveEntry mapEntry = Assert.Single(
archive.Entries,
entry => entry.FullName == "map"
);
Assert.NotNull(archive.GetEntry("audio.mp3"));
Assert.NotNull(archive.GetEntry("cover.png"));
using Stream mapStream = mapEntry.Open();
using JsonDocument json = JsonDocument.Parse(mapStream);
Assert.Equal("audio.mp3", json.RootElement.GetProperty("AudioFileName").GetString());
Assert.Equal("cover.png", json.RootElement.GetProperty("ImagePath").GetString());
Assert.Equal(
JsonValueKind.Number,
json.RootElement.GetProperty("Difficulty").ValueKind
);
}
[Fact]
public void Encode_ConvertsCoordinatesAndDecodeRestoresThem()
{
RhythiaMap map = RhmFixtureData.CreateMap();
map.Notes =
[
new RhythiaNote
{
TimeMilliseconds = 0,
X = 0,
Y = 0,
},
new RhythiaNote
{
TimeMilliseconds = 1,
X = -1,
Y = 1,
},
new RhythiaNote
{
TimeMilliseconds = 2,
X = 1,
Y = -1,
},
];
byte[] data = RhmFile.Encode(map);
using (var stream = new MemoryStream(data))
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
using (Stream mapStream = archive.GetEntry("map")!.Open())
using (JsonDocument json = JsonDocument.Parse(mapStream))
{
JsonElement notes = json.RootElement.GetProperty("Notes");
AssertStored(notes[0], 1, 1);
AssertStored(notes[1], 0, 0);
AssertStored(notes[2], 2, 2);
}
RhythiaMap decoded = RhmFile.Decode(data);
Assert.Equal((0f, 0f), (decoded.Notes[0].X, decoded.Notes[0].Y));
Assert.Equal((-1f, 1f), (decoded.Notes[1].X, decoded.Notes[1].Y));
Assert.Equal((1f, -1f), (decoded.Notes[2].X, decoded.Notes[2].Y));
}
[Fact]
public void Write_LeavesCallerStreamOpen()
{
using var stream = new MemoryStream();
RhmFile.Write(stream, RhmFixtureData.CreateMap());
Assert.True(stream.CanWrite);
}
private static void AssertStored(JsonElement note, float x, float y)
{
Assert.Equal(x, note.GetProperty("X").GetSingle());
Assert.Equal(y, note.GetProperty("Y").GetSingle());
}
}
}
+92
View File
@@ -0,0 +1,92 @@
using System.IO.Compression;
using System.Text;
using RhythiaLib.Maps;
namespace RhythiaLib.Tests.Rhm
{
internal static class RhmFixtureData
{
public const string ValidMapJson = """
{"OnlineId":null,"OnlineStatus":null,"LegacyId":"test-id","SongName":"Artist - Song","Mappers":["Mapper"],"Title":"Artist - Song","Duration":1000,"Difficulty":3,"CustomDifficultyName":"Test","StarRating":4.5,"Notes":[{"Time":10,"X":1,"Y":1}],"ImagePath":"cover","AudioFileName":"song.mp3"}
""";
public static TheoryData<string> FixturePaths()
{
var data = new TheoryData<string>();
string directory = Path.Combine(AppContext.BaseDirectory, "Rhm", "Fixtures");
if (!Directory.Exists(directory))
return data;
foreach (
string path in Directory.EnumerateFiles(
directory,
"*.rhm",
SearchOption.AllDirectories
)
)
{
data.Add(path);
}
return data;
}
public static RhythiaMap CreateMap()
{
return new RhythiaMap
{
Id = "test-id",
Artist = "Artist",
Title = "Artist - Song",
Rating = 4.5f,
Mappers = ["Mapper"],
Difficulty = 3,
DifficultyName = "Test",
LengthMilliseconds = 1000,
Notes =
[
new RhythiaNote
{
Index = 0,
TimeMilliseconds = 10,
X = 0,
Y = 0,
},
],
Audio = [1, 2, 3],
AudioExtension = ".mp3",
Cover = [4, 5, 6],
};
}
public static byte[] CreateArchive(
string mapJson,
bool includeMap = true,
bool includeAudio = true,
bool includeCover = true
)
{
using var stream = new MemoryStream();
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
{
if (includeMap)
WriteEntry(archive, "map", Encoding.UTF8.GetBytes(mapJson));
if (includeAudio)
WriteEntry(archive, "audio", [1, 2, 3]);
if (includeCover)
WriteEntry(archive, "cover", [4, 5, 6]);
}
return stream.ToArray();
}
private static void WriteEntry(ZipArchive archive, string name, byte[] data)
{
ZipArchiveEntry entry = archive.CreateEntry(name);
using Stream stream = entry.Open();
stream.Write(data);
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using RhythiaLib.Maps;
using RhythiaLib.Rhm;
namespace RhythiaLib.Tests.Rhm
{
public sealed class RhmRoundTripTests
{
[Theory]
[MemberData(nameof(RhmFixtureData.FixturePaths), MemberType = typeof(RhmFixtureData))]
public void Fixture_LogicalRoundTripPreservesData(string path)
{
RhythiaMap original = RhmFile.Read(path);
RhythiaMap decoded = RhmFile.Decode(RhmFile.Encode(original));
Assert.Equal(original.Id, decoded.Id);
Assert.Equal(original.Artist, decoded.Artist);
Assert.Equal(original.Title, decoded.Title);
Assert.Equal(original.Rating, decoded.Rating);
Assert.Equal(original.Mappers, decoded.Mappers);
Assert.Equal(original.Difficulty, decoded.Difficulty);
Assert.Equal(original.DifficultyName, decoded.DifficultyName);
Assert.Equal(original.LengthMilliseconds, decoded.LengthMilliseconds);
Assert.Equal(original.Audio, decoded.Audio);
Assert.Equal(original.Cover, decoded.Cover);
Assert.Equal(original.Notes.Count, decoded.Notes.Count);
for (int index = 0; index < original.Notes.Count; index++)
{
RhythiaNote expected = original.Notes[index];
RhythiaNote actual = decoded.Notes[index];
Assert.Equal(expected.Index, actual.Index);
Assert.Equal(expected.TimeMilliseconds, actual.TimeMilliseconds);
Assert.Equal(expected.X, actual.X);
Assert.Equal(expected.Y, actual.Y);
}
}
}
}
+3
View File
@@ -25,5 +25,8 @@
<None Update="Rhr\Fixtures\*.rhr">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Rhm\Fixtures\**\*.rhm">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>