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

100 lines
3.8 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 IHttpClientAccessor? httpApiClient;
2022-05-11 19:07:21 +08:00
private readonly UserScriptRunner? userScriptRunner;
2021-07-05 23:30:13 +08:00
public AdvancedSettingsPage(IHttpClientAccessor? 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(
(IHttpClientAccessor?)(RootPage.ServiceProvider?.GetService(typeof(IHttpClientAccessor))),
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()
{
if (this.httpApiClient is null)
{
MessageBox.Show("No Http Client Available", "Cookie Test - Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var resp = await this.httpApiClient.MainHttpClient.GetStringAsync("https://api.live.bilibili.com/xlive/web-ucenter/user/get_user_info").ConfigureAwait(false);
var jo = JObject.Parse(resp);
if (jo["code"]?.ToObject<int>() != 0)
{
MessageBox.Show("Response:\n" + resp, "Cookie Test - Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
var b = new StringBuilder();
b.Append("User: ");
b.Append(jo["data"]?["uname"]?.ToObject<string>());
b.Append("\nUID (from API response): ");
b.Append(jo["data"]?["uid"]?.ToObject<string>());
b.Append("\nUID (from Cookie): ");
b.Append(this.httpApiClient.GetUid());
MessageBox.Show(b.ToString(), "Cookie Test - Successed", MessageBoxButton.OK, MessageBoxImage.Information);
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
}
}