diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index fbdc878d..7f80149e 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -129,6 +129,30 @@ namespace Wabbajack.Common } } + public static void CatchAndLog(Action a) + { + try + { + a(); + } + catch (Exception ex) + { + Utils.Error(ex); + } + } + + public static async Task CatchAndLog(Func f) + { + try + { + await f(); + } + catch (Exception ex) + { + Utils.Error(ex); + } + } + /// /// MurMur3 hashes the file pointed to by this string /// diff --git a/Wabbajack.Lib/Downloaders/INeedsLogin.cs b/Wabbajack.Lib/Downloaders/INeedsLogin.cs index 68ab9d09..6917fe17 100644 --- a/Wabbajack.Lib/Downloaders/INeedsLogin.cs +++ b/Wabbajack.Lib/Downloaders/INeedsLogin.cs @@ -8,7 +8,7 @@ using System.Windows.Input; namespace Wabbajack.Lib.Downloaders { - public interface INeedsLogin : INotifyPropertyChanged + public interface INeedsLogin { ICommand TriggerLogin { get; } ICommand ClearLogin { get; } @@ -17,6 +17,5 @@ namespace Wabbajack.Lib.Downloaders string MetaInfo { get; } Uri SiteURL { get; } Uri IconUri { get; } - } } diff --git a/Wabbajack.Lib/Downloaders/LoversLabDownloader.cs b/Wabbajack.Lib/Downloaders/LoversLabDownloader.cs index 1b8b795e..1802b605 100644 --- a/Wabbajack.Lib/Downloaders/LoversLabDownloader.cs +++ b/Wabbajack.Lib/Downloaders/LoversLabDownloader.cs @@ -20,7 +20,7 @@ using File = Alphaleonis.Win32.Filesystem.File; namespace Wabbajack.Lib.Downloaders { - public class LoversLabDownloader : ViewModel, IDownloader, INeedsLogin + public class LoversLabDownloader : IDownloader, INeedsLogin { internal HttpClient _authedClient; @@ -40,8 +40,12 @@ namespace Wabbajack.Lib.Downloaders public LoversLabDownloader() { - TriggerLogin = ReactiveCommand.Create(async () => await Utils.Log(new RequestLoversLabLogin()).Task, IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); - ClearLogin = ReactiveCommand.Create(() => Utils.DeleteEncryptedJson("loverslabcookies"), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler)); + TriggerLogin = ReactiveCommand.CreateFromTask( + execute: () => Utils.CatchAndLog(async () => await Utils.Log(new RequestLoversLabLogin()).Task), + canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); + ClearLogin = ReactiveCommand.Create( + execute: () => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson("loverslabcookies")), + canExecute: IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler)); } @@ -222,7 +226,7 @@ namespace Wabbajack.Lib.Downloaders public override void Cancel() { Handled = true; - _source.SetCanceled(); + _source.TrySetCanceled(); } } } diff --git a/Wabbajack.Lib/Downloaders/NexusDownloader.cs b/Wabbajack.Lib/Downloaders/NexusDownloader.cs index e96cf160..e757d272 100644 --- a/Wabbajack.Lib/Downloaders/NexusDownloader.cs +++ b/Wabbajack.Lib/Downloaders/NexusDownloader.cs @@ -13,39 +13,36 @@ using Wabbajack.Lib.Validation; namespace Wabbajack.Lib.Downloaders { - public class NexusDownloader : ViewModel, IDownloader, INeedsLogin + public class NexusDownloader : IDownloader, INeedsLogin { private bool _prepared; private SemaphoreSlim _lock = new SemaphoreSlim(1); private UserStatus _status; private NexusApiClient _client; - public NexusDownloader() - { - TriggerLogin = ReactiveCommand.Create(async () => await NexusApiClient.RequestAndCacheAPIKey(), IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); - ClearLogin = ReactiveCommand.Create(() => Utils.DeleteEncryptedJson("nexusapikey"), IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler)); - } - public IObservable IsLoggedIn => Utils.HaveEncryptedJsonObservable("nexusapikey"); public string SiteName => "Nexus Mods"; - public string MetaInfo - { - get - { - return ""; - } - } - + public string MetaInfo => ""; public Uri SiteURL => new Uri("https://www.nexusmods.com"); public Uri IconUri => new Uri("https://www.nexusmods.com/favicon.ico"); - + public ICommand TriggerLogin { get; } public ICommand ClearLogin { get; } + public NexusDownloader() + { + TriggerLogin = ReactiveCommand.CreateFromTask( + execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey), + canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); + ClearLogin = ReactiveCommand.Create( + execute: () => Utils.CatchAndLog(() => Utils.DeleteEncryptedJson("nexusapikey")), + canExecute: IsLoggedIn.ObserveOn(RxApp.MainThreadScheduler)); + } + public async Task GetDownloaderState(dynamic archiveINI) { var general = archiveINI?.General; diff --git a/Wabbajack.Lib/NexusApi/RequestNexusAuthorization.cs b/Wabbajack.Lib/NexusApi/RequestNexusAuthorization.cs index fb9cffae..bf17e3a3 100644 --- a/Wabbajack.Lib/NexusApi/RequestNexusAuthorization.cs +++ b/Wabbajack.Lib/NexusApi/RequestNexusAuthorization.cs @@ -24,7 +24,7 @@ namespace Wabbajack.Lib.NexusApi public override void Cancel() { Handled = true; - _source.SetCanceled(); + _source.TrySetCanceled(); } } } diff --git a/Wabbajack.Lib/Wabbajack.Lib.csproj b/Wabbajack.Lib/Wabbajack.Lib.csproj index d5a29392..a78d73b3 100644 --- a/Wabbajack.Lib/Wabbajack.Lib.csproj +++ b/Wabbajack.Lib/Wabbajack.Lib.csproj @@ -45,6 +45,9 @@ 7.3 prompt MinimumRecommendedRules.ruleset + false + CS4014 + CS1998 bin\x64\Release\ @@ -55,6 +58,8 @@ 7.3 prompt MinimumRecommendedRules.ruleset + CS4014 + CS1998 diff --git a/Wabbajack/View Models/Compilers/MO2CompilerVM.cs b/Wabbajack/View Models/Compilers/MO2CompilerVM.cs index 55576556..eba68e75 100644 --- a/Wabbajack/View Models/Compilers/MO2CompilerVM.cs +++ b/Wabbajack/View Models/Compilers/MO2CompilerVM.cs @@ -28,8 +28,6 @@ namespace Wabbajack public FilePickerVM ModListLocation { get; } - public IReactiveCommand BeginCommand { get; } - [Reactive] public ACompiler ActiveCompilation { get; private set; } diff --git a/Wabbajack/View Models/Compilers/VortexCompilerVM.cs b/Wabbajack/View Models/Compilers/VortexCompilerVM.cs index 30718b10..d87ff48b 100644 --- a/Wabbajack/View Models/Compilers/VortexCompilerVM.cs +++ b/Wabbajack/View Models/Compilers/VortexCompilerVM.cs @@ -20,8 +20,6 @@ namespace Wabbajack private readonly VortexCompilationSettings _settings; - public IReactiveCommand BeginCommand { get; } - private readonly ObservableAsPropertyHelper _modListSettings; public ModlistSettingsEditorVM ModlistSettings => _modListSettings.Value; diff --git a/Wabbajack/View Models/MainWindowVM.cs b/Wabbajack/View Models/MainWindowVM.cs index 5975a11b..f7fded99 100644 --- a/Wabbajack/View Models/MainWindowVM.cs +++ b/Wabbajack/View Models/MainWindowVM.cs @@ -74,7 +74,29 @@ namespace Wabbajack Utils.LogMessages .OfType() .ObserveOnGuiThread() - .SelectTask(msg => UserInterventionHandlers.Handle(msg)) + .SelectTask(async msg => + { + try + { + await UserInterventionHandlers.Handle(msg); + } + catch (Exception ex) + when (ex.GetType() != typeof(TaskCanceledException)) + { + Utils.Error(ex, $"Error while handling user intervention of type {msg?.GetType()}"); + try + { + if (!msg.Handled) + { + msg.Cancel(); + } + } + catch (Exception cancelEx) + { + Utils.Error(cancelEx, $"Error while cancelling user intervention of type {msg?.GetType()}"); + } + } + }) .Subscribe() .DisposeWith(CompositeDisposable);