- One empty scene; App builds the canvas, safe-area layout and a shared 5x6 Grid in code. Pages (Intro, Main, Choose, Play, Account, Settings) replace 5 scenes, 5 prefabs and the per-cell spawner/display components. - Packs: the game reads Rhyculator/package/ (with All files access) or the app folder, like a plugged-in USB stick. Zips stay where they are and are extracted to a cache keyed by name/size/mtime; folders are read directly. Scans run every second on a background thread. Old songs/ imports and inbox zips are migrated automatically. - Download packs by 8-char code from the new pack website, or via rhyculator://pack/<code> links; opened/shared zips go into the package folder. - Play: pause/resume/retry, auto-pause on background, combo and progress bar; judging and scoring unchanged. Choose page fits 9:16 screens; empty state, pack delete. Android back key handled everywhere. - Slimming: removed TextMesh Pro, unused 2D/Timeline/VisualScripting/etc. packages and modules, scene template, unused sprites; menu theme wav (20MB) -> ogg (2.3MB). Build profile replaced by explicit settings in BuildAndroid. - Tests: PackLibrary/PackReader/migration/serializer tests (39 passing). Co-Authored-By: Claude Opus 5.5 <[email protected]>
202 lines
8.3 KiB
C#
202 lines
8.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
|
|
public class PackLibraryTests
|
|
{
|
|
const string Config = "{\"id\":\"test-song\",\"name\":\"Test\",\"artist\":\"A\",\"bpm\":120,\"normal_num\":5}";
|
|
const string Config2 = "{\"id\":\"other\",\"name\":\"Other\",\"artist\":\"B\",\"easy_num\":2}";
|
|
private string root, package, cache;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
root = Path.Combine(Path.GetTempPath(), "rhyculator-test-" + Path.GetRandomFileName());
|
|
package = Path.Combine(root, "package");
|
|
cache = Path.Combine(root, "cache");
|
|
Directory.CreateDirectory(package);
|
|
PackLibrary.ResetState();
|
|
PackLibrary.SettleTime = TimeSpan.Zero;
|
|
PackStorage.rootOverride = root;
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
PackLibrary.SettleTime = TimeSpan.FromSeconds(2);
|
|
PackLibrary.ResetState();
|
|
PackStorage.rootOverride = null;
|
|
if (Directory.Exists(root)) Directory.Delete(root, true);
|
|
}
|
|
|
|
static byte[] 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)
|
|
{
|
|
using var w = new StreamWriter(zip.CreateEntry(prefix + name).Open(), new UTF8Encoding(false));
|
|
w.Write(content);
|
|
}
|
|
return ms.ToArray();
|
|
}
|
|
|
|
static byte[] GoodPack(string config = Config, string chart = "normal.json")
|
|
=> MakeZip("", ("config.json", config), (chart, "{\"format\":1,\"notes\":[]}"), ("music.ogg", "x"), ("image.png", "x"));
|
|
|
|
void Put(string name, byte[] data, int ageSeconds = 10)
|
|
{
|
|
string path = Path.Combine(package, name);
|
|
File.WriteAllBytes(path, data);
|
|
File.SetLastWriteTimeUtc(path, DateTime.UtcNow.AddSeconds(-ageSeconds));
|
|
}
|
|
|
|
[Test]
|
|
public void Reader_extracts_only_whitelisted_files()
|
|
{
|
|
string dest = Path.Combine(root, "out");
|
|
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 = PackReader.Extract(new MemoryStream(zip), dest);
|
|
|
|
Assert.AreEqual("test-song", info.id);
|
|
Assert.IsTrue(File.Exists(Path.Combine(dest, "normal.json")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(dest, "README.txt")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(dest, "easy.json")));
|
|
Assert.IsFalse(File.Exists(Path.Combine(root, "evil.txt")));
|
|
Assert.AreEqual(2, info.FirstLevel());
|
|
Assert.AreEqual(5, info.levelNum[2]);
|
|
}
|
|
|
|
[Test]
|
|
public void Reader_accepts_folder_prefix()
|
|
{
|
|
var info = PackReader.Extract(new MemoryStream(MakeZip("My Song/",
|
|
("config.json", Config), ("easy.json", "{\"notes\":[]}"), ("music.wav", "x"), ("image.png", "x"))), Path.Combine(root, "o"));
|
|
Assert.IsTrue(info.HasLevel(1));
|
|
}
|
|
|
|
[Test]
|
|
public void Reader_rejects_incomplete_packs_without_leaving_files()
|
|
{
|
|
string dest = Path.Combine(root, "bad");
|
|
Assert.Throws<PackReader.PackException>(() => PackReader.Extract(new MemoryStream(MakeZip("", ("easy.json", "{}"))), dest));
|
|
Assert.Throws<PackReader.PackException>(() => PackReader.Extract(new MemoryStream(MakeZip("", ("config.json", Config), ("music.ogg", "x"), ("image.png", "x"))), dest));
|
|
Assert.Throws<PackReader.PackException>(() => PackReader.Extract(new MemoryStream(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("image.png", "x"))), dest));
|
|
Assert.Throws<PackReader.PackException>(() => PackReader.Extract(new MemoryStream(MakeZip("", ("config.json", Config), ("easy.json", "{}"), ("music.ogg", "x"))), dest));
|
|
Assert.Throws<PackReader.PackException>(() => PackReader.Extract(new MemoryStream(MakeZip("", ("config.json", "{\"id\":\"BAD\",\"name\":\"n\"}"), ("easy.json", "{}"), ("music.ogg", "x"), ("image.png", "x"))), dest));
|
|
Assert.IsFalse(Directory.Exists(dest));
|
|
}
|
|
|
|
[Test]
|
|
public void Scan_reads_zips_and_folders_and_keeps_the_zip()
|
|
{
|
|
Put("a.zip", GoodPack());
|
|
string folder = Path.Combine(package, "Other Song");
|
|
Directory.CreateDirectory(folder);
|
|
File.WriteAllText(Path.Combine(folder, "config.json"), Config2);
|
|
File.WriteAllText(Path.Combine(folder, "easy.json"), "{\"notes\":[]}");
|
|
File.WriteAllText(Path.Combine(folder, "music.mp3"), "x");
|
|
|
|
var r = PackLibrary.Scan(package, cache);
|
|
Assert.AreEqual(2, r.songs.Count);
|
|
Assert.IsEmpty(r.errors);
|
|
Assert.IsTrue(r.changed);
|
|
Assert.IsTrue(File.Exists(Path.Combine(package, "a.zip")), "zip 留在 package 資料夾");
|
|
Assert.AreEqual(folder, r.songs.Find(s => s.id == "other").dir);
|
|
|
|
var again = PackLibrary.Scan(package, cache);
|
|
Assert.IsFalse(again.changed);
|
|
Assert.IsEmpty(again.added);
|
|
}
|
|
|
|
[Test]
|
|
public void Replacing_or_removing_a_zip_updates_the_cache()
|
|
{
|
|
Put("a.zip", GoodPack());
|
|
var first = PackLibrary.Scan(package, cache).songs[0];
|
|
Assert.IsTrue(first.HasLevel(2));
|
|
|
|
Put("a.zip", GoodPack(chart: "hard.json"), ageSeconds: 5);
|
|
var r = PackLibrary.Scan(package, cache);
|
|
Assert.IsTrue(r.changed);
|
|
Assert.IsTrue(r.songs[0].HasLevel(3));
|
|
Assert.IsFalse(r.songs[0].HasLevel(2));
|
|
Assert.IsFalse(Directory.Exists(first.dir), "舊的快取被清掉");
|
|
|
|
File.Delete(Path.Combine(package, "a.zip"));
|
|
r = PackLibrary.Scan(package, cache);
|
|
Assert.AreEqual(0, r.songs.Count);
|
|
Assert.AreEqual(0, Directory.GetDirectories(cache).Length);
|
|
}
|
|
|
|
[Test]
|
|
public void Bad_zip_is_reported_once_and_new_files_are_listed_as_added()
|
|
{
|
|
Put("a.zip", GoodPack());
|
|
PackLibrary.Scan(package, cache);
|
|
Put("bad.zip", Encoding.UTF8.GetBytes("not a zip"));
|
|
Put("b.zip", GoodPack(Config2, "easy.json"));
|
|
|
|
var r = PackLibrary.Scan(package, cache);
|
|
Assert.AreEqual(1, r.errors.Count);
|
|
StringAssert.Contains("bad.zip", r.errors[0]);
|
|
Assert.AreEqual(1, r.added.Count);
|
|
Assert.AreEqual("other", r.added[0].id);
|
|
Assert.IsEmpty(PackLibrary.Scan(package, cache).errors);
|
|
}
|
|
|
|
[Test]
|
|
public void Duplicate_ids_keep_the_newest_file()
|
|
{
|
|
Put("old.zip", GoodPack(), ageSeconds: 100);
|
|
Put("new.zip", GoodPack(chart: "hard.json"), ageSeconds: 10);
|
|
var r = PackLibrary.Scan(package, cache);
|
|
Assert.AreEqual(1, r.songs.Count);
|
|
StringAssert.EndsWith("new.zip", r.songs[0].source);
|
|
Assert.AreEqual(1, r.errors.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void Fresh_files_wait_until_settled()
|
|
{
|
|
PackLibrary.SettleTime = TimeSpan.FromSeconds(30);
|
|
Put("a.zip", GoodPack(), ageSeconds: 0);
|
|
var r = PackLibrary.Scan(package, cache);
|
|
Assert.IsTrue(r.pending);
|
|
Assert.AreEqual(0, r.songs.Count);
|
|
}
|
|
|
|
[Test]
|
|
public void Migrate_moves_old_imports_and_inbox_zips_into_package()
|
|
{
|
|
string songs = Path.Combine(root, "songs", "test-song");
|
|
Directory.CreateDirectory(songs);
|
|
File.WriteAllText(Path.Combine(songs, "config.json"), Config);
|
|
File.WriteAllBytes(Path.Combine(root, "inbox.zip"), GoodPack(Config2, "easy.json"));
|
|
|
|
PackStorage.Migrate(package);
|
|
Assert.IsTrue(File.Exists(Path.Combine(package, "test-song", "config.json")));
|
|
Assert.IsTrue(File.Exists(Path.Combine(package, "inbox.zip")));
|
|
Assert.IsFalse(Directory.Exists(Path.Combine(root, "songs")));
|
|
}
|
|
|
|
[Test]
|
|
public void Serialize_round_trips()
|
|
{
|
|
var d = new Dictionary<string, object> { ["s"] = "a\"b\n中", ["i"] = 3, ["f"] = 1.5, ["b"] = true, ["n"] = null, ["l"] = new List<object> { 1, "x" } };
|
|
var back = MiniJson.AsObject(MiniJson.Parse(MiniJson.Serialize(d)));
|
|
Assert.AreEqual("a\"b\n中", back["s"]);
|
|
Assert.AreEqual(3.0, back["i"]);
|
|
Assert.AreEqual(1.5, back["f"]);
|
|
Assert.AreEqual(true, back["b"]);
|
|
Assert.IsNull(back["n"]);
|
|
Assert.AreEqual(2, MiniJson.AsList(back["l"]).Count);
|
|
}
|
|
}
|