From e1e55df5ec3e8c64a4a61b27836e8b23b29c1bd6 Mon Sep 17 00:00:00 2001 From: Jamie Sinn Date: Mon, 11 Feb 2019 12:30:03 -0500 Subject: [PATCH] WPILib and VSCode download works now. Close #12 --- CSAUSBTool/CSAUSBTool.Designer.cs | 10 +-- CSAUSBTool/CSAUSBTool.cs | 10 +++ CSAUSBTool/CSAUSBTool.csproj | 25 +++--- CSAUSBTool/ControlSystemsSoftware.cs | 56 ++++++++++---- CSAUSBTool/FRCYear.cs | 2 + CSAUSBTool/FodyWeavers.xml | 4 +- CSAUSBTool/FodyWeavers.xsd | 111 +++++++++++++++++++++++++++ CSAUSBTool/HttpClientDownload.cs | 109 ++++++++++++++++++++++++++ CSAUSBTool/packages.config | 6 +- 9 files changed, 297 insertions(+), 36 deletions(-) create mode 100644 CSAUSBTool/FodyWeavers.xsd create mode 100644 CSAUSBTool/HttpClientDownload.cs diff --git a/CSAUSBTool/CSAUSBTool.Designer.cs b/CSAUSBTool/CSAUSBTool.Designer.cs index a526ba4..1e06ca1 100644 --- a/CSAUSBTool/CSAUSBTool.Designer.cs +++ b/CSAUSBTool/CSAUSBTool.Designer.cs @@ -41,7 +41,7 @@ private void InitializeComponent() this.buildISOButton = new System.Windows.Forms.Button(); this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); this.statusStrip = new System.Windows.Forms.StatusStrip(); - this.toolStripProgressBar = new System.Windows.Forms.ToolStripProgressBar(); + toolStripProgressBar = new System.Windows.Forms.ToolStripProgressBar(); this.toolStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel(); this.downloadAsyncCheckbox = new System.Windows.Forms.CheckBox(); this.statusStrip.SuspendLayout(); @@ -146,7 +146,7 @@ private void InitializeComponent() // this.statusStrip.BackColor = System.Drawing.SystemColors.Control; this.statusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripProgressBar, + toolStripProgressBar, this.toolStripStatusLabel}); this.statusStrip.Location = new System.Drawing.Point(0, 183); this.statusStrip.Name = "statusStrip"; @@ -156,8 +156,8 @@ private void InitializeComponent() // // toolStripProgressBar // - this.toolStripProgressBar.Name = "toolStripProgressBar"; - this.toolStripProgressBar.Size = new System.Drawing.Size(100, 16); + toolStripProgressBar.Name = "toolStripProgressBar"; + toolStripProgressBar.Size = new System.Drawing.Size(100, 16); // // toolStripStatusLabel // @@ -217,7 +217,7 @@ private void InitializeComponent() private System.Windows.Forms.Button buildISOButton; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog; private System.Windows.Forms.StatusStrip statusStrip; - private System.Windows.Forms.ToolStripProgressBar toolStripProgressBar; + public static System.Windows.Forms.ToolStripProgressBar toolStripProgressBar; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel; private System.Windows.Forms.CheckBox downloadAsyncCheckbox; } diff --git a/CSAUSBTool/CSAUSBTool.cs b/CSAUSBTool/CSAUSBTool.cs index c99c6b2..6aa2104 100644 --- a/CSAUSBTool/CSAUSBTool.cs +++ b/CSAUSBTool/CSAUSBTool.cs @@ -5,6 +5,7 @@ using System.Drawing; using System.IO; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -22,6 +23,8 @@ public partial class CSAUSBTool : Form public CSAUSBTool(string[] args) { + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; InitializeComponent(); if (args.Length >= 1) @@ -29,9 +32,11 @@ public CSAUSBTool(string[] args) Console.Out.WriteLine(args[0]); competitions[9999] = new FRCYear(9999, FRCYear.GetWebList(args[0])); } + competitions[2019] = new FRCYear(2019, FRCYear.GetWebList(2019)); competitions[2018] = new FRCYear(2018, FRCYear.GetWebList(2018)); competitions[2017] = new FRCYear(2017, FRCYear.GetWebList(2017)); + buildISOButton.Enabled = false; yearSelection.DataSource = new BindingSource(competitions, null); yearSelection.DisplayMember = "Key"; @@ -98,5 +103,10 @@ private void buildISOButton_Click(object sender, EventArgs e) selectedFrc.BuildISO(downloadFolderPath, isoFolderPath, toolStripProgressBar); toolStripStatusLabel.Text = @"Idle"; } + + public static ToolStripProgressBar GetProgressBar() + { + return toolStripProgressBar; + } } } diff --git a/CSAUSBTool/CSAUSBTool.csproj b/CSAUSBTool/CSAUSBTool.csproj index a629b31..46faf2a 100644 --- a/CSAUSBTool/CSAUSBTool.csproj +++ b/CSAUSBTool/CSAUSBTool.csproj @@ -1,5 +1,6 @@  + Debug @@ -56,13 +57,8 @@ CSAUSBTool.Program - - ..\packages\Costura.Fody.1.6.2\lib\dotnet\Costura.dll - False - - - ..\packages\DiscUtils.0.13.0-alpha\lib\net45\DiscUtils.dll - True + + ..\packages\Costura.Fody.3.2.2\lib\net40\Costura.dll ..\packages\DiscUtils.BootConfig.0.13.0-alpha\lib\net45\DiscUtils.BootConfig.dll @@ -175,6 +171,9 @@ + + ..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll + @@ -194,6 +193,7 @@ CSAUSBTool.cs + @@ -225,7 +225,9 @@ - + + Designer + @@ -240,15 +242,14 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + + - + + + + + + + + + A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with line breaks + + + + + A list of assembly names to include from the default action of "embed all Copy Local references", delimited with line breaks. + + + + + A list of unmanaged 32 bit assembly names to include, delimited with line breaks. + + + + + A list of unmanaged 64 bit assembly names to include, delimited with line breaks. + + + + + The order of preloaded assemblies, delimited with line breaks. + + + + + + This will copy embedded files to disk before loading them into memory. This is helpful for some scenarios that expected an assembly to be loaded from a physical file. + + + + + Controls if .pdbs for reference assemblies are also embedded. + + + + + Embedded assemblies are compressed by default, and uncompressed when they are loaded. You can turn compression off with this option. + + + + + As part of Costura, embedded assemblies are no longer included as part of the build. This cleanup can be turned off. + + + + + Costura by default will load as part of the module initialization. This flag disables that behavior. Make sure you call CosturaUtility.Initialize() somewhere in your code. + + + + + Costura will by default use assemblies with a name like 'resources.dll' as a satellite resource and prepend the output path. This flag disables that behavior. + + + + + A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with | + + + + + A list of assembly names to include from the default action of "embed all Copy Local references", delimited with |. + + + + + A list of unmanaged 32 bit assembly names to include, delimited with |. + + + + + A list of unmanaged 64 bit assembly names to include, delimited with |. + + + + + The order of preloaded assemblies, delimited with |. + + + + + + + + 'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed. + + + + + A comma-separated list of error codes that can be safely ignored in assembly verification. + + + + + 'false' to turn off automatic generation of the XML Schema file. + + + + + \ No newline at end of file diff --git a/CSAUSBTool/HttpClientDownload.cs b/CSAUSBTool/HttpClientDownload.cs new file mode 100644 index 0000000..72837fb --- /dev/null +++ b/CSAUSBTool/HttpClientDownload.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace CSAUSBTool +{ + + public class HttpClientDownloadWithProgress : IDisposable + { + private readonly string _downloadUrl; + private readonly string _destinationFilePath; + + private HttpClient _httpClient; + + public delegate void ProgressChangedHandler(long? totalFileSize, long totalBytesDownloaded, double? progressPercentage); + + public event ProgressChangedHandler ProgressChanged; + + public HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath) + { + _downloadUrl = downloadUrl; + _destinationFilePath = destinationFilePath; + } + + public async Task StartDownload(CancellationToken token) + { + _httpClient = new HttpClient { Timeout = TimeSpan.FromDays(1) }; + + using (var response = await _httpClient.GetAsync(_downloadUrl, HttpCompletionOption.ResponseHeadersRead)) + return await DownloadFileFromHttpResponseMessage(response, token); + } + + private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response, CancellationToken token) + { + response.EnsureSuccessStatusCode(); + + var totalBytes = response.Content.Headers.ContentLength; + + using (var contentStream = await response.Content.ReadAsStreamAsync()) + return await ProcessContentStream(totalBytes, contentStream, token); + } + + private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream, CancellationToken token) + { + var totalBytesRead = 0L; + var readCount = 0L; + var buffer = new byte[8192]; + var isMoreToRead = true; + + using (var fileStream = new FileStream(_destinationFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) + { + do + { + var bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length); + if (bytesRead == 0) + { + isMoreToRead = false; + TriggerProgressChanged(totalDownloadSize, totalBytesRead); + continue; + } + + await fileStream.WriteAsync(buffer, 0, bytesRead); + + totalBytesRead += bytesRead; + readCount += 1; + + if (readCount % 100 == 0) + TriggerProgressChanged(totalDownloadSize, totalBytesRead); + } + while (isMoreToRead && !token.IsCancellationRequested); + } + if (token.IsCancellationRequested) + { + try + { + File.Delete(_destinationFilePath); + } + catch (IOException) + { + + } + return false; + } + return true; + } + + private void TriggerProgressChanged(long? totalDownloadSize, long totalBytesRead) + { + if (ProgressChanged == null) + return; + + double? progressPercentage = null; + if (totalDownloadSize.HasValue) + progressPercentage = Math.Round((double)totalBytesRead / totalDownloadSize.Value * 100, 2); + + ProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage); + } + + public void Dispose() + { + _httpClient?.Dispose(); + } + } +} diff --git a/CSAUSBTool/packages.config b/CSAUSBTool/packages.config index b327764..8a1cdab 100644 --- a/CSAUSBTool/packages.config +++ b/CSAUSBTool/packages.config @@ -1,7 +1,6 @@  - - + @@ -29,5 +28,6 @@ - + + \ No newline at end of file