From 873cb51585ff447b0d56da08c859062c313d5654 Mon Sep 17 00:00:00 2001 From: Chris Bessent Date: Tue, 9 Mar 2021 05:31:45 -0700 Subject: [PATCH] Add error reporting to the launcher This reports errors to the user and exits the launcher but ONLY if a previous version of WJ could not be found. Existing users will fail silently and be unable to update. New users will get some kind of error they can report to the team without the launcher getting stuck. --- Wabbajack.Launcher/MainWindowVM.cs | 123 ++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 36 deletions(-) diff --git a/Wabbajack.Launcher/MainWindowVM.cs b/Wabbajack.Launcher/MainWindowVM.cs index d7dae913..c632e95c 100644 --- a/Wabbajack.Launcher/MainWindowVM.cs +++ b/Wabbajack.Launcher/MainWindowVM.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; using Newtonsoft.Json; using Wabbajack.Launcher.Annotations; +using System.Collections.Generic; namespace Wabbajack.Launcher { @@ -27,6 +28,7 @@ namespace Wabbajack.Launcher private string _status = "Checking for Updates"; private Release _version; + private List _errors = new List(); public string Status { @@ -61,24 +63,33 @@ namespace Wabbajack.Launcher return new Version(0, 0, 0, 0); }).FirstOrDefault(); } - catch (Exception) + catch (Exception ex) { - FinishAndExit(); + _errors.Add(ex.Message); + await FinishAndExit(); } if (_version == null) - FinishAndExit(); + { + _errors.Add("Unable to parse Github releases"); + await FinishAndExit(); + } Status = "Looking for Updates"; var base_folder = Path.Combine(Directory.GetCurrentDirectory(), _version.Tag); if (File.Exists(Path.Combine(base_folder, "Wabbajack.exe"))) - FinishAndExit(); + { + await FinishAndExit(); + } var asset = _version.Assets.FirstOrDefault(a => a.Name == _version.Tag + ".zip"); if (asset == null) - FinishAndExit(); + { + _errors.Add("No zip file for release " + _version.Tag); + await FinishAndExit(); + } var wc = new WebClient(); wc.DownloadProgressChanged += UpdateProgress; @@ -88,46 +99,86 @@ namespace Wabbajack.Launcher { data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrlFast); } - catch (Exception) + catch (Exception ex) { + _errors.Add(ex.Message); // Something went wrong so fallback to original URL - data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl); - } - - using (var zip = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read)) - { - foreach (var entry in zip.Entries) + try { - Status = $"Extracting: {entry.Name}"; - var outPath = Path.Combine(base_folder, entry.FullName); - if (!Directory.Exists(Path.GetDirectoryName(outPath))) - Directory.CreateDirectory(Path.GetDirectoryName(outPath)); - - if (entry.FullName.EndsWith("/") || entry.FullName.EndsWith("\\")) - continue; - await using var o = entry.Open(); - await using var of = File.Create(outPath); - await o.CopyToAsync(of); + data = await wc.DownloadDataTaskAsync(asset.BrowserDownloadUrl); + } + catch (Exception ex2) + { + _errors.Add(ex2.Message); + await FinishAndExit(); + throw; // avoid unsigned variable 'data' } } - FinishAndExit(); + + try + { + using (var zip = new ZipArchive(new MemoryStream(data), ZipArchiveMode.Read)) + { + foreach (var entry in zip.Entries) + { + Status = $"Extracting: {entry.Name}"; + var outPath = Path.Combine(base_folder, entry.FullName); + if (!Directory.Exists(Path.GetDirectoryName(outPath))) + Directory.CreateDirectory(Path.GetDirectoryName(outPath)); + + if (entry.FullName.EndsWith("/") || entry.FullName.EndsWith("\\")) + continue; + await using var o = entry.Open(); + await using var of = File.Create(outPath); + await o.CopyToAsync(of); + } + } + } + catch (Exception ex) + { + _errors.Add(ex.Message); + } + finally + { + await FinishAndExit(); + } + } - private void FinishAndExit() + private async Task FinishAndExit() { - Status = "Launching..."; - var wjFolder = Directory.EnumerateDirectories(Directory.GetCurrentDirectory()) - .OrderByDescending(v => - Version.TryParse(Path.GetFileName(v), out var ver) ? ver : new Version(0, 0, 0, 0)) - .FirstOrDefault(); - var info = new ProcessStartInfo + try { - FileName = Path.Combine(wjFolder, "Wabbajack.exe"), - Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(s => s.Contains(' ') ? '\"' + s + '\"' : s)), - WorkingDirectory = wjFolder, - }; - Process.Start(info); - Environment.Exit(0); + Status = "Launching..."; + var wjFolder = Directory.EnumerateDirectories(Directory.GetCurrentDirectory()) + .OrderByDescending(v => + Version.TryParse(Path.GetFileName(v), out var ver) ? ver : new Version(0, 0, 0, 0)) + .FirstOrDefault(); + var info = new ProcessStartInfo + { + FileName = Path.Combine(wjFolder, "Wabbajack.exe"), + Arguments = string.Join(" ", Environment.GetCommandLineArgs().Skip(1).Select(s => s.Contains(' ') ? '\"' + s + '\"' : s)), + WorkingDirectory = wjFolder, + }; + Process.Start(info); + } + catch (Exception ex) + { + if (_errors.Count == 0) + { + Status = "Failed: Unknown error"; + await Task.Delay(10000); + } + foreach (var error in _errors) + { + Status = "Failed: " + error; + await Task.Delay(10000); + } + } + finally + { + Environment.Exit(0); + } } private void UpdateProgress(object sender, DownloadProgressChangedEventArgs e)