|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Windows.Media; |
| 3 | +using System.Windows.Media.Imaging; |
| 4 | +using ChuChartManager.CLI.Utils; |
| 5 | +using Spectre.Console; |
| 6 | +using Spectre.Console.Cli; |
| 7 | + |
| 8 | +namespace ChuChartManager.CLI.Commands; |
| 9 | + |
| 10 | +public class ExportJacketCommand : AsyncCommand<ExportJacketCommand.Settings> |
| 11 | +{ |
| 12 | + public class Settings : GameSettings |
| 13 | + { |
| 14 | + [CommandOption("-i|--id <ID>")] |
| 15 | + [Description("导出指定 ID 的封面")] |
| 16 | + [DefaultValue(-1)] |
| 17 | + public int MusicId { get; set; } |
| 18 | + |
| 19 | + [CommandOption("-a|--all")] |
| 20 | + [Description("导出全部封面")] |
| 21 | + [DefaultValue(false)] |
| 22 | + public bool All { get; set; } |
| 23 | + |
| 24 | + [CommandOption("-o|--output <DIR>")] |
| 25 | + [Description("输出目录(默认当前目录)")] |
| 26 | + public string? OutputDir { get; set; } |
| 27 | + |
| 28 | + public override ValidationResult Validate() |
| 29 | + { |
| 30 | + var baseResult = base.Validate(); |
| 31 | + if (!baseResult.Successful) return baseResult; |
| 32 | + |
| 33 | + if (MusicId < 0 && !All) |
| 34 | + return ValidationResult.Error("请指定 -i <ID> 导出单曲封面,或 -a 导出全部"); |
| 35 | + |
| 36 | + return ValidationResult.Success(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public override async Task<int> ExecuteAsync(CommandContext context, Settings settings) |
| 41 | + { |
| 42 | + var outputDir = settings.OutputDir ?? Directory.GetCurrentDirectory(); |
| 43 | + Directory.CreateDirectory(outputDir); |
| 44 | + |
| 45 | + var scanner = new MusicScanner(settings.GamePath); |
| 46 | + AnsiConsole.MarkupLine("[dim]正在扫描...[/]"); |
| 47 | + await Task.Run(scanner.ScanAll); |
| 48 | + |
| 49 | + var allMusic = scanner.MusicBySource.Values.SelectMany(x => x); |
| 50 | + var targets = settings.All |
| 51 | + ? allMusic.ToList() |
| 52 | + : allMusic.Where(m => m.Id == settings.MusicId).ToList(); |
| 53 | + |
| 54 | + if (targets.Count == 0) |
| 55 | + { |
| 56 | + AnsiConsole.MarkupLine(settings.All |
| 57 | + ? "[red]扫描完成但未找到任何曲目[/]" |
| 58 | + : $"[red]未找到 ID={settings.MusicId} 的曲目[/]"); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + |
| 62 | + int done = 0, failed = 0; |
| 63 | + foreach (var music in targets) |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + TerminalProgress.Set(done * 100 / targets.Count); |
| 68 | + var jacketPath = music.GetJacketFullPath(); |
| 69 | + if (jacketPath == null) |
| 70 | + { |
| 71 | + AnsiConsole.MarkupLine($"[yellow]跳过[/] #{music.Id:D4} {Markup.Escape(music.Name)} — 未找到封面文件"); |
| 72 | + failed++; |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + var outPath = Path.Combine(outputDir, $"{music.Id:D4}.png"); |
| 77 | + await Task.Run(() => ConvertToPng(jacketPath, outPath)); |
| 78 | + AnsiConsole.MarkupLine($"[green]✓[/] #{music.Id:D4} {Markup.Escape(music.Name)} → {outPath}"); |
| 79 | + done++; |
| 80 | + } |
| 81 | + catch (Exception ex) |
| 82 | + { |
| 83 | + AnsiConsole.MarkupLine($"[red]✗[/] #{music.Id:D4} {Markup.Escape(music.Name)}: {Markup.Escape(ex.Message)}"); |
| 84 | + failed++; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + TerminalProgress.Clear(); |
| 89 | + AnsiConsole.MarkupLine($"\n[green]完成: {done} 张导出[/]{(failed > 0 ? $", [yellow]{failed} 张失败[/]" : "")}"); |
| 90 | + return failed > 0 ? 1 : 0; |
| 91 | + } |
| 92 | + |
| 93 | + private static void ConvertToPng(string ddsPath, string outPath) |
| 94 | + { |
| 95 | + BitmapSource bmp; |
| 96 | + if (ddsPath.EndsWith(".dds", StringComparison.OrdinalIgnoreCase)) |
| 97 | + { |
| 98 | + using var pfim = Pfim.Pfimage.FromFile(ddsPath); |
| 99 | + if (pfim.Compressed) pfim.Decompress(); |
| 100 | + var fmt = pfim.Format == Pfim.ImageFormat.Rgba32 |
| 101 | + ? PixelFormats.Bgra32 : PixelFormats.Bgr24; |
| 102 | + bmp = BitmapSource.Create( |
| 103 | + pfim.Width, pfim.Height, 96, 96, fmt, null, |
| 104 | + pfim.Data, pfim.Stride); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + var img = new BitmapImage(new Uri(Path.GetFullPath(ddsPath))); |
| 109 | + img.Freeze(); |
| 110 | + bmp = img; |
| 111 | + } |
| 112 | + |
| 113 | + var encoder = new PngBitmapEncoder(); |
| 114 | + encoder.Frames.Add(BitmapFrame.Create(bmp)); |
| 115 | + using var fs = File.Create(outPath); |
| 116 | + encoder.Save(fs); |
| 117 | + } |
| 118 | +} |
0 commit comments