From 33161f98173623ea41f0fdfb15e9cadff271d5b5 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 26 Dec 2021 17:38:44 -0700 Subject: [PATCH] Several high-level compile errors fixed, ported several files --- .../ClientAPIAdditions/ClientAPIEx.cs | 5 +- .../AbsolutePathToStringConverter.cs | 3 +- .../Converters/PercentToDoubleConverter.cs | 1 + .../Error States/ErrorResponse.cs | 106 ++++++++++++++ Wabbajack.App.Wpf/Error States/GetResponse.cs | 135 ++++++++++++++++++ Wabbajack.App.Wpf/Settings.cs | 13 +- Wabbajack.App.Wpf/Util/FilePickerVM.cs | 2 + .../View Models/Gallery/ModListGalleryVM.cs | 2 +- .../View Models/Gallery/ModListMetadataVM.cs | 5 + Wabbajack.App.Wpf/View Models/GameVM.cs | 2 +- .../View Models/Installers/InstallerVM.cs | 4 + .../View Models/Settings/AuthorFilesVM.cs | 24 +++- Wabbajack.App.Wpf/View Models/SlideShow.cs | 2 + Wabbajack.App.Wpf/View Models/WebBrowserVM.cs | 12 +- Wabbajack.App.Wpf/Wabbajack.csproj | 4 +- Wabbajack.Lib/FodyWeavers.xml | 1 - Wabbajack.Lib/Wabbajack.Lib.csproj | 15 -- 17 files changed, 291 insertions(+), 45 deletions(-) create mode 100644 Wabbajack.App.Wpf/Error States/ErrorResponse.cs create mode 100644 Wabbajack.App.Wpf/Error States/GetResponse.cs diff --git a/Wabbajack.App.Wpf/ClientAPIAdditions/ClientAPIEx.cs b/Wabbajack.App.Wpf/ClientAPIAdditions/ClientAPIEx.cs index cafef8f8..93ae2a1c 100644 --- a/Wabbajack.App.Wpf/ClientAPIAdditions/ClientAPIEx.cs +++ b/Wabbajack.App.Wpf/ClientAPIAdditions/ClientAPIEx.cs @@ -2,10 +2,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; -using Wabbajack.Common; -using Wabbajack.Common.Serialization.Json; +using Wabbajack.DTOs; +using Wabbajack.DTOs.JsonConverters; using Wabbajack.Lib; -using Wabbajack.Lib.ModListRegistry; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/Converters/AbsolutePathToStringConverter.cs b/Wabbajack.App.Wpf/Converters/AbsolutePathToStringConverter.cs index 39aae18d..c3b18f49 100644 --- a/Wabbajack.App.Wpf/Converters/AbsolutePathToStringConverter.cs +++ b/Wabbajack.App.Wpf/Converters/AbsolutePathToStringConverter.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Windows.Data; using ReactiveUI; using Wabbajack.Common; +using Wabbajack.Paths; namespace Wabbajack { @@ -51,7 +52,7 @@ namespace Wabbajack if (@from is AbsolutePath abs) { - result = (string)abs; + result = abs.ToString(); return true; } } diff --git a/Wabbajack.App.Wpf/Converters/PercentToDoubleConverter.cs b/Wabbajack.App.Wpf/Converters/PercentToDoubleConverter.cs index 850519dd..2eb47d55 100644 --- a/Wabbajack.App.Wpf/Converters/PercentToDoubleConverter.cs +++ b/Wabbajack.App.Wpf/Converters/PercentToDoubleConverter.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using System.Windows.Input; using ReactiveUI; using Wabbajack.Common; +using Wabbajack.RateLimiter; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/Error States/ErrorResponse.cs b/Wabbajack.App.Wpf/Error States/ErrorResponse.cs new file mode 100644 index 00000000..2aedfe1b --- /dev/null +++ b/Wabbajack.App.Wpf/Error States/ErrorResponse.cs @@ -0,0 +1,106 @@ +using System; + +namespace Wabbajack +{ + public struct ErrorResponse : IErrorResponse + { + public readonly static ErrorResponse Success = Succeed(); + public readonly static ErrorResponse Failure = new ErrorResponse(); + + public bool Succeeded { get; } + public Exception? Exception { get; } + private readonly string _reason; + + public bool Failed => !Succeeded; + public string Reason + { + get + { + if (Exception != null) + { + if (string.IsNullOrWhiteSpace(_reason)) + { + return Exception.ToString(); + } + else + { + return $"{_reason}: {Exception.Message}"; + } + } + return _reason; + } + } + + bool IErrorResponse.Succeeded => Succeeded; + Exception? IErrorResponse.Exception => Exception; + + private ErrorResponse( + bool succeeded, + string? reason = null, + Exception? ex = null) + { + Succeeded = succeeded; + _reason = reason ?? string.Empty; + Exception = ex; + } + + public override string ToString() + { + return $"({(Succeeded ? "Success" : "Fail")}, {Reason})"; + } + + #region Factories + public static ErrorResponse Succeed() + { + return new ErrorResponse(true); + } + + public static ErrorResponse Succeed(string reason) + { + return new ErrorResponse(true, reason); + } + + public static ErrorResponse Fail(string reason, Exception? ex = null) + { + return new ErrorResponse(false, reason: reason, ex: ex); + } + + public static ErrorResponse Fail(Exception ex) + { + return new ErrorResponse(false, ex: ex); + } + + public static ErrorResponse Fail() + { + return new ErrorResponse(false); + } + + public static ErrorResponse Create(bool successful, string? reason = null) + { + return new ErrorResponse(successful, reason); + } + #endregion + + public static ErrorResponse Convert(IErrorResponse err, bool nullIsSuccess = true) + { + if (err == null) return Create(nullIsSuccess); + return new ErrorResponse(err.Succeeded, err.Reason, err.Exception); + } + + public static ErrorResponse FirstFail(params ErrorResponse[] responses) + { + foreach (var resp in responses) + { + if (resp.Failed) return resp; + } + return ErrorResponse.Success; + } + } + + public interface IErrorResponse + { + bool Succeeded { get; } + Exception? Exception { get; } + string Reason { get; } + } +} diff --git a/Wabbajack.App.Wpf/Error States/GetResponse.cs b/Wabbajack.App.Wpf/Error States/GetResponse.cs new file mode 100644 index 00000000..71f823ba --- /dev/null +++ b/Wabbajack.App.Wpf/Error States/GetResponse.cs @@ -0,0 +1,135 @@ +using System; + +namespace Wabbajack +{ + public struct GetResponse : IEquatable>, IErrorResponse + { + public static readonly GetResponse Failure = new GetResponse(); + + public readonly T Value; + public readonly bool Succeeded; + public readonly Exception? Exception; + private readonly string _reason; + + public bool Failed => !Succeeded; + public string Reason + { + get + { + if (Exception != null) + { + return Exception.ToString(); + } + return _reason; + } + } + + bool IErrorResponse.Succeeded => Succeeded; + Exception? IErrorResponse.Exception => Exception; + + private GetResponse( + bool succeeded, + T? val = default, + string? reason = null, + Exception? ex = null) + { + Value = val!; + Succeeded = succeeded; + _reason = reason ?? string.Empty; + Exception = ex; + } + + public bool Equals(GetResponse other) + { + return Succeeded == other.Succeeded + && Equals(Value, other.Value); + } + + public override bool Equals(object? obj) + { + if (!(obj is GetResponse rhs)) return false; + return Equals(rhs); + } + + public override int GetHashCode() + { + System.HashCode hash = new HashCode(); + hash.Add(Value); + hash.Add(Succeeded); + return hash.ToHashCode(); + } + + public override string ToString() + { + return $"({(Succeeded ? "Success" : "Fail")}, {Value}, {Reason})"; + } + + public GetResponse BubbleFailure() + { + return new GetResponse( + succeeded: false, + reason: _reason, + ex: Exception); + } + + public GetResponse Bubble(Func conv) + { + return new GetResponse( + succeeded: Succeeded, + val: conv(Value), + reason: _reason, + ex: Exception); + } + + public T EvaluateOrThrow() + { + if (Succeeded) + { + return Value; + } + throw new ArgumentException(Reason); + } + + #region Factories + public static GetResponse Succeed(T value) + { + return new GetResponse(true, value); + } + + public static GetResponse Succeed(T value, string reason) + { + return new GetResponse(true, value, reason); + } + + public static GetResponse Fail(string reason) + { + return new GetResponse(false, reason: reason); + } + + public static GetResponse Fail(T val, string reason) + { + return new GetResponse(false, val, reason); + } + + public static GetResponse Fail(Exception ex) + { + return new GetResponse(false, ex: ex); + } + + public static GetResponse Fail(T val, Exception ex) + { + return new GetResponse(false, val, ex: ex); + } + + public static GetResponse Fail(T val) + { + return new GetResponse(false, val); + } + + public static GetResponse Create(bool successful, T? val = default(T), string? reason = null) + { + return new GetResponse(successful, val!, reason); + } + #endregion + } +} diff --git a/Wabbajack.App.Wpf/Settings.cs b/Wabbajack.App.Wpf/Settings.cs index 8b11a0e0..52997931 100644 --- a/Wabbajack.App.Wpf/Settings.cs +++ b/Wabbajack.App.Wpf/Settings.cs @@ -1,13 +1,8 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.IO; -using System.Reactive; -using System.Reactive.Subjects; -using System.Threading.Tasks; -using Wabbajack.Common; -using Wabbajack.Common.Serialization.Json; +using Wabbajack.Compiler; +using Wabbajack.DTOs.JsonConverters; using Wabbajack.Lib; +using Wabbajack.Paths; +using Consts = Wabbajack.Lib.Consts; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/Util/FilePickerVM.cs b/Wabbajack.App.Wpf/Util/FilePickerVM.cs index 9b753322..da08043f 100644 --- a/Wabbajack.App.Wpf/Util/FilePickerVM.cs +++ b/Wabbajack.App.Wpf/Util/FilePickerVM.cs @@ -10,6 +10,8 @@ using System.Reactive.Linq; using System.Windows.Input; using Wabbajack.Common; using Wabbajack.Lib; +using Wabbajack.Lib.Extensions; +using Wabbajack.Paths; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/View Models/Gallery/ModListGalleryVM.cs b/Wabbajack.App.Wpf/View Models/Gallery/ModListGalleryVM.cs index 160e3bba..99c57318 100644 --- a/Wabbajack.App.Wpf/View Models/Gallery/ModListGalleryVM.cs +++ b/Wabbajack.App.Wpf/View Models/Gallery/ModListGalleryVM.cs @@ -98,7 +98,7 @@ namespace Wabbajack var sourceList = Observable.Return(Unit.Default) .ObserveOn(RxApp.TaskpoolScheduler) - .SelectTask(async _ => + .SelectMany(async _ => { try { diff --git a/Wabbajack.App.Wpf/View Models/Gallery/ModListMetadataVM.cs b/Wabbajack.App.Wpf/View Models/Gallery/ModListMetadataVM.cs index bb54f344..fe0e70b5 100644 --- a/Wabbajack.App.Wpf/View Models/Gallery/ModListMetadataVM.cs +++ b/Wabbajack.App.Wpf/View Models/Gallery/ModListMetadataVM.cs @@ -16,9 +16,14 @@ using DynamicData; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Wabbajack.Common; +using Wabbajack.Downloaders; +using Wabbajack.DTOs; using Wabbajack.Lib; using Wabbajack.Lib.Downloaders; +using Wabbajack.Lib.Extensions; using Wabbajack.Lib.ModListRegistry; +using Wabbajack.Paths; +using Wabbajack.RateLimiter; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/View Models/GameVM.cs b/Wabbajack.App.Wpf/View Models/GameVM.cs index 89e9ad94..aa084228 100644 --- a/Wabbajack.App.Wpf/View Models/GameVM.cs +++ b/Wabbajack.App.Wpf/View Models/GameVM.cs @@ -1,4 +1,4 @@ -using Wabbajack.Common; +using Wabbajack.DTOs; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/View Models/Installers/InstallerVM.cs b/Wabbajack.App.Wpf/View Models/Installers/InstallerVM.cs index 7320eb29..c0993f71 100644 --- a/Wabbajack.App.Wpf/View Models/Installers/InstallerVM.cs +++ b/Wabbajack.App.Wpf/View Models/Installers/InstallerVM.cs @@ -21,8 +21,12 @@ using System.Collections.Generic; using System.Reactive.Subjects; using System.Windows.Input; using Microsoft.WindowsAPICodePack.Dialogs; +using Microsoft.WindowsAPICodePack.Shell; using Wabbajack.Common.IO; +using Wabbajack.Lib.Extensions; using Wabbajack.Lib.Interventions; +using Wabbajack.Paths; +using Wabbajack.RateLimiter; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/View Models/Settings/AuthorFilesVM.cs b/Wabbajack.App.Wpf/View Models/Settings/AuthorFilesVM.cs index 3bd2bb9e..16c4b66d 100644 --- a/Wabbajack.App.Wpf/View Models/Settings/AuthorFilesVM.cs +++ b/Wabbajack.App.Wpf/View Models/Settings/AuthorFilesVM.cs @@ -5,18 +5,23 @@ using System.Reactive.Subjects; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; +using System.Windows.Threading; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Wabbajack.Common; using Wabbajack.Lib.AuthorApi; using Wabbajack.Lib.FileUploader; +using Wabbajack.Networking.Http.Interfaces; +using Wabbajack.Services.OSIntegrated.TokenProviders; namespace Wabbajack { public class AuthorFilesVM : BackNavigatingVM { private readonly ObservableAsPropertyHelper _isVisible; - public Visibility IsVisible => _isVisible.Value; + + [Reactive] + public Visibility IsVisible { get; set; }; public ICommand SelectFile { get; } public ICommand HyperlinkCommand { get; } @@ -25,21 +30,26 @@ namespace Wabbajack [Reactive] public double UploadProgress { get; set; } [Reactive] public string FinalUrl { get; set; } - - private WorkQueue Queue = new WorkQueue(1); - public FilePickerVM Picker { get;} private Subject _isUploading = new Subject(); + private readonly WabbajackApiTokenProvider _token; private IObservable IsUploading { get; } - public AuthorFilesVM(SettingsVM vm) : base(vm.MWVM) + public AuthorFilesVM(WabbajackApiTokenProvider token, SettingsVM vm) : base(vm.MWVM) { + _token = token; IsUploading = _isUploading; Picker = new FilePickerVM(this); - _isVisible = AuthorAPI.HaveAuthorAPIKey.Select(h => h ? Visibility.Visible : Visibility.Collapsed) - .ToProperty(this, x => x.IsVisible); + + IsVisible = Visibility.Hidden; + + Task.Run(async () => + { + var isAuthor = string.IsNullOrWhiteSpace((await _token.Get())?.AuthorKey); + IsVisible = isAuthor ? Visibility.Visible : Visibility.Collapsed; + }); SelectFile = Picker.ConstructTypicalPickerCommand(IsUploading.StartWith(false).Select(u => !u)); diff --git a/Wabbajack.App.Wpf/View Models/SlideShow.cs b/Wabbajack.App.Wpf/View Models/SlideShow.cs index d1d9fbdd..9926e88b 100644 --- a/Wabbajack.App.Wpf/View Models/SlideShow.cs +++ b/Wabbajack.App.Wpf/View Models/SlideShow.cs @@ -10,8 +10,10 @@ using System.Reactive.Linq; using System.Text.RegularExpressions; using System.Windows.Media.Imaging; using Wabbajack.Common; +using Wabbajack.DTOs.DownloadStates; using Wabbajack.Lib; using Wabbajack.Lib.Downloaders; +using Wabbajack.Lib.Extensions; namespace Wabbajack { diff --git a/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs b/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs index d5876b77..5db62e9b 100644 --- a/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs +++ b/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using CefSharp; using CefSharp.Wpf; +using Microsoft.Extensions.Logging; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Wabbajack.Lib; @@ -18,11 +19,13 @@ namespace Wabbajack { public class WebBrowserVM : ViewModel, IBackNavigatingVM, IDisposable { + private readonly ILogger _logger; + [Reactive] public string Instructions { get; set; } public IWebBrowser Browser { get; } = new ChromiumWebBrowser(); - public CefSharpWrapper Driver => new CefSharpWrapper(Browser); + public CefSharpWrapper Driver => new(_logger, Browser); [Reactive] public ViewModel NavigateBackTarget { get; set; } @@ -33,16 +36,17 @@ namespace Wabbajack public Subject IsBackEnabledSubject { get; } = new Subject(); public IObservable IsBackEnabled { get; } - private WebBrowserVM(string url = "http://www.wabbajack.org") + private WebBrowserVM(ILogger logger, string url = "http://www.wabbajack.org") { + _logger = logger; IsBackEnabled = IsBackEnabledSubject.StartWith(true); Instructions = "Wabbajack Web Browser"; } - public static async Task GetNew(string url = "http://www.wabbajack.org") + public static async Task GetNew(ILogger logger, string url = "http://www.wabbajack.org") { // Make sure libraries are extracted first - return new WebBrowserVM(url); + return new WebBrowserVM(logger, url); } public override void Dispose() diff --git a/Wabbajack.App.Wpf/Wabbajack.csproj b/Wabbajack.App.Wpf/Wabbajack.csproj index 1e22f8b8..c9630152 100644 --- a/Wabbajack.App.Wpf/Wabbajack.csproj +++ b/Wabbajack.App.Wpf/Wabbajack.csproj @@ -85,10 +85,8 @@ - - - + diff --git a/Wabbajack.Lib/FodyWeavers.xml b/Wabbajack.Lib/FodyWeavers.xml index 95eaad52..63fc1484 100644 --- a/Wabbajack.Lib/FodyWeavers.xml +++ b/Wabbajack.Lib/FodyWeavers.xml @@ -1,4 +1,3 @@  - \ No newline at end of file diff --git a/Wabbajack.Lib/Wabbajack.Lib.csproj b/Wabbajack.Lib/Wabbajack.Lib.csproj index 5433596b..10d0e524 100644 --- a/Wabbajack.Lib/Wabbajack.Lib.csproj +++ b/Wabbajack.Lib/Wabbajack.Lib.csproj @@ -25,12 +25,6 @@ 2.2.2.1 - - 1.11.34 - - - 1.9.0 - 4.7.0 @@ -61,22 +55,13 @@ 5.0.0 - - 1.0.1 - - - 1.0.0 - 1.0.0 - - -