Skip to content

Commit

Permalink
批量处理文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatDamon committed Aug 20, 2024
1 parent 24e9137 commit c8aabf7
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 60 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
# NCMDumpGUI

> [!CAUTION]
> 此应用只用于学习用途,禁止用于商业或违法用途,请在遵守NCM文件提供平台的服务条款下使用本应用,作者对商业或违法使用本软件造成的任何后果不承担任何责任!
这是一个简单程序,使用到了 `ncmdump` 项目实现 ncm 解密功能

适用于对命令行一问三不知的小白用户

本项目使用 .NET 8 和 C# WinForm 编写,主体代码为 `ncmdump` 的示例,本项目只是写了一个用户界面

## 特点

- 小体积
- 低内存占用
- 功能强悍
- 开箱即用

## 下载

请在 [Releases](https://github.com/WhatDamon/NCMDumpGUI/releases) 页面下载最新版本

注意请提前安装好 .NET 8 运行时,点击[此处](https://windows.net)前往官网

仅适用于 Windows 平台
仅适用于 Windows 平台,系统支持详见[此处](https://github.com/dotnet/core/blob/main/release-notes/8.0/supported-os.md#Windows)

如果您正在使用 Windows 不受支持,可以修改项目属性 .NET 版本为 .NET 6 以获得对 Windows 7、8.1 等版本的支持

## TODO

- [x] 基本用户界面
- [x] 异常处理
- [x] 批量处理
- [ ] 更详细的状态信息
- [ ] 批量处理
- [ ] 导出位置选择

## 鸣谢

Expand Down
23 changes: 18 additions & 5 deletions WndMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 115 additions & 53 deletions WndMain.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
Expand All @@ -14,6 +15,7 @@ public WndMain()
InitializeComponent();
toolTip.SetToolTip(fixMetaDataCheckBox, "将歌曲的详细信息添加到转换后的文件\n注意:不能保证100%正常工作,部分元数据可能无法修复!");
toolTip.SetToolTip(convertButton, "点击开始转换文件到能被主流播放器识别的格式");
fileFolderComboBox.SelectedIndex = 0;

}

Expand Down Expand Up @@ -63,7 +65,7 @@ protected override void WndProc(ref Message m)
switch ((SystemMenuItem)m.WParam)
{
case SystemMenuItem.About:
MessageBox.Show("NCMDumpGUI v1.0.0.3\n基于libncmdump开发\n使用MIT许可证开源", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("NCMDumpGUI v1.0.1.0\n基于libncmdump开发\n使用MIT许可证开源", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
Expand All @@ -73,12 +75,26 @@ protected override void WndProc(ref Message m)
// “浏览”按钮被点击
private void browseButton_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "NCM加密歌曲|*.ncm";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
if (fileFolderComboBox.SelectedIndex == 0)
{
filepathTextBox.Text = dialog.FileName;
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "NCM加密歌曲|*.ncm";
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
filepathTextBox.Text = dialog.FileName;
}
}
else if (fileFolderComboBox.SelectedIndex == 1)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.ShowNewFolderButton = true;
dialog.RootFolder = Environment.SpecialFolder.ApplicationData;
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
filepathTextBox.Text = dialog.SelectedPath;
}
}
}

Expand All @@ -99,28 +115,36 @@ private bool CheckNCMBinary(string filePath)
string header = Encoding.ASCII.GetString(bytes);
if (header == correctHeader)
{
toolStripProgressBar1.Value += 1;
return true;
}
else
{
MessageBox.Show("不是ncm文件\n文件头为:" + header, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "不是ncm文件!";
if (fileFolderComboBox.SelectedIndex == 0)
{
MessageBox.Show("不是ncm文件\n文件头为:" + header, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "不是ncm文件!";
}
return false;
}
}
else
{
MessageBox.Show("文件大小异常", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "文件大小异常,并不是ncm文件";
if (fileFolderComboBox.SelectedIndex == 0)
{
MessageBox.Show("文件大小异常", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "文件大小异常,并不是ncm文件";
}
return false;
}
}
}
catch (Exception ex)
{
MessageBox.Show("发生错误: \n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "载入文件时发生错误";
if (fileFolderComboBox.SelectedIndex == 0)
{
MessageBox.Show("发生错误: \n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "载入文件时发生错误";
}
return false;
}
}
Expand All @@ -143,10 +167,27 @@ public static bool IsValidFilePath(string path)
}
}

public int ProcessNCMFile(string path)
{
NeteaseCrypt neteaseCrypt = new NeteaseCrypt(path);
int result = neteaseCrypt.Dump();
if (fixMetaDataCheckBox.Checked)
{
neteaseCrypt.FixMetadata();
}
neteaseCrypt.Destroy();
return result;
}

// “转换”按钮被点击
private void convertButton_Click(object sender, EventArgs e)
{
if (filepathTextBox.Text == "")
if (!File.Exists("libncmdump.dll"))
{
MessageBox.Show("核心不存在\n请确认libncmdump.dll与本程序在同一目录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "核心不存在!请检查libncmdump.dll";
}
else if (filepathTextBox.Text == "")
{
MessageBox.Show("文件路径为空!\n请提供ncm文件路径", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "请提供文件";
Expand All @@ -156,43 +197,73 @@ private void convertButton_Click(object sender, EventArgs e)
MessageBox.Show("非法文件路径\n文件路径中包含非法字符!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "非法文件路径";
}
else if (!filepathTextBox.Text.EndsWith(".ncm"))
{
MessageBox.Show("这似乎并不是ncm文件!\n请提供ncm文件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "请提供正确的ncm文件";
}
else if (!File.Exists("libncmdump.dll"))
{
MessageBox.Show("核心不存在\n请确认libncmdump.dll与本程序在同一目录", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "核心不存在!请检查libncmdump.dll";
}
else if (CheckNCMBinary(filepathTextBox.Text))
else
{
filepathTextBox.Enabled = false;
browseButton.Enabled = false;
convertButton.Enabled = false;
NeteaseCrypt neteaseCrypt = new NeteaseCrypt(filepathTextBox.Text);
toolStripProgressBar1.Value += 1;
int result = neteaseCrypt.Dump();
toolStripProgressBar1.Value += 1;
if (fixMetaDataCheckBox.Checked)
fileFolderComboBox.Enabled = false;
fixMetaDataCheckBox.Enabled = false;
if (fileFolderComboBox.SelectedIndex == 1)
{
neteaseCrypt.FixMetadata();
toolStripProgressBar1.Value += 1;
}
neteaseCrypt.Destroy();
if (result != 0)
{
toolStripStatusLabel2.Text = "发生错误,返回值为:" + result.ToString();
int bypassFiles = 0;
int processedFiles = 0;
int allProcessedFiles = 0;
string directoryPath = filepathTextBox.Text;
string fileExtension = ".ncm";
string[] files = Directory.GetFiles(directoryPath, "*.*", SearchOption.TopDirectoryOnly)
.Where(file => Path.GetExtension(file).ToLower() == fileExtension.ToLower())
.ToArray();

toolStripProgressBar1.Maximum = files.Length;
foreach (var file in files)
{
if (CheckNCMBinary(file))
{
if (ProcessNCMFile(file) != 0)
{
bypassFiles += 1;
}
else
{
processedFiles += 1;
}
}
allProcessedFiles += 1;
Debug.WriteLine(allProcessedFiles.ToString());
toolStripProgressBar1.Value = allProcessedFiles;
toolStripStatusLabel2.Text = "已处理:" + allProcessedFiles.ToString();
}
toolStripStatusLabel2.Text = "成功:" + processedFiles.ToString() + ";失败:" + bypassFiles.ToString();
}
else
else if (fileFolderComboBox.SelectedIndex == 0)
{
toolStripStatusLabel2.Text = "转换完成!文件在ncm歌曲同级目录下";
toolStripProgressBar1.Value += 1;
toolStripProgressBar1.Maximum = 1;
if (!filepathTextBox.Text.EndsWith(".ncm"))
{
MessageBox.Show("这似乎并不是ncm文件!\n请提供ncm文件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
toolStripStatusLabel2.Text = "请提供正确的ncm文件";
}
else if (CheckNCMBinary(filepathTextBox.Text))
{
int result = ProcessNCMFile(filepathTextBox.Text);
toolStripProgressBar1.Value += 1;
if (result != 0)
{
toolStripStatusLabel2.Text = "发生错误,返回值为:" + result.ToString();
}
else
{
toolStripStatusLabel2.Text = "转换完成!文件在ncm歌曲同级目录下";
toolStripProgressBar1.Value += 1;
}
}
}
filepathTextBox.Enabled = true;
browseButton.Enabled = true;
convertButton.Enabled = true;
fileFolderComboBox.Enabled = true;
fixMetaDataCheckBox.Enabled = true;
toolStripProgressBar1.Value = 0;
}
}
Expand Down Expand Up @@ -224,23 +295,14 @@ private void WndMain_DragDrop(object sender, DragEventArgs e)
filepathTextBox.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 295 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}

// 进度条最大值修改
private void fixMetaDataCheckBox_CheckedChanged(object sender, EventArgs e)
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (fixMetaDataCheckBox.Checked)
{
toolStripProgressBar1.Maximum = 5;
}
else
{
toolStripProgressBar1.Maximum = 4;
}

MessageBox.Show("注意!\n此应用只用于学习用途,禁止用于商业或违法用途,\n请在遵守NCM文件提供平台的服务条款下使用本应用,\n作者对商业或违法使用本软件造成的任何后果不承担任何责任!", "免责声明", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
private void fileFolderComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("注意!\n此应用只用于学习用途,禁止用于商业或违法用途,\n请在遵守NCM文件提供平台的服务条款下使用本应用,\n作者对商业或违法使用本软件造成的任何后果不承担任何责任!", "免责声明", MessageBoxButtons.OK, MessageBoxIcon.Information);
filepathTextBox.Text = "";
}
}
}

0 comments on commit c8aabf7

Please sign in to comment.