2023-01-26 06:29:00 +00:00
|
|
|
|
using BeaconLib;
|
|
|
|
|
using LANCommander.SDK.Models;
|
2023-01-06 00:37:13 +00:00
|
|
|
|
using Playnite.SDK;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2023-03-13 22:37:36 +00:00
|
|
|
|
using System.Threading;
|
2023-01-06 00:37:13 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using System.Windows.Navigation;
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
|
2023-01-08 18:09:57 +00:00
|
|
|
|
namespace LANCommander.PlaynitePlugin.Views
|
2023-01-06 00:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
public partial class Authentication : UserControl
|
|
|
|
|
{
|
2023-08-21 23:44:20 +00:00
|
|
|
|
public static readonly ILogger Logger = LogManager.GetLogger();
|
|
|
|
|
|
2023-01-14 21:15:31 +00:00
|
|
|
|
private LANCommanderLibraryPlugin Plugin;
|
2023-01-06 00:37:13 +00:00
|
|
|
|
private ViewModels.Authentication Context { get { return (ViewModels.Authentication)DataContext; } }
|
|
|
|
|
|
2023-01-14 21:15:31 +00:00
|
|
|
|
public Authentication(LANCommanderLibraryPlugin plugin)
|
2023-01-06 00:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
Plugin = plugin;
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
2023-01-26 06:29:00 +00:00
|
|
|
|
|
|
|
|
|
var probe = new Probe("LANCommander");
|
|
|
|
|
|
2023-08-21 23:44:20 +00:00
|
|
|
|
Logger.Trace("Attempting to find a LANCommander server on the local network...");
|
|
|
|
|
|
2023-01-26 06:29:00 +00:00
|
|
|
|
probe.BeaconsUpdated += beacons => Dispatcher.BeginInvoke((System.Action)(() =>
|
|
|
|
|
{
|
|
|
|
|
var beacon = beacons.First();
|
|
|
|
|
|
2023-10-30 23:45:11 +00:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(beacon.Data) && Uri.TryCreate(beacon.Data, UriKind.Absolute, out var beaconUri))
|
|
|
|
|
Context.ServerAddress = beaconUri.ToString();
|
|
|
|
|
else
|
|
|
|
|
Context.ServerAddress = $"http://{beacon.Address.Address}:{beacon.Address.Port}";
|
2023-01-26 06:29:00 +00:00
|
|
|
|
|
|
|
|
|
this.ServerAddress.Text = Context.ServerAddress;
|
|
|
|
|
|
2023-08-21 23:44:20 +00:00
|
|
|
|
Logger.Trace($"The beacons have been lit! LANCommander calls for aid! {Context.ServerAddress}");
|
|
|
|
|
|
2023-01-26 06:29:00 +00:00
|
|
|
|
probe.Stop();
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
probe.Start();
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TextBox_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
|
{
|
2023-01-16 02:45:37 +00:00
|
|
|
|
if (e.Key == System.Windows.Input.Key.Enter || e.Key == System.Windows.Input.Key.Return)
|
2023-01-06 00:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
Authenticate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoginButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Authenticate();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 07:31:42 +00:00
|
|
|
|
private void RegisterButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Register();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
private void Password_PasswordChanged(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (DataContext != null)
|
|
|
|
|
{
|
|
|
|
|
((ViewModels.Authentication)DataContext).Password = ((PasswordBox)sender).Password;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 22:37:36 +00:00
|
|
|
|
public RelayCommand<object> LoginCommand
|
|
|
|
|
{
|
|
|
|
|
get => new RelayCommand<object>(async (a) =>
|
|
|
|
|
{
|
|
|
|
|
await Authenticate();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task Authenticate()
|
2023-01-06 00:37:13 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-03-13 22:37:36 +00:00
|
|
|
|
LoginButton.Dispatcher.Invoke(new System.Action(() =>
|
|
|
|
|
{
|
|
|
|
|
LoginButton.IsEnabled = false;
|
|
|
|
|
LoginButton.Content = "Logging in...";
|
|
|
|
|
}));
|
|
|
|
|
|
2023-11-10 07:32:30 +00:00
|
|
|
|
if (Plugin.LANCommanderClient == null)
|
|
|
|
|
Plugin.LANCommanderClient = new LANCommander.SDK.Client(Context.ServerAddress);
|
2023-11-18 02:47:11 +00:00
|
|
|
|
else
|
|
|
|
|
Plugin.LANCommanderClient.UseServerAddress(Context.ServerAddress);
|
2023-01-07 18:34:12 +00:00
|
|
|
|
|
2023-11-10 07:32:30 +00:00
|
|
|
|
var response = await Plugin.LANCommanderClient.AuthenticateAsync(Context.UserName, Context.Password);
|
2023-01-06 00:37:13 +00:00
|
|
|
|
|
2023-01-07 18:34:12 +00:00
|
|
|
|
Plugin.Settings.ServerAddress = Context.ServerAddress;
|
|
|
|
|
Plugin.Settings.AccessToken = response.AccessToken;
|
|
|
|
|
Plugin.Settings.RefreshToken = response.RefreshToken;
|
2023-01-06 00:37:13 +00:00
|
|
|
|
|
2023-11-10 07:32:30 +00:00
|
|
|
|
var profile = Plugin.LANCommanderClient.GetProfile();
|
2023-10-17 01:48:12 +00:00
|
|
|
|
|
|
|
|
|
Plugin.Settings.PlayerName = String.IsNullOrWhiteSpace(profile.Alias) ? profile.UserName : profile.Alias;
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
// Probably unneeded, but why not be more secure?
|
|
|
|
|
Context.Password = String.Empty;
|
|
|
|
|
|
2023-01-12 02:48:39 +00:00
|
|
|
|
Plugin.SavePluginSettings(Plugin.Settings);
|
2023-01-06 00:37:13 +00:00
|
|
|
|
|
|
|
|
|
Window.GetWindow(this).Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-11-18 02:47:11 +00:00
|
|
|
|
Logger.Error(ex, ex.Message);
|
|
|
|
|
|
2023-01-06 00:37:13 +00:00
|
|
|
|
Plugin.PlayniteApi.Dialogs.ShowErrorMessage(ex.Message);
|
2023-03-14 06:10:02 +00:00
|
|
|
|
|
|
|
|
|
LoginButton.Dispatcher.Invoke(new System.Action(() =>
|
|
|
|
|
{
|
|
|
|
|
LoginButton.IsEnabled = true;
|
|
|
|
|
LoginButton.Content = "Login";
|
|
|
|
|
}));
|
2023-01-06 00:37:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-14 07:31:42 +00:00
|
|
|
|
|
|
|
|
|
private async Task Register()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LoginButton.IsEnabled = false;
|
|
|
|
|
RegisterButton.IsEnabled = false;
|
|
|
|
|
RegisterButton.Content = "Working...";
|
|
|
|
|
|
2023-11-10 07:32:30 +00:00
|
|
|
|
if (Plugin.LANCommanderClient == null)
|
|
|
|
|
Plugin.LANCommanderClient = new LANCommander.SDK.Client(Context.ServerAddress);
|
2023-03-14 07:31:42 +00:00
|
|
|
|
|
2023-11-10 07:32:30 +00:00
|
|
|
|
var response = await Plugin.LANCommanderClient.RegisterAsync(Context.UserName, Context.Password);
|
2023-03-14 07:31:42 +00:00
|
|
|
|
|
|
|
|
|
Plugin.Settings.ServerAddress = Context.ServerAddress;
|
|
|
|
|
Plugin.Settings.AccessToken = response.AccessToken;
|
|
|
|
|
Plugin.Settings.RefreshToken = response.RefreshToken;
|
2023-03-15 22:44:26 +00:00
|
|
|
|
Plugin.Settings.PlayerName = Context.UserName;
|
2023-03-14 07:31:42 +00:00
|
|
|
|
|
|
|
|
|
Context.Password = String.Empty;
|
|
|
|
|
|
|
|
|
|
Plugin.SavePluginSettings(Plugin.Settings);
|
|
|
|
|
|
|
|
|
|
Window.GetWindow(this).Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Plugin.PlayniteApi.Dialogs.ShowErrorMessage(ex.Message);
|
|
|
|
|
|
|
|
|
|
LoginButton.IsEnabled = true;
|
|
|
|
|
RegisterButton.IsEnabled = true;
|
|
|
|
|
RegisterButton.Content = "Register";
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-06 00:37:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|