- 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]>
108 lines
4.6 KiB
C#
108 lines
4.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
|
||
// 曲包(.zip)檢查與解壓。曲包格式見 RhyculatorMaker 的 README
|
||
// 不使用 UnityEngine,可在背景執行緒與 EditMode 測試中使用
|
||
public static class PackReader
|
||
{
|
||
public class PackException : Exception
|
||
{
|
||
public PackException(string message) : base(message) { }
|
||
}
|
||
|
||
// 只解出這些檔名;zip 內其他東西一律忽略(也避免 zip 內的路徑寫到別處)
|
||
private static readonly HashSet<string> Allowed = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||
{
|
||
SongInfo.ConfigName, SongInfo.ImageName, SongInfo.VideoName,
|
||
"easy.json", "normal.json", "hard.json",
|
||
"music.ogg", "music.wav", "music.mp3",
|
||
};
|
||
private const long MaxEntryBytes = 512L * 1024 * 1024;
|
||
|
||
// 檢查 zip 並解壓到 destDir(先解到暫存資料夾,全部成功才取代 destDir)
|
||
public static SongInfo Extract(string zipPath, string destDir)
|
||
{
|
||
using var fs = File.OpenRead(zipPath);
|
||
return Extract(fs, destDir);
|
||
}
|
||
|
||
public static SongInfo Extract(Stream zipStream, string destDir)
|
||
{
|
||
using var zip = new ZipArchive(zipStream, ZipArchiveMode.Read, leaveOpen: true);
|
||
var files = Collect(zip, out string configText);
|
||
|
||
SongInfo info;
|
||
try { info = SongInfo.FromConfig(configText, null); }
|
||
catch (FormatException e) { throw new PackException(e.Message); }
|
||
|
||
bool hasChart = false;
|
||
foreach (var n in SongInfo.LevelNames) if (files.ContainsKey(n + ".json")) hasChart = true;
|
||
if (!hasChart) throw new PackException("曲包沒有任何難度的譜面");
|
||
bool hasMusic = false;
|
||
foreach (var n in SongInfo.MusicNames) if (files.ContainsKey(n)) hasMusic = true;
|
||
if (!hasMusic) throw new PackException("曲包沒有音樂(music.ogg / wav / mp3)");
|
||
if (!files.ContainsKey(SongInfo.ImageName)) throw new PackException("曲包沒有封面 image.png");
|
||
|
||
string temp = destDir.TrimEnd('/', '\\') + ".tmp";
|
||
if (Directory.Exists(temp)) Directory.Delete(temp, true);
|
||
Directory.CreateDirectory(temp);
|
||
try
|
||
{
|
||
foreach (var kv in files)
|
||
{
|
||
using var src = kv.Value.Open();
|
||
using var dst = File.Create(Path.Combine(temp, kv.Key));
|
||
src.CopyTo(dst);
|
||
}
|
||
if (Directory.Exists(destDir)) Directory.Delete(destDir, true);
|
||
Directory.Move(temp, destDir);
|
||
}
|
||
catch
|
||
{
|
||
if (Directory.Exists(temp)) Directory.Delete(temp, true);
|
||
throw;
|
||
}
|
||
return SongInfo.FromDir(destDir);
|
||
}
|
||
|
||
// 白名單內的檔案(檔名小寫 → entry);允許整個資料夾被壓進去:以最淺的 config.json 所在位置為根
|
||
private static Dictionary<string, ZipArchiveEntry> Collect(ZipArchive zip, out string configText)
|
||
{
|
||
ZipArchiveEntry config = null;
|
||
foreach (var e in zip.Entries)
|
||
{
|
||
if (!string.Equals(e.Name, SongInfo.ConfigName, StringComparison.OrdinalIgnoreCase)) continue;
|
||
if (config == null || e.FullName.Length < config.FullName.Length) config = e;
|
||
}
|
||
if (config == null) throw new PackException("zip 裡沒有 config.json,不是曲包");
|
||
string prefix = config.FullName.Substring(0, config.FullName.Length - config.Name.Length);
|
||
|
||
var files = new Dictionary<string, ZipArchiveEntry>();
|
||
foreach (var e in zip.Entries)
|
||
{
|
||
if (!e.FullName.StartsWith(prefix, StringComparison.Ordinal)) continue;
|
||
string rel = e.FullName.Substring(prefix.Length);
|
||
if (rel.Length == 0 || rel.IndexOf('/') >= 0 || rel.IndexOf('\\') >= 0) continue; // 目錄項目、子資料夾
|
||
if (!Allowed.Contains(rel)) continue;
|
||
if (e.Length > MaxEntryBytes) throw new PackException($"{rel} 太大");
|
||
files[rel.ToLowerInvariant()] = e;
|
||
}
|
||
|
||
using var reader = new StreamReader(config.Open(), System.Text.Encoding.UTF8);
|
||
configText = reader.ReadToEnd();
|
||
return files;
|
||
}
|
||
|
||
// 給玩家看的錯誤訊息
|
||
public static string Describe(Exception e)
|
||
{
|
||
if (e is PackException || e is FormatException) return e.Message;
|
||
if (e is InvalidDataException) return "不是有效的 zip 檔";
|
||
if (e is IOException) return "檔案讀取失敗(可能還在複製中)";
|
||
if (e is UnauthorizedAccessException) return "沒有讀取權限";
|
||
return e.GetType().Name + ": " + e.Message;
|
||
}
|
||
}
|