From 3640d2eebd892df85756a978172849977e756b98 Mon Sep 17 00:00:00 2001 From: Samuel Date: Sun, 26 Jun 2022 13:22:02 +0200 Subject: [PATCH] Merged duplicated download code, and added .part to file name --- CHANGELOG.md | 3 + TinyNvidiaUpdateChecker/MainConsole.cs | 112 +++++++++++++------------ 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 008d359..25e4a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Non-DCH drivers are no longer supported by NVIDIA. So they are always opted to be installed. See issue #101 +### Changed +- Merged duplicate code, and improved driver download code (more improvements are needed) + ## [1.14.5] - 2021-09-06 ### Added - DCH driver support diff --git a/TinyNvidiaUpdateChecker/MainConsole.cs b/TinyNvidiaUpdateChecker/MainConsole.cs index ce2d794..e97fa57 100644 --- a/TinyNvidiaUpdateChecker/MainConsole.cs +++ b/TinyNvidiaUpdateChecker/MainConsole.cs @@ -833,28 +833,10 @@ private static void DownloadDriver() // don't download driver if it already exists Console.Write("Downloading the driver . . . "); if (showUI && !File.Exists(savePath + driverFileName)) { + var ex = HandleDownload(downloadURL, savePath + driverFileName); - using (WebClient webClient = new WebClient()) { - var notifier = new AutoResetEvent(false); - var progress = new Handlers.ProgressBar(); - - webClient.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e) { - progress.Report((double)e.ProgressPercentage / 100); - }; - - // Only set notifier here! - webClient.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e) { - if(e.Cancelled || e.Error != null) { - File.Delete(savePath + driverFileName); - } else { - notifier.Set(); - } - }; - - webClient.DownloadFileAsync(new Uri(downloadURL), savePath + driverFileName); - - notifier.WaitOne(); // sync with the above - progress.Dispose(); // get rid of the progress bar + if (ex != null) { + throw ex; } } // show the progress bar gui @@ -918,40 +900,20 @@ private static void DownloadDriverQuiet(bool minimized) Console.Write("Downloading the driver . . . "); if (showUI || confirmDL) { - using (var webClient = new WebClient()) { - var notifier = new AutoResetEvent(false); - var progress = new Handlers.ProgressBar(); - var error = false; - - webClient.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e) { - progress.Report((double)e.ProgressPercentage / 100); - }; - - webClient.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e) { - if (e.Cancelled || e.Error != null) { - File.Delete(savePath + driverFileName); - } else { - notifier.Set(); - } - }; + try { + var ex = HandleDownload(downloadURL, FULL_PATH_DRIVER); - try { - webClient.DownloadFileAsync(new Uri(downloadURL), FULL_PATH_DRIVER); - notifier.WaitOne(); - } catch (Exception ex) { - error = true; - Console.Write("ERROR!"); - Console.WriteLine(); - Console.WriteLine(ex.ToString()); - Console.WriteLine(); + if (ex != null) { + throw ex; } - progress.Dispose(); // dispone the progress bar - - if (!error) { - Console.Write("OK!"); - Console.WriteLine(); - } + Console.Write("OK!"); + Console.WriteLine(); + } catch (Exception ex) { + Console.Write("ERROR!"); + Console.WriteLine(); + Console.WriteLine(ex.ToString()); + Console.WriteLine(); } } else { using (DownloaderForm dlForm = new DownloaderForm()) { @@ -991,6 +953,52 @@ private static void DownloadDriverQuiet(bool minimized) } + /// + /// Shared method for the accual downloading of a file with the command line progress bar. + /// + /// URL path for download + /// Absolute file path + /// + private static Exception HandleDownload(string url, string path) + { + Exception ex = null; + path += ".part"; // add 'partial' to file name making it easier to identify as an incomplete download + + // if a partial file download exists, delete it now + if (File.Exists(path)) { + File.Delete(path); + } + + try { + using (var webClient = new WebClient()) { + var notifier = new AutoResetEvent(false); + var progress = new Handlers.ProgressBar(); + + webClient.DownloadProgressChanged += delegate (object sender, DownloadProgressChangedEventArgs e) { + progress.Report((double)e.ProgressPercentage / 100); + }; + + webClient.DownloadFileCompleted += delegate (object sender, AsyncCompletedEventArgs e) { + if (e.Cancelled || e.Error != null) { + File.Delete(path); + ex = e.Error; + } + + File.Move(path, path.Substring(0, path.Length - 5)); // rename back + notifier.Set(); + }; + + webClient.DownloadFileAsync(new Uri(url), path); + notifier.WaitOne(); + progress.Dispose(); + + return ex; + } + } catch (Exception ex2) { + return ex2; + } + } + /// /// Remove telementry and only extract basic drivers ///