From 0dfbafe866f022a1808bfae544ae130070702620 Mon Sep 17 00:00:00 2001 From: Rebel Date: Sat, 15 Apr 2017 15:48:54 +0300 Subject: [PATCH] Use md5 for classnames, move calculations to other thread, progress bar, copy to clipboard button. --- .../KlpqMusicConfigurator/Form1.Designer.cs | 31 ++++++- .../KlpqMusicConfigurator/Form1.cs | 86 +++++++++++++------ 2 files changed, 90 insertions(+), 27 deletions(-) diff --git a/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.Designer.cs b/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.Designer.cs index beaf5b0..c5ab5e2 100644 --- a/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.Designer.cs +++ b/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.Designer.cs @@ -35,12 +35,15 @@ private void InitializeComponent() this.button1 = new System.Windows.Forms.Button(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.button2 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); + this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.SuspendLayout(); // // path_textBox // this.path_textBox.Location = new System.Drawing.Point(12, 12); this.path_textBox.Name = "path_textBox"; + this.path_textBox.ReadOnly = true; this.path_textBox.Size = new System.Drawing.Size(387, 20); this.path_textBox.TabIndex = 0; // @@ -74,31 +77,53 @@ private void InitializeComponent() // this.richTextBox1.Location = new System.Drawing.Point(12, 38); this.richTextBox1.Name = "richTextBox1"; + this.richTextBox1.ReadOnly = true; this.richTextBox1.Size = new System.Drawing.Size(422, 498); this.richTextBox1.TabIndex = 3; this.richTextBox1.Text = ""; // // button2 // - this.button2.Location = new System.Drawing.Point(315, 542); + this.button2.Location = new System.Drawing.Point(315, 576); this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(119, 62); + this.button2.Size = new System.Drawing.Size(119, 28); this.button2.TabIndex = 4; this.button2.Text = "Create Config"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // + // button3 + // + this.button3.Location = new System.Drawing.Point(315, 542); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(119, 28); + this.button3.TabIndex = 5; + this.button3.Text = "Copy Config"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); + // + // progressBar1 + // + this.progressBar1.Location = new System.Drawing.Point(12, 576); + this.progressBar1.Name = "progressBar1"; + this.progressBar1.Size = new System.Drawing.Size(297, 28); + this.progressBar1.TabIndex = 6; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(807, 616); + this.Controls.Add(this.progressBar1); + this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.richTextBox1); this.Controls.Add(this.button1); this.Controls.Add(this.listView1); this.Controls.Add(this.path_textBox); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "KlpqMusicConfigurator"; @@ -115,6 +140,8 @@ private void InitializeComponent() private System.Windows.Forms.RichTextBox richTextBox1; private System.Windows.Forms.Button button2; private System.Windows.Forms.ColumnHeader columnHeader1; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.ProgressBar progressBar1; } } diff --git a/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.cs b/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.cs index 0b1a176..1a310dc 100644 --- a/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.cs +++ b/KlpqMusicConfigurator/KlpqMusicConfigurator/Form1.cs @@ -10,6 +10,7 @@ using System.IO; using Ookii.Dialogs.Wpf; using System.Text.RegularExpressions; +using System.Security.Cryptography; namespace KlpqMusicConfigurator { @@ -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 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 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); @@ -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 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); + } } }