BililiveRecorder/BililiveRecorder.WPF/Pages/AdvancedSettingsPage.xaml.cs

89 lines
3.2 KiB
C#
Raw Normal View History

2020-11-27 18:51:02 +08:00
using System;
using System.Runtime.Serialization;
using System.Text;
2021-05-18 22:36:37 +08:00
using System.Threading.Tasks;
2020-11-27 18:51:02 +08:00
using System.Windows;
using BililiveRecorder.Core.Api;
2022-05-11 19:07:21 +08:00
using BililiveRecorder.Core.Scripting;
2021-07-05 23:30:13 +08:00
using Newtonsoft.Json.Linq;
using Serilog;
2020-11-27 18:51:02 +08:00
2021-07-05 23:30:13 +08:00
#nullable enable
2020-11-27 18:51:02 +08:00
namespace BililiveRecorder.WPF.Pages
{
/// <summary>
/// Interaction logic for AdvancedSettingsPage.xaml
/// </summary>
public partial class AdvancedSettingsPage
{
2021-07-05 23:30:13 +08:00
private static readonly ILogger logger = Log.ForContext<AdvancedSettingsPage>();
private readonly ICookieTester? httpApiClient;
2022-05-11 19:07:21 +08:00
private readonly UserScriptRunner? userScriptRunner;
2021-07-05 23:30:13 +08:00
public AdvancedSettingsPage(ICookieTester? httpApiClient, UserScriptRunner? userScriptRunner)
2020-11-27 18:51:02 +08:00
{
2021-01-01 14:46:27 +08:00
this.InitializeComponent();
2021-07-05 23:30:13 +08:00
this.httpApiClient = httpApiClient;
2022-05-11 19:07:21 +08:00
this.userScriptRunner = userScriptRunner;
2021-07-05 23:30:13 +08:00
}
2022-05-11 19:07:21 +08:00
public AdvancedSettingsPage()
: this(
(ICookieTester?)(RootPage.ServiceProvider?.GetService(typeof(ICookieTester))),
2022-05-11 19:07:21 +08:00
(UserScriptRunner?)(RootPage.ServiceProvider?.GetService(typeof(UserScriptRunner)))
)
{ }
2021-05-18 22:36:37 +08:00
private void Crash_Click(object sender, RoutedEventArgs e) => throw new TestException("test crash triggered");
public class TestException : Exception
{
public TestException() { }
public TestException(string message) : base(message) { }
public TestException(string message, Exception innerException) : base(message, innerException) { }
protected TestException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
2021-05-18 22:36:37 +08:00
private void Throw_In_Task_Click(object sender, RoutedEventArgs e) => _ = Task.Run(() =>
{
throw new TestException("test task exception triggered");
});
2021-07-05 23:30:13 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
private async void TestCookie_Click(object sender, RoutedEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
{
try
{
await this.TestCookieAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
logger.Warning(ex, "Exception in TestCookie");
MessageBox.Show(ex.ToString(), "Cookie Test - Error", MessageBoxButton.OK);
}
}
private async Task TestCookieAsync()
{
bool succeed;
string message;
2021-07-05 23:30:13 +08:00
if (this.httpApiClient is null)
(succeed, message) = (false, "No Http Client Available");
else
(succeed, message) = await this.httpApiClient.TestCookieAsync().ConfigureAwait(false);
if (succeed)
MessageBox.Show(message, "Cookie Test - Succeed", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show(message, "Cookie Test - Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
2021-07-05 23:30:13 +08:00
}
2022-05-11 19:07:21 +08:00
private void TestScript_Click(object sender, RoutedEventArgs e)
{
_ = Task.Run(() => this.userScriptRunner?.CallOnTest(Log.Logger, str => MessageBox.Show(str)));
}
2020-11-27 18:51:02 +08:00
}
}