From 97e005a4d3385ff461b44eefd8397b20966c68d2 Mon Sep 17 00:00:00 2001 From: Genteure Date: Sat, 1 May 2021 17:57:43 +0800 Subject: [PATCH] Minor updates --- .../Api/Http/HttpApiClient.cs | 4 +- BililiveRecorder.Core/Config/ConfigParser.cs | 4 +- BililiveRecorder.Core/PollyPolicy.cs | 2 +- BililiveRecorder.WPF/App.xaml.cs | 2 +- .../BililiveRecorder.WPF.csproj | 1 - .../Controls/AddRoomCard.xaml.cs | 3 +- .../Controls/RoomCard.xaml.cs | 5 +- .../Converters/RatioToArrowIconConverter.cs | 3 +- .../Converters/RoomListInterceptConverter.cs | 59 ------------------- BililiveRecorder.WPF/Models/AboutModel.cs | 3 +- BililiveRecorder.WPF/Models/RootModel.cs | 7 ++- BililiveRecorder.WPF/NewMainWindow.xaml.cs | 2 + .../Pages/AnnouncementPage.xaml.cs | 13 +++- BililiveRecorder.WPF/Pages/RootPage.xaml.cs | 2 + .../Pages/ToolboxAutoFixPage.xaml.cs | 9 +++ 15 files changed, 45 insertions(+), 74 deletions(-) delete mode 100644 BililiveRecorder.WPF/Converters/RoomListInterceptConverter.cs diff --git a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs index 7c92a18..df0fd53 100644 --- a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs +++ b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs @@ -85,8 +85,8 @@ namespace BililiveRecorder.Core.Api.Http var text = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); var obj = JsonConvert.DeserializeObject>(text); - if (obj.Code != 0) - throw new BilibiliApiResponseCodeNotZeroException("Bilibili api code: " + (obj.Code?.ToString() ?? "(null)") + "\n" + text); + if (obj?.Code != 0) + throw new BilibiliApiResponseCodeNotZeroException("Bilibili api code: " + (obj?.Code?.ToString() ?? "(null)") + "\n" + text); return obj; } diff --git a/BililiveRecorder.Core/Config/ConfigParser.cs b/BililiveRecorder.Core/Config/ConfigParser.cs index 41ff76c..9a7bb27 100644 --- a/BililiveRecorder.Core/Config/ConfigParser.cs +++ b/BililiveRecorder.Core/Config/ConfigParser.cs @@ -60,8 +60,10 @@ namespace BililiveRecorder.Core.Config #pragma warning disable CS0612 var v1Data = JsonConvert.DeserializeObject(v1.Data ?? string.Empty); #pragma warning restore CS0612 - var newConfig = ConfigMapper.Map1To2(v1Data); + if (v1Data is null) + return new V2.ConfigV2(); + var newConfig = ConfigMapper.Map1To2(v1Data); return newConfig; } case V2.ConfigV2 v2: diff --git a/BililiveRecorder.Core/PollyPolicy.cs b/BililiveRecorder.Core/PollyPolicy.cs index 3104a62..3a71189 100644 --- a/BililiveRecorder.Core/PollyPolicy.cs +++ b/BililiveRecorder.Core/PollyPolicy.cs @@ -24,7 +24,7 @@ namespace BililiveRecorder.Core durationOfBreak: TimeSpan.FromMinutes(2), onBreak: (_, _) => { - logger.Warning("检测到被屏蔽,暂停发送请求"); + logger.Warning("检测到被屏蔽(HTTP 412),暂停发送请求"); }, onReset: () => { diff --git a/BililiveRecorder.WPF/App.xaml.cs b/BililiveRecorder.WPF/App.xaml.cs index a884631..148718e 100644 --- a/BililiveRecorder.WPF/App.xaml.cs +++ b/BililiveRecorder.WPF/App.xaml.cs @@ -52,6 +52,6 @@ namespace BililiveRecorder.WPF private void SystemEvents_TimeChanged(object sender, System.EventArgs e) => this.loggerSystemEvents.Debug("TimeChanged"); - private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) => this.loggerSystemEvents.Warning("PowerModeChanged {Mode}", e.Mode); + private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) => this.loggerSystemEvents.Debug("PowerModeChanged {Mode}", e.Mode); } } diff --git a/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj b/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj index 08fdf21..ec58233 100644 --- a/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj +++ b/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj @@ -140,7 +140,6 @@ - ToolboxAutoFixPage.xaml diff --git a/BililiveRecorder.WPF/Controls/AddRoomCard.xaml.cs b/BililiveRecorder.WPF/Controls/AddRoomCard.xaml.cs index 947470e..2a23d00 100644 --- a/BililiveRecorder.WPF/Controls/AddRoomCard.xaml.cs +++ b/BililiveRecorder.WPF/Controls/AddRoomCard.xaml.cs @@ -3,6 +3,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; +#nullable enable namespace BililiveRecorder.WPF.Controls { /// @@ -10,7 +11,7 @@ namespace BililiveRecorder.WPF.Controls /// public partial class AddRoomCard : UserControl { - public event EventHandler AddRoomRequested; + public event EventHandler? AddRoomRequested; public AddRoomCard() { diff --git a/BililiveRecorder.WPF/Controls/RoomCard.xaml.cs b/BililiveRecorder.WPF/Controls/RoomCard.xaml.cs index 2c81dc6..7ce2222 100644 --- a/BililiveRecorder.WPF/Controls/RoomCard.xaml.cs +++ b/BililiveRecorder.WPF/Controls/RoomCard.xaml.cs @@ -4,6 +4,7 @@ using System.Windows; using System.Windows.Controls; using BililiveRecorder.Core; +#nullable enable namespace BililiveRecorder.WPF.Controls { /// @@ -16,9 +17,9 @@ namespace BililiveRecorder.WPF.Controls this.InitializeComponent(); } - public event EventHandler DeleteRequested; + public event EventHandler? DeleteRequested; - public event EventHandler ShowSettingsRequested; + public event EventHandler? ShowSettingsRequested; private void MenuItem_StartRecording_Click(object sender, RoutedEventArgs e) => (this.DataContext as IRoom)?.StartRecord(); diff --git a/BililiveRecorder.WPF/Converters/RatioToArrowIconConverter.cs b/BililiveRecorder.WPF/Converters/RatioToArrowIconConverter.cs index 68c2f8f..dfeadfc 100644 --- a/BililiveRecorder.WPF/Converters/RatioToArrowIconConverter.cs +++ b/BililiveRecorder.WPF/Converters/RatioToArrowIconConverter.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Windows; using System.Windows.Data; +#nullable enable namespace BililiveRecorder.WPF.Converters { public class RatioToArrowIconConverter : DependencyObject, IValueConverter @@ -13,7 +14,7 @@ namespace BililiveRecorder.WPF.Converters public object UpArrow { get => this.GetValue(UpArrowProperty); set => this.SetValue(UpArrowProperty, value); } public object DownArrow { get => this.GetValue(DownArrowProperty); set => this.SetValue(DownArrowProperty, value); } - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => + public object? Convert(object value, Type targetType, object parameter, CultureInfo culture) => value is double num ? num < 0.97 ? this.DownArrow : num > 1.03 ? this.UpArrow : null : null; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); diff --git a/BililiveRecorder.WPF/Converters/RoomListInterceptConverter.cs b/BililiveRecorder.WPF/Converters/RoomListInterceptConverter.cs deleted file mode 100644 index 2874d15..0000000 --- a/BililiveRecorder.WPF/Converters/RoomListInterceptConverter.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.Globalization; -using System.Windows.Data; -using BililiveRecorder.Core; - -namespace BililiveRecorder.WPF.Converters -{ - internal class RoomListInterceptConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return value is RecorderWrapper ? value : value is IRecorder recorder ? new RecorderWrapper(recorder) : value; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - return value; - } - - private class RecorderWrapper : ObservableCollection - { - private readonly IRecorder recorder; - - public RecorderWrapper(IRecorder recorder) : base(recorder.Rooms) - { - this.recorder = recorder; - this.Add(null); - - // TODO fix me - //recorder.Rooms.CollectionChanged += (sender, e) => - //{ - // switch (e.Action) - // { - // case NotifyCollectionChangedAction.Add: - // if (e.NewItems.Count != 1) throw new NotImplementedException("Wrapper Add Item Count != 1"); - // this.InsertItem(e.NewStartingIndex, e.NewItems[0] as IRecordedRoom); - // break; - // case NotifyCollectionChangedAction.Remove: - // if (e.OldItems.Count != 1) throw new NotImplementedException("Wrapper Remove Item Count != 1"); - // if (!this.Remove(e.OldItems[0] as IRecordedRoom)) throw new NotImplementedException("Wrapper Remove Item Sync Fail"); - // break; - // case NotifyCollectionChangedAction.Replace: - // throw new NotImplementedException("Wrapper Replace Item"); - // case NotifyCollectionChangedAction.Move: - // throw new NotImplementedException("Wrapper Move Item"); - // case NotifyCollectionChangedAction.Reset: - // this.ClearItems(); - // this.Add(null); - // break; - // default: - // break; - // } - //}; - } - } - } -} diff --git a/BililiveRecorder.WPF/Models/AboutModel.cs b/BililiveRecorder.WPF/Models/AboutModel.cs index 2ba4480..59bd364 100644 --- a/BililiveRecorder.WPF/Models/AboutModel.cs +++ b/BililiveRecorder.WPF/Models/AboutModel.cs @@ -5,11 +5,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +#nullable enable namespace BililiveRecorder.WPF.Models { public class AboutModel : INotifyPropertyChanged { - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; public string InformationalVersion => GitVersionInformation.InformationalVersion; } diff --git a/BililiveRecorder.WPF/Models/RootModel.cs b/BililiveRecorder.WPF/Models/RootModel.cs index 735c85b..7de92e6 100644 --- a/BililiveRecorder.WPF/Models/RootModel.cs +++ b/BililiveRecorder.WPF/Models/RootModel.cs @@ -4,16 +4,17 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using BililiveRecorder.Core; +#nullable enable namespace BililiveRecorder.WPF.Models { internal class RootModel : INotifyPropertyChanged, IDisposable { private bool disposedValue; - private IRecorder recorder; + private IRecorder? recorder; - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; - public IRecorder Recorder { get => this.recorder; internal set => this.SetField(ref this.recorder, value); } + public IRecorder? Recorder { get => this.recorder; internal set => this.SetField(ref this.recorder, value); } public RootModel() { diff --git a/BililiveRecorder.WPF/NewMainWindow.xaml.cs b/BililiveRecorder.WPF/NewMainWindow.xaml.cs index 08e9a8e..0460686 100644 --- a/BililiveRecorder.WPF/NewMainWindow.xaml.cs +++ b/BililiveRecorder.WPF/NewMainWindow.xaml.cs @@ -88,7 +88,9 @@ namespace BililiveRecorder.WPF public bool PromptCloseConfirm { get; set; } = true; +#pragma warning disable VSTHRD100 // Avoid async void methods private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { if (this.PromptCloseConfirm && !this.CloseConfirmed) { diff --git a/BililiveRecorder.WPF/Pages/AnnouncementPage.xaml.cs b/BililiveRecorder.WPF/Pages/AnnouncementPage.xaml.cs index 4a22c6e..95ae91f 100644 --- a/BililiveRecorder.WPF/Pages/AnnouncementPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/AnnouncementPage.xaml.cs @@ -36,7 +36,16 @@ namespace BililiveRecorder.WPF.Pages _ = this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Func)(async () => await this.LoadAnnouncementAsync(ignore_cache: false, show_error: false))); } - private async void Button_Click(object sender, RoutedEventArgs e) => await this.LoadAnnouncementAsync(ignore_cache: true, show_error: Keyboard.Modifiers.HasFlag(ModifierKeys.Control)); +#pragma warning disable VSTHRD100 // Avoid async void methods + private async void Button_Click(object sender, RoutedEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods + { + try + { + await this.LoadAnnouncementAsync(ignore_cache: true, show_error: Keyboard.Modifiers.HasFlag(ModifierKeys.Control)); + } + catch (Exception) { } + } private async Task LoadAnnouncementAsync(bool ignore_cache, bool show_error) { @@ -110,7 +119,9 @@ namespace BililiveRecorder.WPF.Pages } } +#pragma warning disable VSTHRD100 // Avoid async void methods private async void Button_MouseRightButtonUp(object sender, MouseButtonEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || !Keyboard.Modifiers.HasFlag(ModifierKeys.Alt)) return; diff --git a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs index 8fef955..ef3e88c 100644 --- a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs @@ -85,7 +85,9 @@ namespace BililiveRecorder.WPF.Pages SingleInstance.Cleanup(); } +#pragma warning disable VSTHRD100 // Avoid async void methods private async void RootPage_Loaded(object sender, RoutedEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { var first_time = true; var from_argument = false; diff --git a/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs b/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs index a8c5bc5..6803f03 100644 --- a/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs @@ -49,7 +49,9 @@ namespace BililiveRecorder.WPF.Pages } } +#pragma warning disable VSTHRD100 // Avoid async void methods private async void Fix_Button_Click(object sender, RoutedEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { AutoFixProgressDialog? progressDialog = null; try @@ -74,6 +76,7 @@ namespace BililiveRecorder.WPF.Pages EnsureValidNames = true, NavigateToShortcut = true, OverwritePrompt = false, + InitialDirectory = Path.GetDirectoryName(inputPath), DefaultDirectory = Path.GetDirectoryName(inputPath), DefaultFileName = Path.GetFileName(inputPath) }; @@ -129,7 +132,9 @@ namespace BililiveRecorder.WPF.Pages } } +#pragma warning disable VSTHRD100 // Avoid async void methods private async void Analyze_Button_Click(object sender, RoutedEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { AutoFixProgressDialog? progressDialog = null; try @@ -192,7 +197,9 @@ namespace BililiveRecorder.WPF.Pages } } +#pragma warning disable VSTHRD100 // Avoid async void methods private async void Export_Button_Click(object sender, RoutedEventArgs e) +#pragma warning restore VSTHRD100 // Avoid async void methods { AutoFixProgressDialog? progressDialog = null; try @@ -216,6 +223,8 @@ namespace BililiveRecorder.WPF.Pages EnsurePathExists = true, EnsureValidNames = true, NavigateToShortcut = true, + OverwritePrompt = false, + InitialDirectory = Path.GetDirectoryName(inputPath), DefaultDirectory = Path.GetDirectoryName(inputPath), DefaultFileName = Path.GetFileNameWithoutExtension(inputPath) + ".brec.xml.gz" };