From be5986a09cc21200e1994e752794c57cf4d2e528 Mon Sep 17 00:00:00 2001 From: Bitl Date: Mon, 13 Sep 2021 14:56:45 -0700 Subject: [PATCH] fixed up downloading --- .../StorageAndFunctions/GlobalFuncs.cs | 8 +++++ .../NovetusLauncher/Classes/SDK/Downloader.cs | 34 +++++++++++++------ Novetus/NovetusLauncher/Forms/SDK/AssetSDK.cs | 31 ++++++++++++++--- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/Novetus/NovetusCore/StorageAndFunctions/GlobalFuncs.cs b/Novetus/NovetusCore/StorageAndFunctions/GlobalFuncs.cs index 3c2e7ec..7897069 100644 --- a/Novetus/NovetusCore/StorageAndFunctions/GlobalFuncs.cs +++ b/Novetus/NovetusCore/StorageAndFunctions/GlobalFuncs.cs @@ -1908,5 +1908,13 @@ public class GlobalFuncs log.Error("EXCEPTION|ADDITIONAL INFO: " + (ex != null ? ex.ToString() : "N/A")); } #endif + + //http://stevenhollidge.blogspot.com/2012/06/async-taskdelay.html + public static Task Delay(int milliseconds) + { + var tcs = new TaskCompletionSource(); + new System.Threading.Timer(_ => tcs.SetResult(null)).Change(milliseconds, -1); + return tcs.Task; + } } #endregion diff --git a/Novetus/NovetusLauncher/Classes/SDK/Downloader.cs b/Novetus/NovetusLauncher/Classes/SDK/Downloader.cs index 4271233..6018b33 100644 --- a/Novetus/NovetusLauncher/Classes/SDK/Downloader.cs +++ b/Novetus/NovetusLauncher/Classes/SDK/Downloader.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Net; +using System.Threading; using System.Windows.Forms; #endregion @@ -65,7 +66,6 @@ class Downloader { outputfilename = fileName + fileext; } - string fullpath = path + "\\" + outputfilename; @@ -90,23 +90,37 @@ class Downloader public void InitDownloadNoDialog(string name, string additionalText = "") { + int read = 0; + try { - int read = DownloadFile(fileURL, name); - if (string.IsNullOrWhiteSpace(downloadOutcomeException)) - { - downloadOutcome = "File " + Path.GetFileName(name) + " downloaded! " + GlobalFuncs.SizeSuffix(Convert.ToInt64(read), 2) + " written (" + read + " bytes)! " + additionalText; - } - else - { - downloadOutcome = "Download of file " + Path.GetFileName(name) + " failed. " + downloadOutcomeException; - } + read = DownloadFile(fileURL, name); } catch (Exception ex) { GlobalFuncs.LogExceptions(ex); downloadOutcome = "Error when downloading file: " + ex.Message; } + finally + { + //wait a few seconds for the download to finish + Thread.Sleep(2000); + if (File.Exists(name)) + { + downloadOutcome = "File " + Path.GetFileName(name) + " downloaded! " + GlobalFuncs.SizeSuffix(Convert.ToInt64(read), 2) + " written (" + read + " bytes)! " + additionalText; + } + else + { + if (!string.IsNullOrWhiteSpace(downloadOutcomeException)) + { + downloadOutcome = "Error: Download of file " + Path.GetFileName(name) + " failed. " + downloadOutcomeException; + } + else + { + downloadOutcome = "Error: Download of file " + Path.GetFileName(name) + " failed. The file wasn't downloaded to the assigned directory."; + } + } + } } public string GetFullDLPath() diff --git a/Novetus/NovetusLauncher/Forms/SDK/AssetSDK.cs b/Novetus/NovetusLauncher/Forms/SDK/AssetSDK.cs index 3560657..96e6969 100644 --- a/Novetus/NovetusLauncher/Forms/SDK/AssetSDK.cs +++ b/Novetus/NovetusLauncher/Forms/SDK/AssetSDK.cs @@ -121,7 +121,14 @@ public partial class AssetSDK : Form if (!string.IsNullOrWhiteSpace(download.getDownloadOutcome())) { - MessageBox.Show(download.getDownloadOutcome(), "Novetus Asset SDK - Download Completed", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBoxIcon boxicon = MessageBoxIcon.Information; + + if (download.getDownloadOutcome().Contains("Error")) + { + boxicon = MessageBoxIcon.Error; + } + + MessageBox.Show(download.getDownloadOutcome(), "Novetus Asset SDK - Download Completed", MessageBoxButtons.OK, boxicon); } } else @@ -136,8 +143,10 @@ public partial class AssetSDK : Form } } - public static void StartItemBatchDownload(string name, string url, string id, int ver, bool iswebsite, string path) + public static bool StartItemBatchDownload(string name, string url, string id, int ver, bool iswebsite, string path) { + bool noErrors = true; + try { string version = ((ver != 0) && (!iswebsite)) ? "&version=" + ver : ""; @@ -154,6 +163,7 @@ public partial class AssetSDK : Form catch (Exception ex) { GlobalFuncs.LogExceptions(ex); + noErrors = false; } } else @@ -164,8 +174,12 @@ public partial class AssetSDK : Form catch (Exception ex) { GlobalFuncs.LogExceptions(ex); + noErrors = false; } + + return noErrors; } + private void AssetDownloader_URLSelection_SelectedIndexChanged(object sender, EventArgs e) { switch (AssetDownloader_URLSelection.SelectedIndex) @@ -222,20 +236,29 @@ public partial class AssetSDK : Form string[] lines = AssetDownloaderBatch_BatchIDBox.Lines; + int lineCount = lines.Count(); + foreach (var line in lines) { string[] linesplit = line.Split('|'); - StartItemBatchDownload( + bool noErrors = StartItemBatchDownload( linesplit[0] + extension, url, linesplit[1], Convert.ToInt32(linesplit[2]), isWebSite, basepath); + + if (!noErrors) + { + --lineCount; + } } AssetDownloaderBatch_Status.Visible = false; - MessageBox.Show("Batch download complete! " + lines.Count() + " items downloaded!", "Novetus Asset SDK - Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); + string extraText = (lines.Count() != lineCount) ? "\n" + (lines.Count() - lineCount) + " errors were detected during the download. Make sure your IDs and links are valid." : ""; + + MessageBox.Show("Batch download complete! " + lineCount + " items downloaded!" + extraText, "Novetus Asset SDK - Download Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }