From 2d2f086662cc2414aa237c75150245fccc4971bd Mon Sep 17 00:00:00 2001 From: Damon Lu <59256766+WhatDamon@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96+=E6=9E=81?= =?UTF-8?q?=E5=85=B6=E6=9C=89=E9=99=90=E7=9A=84=E5=86=85=E5=AD=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- WndMain.cs | 240 +++++++++++++++++++++++------------------------------ 2 files changed, 105 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index 99a777f..ef63655 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ ## 特点 - 小体积 -- 低内存占用 +- 低内存占用(前提是少量,目前还需要完善内存清理) - 功能强悍 - 开箱即用 diff --git a/WndMain.cs b/WndMain.cs index 5701e44..94bcb46 100644 --- a/WndMain.cs +++ b/WndMain.cs @@ -102,24 +102,40 @@ protected override void WndProc(ref Message m) switch ((SystemMenuItem)m.WParam) { case SystemMenuItem.About: - TaskDialog.ShowDialog(this, new TaskDialogPage() + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append("版本:v1.0.2.1\n基于libncmdump开发\n使用MIT许可证开源\n其他依赖:\n · Costura.Fody\n · NAudio\n当前.NET版本:").Append(Environment.Version.ToString()); + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "版本:v1.0.2.1\n基于libncmdump开发\n使用MIT许可证开源\n其他依赖:\n · Costura.Fody\n · NAudio\n当前.NET版本:" + Environment.Version.ToString(), - Heading = "NCMDumpGUI", - Caption = "关于", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Information, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog(stringBuilder.ToString(), "NCMDumpGUI", "关于", TaskDialogIcon.Information, buttons, TaskDialogButton.OK); break; } } } #endregion + public TaskDialogButton showTaskDialog(string text, string heading, string caption, TaskDialogIcon icon, TaskDialogButton[] buttons, TaskDialogButton defaultBtn) + { + TaskDialogPage page = new TaskDialogPage() + { + Text = text, + Heading = heading, + Caption = caption, + Icon = icon, + DefaultButton = defaultBtn + }; + + foreach (var button in buttons) + { + page.Buttons.Add(button); + } + + TaskDialogButton result = TaskDialog.ShowDialog(this, page); + return result; + } + + // “浏览”按钮被点击 private void browseButton_Click(object sender, EventArgs e) { @@ -150,7 +166,7 @@ public class FileFolderSizeRetriever { public static string GetSizeAsString(string path, bool includeSubdirectories) { - long fileSizeBytes = 0; // 初始化变量 + long fileSizeBytes = 0; if (File.Exists(path)) { FileInfo fileInfo = new FileInfo(path); @@ -228,18 +244,13 @@ private bool CheckNCMBinary(string filePath) { if (fileFolderComboBox.SelectedIndex == 0) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append("文件头为:").Append(header); + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "文件头为:" + header, - Heading = "不是ncm文件", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog(stringBuilder.ToString(), "不是ncm文件", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "不是ncm文件!"; } return false; @@ -249,18 +260,11 @@ private bool CheckNCMBinary(string filePath) { if (fileFolderComboBox.SelectedIndex == 0) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "不是ncm文件", - Heading = "文件大小异常", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("文件大小异常", "不是ncm文件", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "文件大小异常,并不是ncm文件"; } return false; @@ -271,18 +275,13 @@ private bool CheckNCMBinary(string filePath) { if (fileFolderComboBox.SelectedIndex == 0) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append("详细信息:").Append(ex.Message); + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "详细信息:" + ex.Message, - Heading = "发生错误", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog(stringBuilder.ToString(), "发生错误", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "载入文件时发生错误"; } return false; @@ -307,6 +306,7 @@ public static bool IsValidFilePath(string path) } } + // 处理文件 public int ProcessNCMFile(string path) { NeteaseCrypt neteaseCrypt = new NeteaseCrypt(path); @@ -327,20 +327,12 @@ private void convertButton_Click(object sender, EventArgs e) filesizeLabel.Text = FileFolderSizeRetriever.GetSizeAsString(filepathTextBox.Text, scanMoreFoldersCheckBox.Checked); if (!File.Exists(GlobalVariables.libncmdumpPath)) { - TaskDialogButton result = TaskDialog.ShowDialog(this, new TaskDialogPage() - { - Text = "请确认libncmdump.dll与本程序在同一目录", - Heading = "核心不存在", - Caption = "错误", - Buttons = + TaskDialogButton[] buttons = new TaskDialogButton[] { TaskDialogButton.Retry, - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); - if (result == TaskDialogButton.Retry) + TaskDialogButton.OK, + }; + if (showTaskDialog("请确认libncmdump.dll与本程序在同一目录", "核心不存在", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK) == TaskDialogButton.Retry) { convertButton.PerformClick(); } @@ -348,34 +340,20 @@ private void convertButton_Click(object sender, EventArgs e) } else if (filepathTextBox.Text == "") { - TaskDialog.ShowDialog(this, new TaskDialogPage() - { - Text = "请提供ncm文件路径", - Heading = "文件路径为空!", - Caption = "错误", - Buttons = + TaskDialogButton[] buttons = new TaskDialogButton[] { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("请提供ncm文件路径", "文件路径为空!", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "请提供文件"; } else if (!IsValidFilePath(filepathTextBox.Text)) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "文件路径中包含非法字符!", - Heading = "非法文件路径", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("文件路径中包含非法字符!", "非法文件路径", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "非法文件路径"; } else @@ -413,25 +391,21 @@ private void convertButton_Click(object sender, EventArgs e) toolStripProgressBar1.Value = allProcessedFiles; toolStripStatusLabel2.Text = "已处理:" + allProcessedFiles.ToString(); } - toolStripStatusLabel2.Text = "成功:" + processedFiles.ToString() + ";失败:" + bypassFiles.ToString() + ";跳过:" + unprocessFiles.ToString(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append("成功:").Append(processedFiles.ToString()).Append(";失败:").Append(bypassFiles.ToString()).Append(";跳过:").Append(unprocessFiles.ToString()); + // toolStripStatusLabel2.Text = "成功:" + processedFiles.ToString() + ";失败:" + bypassFiles.ToString() + ";跳过:" + unprocessFiles.ToString(); + toolStripStatusLabel2.Text = stringBuilder.ToString(); } else if (fileFolderComboBox.SelectedIndex == 0) { toolStripProgressBar1.Maximum = 2; if (!filepathTextBox.Text.EndsWith(".ncm")) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "请提供ncm文件路径", - Heading = "文件路径为空!", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("请提供ncm文件路径", "文件路径为空!", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "请提供正确的ncm文件"; } else if (CheckNCMBinary(filepathTextBox.Text)) @@ -460,7 +434,10 @@ private void convertButton_Click(object sender, EventArgs e) displayFileName = Path.GetFileName(resultAudioPath); } - toolStripStatusLabel2.Text = "完成!文件名:" + displayFileName; + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append("完成!文件名:").Append(displayFileName); + // toolStripStatusLabel2.Text = "完成!文件名:" + displayFileName; + toolStripStatusLabel2.Text = stringBuilder.ToString(); toolStripProgressBar1.Value += 1; playGroupBox.Enabled = true; } @@ -506,6 +483,10 @@ private void WndMain_DragDrop(object sender, DragEventArgs e) { string path = filePaths[0]; modifyByDrag = true; + if (path.Length > 1) + { + toolStripStatusLabel2.Text = "提供了多个文件,仅加载了第一个"; + } if (Directory.Exists(path)) { filepathTextBox.Text = path; @@ -545,18 +526,11 @@ private void fileFolderComboBox_SelectedIndexChanged(object sender, EventArgs e) private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = "此应用只用于学习用途,禁止用于商业或违法用途,\n请在遵守NCM文件提供平台的服务条款下使用本应用,\n作者对商业或违法使用本软件造成的任何后果不承担任何责任!", - Heading = "注意!", - Caption = "免责声明", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Information, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("此应用只用于学习用途,禁止用于商业或违法用途,\n请在遵守NCM文件提供平台的服务条款下使用本应用,\n作者对商业或违法使用本软件造成的任何后果不承担任何责任!", "注意!", "免责声明", TaskDialogIcon.Information, buttons, TaskDialogButton.OK); } private void filepathTextBox_TextChanged(object sender, EventArgs e) @@ -601,6 +575,14 @@ private void audioPlay() outputDevice.Init(audioFileReader); } + if (timerTrackBar == null) + { + timerTrackBar = new System.Windows.Forms.Timer(); + timerTrackBar.Interval = 1000; + timerTrackBar.Tick += timerTrackBar_Tick; + timerTrackBar.Start(); + } + if (outputDevice.PlaybackState != PlaybackState.Playing) { outputDevice.Play(); @@ -639,6 +621,13 @@ private void audioStop() playResumeButton.Text = "▶️"; audioProgressTrackBar.Value = 0; audioProgressLabel.Text = "00:00 / 00:00"; + if (timerTrackBar != null) + { + timerTrackBar.Stop(); + timerTrackBar.Tick -= timerTrackBar_Tick; + timerTrackBar.Dispose(); + timerTrackBar = null; + } } private void timerTrackBar_Tick(object sender, EventArgs e) @@ -699,18 +688,11 @@ private void autoDumpFolderOnChanged(object source, FileSystemEventArgs e) private void autoDumpOnError(object source, ErrorEventArgs e) { - TaskDialog.ShowDialog(this, new TaskDialogPage() + TaskDialogButton[] buttons = new TaskDialogButton[] { - Text = $"{e.GetException()}", - Heading = "发生错误", - Caption = "错误", - Buttons = - { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog($"{e.GetException()}", "发生错误", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "发生错误"; } @@ -719,18 +701,11 @@ private void autoDumpToolStripMenuItem_Click(object sender, EventArgs e) Debug.WriteLine(filepathTextBox.Text); if (filepathTextBox.Text == "") { - TaskDialog.ShowDialog(this, new TaskDialogPage() - { - Text = "请提供ncm文件路径", - Heading = "文件路径为空!", - Caption = "错误", - Buttons = + TaskDialogButton[] buttons = new TaskDialogButton[] { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("请提供ncm文件路径", "文件路径为空!", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "请提供文件"; } else if (Directory.Exists(filepathTextBox.Text)) @@ -758,18 +733,11 @@ private void autoDumpToolStripMenuItem_Click(object sender, EventArgs e) } else { - TaskDialog.ShowDialog(this, new TaskDialogPage() - { - Text = "请确认路径是否正确", - Heading = "找不到文件", - Caption = "错误", - Buttons = + TaskDialogButton[] buttons = new TaskDialogButton[] { - TaskDialogButton.OK - }, - Icon = TaskDialogIcon.Error, - DefaultButton = TaskDialogButton.OK - }); + TaskDialogButton.OK, + }; + showTaskDialog("请确认路径是否正确", "找不到文件", "错误", TaskDialogIcon.Error, buttons, TaskDialogButton.OK); toolStripStatusLabel2.Text = "请确认路径是否正确"; } }