diff --git a/OpenRA.Launcher/ConfigureModsDialog.cs b/OpenRA.Launcher/ConfigureModsDialog.cs index c4be6d404c..ee7fe062b1 100644 --- a/OpenRA.Launcher/ConfigureModsDialog.cs +++ b/OpenRA.Launcher/ConfigureModsDialog.cs @@ -13,6 +13,8 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; +using System.IO.Pipes; +using System.IO; namespace OpenRA.Launcher { @@ -113,7 +115,13 @@ namespace OpenRA.Launcher private void InstallMod(object sender, EventArgs e) { if (installModDialog.ShowDialog() != DialogResult.OK) return; - using (var response = UtilityProgram.CallWithAdmin("--install-mod", installModDialog.FileName)) + var p = UtilityProgram.CallWithAdmin("--install-mod", installModDialog.FileName); + var pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In); + pipe.Connect(); + + p.WaitForExit(); + + using (var response = new StreamReader(pipe)) { string s = response.ReadToEnd(); } diff --git a/OpenRA.Launcher/InstallPackagesDialog.Designer.cs b/OpenRA.Launcher/InstallPackagesDialog.Designer.cs index 5b318b388b..950c70b173 100644 --- a/OpenRA.Launcher/InstallPackagesDialog.Designer.cs +++ b/OpenRA.Launcher/InstallPackagesDialog.Designer.cs @@ -34,6 +34,13 @@ this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); + this.radioPanel = new System.Windows.Forms.Panel(); + this.progressPanel = new System.Windows.Forms.Panel(); + this.progressBar1 = new System.Windows.Forms.ProgressBar(); + this.progressLabel = new System.Windows.Forms.Label(); + this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); + this.radioPanel.SuspendLayout(); + this.progressPanel.SuspendLayout(); this.SuspendLayout(); // // label1 @@ -68,10 +75,9 @@ // // radioButton1 // - this.radioButton1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButton1.AutoSize = true; this.radioButton1.Checked = true; - this.radioButton1.Location = new System.Drawing.Point(16, 113); + this.radioButton1.Location = new System.Drawing.Point(3, 3); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(138, 17); this.radioButton1.TabIndex = 3; @@ -81,9 +87,8 @@ // // radioButton2 // - this.radioButton2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButton2.AutoSize = true; - this.radioButton2.Location = new System.Drawing.Point(16, 136); + this.radioButton2.Location = new System.Drawing.Point(3, 26); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(96, 17); this.radioButton2.TabIndex = 4; @@ -96,6 +101,48 @@ this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer; this.folderBrowserDialog1.ShowNewFolderButton = false; // + // radioPanel + // + this.radioPanel.Controls.Add(this.radioButton1); + this.radioPanel.Controls.Add(this.radioButton2); + this.radioPanel.Location = new System.Drawing.Point(12, 105); + this.radioPanel.Name = "radioPanel"; + this.radioPanel.Size = new System.Drawing.Size(281, 52); + this.radioPanel.TabIndex = 5; + // + // progressPanel + // + this.progressPanel.Controls.Add(this.progressLabel); + this.progressPanel.Controls.Add(this.progressBar1); + this.progressPanel.Location = new System.Drawing.Point(12, 105); + this.progressPanel.Name = "progressPanel"; + this.progressPanel.Size = new System.Drawing.Size(281, 52); + this.progressPanel.TabIndex = 6; + this.progressPanel.Visible = false; + // + // progressBar1 + // + this.progressBar1.Location = new System.Drawing.Point(3, 3); + this.progressBar1.Name = "progressBar1"; + this.progressBar1.Size = new System.Drawing.Size(275, 23); + this.progressBar1.TabIndex = 0; + // + // progressLabel + // + this.progressLabel.AutoSize = true; + this.progressLabel.Location = new System.Drawing.Point(3, 29); + this.progressLabel.Name = "progressLabel"; + this.progressLabel.Size = new System.Drawing.Size(0, 13); + this.progressLabel.TabIndex = 1; + // + // backgroundWorker1 + // + this.backgroundWorker1.WorkerReportsProgress = true; + this.backgroundWorker1.WorkerSupportsCancellation = true; + this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); + this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); + this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged); + // // InstallPackagesDialog // this.AcceptButton = this.button2; @@ -103,8 +150,8 @@ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.cancelButton; this.ClientSize = new System.Drawing.Size(305, 198); - this.Controls.Add(this.radioButton2); - this.Controls.Add(this.radioButton1); + this.Controls.Add(this.progressPanel); + this.Controls.Add(this.radioPanel); this.Controls.Add(this.button2); this.Controls.Add(this.cancelButton); this.Controls.Add(this.label1); @@ -114,8 +161,11 @@ this.Name = "InstallPackagesDialog"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Install Mod Files"; + this.radioPanel.ResumeLayout(false); + this.radioPanel.PerformLayout(); + this.progressPanel.ResumeLayout(false); + this.progressPanel.PerformLayout(); this.ResumeLayout(false); - this.PerformLayout(); } @@ -127,5 +177,10 @@ private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1; + private System.Windows.Forms.Panel radioPanel; + private System.Windows.Forms.Panel progressPanel; + private System.Windows.Forms.Label progressLabel; + private System.Windows.Forms.ProgressBar progressBar1; + private System.ComponentModel.BackgroundWorker backgroundWorker1; } } \ No newline at end of file diff --git a/OpenRA.Launcher/InstallPackagesDialog.cs b/OpenRA.Launcher/InstallPackagesDialog.cs index 789ed232aa..d8ceddbf54 100644 --- a/OpenRA.Launcher/InstallPackagesDialog.cs +++ b/OpenRA.Launcher/InstallPackagesDialog.cs @@ -11,6 +11,8 @@ using System; using System.IO; using System.Windows.Forms; +using System.Text.RegularExpressions; +using System.IO.Pipes; namespace OpenRA.Launcher { @@ -30,24 +32,80 @@ namespace OpenRA.Launcher private void button2_Click(object sender, EventArgs e) { - StreamReader response = null; if (radioButton1.Checked) - response = UtilityProgram.CallWithAdmin("--download-packages", mod); - - if (radioButton2.Checked) { - if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) - response = UtilityProgram.CallWithAdmin(string.Format("--install-{0}-packages", mod), - folderBrowserDialog1.SelectedPath + Path.DirectorySeparatorChar); + radioPanel.Visible = false; + progressPanel.Visible = true; + button2.Enabled = false; + cancelButton.Enabled = false; + backgroundWorker1.RunWorkerAsync(); } - string s = response.ReadToEnd(); - if (Util.IsError(ref s)) + if (radioButton2.Checked && folderBrowserDialog1.ShowDialog() == DialogResult.OK) + { + var p = UtilityProgram.CallWithAdmin(string.Format("--install-{0}-packages", mod), + folderBrowserDialog1.SelectedPath + Path.DirectorySeparatorChar); + + NamedPipeClientStream pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In); + pipe.Connect(); + + p.WaitForExit(); + + using (var response = new StreamReader(pipe)) + { + string s = response.ReadToEnd(); + if (Util.IsError(ref s)) + DialogResult = DialogResult.No; + else + DialogResult = DialogResult.OK; + } + Close(); + } + } + + private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) + { + var p = UtilityProgram.CallWithAdmin("--download-packages", mod); + Regex r = new Regex(@"(\d{1,3})% (\d+/\d+ bytes)"); + + NamedPipeClientStream pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In); + pipe.Connect(); + + using (var response = new StreamReader(pipe)) + { + while (!p.HasExited) + { + string s = response.ReadLine(); + if (Util.IsError(ref s)) + { + e.Cancel = true; + e.Result = s; + return; + } + if (!r.IsMatch(s)) continue; + var m = r.Match(s); + backgroundWorker1.ReportProgress(int.Parse(m.Groups[1].Value), m.Groups[2].Value); + } + } + } + + private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) + { + progressBar1.Value = e.ProgressPercentage; + progressLabel.Text = (string)e.UserState; + } + + private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) + { + if (e.Cancelled) + { + MessageBox.Show((string)e.Result); DialogResult = DialogResult.No; + } else DialogResult = DialogResult.OK; - - response.Close(); + progressPanel.Visible = false; + radioPanel.Visible = true; Close(); } } diff --git a/OpenRA.Launcher/InstallPackagesDialog.resx b/OpenRA.Launcher/InstallPackagesDialog.resx index c3fe8f176d..4b460ef5c7 100644 --- a/OpenRA.Launcher/InstallPackagesDialog.resx +++ b/OpenRA.Launcher/InstallPackagesDialog.resx @@ -120,4 +120,7 @@ 17, 17 + + 186, 17 + \ No newline at end of file diff --git a/OpenRA.Launcher/UtilityProgram.cs b/OpenRA.Launcher/UtilityProgram.cs index 0282f3debd..7476dfff9b 100644 --- a/OpenRA.Launcher/UtilityProgram.cs +++ b/OpenRA.Launcher/UtilityProgram.cs @@ -71,7 +71,7 @@ namespace OpenRA.Launcher return new StreamReader(pipe); } - public static StreamReader CallWithAdmin(string command, params string[] args) + public static Process CallWithAdmin(string command, params string[] args) { Process p = new Process(); p.StartInfo.FileName = "OpenRA.Utility.exe"; @@ -89,12 +89,7 @@ namespace OpenRA.Launcher throw e; } - NamedPipeClientStream pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In); - pipe.Connect(); - - p.WaitForExit(); - - return new StreamReader(pipe); + return p; } } } diff --git a/OpenRA.Utility/Command.cs b/OpenRA.Utility/Command.cs index e4f3d8cb05..16c52c8b39 100644 --- a/OpenRA.Utility/Command.cs +++ b/OpenRA.Utility/Command.cs @@ -100,10 +100,12 @@ namespace OpenRA.Utility destFile, new string[] { mod, destPath }); - while (wc.IsBusy) + while (!completed) Thread.Sleep(500); } + static bool completed = false; + static void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { if (e.Error != null) @@ -115,11 +117,11 @@ namespace OpenRA.Utility Console.WriteLine("Download Completed"); string[] modAndDest = (string[])e.UserState; Util.ExtractPackagesFromZip(modAndDest[0], modAndDest[1]); + completed = true; } static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { - Console.SetCursorPosition(0, Console.CursorTop-1); Console.WriteLine("{0}% {1}/{2} bytes", e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive); }