using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; // 曲包(.zip)匯入:檢查內容 → 解壓到 songs//(同 id 的舊曲包會被覆蓋) public static class PackImporter { public class ImportException : Exception { public ImportException(string message) : base(message) { } } // 只解出這些檔名;zip 內其他東西一律忽略(也避免 zip 內的路徑寫到別處) private static readonly HashSet Allowed = new HashSet(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; public static SongInfo Import(string zipPath) { using var fs = File.OpenRead(zipPath); return Import(fs); } public static SongInfo Import(Stream zipStream) { using var zip = new ZipArchive(zipStream, ZipArchiveMode.Read, leaveOpen: true); // 允許整個資料夾被壓進去(檔案都在同一層子資料夾):以最淺的 config.json 所在位置為根 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 ImportException("zip 裡沒有 config.json,不是曲包"); string prefix = config.FullName.Substring(0, config.FullName.Length - config.Name.Length); var files = new Dictionary(); 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 ImportException($"{rel} 太大"); files[rel.ToLowerInvariant()] = e; } string configText; using (var reader = new StreamReader(config.Open(), System.Text.Encoding.UTF8)) configText = reader.ReadToEnd(); SongInfo info; try { info = SongInfo.FromConfig(configText, null); } catch (FormatException e) { throw new ImportException(e.Message); } bool hasChart = false; foreach (var n in SongInfo.LevelNames) if (files.ContainsKey(n + ".json")) hasChart = true; if (!hasChart) throw new ImportException("曲包沒有任何難度的譜面"); bool hasMusic = false; foreach (var n in SongInfo.MusicNames) if (files.ContainsKey(n)) hasMusic = true; if (!hasMusic) throw new ImportException("曲包沒有音樂(music.ogg / wav / mp3)"); if (!files.ContainsKey(SongInfo.ImageName)) throw new ImportException("曲包沒有封面 image.png"); // 先解到暫存資料夾,全部成功才取代舊的 string root = SongLibrary.RootDir; Directory.CreateDirectory(root); string final = Path.Combine(root, info.id); string temp = Path.Combine(root, info.id + ".importing"); 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(final)) Directory.Delete(final, true); Directory.Move(temp, final); } catch { if (Directory.Exists(temp)) Directory.Delete(temp, true); throw; } return SongInfo.FromDir(final); } // 匯入收件匣裡的所有 *.zip;不論成功失敗都會刪掉 zip,避免每次開啟都重試 public static List ImportInbox(List errors) { var imported = new List(); string inbox = SongLibrary.InboxDir; if (!Directory.Exists(inbox)) return imported; foreach (var zip in Directory.GetFiles(inbox, "*.zip")) { try { imported.Add(Import(zip)); } catch (Exception e) { errors.Add($"{Path.GetFileName(zip)}:{Describe(e)}"); } try { File.Delete(zip); } catch { } } return imported; } // 給玩家看的錯誤訊息 public static string Describe(Exception e) { if (e is ImportException || e is FormatException) return e.Message; if (e is InvalidDataException) return "不是有效的 zip 檔"; return e.GetType().Name + ": " + e.Message; } }