Skip to content

Commit

Permalink
Download progress percentage for web installer + err handling
Browse files Browse the repository at this point in the history
  • Loading branch information
N00MKRAD committed Nov 12, 2020
1 parent b6acf66 commit 4479257
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Code/Forms/DialogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void ChangeText (string s)
mainLabel.Text = s;
}

public string GetText ()
{
return mainLabel.Text;
}

private async Task SelfDestruct (float time)
{
await Task.Delay((time * 1000f).RoundToInt());
Expand Down
47 changes: 35 additions & 12 deletions Code/IO/Installer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -95,38 +96,60 @@ public static async Task Install ()
path7za = Path.Combine(path, "7za.exe");
File.WriteAllBytes(path7za, Resources.x64_7za);

await DownloadAndInstall(exeFilesVersion, "esrgan.7z");
await DownloadAndInstall(exeFilesVersion, "esrgan-ncnn.7z");
await DownloadAndInstall(exeFilesVersion, "av.7z");
await DownloadAndInstall(exeFilesVersion, "shipped-files-version.txt", false);
try
{
await DownloadAndInstall(exeFilesVersion, "esrgan.7z");
await DownloadAndInstall(exeFilesVersion, "esrgan-ncnn.7z");
await DownloadAndInstall(exeFilesVersion, "av.7z");
await DownloadAndInstall(exeFilesVersion, "shipped-files-version.txt", false);
}
catch (Exception e)
{
MsgBox msg = Logger.ErrorMessage("Web Installer failed to run!\n", e);
while (DialogQueue.IsOpen(msg)) await Task.Delay(50);
Environment.Exit(1);
return;
}

dialog.Close();
Program.mainForm.Enabled = true;
Program.mainForm.WindowState = System.Windows.Forms.FormWindowState.Maximized;
Program.mainForm.BringToFront();
}

static DialogForm currentDlDialog;

static async Task DownloadAndInstall(int version, string filename, bool showDialog = true)
{
string savePath = Path.Combine(IOUtils.GetAppDataDir(), filename);
string url = $"https://dl.nmkd.de/cupscale/shippedfiles/{version}/{filename}";
Logger.Log($"[Installer] Downloading {url}");
var client = new WebClient();
DialogForm dialog = null;
if (showDialog)
dialog = new DialogForm($"Downloading {filename}...");
currentDlDialog = new DialogForm($"Downloading {filename}…");
sw.Restart();
client.DownloadProgressChanged += DownloadProgressChanged;
await client.DownloadFileTaskAsync(new Uri(url), savePath);
if(Path.GetExtension(filename).ToLower() == ".7z") // Only run extractor if it's a 7z archive
{
if (dialog != null)
dialog.ChangeText($"Installing {filename}...");
if (currentDlDialog != null)
currentDlDialog.ChangeText($"Installing {filename}...");
await UnSevenzip(Path.Combine(IOUtils.GetAppDataDir(), filename));
}
if(dialog != null)
dialog.Close();
if(currentDlDialog != null)
currentDlDialog.Close();
currentDlDialog = null;
}


static Stopwatch sw = new Stopwatch();
static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if(sw.ElapsedMilliseconds > 500)
{
sw.Restart();
string newText = currentDlDialog.GetText().Split('…')[0] + "… " + e.ProgressPercentage + "%";
currentDlDialog.ChangeText(newText);
}
}

static async Task UnSevenzip (string path)
{
Expand Down
5 changes: 3 additions & 2 deletions Code/Main/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Cupscale.Forms;
using System;
using System.IO;
using System.Windows.Forms;
Expand Down Expand Up @@ -77,12 +78,12 @@ public static string GetSessionLog ()
return sessionLog;
}

public static void ErrorMessage (string msg, Exception e)
public static MsgBox ErrorMessage (string msg, Exception e)
{
string text = $"{msg}\n{e.Message}\n\nStack Trace:\n{e.StackTrace}";
Clipboard.SetText(text);
Program.ShowMessage(text + "\n\nThe error message was copied to the clipboard.", "Error");
Log(text);
return Program.ShowMessage(text + "\n\nThe error message was copied to the clipboard.", "Error");
}
}
}

0 comments on commit 4479257

Please sign in to comment.