Initial import of Rhyculator

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
SahikandClaude Opus 5.5 committed 2026-10-24 11:55:22 +08:00
commit 728bb0c657
315 files changed
+96030

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 20b1dad3aac98c34d9293804a6a35b28
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
+75
View File
@@ -0,0 +1,75 @@
using NUnit.Framework;
public class NoteJudgeTests
{
[TestCase(0.0, Judgement.Perfect)]
[TestCase(0.050, Judgement.Perfect)]
[TestCase(-0.050, Judgement.Perfect)]
[TestCase(-0.051, Judgement.Fast)]
[TestCase(-0.130, Judgement.Fast)]
[TestCase(0.051, Judgement.Slow)]
[TestCase(0.130, Judgement.Slow)]
[TestCase(-0.150, Judgement.Bad)]
[TestCase(0.200, Judgement.Bad)]
[TestCase(-0.201, Judgement.None)] // 太早:忽略,不吃掉音符
[TestCase(-1.0, Judgement.None)]
[TestCase(0.201, Judgement.Miss)] // 太晚:Miss
public void Evaluate_ReturnsExpectedJudgement(double delta, Judgement expected)
{
Assert.AreEqual(expected, NoteJudge.Evaluate(delta));
}
[Test]
public void Score_MatchesOriginalPoints()
{
Assert.AreEqual(212, NoteJudge.Score(Judgement.Perfect));
Assert.AreEqual(159, NoteJudge.Score(Judgement.Fast));
Assert.AreEqual(159, NoteJudge.Score(Judgement.Slow));
Assert.AreEqual(53, NoteJudge.Score(Judgement.Bad));
Assert.AreEqual(0, NoteJudge.Score(Judgement.Miss));
Assert.AreEqual(0, NoteJudge.Score(Judgement.None));
}
}
public class PlayResultTests
{
[Test]
public void Combo_ResetsOnMiss_AndTracksMax()
{
var r = new PlayResult();
r.Add(Judgement.Perfect, 212);
r.Add(Judgement.Fast, 159);
r.Add(Judgement.Bad, 53);
r.Add(Judgement.Miss, 0);
r.Add(Judgement.Slow, 159);
Assert.AreEqual(212 + 159 + 53 + 159, r.score);
Assert.AreEqual(3, r.maxCombo);
Assert.AreEqual(1, r.combo);
Assert.AreEqual(1, r.perfect);
Assert.AreEqual(1, r.miss);
}
}
public class CalibrationMathTests
{
[Test]
public void BeatDelta_UsesNearestBeat_AndSkipsWarmup()
{
// 第一拍 1.0s、間隔 0.6s、16 拍、暖身 4 拍
Assert.IsFalse(CalibrationMath.TryBeatDelta(1.0 + 0.6 * 2, 1.0, 0.6, 16, 4, out _)); // 暖身拍
Assert.IsTrue(CalibrationMath.TryBeatDelta(1.0 + 0.6 * 5 + 0.08, 1.0, 0.6, 16, 4, out double late));
Assert.AreEqual(0.08, late, 1e-9);
Assert.IsTrue(CalibrationMath.TryBeatDelta(1.0 + 0.6 * 6 - 0.05, 1.0, 0.6, 16, 4, out double early));
Assert.AreEqual(-0.05, early, 1e-9);
Assert.IsFalse(CalibrationMath.TryBeatDelta(1.0 + 0.6 * 20, 1.0, 0.6, 16, 4, out _)); // 超出範圍
}
[Test]
public void Median_IgnoresOutliers()
{
var v = new System.Collections.Generic.List<double> { 0.10, 0.11, 0.09, 0.12, 0.29 };
Assert.AreEqual(0.11, CalibrationMath.Median(v), 1e-9);
Assert.AreEqual(0.105, CalibrationMath.Median(new System.Collections.Generic.List<double> { 0.10, 0.11 }), 1e-9);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9d189bef24ded5e4c94a6aed24041599
+221
View File
@@ -0,0 +1,221 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using NUnit.Framework;
public class MiniJsonTests
{
[Test]
public void Parses_nested_values()
{
var o = MiniJson.AsObject(MiniJson.Parse("{\"a\":1,\"b\":[true,null,\"x\\n\\u0041\"],\"c\":{\"d\":-2.5e1}}"));
Assert.AreEqual(1.0, MiniJson.GetNumber(o, "a"));
var b = MiniJson.AsList(o["b"]);
Assert.AreEqual(true, b[0]);
Assert.IsNull(b[1]);
Assert.AreEqual("x\nA", b[2]);
Assert.AreEqual(-25.0, MiniJson.GetNumber(MiniJson.AsObject(o["c"]), "d"));
}
[Test]
public void Missing_keys_are_distinguishable_from_zero()
{
var o = MiniJson.AsObject(MiniJson.Parse("{\"pos\":{\"x\":0,\"y\":0}}"));
Assert.IsNotNull(MiniJson.Get(o, "pos"));
Assert.IsNull(MiniJson.Get(o, "path"));
Assert.IsFalse(MiniJson.TryGetNumber(o, "end", out _));
Assert.AreEqual(7, MiniJson.GetInt(o, "end", 7));
}
[TestCase("{\"a\":}")]
[TestCase("[1,2")]
[TestCase("{\"a\":1} x")]
[TestCase("")]
public void Rejects_malformed_json(string json)
{
Assert.Throws<FormatException>(() => MiniJson.Parse(json));
}
}
public class ChartLoaderTests
{
// Maker 統一格式:tap、hold、slide,加上三顆應該被略過的音符
const string Chart =
"{\"format\":1,\"divisor\":4,\"notes\":[" +
"{\"t\":1000,\"lane\":\"L\",\"type\":\"tap\",\"pos\":{\"x\":0,\"y\":3}}," +
"{\"t\":2000,\"lane\":\"R\",\"type\":\"hold\",\"end\":2500,\"pos\":{\"x\":3,\"y\":1},\"locked\":true}," +
"{\"t\":3000,\"lane\":\"L\",\"type\":\"slide\",\"end\":3400,\"path\":[{\"x\":0,\"y\":0},{\"x\":1,\"y\":1},{\"x\":2,\"y\":2}]}," +
"{\"t\":4000,\"lane\":\"M\",\"type\":\"tap\"}," + // 未配置座標
"{\"t\":5000,\"type\":\"tap\",\"pos\":{\"x\":9,\"y\":0}}," + // 超出格子
"{\"t\":6000,\"type\":\"spin\",\"pos\":{\"x\":1,\"y\":1}}]}"; // 不認識的型別
[Test]
public void Parses_unified_format_and_skips_unusable_notes()
{
var ev = ChartLoader.Parse(Chart, 0, out int skipped);
Assert.AreEqual(3, skipped);
Assert.AreEqual(5, ev.Count);
Assert.AreEqual("tap", ev[0].type);
Assert.AreEqual(1.0, ev[0].hitTime, 1e-9);
Assert.AreEqual((0, 3), (ev[0].x, ev[0].y));
Assert.AreEqual("hold", ev[1].type);
Assert.AreEqual(500, ev[1].holdMs);
// slide:第一格在 t、最後一格在 end,中間平均分布
Assert.AreEqual("slide", ev[2].type);
Assert.AreEqual(3.0, ev[2].hitTime, 1e-9);
Assert.AreEqual(3.2, ev[3].hitTime, 1e-9);
Assert.AreEqual(3.4, ev[4].hitTime, 1e-9);
Assert.AreEqual((2, 2), (ev[4].x, ev[4].y));
}
[Test]
public void Applies_pack_offset()
{
var ev = ChartLoader.Parse(Chart, 30, out _);
Assert.AreEqual(1.03, ev[0].hitTime, 1e-9);
Assert.AreEqual(3.43, ev[4].hitTime, 1e-9);
}
[Test]
public void Accepts_bare_note_array()
{
var ev = ChartLoader.Parse("[{\"t\":500,\"type\":\"tap\",\"pos\":{\"x\":2,\"y\":2}}]", 0, out int skipped);
Assert.AreEqual(1, ev.Count);
Assert.AreEqual(0, skipped);
}
}
public class SongInfoTests
{
[Test]
public void Parses_config()
{
var s = SongInfo.FromConfig(
"{\"id\":\"my-song\",\"name\":\"N\",\"artist\":\"A\",\"bpm\":174.5,\"beat_offset\":1216,\"offset\":-10," +
"\"preview_start\":60000,\"easy_num\":4,\"hard_num\":15}", "x");
Assert.AreEqual("my-song", s.id);
Assert.AreEqual(174.5, s.bpm);
Assert.AreEqual(-10, s.offsetMs);
Assert.AreEqual(60000, s.previewStartMs);
Assert.AreEqual(15000, s.previewLengthMs); // 預設值
Assert.AreEqual(4, s.levelNum[1]);
Assert.AreEqual(0, s.levelNum[2]);
Assert.AreEqual(15, s.levelNum[3]);
}
[TestCase("{\"id\":\"Bad ID\",\"name\":\"N\"}")]
[TestCase("{\"id\":\"ok-id\"}")]
[TestCase("[1,2]")]
public void Rejects_invalid_config(string json)
{
Assert.Throws<FormatException>(() => SongInfo.FromConfig(json, "x"));
}
}
public class PackImporterTests
{
const string Config = "{\"id\":\"test-song\",\"name\":\"Test\",\"artist\":\"A\",\"bpm\":120,\"normal_num\":5}";
private string tempRoot;
[SetUp]
public void SetUp()
{
tempRoot = Path.Combine(Path.GetTempPath(), "rhyculator-test-" + Path.GetRandomFileName());
Directory.CreateDirectory(tempRoot);
SongLibrary.rootOverride = tempRoot;
}
[TearDown]
public void TearDown()
{
SongLibrary.rootOverride = null;
SongLibrary.Reload();
if (Directory.Exists(tempRoot)) Directory.Delete(tempRoot, true);
}
static MemoryStream MakeZip(string prefix, params (string name, string content)[] files)
{
var ms = new MemoryStream();
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var (name, content) in files)
{
var entry = zip.CreateEntry(prefix + name);
using var w = new StreamWriter(entry.Open(), new UTF8Encoding(false));
w.Write(content);
}
}
ms.Position = 0;
return ms;
}
[Test]
public void Imports_pack_into_library_and_ignores_other_entries()
{
var zip = MakeZip("",
("config.json", Config), ("normal.json", "{\"format\":1,\"notes\":[]}"),
("music.ogg", "x"), ("image.png", "x"),
("README.txt", "ignored"), ("../evil.txt", "ignored"), ("sub/easy.json", "ignored"));
var info = PackImporter.Import(zip);
Assert.AreEqual("test-song", info.id);
string dir = Path.Combine(tempRoot, "test-song");
Assert.IsTrue(File.Exists(Path.Combine(dir, "normal.json")));
Assert.IsFalse(File.Exists(Path.Combine(dir, "README.txt")));
Assert.IsFalse(File.Exists(Path.Combine(dir, "easy.json")));
Assert.IsFalse(File.Exists(Path.Combine(tempRoot, "evil.txt")));
Assert.IsTrue(info.HasLevel(2));
Assert.IsFalse(info.HasLevel(1));
Assert.AreEqual(2, info.FirstLevel());
Assert.AreEqual(5, info.levelNum[2]);
SongLibrary.Reload();
Assert.AreEqual(1, SongLibrary.Songs.Count);
Assert.AreEqual("test-song", SongLibrary.Find("test-song").id);
}
[Test]
public void Accepts_folder_prefix_and_reimport_replaces_old_files()
{
PackImporter.Import(MakeZip("test-song/", ("config.json", Config), ("easy.json", "{\"notes\":[]}"), ("music.wav", "x"), ("image.png", "x")));
PackImporter.Import(MakeZip("", ("config.json", Config), ("hard.json", "{\"notes\":[]}"), ("music.ogg", "x"), ("image.png", "x")));
string dir = Path.Combine(tempRoot, "test-song");
Assert.IsTrue(File.Exists(Path.Combine(dir, "hard.json")));
Assert.IsFalse(File.Exists(Path.Combine(dir, "easy.json")), "重新匯入要整個取代舊資料夾");
Assert.IsFalse(File.Exists(Path.Combine(dir, "music.wav")));
}
[Test]
public void Rejects_incomplete_packs_without_leaving_files()
{
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("easy.json", "{}"))));
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("music.ogg", "x"), ("image.png", "x"))));
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("image.png", "x"))));
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("music.ogg", "x"))));
Assert.Throws<PackImporter.ImportException>(() => PackImporter.Import(MakeZip("", ("config.json", "{\"id\":\"BAD\",\"name\":\"n\"}"), ("easy.json", "{}"), ("music.ogg", "x"), ("image.png", "x"))));
Assert.IsFalse(Directory.Exists(Path.Combine(tempRoot, "test-song")));
}
[Test]
public void Inbox_imports_and_deletes_zips()
{
string inbox = SongLibrary.InboxDir;
Directory.CreateDirectory(inbox);
File.WriteAllBytes(Path.Combine(inbox, "a.zip"),
MakeZip("", ("config.json", Config), ("easy.json", "{\"notes\":[]}"), ("music.ogg", "x"), ("image.png", "x")).ToArray());
File.WriteAllText(Path.Combine(inbox, "bad.zip"), "not a zip");
var errors = new List<string>();
var imported = PackImporter.ImportInbox(errors);
Assert.AreEqual(1, imported.Count);
Assert.AreEqual(1, errors.Count);
Assert.AreEqual(0, Directory.GetFiles(inbox, "*.zip").Length);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c5a3d21ba96d03444887bfd99babe8d0
@@ -0,0 +1,24 @@
{
"name": "Rhyculator.Tests.EditMode",
"rootNamespace": "",
"references": [
"Rhyculator.Game",
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a945ca22aa3a0304cb55013aa3367282
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: