Skip to content

Commit

Permalink
代码优化+极其有限的内存优化
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatDamon committed Aug 27, 2024
1 parent ef7e4c9 commit 2d2f086
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 137 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## 特点

- 小体积
- 低内存占用
- 低内存占用(前提是少量,目前还需要完善内存清理)
- 功能强悍
- 开箱即用

Expand Down
240 changes: 104 additions & 136 deletions WndMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -307,6 +306,7 @@ public static bool IsValidFilePath(string path)
}
}

// 处理文件
public int ProcessNCMFile(string path)
{
NeteaseCrypt neteaseCrypt = new NeteaseCrypt(path);
Expand All @@ -327,55 +327,33 @@ 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();
}
toolStripStatusLabel2.Text = "核心不存在!请检查libncmdump.dll";
}
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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Check warning on line 582 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void WndMain.timerTrackBar_Tick(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
timerTrackBar.Start();
}

if (outputDevice.PlaybackState != PlaybackState.Playing)
{
outputDevice.Play();
Expand Down Expand Up @@ -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;

Check warning on line 627 in WndMain.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void WndMain.timerTrackBar_Tick(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
timerTrackBar.Dispose();
timerTrackBar = null;
}
}

private void timerTrackBar_Tick(object sender, EventArgs e)
Expand Down Expand Up @@ -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 = "发生错误";
}

Expand All @@ -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))
Expand Down Expand Up @@ -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 = "请确认路径是否正确";
}
}
Expand Down

0 comments on commit 2d2f086

Please sign in to comment.