Skip to content

Commit

Permalink
Merged duplicated download code, and added .part to file name
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPumpo committed Jun 26, 2022
1 parent e825bde commit 3640d2e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 60 additions & 52 deletions TinyNvidiaUpdateChecker/MainConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -991,6 +953,52 @@ private static void DownloadDriverQuiet(bool minimized)

}

/// <summary>
/// Shared method for the accual downloading of a file with the command line progress bar.
/// </summary>
/// <param name="url">URL path for download</param>
/// <param name="path">Absolute file path</param>
/// <returns></returns>
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;
}
}

/// <summary>
/// Remove telementry and only extract basic drivers
/// </summary>
Expand Down

0 comments on commit 3640d2e

Please sign in to comment.