Skip to content

Commit

Permalink
Use md5 for classnames, move calculations to other thread, progress b…
Browse files Browse the repository at this point in the history
…ar, copy to clipboard button.
  • Loading branch information
rebelvg committed Apr 15, 2017
1 parent a84d165 commit 0dfbafe
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
31 changes: 29 additions & 2 deletions KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.Designer.cs

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

86 changes: 61 additions & 25 deletions KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using Ookii.Dialogs.Wpf;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace KlpqMusicConfigurator
{
Expand All @@ -20,44 +21,44 @@ public Form1()
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
public string GetMD5(string filename)
{
VistaFolderBrowserDialog chosenFolder = new VistaFolderBrowserDialog();
chosenFolder.Description = "Select folder...";

if (chosenFolder.ShowDialog().Value)
using (var md5 = MD5.Create())
{
path_textBox.Text = chosenFolder.SelectedPath;

List<string> folder_filesArray = Directory.GetFiles(path_textBox.Text, "*.ogg", SearchOption.AllDirectories).Select(x => x.Replace(path_textBox.Text + "\\", "")).ToList();

listView1.Items.Clear();

foreach (string X in folder_filesArray)
using (var stream = File.OpenRead(filename))
{
listView1.Items.Add(X);
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
}
}
}

private void button2_Click(object sender, EventArgs e)
public async Task<string> GenerateConfig()
{
richTextBox1.Text = "";

string finalConfig = "class CfgPatches {\n class klpq_musicRadio_configs {\n units[] = {};\n weapons[] = {};\n requiredVersion = 1;\n requiredAddons[] = {};\n };\n};";

finalConfig += "\n\n";

finalConfig += "class CfgMusic {\n tracks[] = {};\n";
string cfgMusicConfig = "";
string cfgSoundsConfig = "";

progressBar1.Minimum = 0;
progressBar1.Maximum = listView1.Items.Count;
progressBar1.Value = 0;
progressBar1.Step = 1;

foreach (ListViewItem X in listView1.Items)
{
string className = "klpq_musicRadio_" + X.Index;
string path = Path.GetFileName(path_textBox.Text) + Path.DirectorySeparatorChar + X.Text;
string fullPath = path_textBox.Text + Path.DirectorySeparatorChar + X.Text;
string className = "klpq_musicRadio_" + await Task.Run(() => GetMD5(fullPath));
string localPath = Path.GetFileName(path_textBox.Text) + Path.DirectorySeparatorChar + X.Text;
string theme = "";
int duration = 0;
string artist = "";
string title = "";

using (var vorbisStream = new NAudio.Vorbis.VorbisWaveReader(path_textBox.Text + "\\" + X.Text))
using (var vorbisStream = await Task.Run(() => new NAudio.Vorbis.VorbisWaveReader(fullPath)))
{
duration = (int)Math.Round(vorbisStream.TotalTime.TotalSeconds);

Expand All @@ -79,25 +80,60 @@ private void button2_Click(object sender, EventArgs e)
theme = "\n theme = \"" + X.Text.Split(Path.DirectorySeparatorChar)[0] + "\";";
}

finalConfig += "\n class " + className + " {\n sound[] = {\"" + path + "\", db+3, 1};\n tag = \"klpq_musicRadio\";" + theme + "\n duration = " + duration + ";\n artist = \"" + artist + "\";\n title = \"" + title + "\";\n };";
cfgMusicConfig += "\n class " + className + " {\n sound[] = {\"" + localPath + "\", db+3, 1};\n tag = \"klpq_musicRadio\";" + theme + "\n duration = " + duration + ";\n artist = \"" + artist + "\";\n title = \"" + title + "\";\n };";

cfgSoundsConfig += "\n class " + className + " {\n sound[] = {\"" + localPath + "\", db+6, 1, 100};\n titles[] = {};\n };";
cfgSoundsConfig += "\n class " + className + "_loud {\n sound[] = {\"" + localPath + "\", db+12, 1, 500};\n titles[] = {};\n };";

progressBar1.PerformStep();
}

finalConfig += "class CfgMusic {\n tracks[] = {};\n";

finalConfig += cfgMusicConfig;

finalConfig += "\n};\n\n";

finalConfig += "class CfgSounds {\n tracks[] = {};\n";

foreach (ListViewItem X in listView1.Items)
finalConfig += cfgSoundsConfig;

finalConfig += "\n};\n";

return finalConfig;
}

private void button1_Click(object sender, EventArgs e)
{
VistaFolderBrowserDialog chosenFolder = new VistaFolderBrowserDialog();
chosenFolder.Description = "Select folder...";

if (chosenFolder.ShowDialog().Value)
{
string className = "klpq_musicRadio_" + X.Index;
string path = Path.GetFileName(path_textBox.Text) + Path.DirectorySeparatorChar + X.Text;
path_textBox.Text = chosenFolder.SelectedPath;

List<string> folder_filesArray = Directory.GetFiles(path_textBox.Text, "*.ogg", SearchOption.AllDirectories).Select(x => x.Replace(path_textBox.Text + "\\", "")).ToList();

finalConfig += "\n class " + className + " {\n sound[] = {\"" + path + "\", db+6, 1, 100};\n titles[] = {};\n };";
finalConfig += "\n class " + className + "_loud {\n sound[] = {\"" + path + "\", db+12, 1, 500};\n titles[] = {};\n };";
listView1.Items.Clear();

foreach (string X in folder_filesArray)
{
listView1.Items.Add(X);
}
}
}

finalConfig += "\n};\n";
private async void button2_Click(object sender, EventArgs e)
{
string finalConfig = await GenerateConfig();

richTextBox1.Text = finalConfig;
}

private void button3_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length != 0)
Clipboard.SetText(richTextBox1.Text);
}
}
}

0 comments on commit 0dfbafe

Please sign in to comment.