diff --git a/LANCommander.Playnite.Extension/LANCommanderClient.cs b/LANCommander.Playnite.Extension/LANCommanderClient.cs index c15f644..f324975 100644 --- a/LANCommander.Playnite.Extension/LANCommanderClient.cs +++ b/LANCommander.Playnite.Extension/LANCommanderClient.cs @@ -3,6 +3,7 @@ using RestSharp; using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -11,17 +12,60 @@ namespace LANCommander.Playnite.Extension internal class LANCommanderClient { private readonly RestClient Client; + private AuthToken Token; public LANCommanderClient() { Client = new RestClient("https://localhost:7087"); } + private T PostRequest(string route, object body) + { + var request = new RestRequest(route) + .AddJsonBody(body) + .AddHeader("Authorization", $"Bearer {Token.AccessToken}"); + + var response = Client.Post(request); + + return response.Data; + } + + public AuthResponse Authenticate(string username, string password) + { + var response = Client.Post(new RestRequest("/api/Auth").AddJsonBody(new AuthRequest() + { + UserName = username, + Password = password + })); + + return response.Data; + } + + public AuthResponse RefreshToken(AuthToken token) + { + var request = new RestRequest("/api/Auth/Refresh") + .AddJsonBody(token); + + var response = Client.Post(request); + + return response.Data; + } + + public bool ValidateToken(AuthToken token) + { + var request = new RestRequest("/api/Auth/Validate") + .AddHeader("Authorization", $"Bearer {token.AccessToken}"); + + var response = Client.Post(request); + + return response.StatusCode == HttpStatusCode.OK; + } + public IEnumerable GetGames() { var response = Client.Get>(new RestRequest("/api/Games")); return response.Data; - } + } } } diff --git a/LANCommander.Playnite.Extension/PlayniteClient.cs b/LANCommander.Playnite.Extension/PlayniteClient.cs index c630116..b4db8a6 100644 --- a/LANCommander.Playnite.Extension/PlayniteClient.cs +++ b/LANCommander.Playnite.Extension/PlayniteClient.cs @@ -13,7 +13,8 @@ namespace LANCommander.Playnite.Extension public override void Open() { - throw new NotImplementedException(); + + System.Diagnostics.Process.Start("https://localhost:7087"); } } } diff --git a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs index 805fc7a..ad96393 100644 --- a/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs +++ b/LANCommander.Playnite.Extension/PlayniteLibraryPlugin.cs @@ -13,8 +13,8 @@ namespace LANCommander.Playnite.Extension public class PlayniteLibraryPlugin : LibraryPlugin { public static readonly ILogger Logger = LogManager.GetLogger(); - private SettingsViewModel Settings { get; set; } - private LANCommanderClient LANCommander { get; set; } + private PlayniteSettingsViewModel Settings { get; set; } + internal LANCommanderClient LANCommander { get; set; } public override Guid Id { get; } = Guid.Parse("48e1bac7-e0a0-45d7-ba83-36f5e9e959fc"); public override string Name => "LANCommander"; @@ -23,7 +23,7 @@ namespace LANCommander.Playnite.Extension public PlayniteLibraryPlugin(IPlayniteAPI api) : base(api) { LANCommander = new LANCommanderClient(); - Settings = new SettingsViewModel(this); + Settings = new PlayniteSettingsViewModel(this); Properties = new LibraryPluginProperties { HasSettings = true, @@ -32,28 +32,51 @@ namespace LANCommander.Playnite.Extension public override IEnumerable GetGames(LibraryGetGamesArgs args) { - // Implement LANCommander client here - var games = LANCommander.GetGames().Select(g => new GameMetadata() + try { - Name = g.Title, - Description = g.Description, - GameId = g.Id.ToString(), - ReleaseDate = new ReleaseDate(g.ReleasedOn), - SortingName = g.SortTitle, - Version = g.Archives != null && g.Archives.Count() > 0 ? g.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version : null, - }); + // Implement LANCommander client here + var games = LANCommander.GetGames().Select(g => new GameMetadata() + { + Name = g.Title, + Description = g.Description, + GameId = g.Id.ToString(), + ReleaseDate = new ReleaseDate(g.ReleasedOn), + SortingName = g.SortTitle, + Version = g.Archives != null && g.Archives.Count() > 0 ? g.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Version : null, + }); - return games; + return games; + } + catch + { + return new List(); + } } public override ISettings GetSettings(bool firstRunSettings) { - return base.GetSettings(firstRunSettings); + return Settings; } public override UserControl GetSettingsView(bool firstRunView) { - return base.GetSettingsView(firstRunView); + return new PlayniteSettingsView(this); + } + + public void ShowAuthenticationWindow() + { + var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions() + { + ShowMinimizeButton = false, + }); + + window.Title = "Authenticate to LANCommander"; + + window.Content = new PlayniteSettingsView(this); + + window.Owner = PlayniteApi.Dialogs.GetCurrentAppWindow(); + window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; + window.ShowDialog(); } } } diff --git a/LANCommander.Playnite.Extension/PlayniteSettings.cs b/LANCommander.Playnite.Extension/PlayniteSettings.cs index 13af1f5..cf22d2f 100644 --- a/LANCommander.Playnite.Extension/PlayniteSettings.cs +++ b/LANCommander.Playnite.Extension/PlayniteSettings.cs @@ -8,44 +8,44 @@ using System.Threading.Tasks; namespace LANCommander.Playnite.Extension { - public class PlayniteSettings : ObservableObject - { - private string Option1 = String.Empty; - private bool Option2 = false; - private bool OptionThatWontBeSaved = false; - } - - public class SettingsViewModel : ObservableObject, ISettings + public class PlayniteSettingsViewModel : ObservableObject, ISettings { private readonly PlayniteLibraryPlugin Plugin; - private PlayniteSettings EditingClone { get; set; } - private PlayniteSettings Settings { get; set; } - public SettingsViewModel(PlayniteLibraryPlugin plugin) + public string ServerUrl { get; set; } = String.Empty; + public string AccessToken { get; set; } = String.Empty; + public string RefreshToken { get; set; } = String.Empty; + + public PlayniteSettingsViewModel() + { + + } + + public PlayniteSettingsViewModel(PlayniteLibraryPlugin plugin) { Plugin = plugin; - var savedSettings = Plugin.LoadPluginSettings(); + var savedSettings = Plugin.LoadPluginSettings(); if (savedSettings != null) - Settings = savedSettings; - else - Settings = new PlayniteSettings(); + { + ServerUrl = savedSettings.ServerUrl; + } } public void BeginEdit() { - EditingClone = Serialization.GetClone(Settings); + } public void CancelEdit() { - Settings = EditingClone; + } public void EndEdit() { - Plugin.SavePluginSettings(Settings); + Plugin.SavePluginSettings(this); } public bool VerifySettings(out List errors) diff --git a/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml b/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml index 9268644..c57429d 100644 --- a/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml +++ b/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml @@ -1,14 +1,21 @@  - - - + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:sys="clr-namespace:System;assembly=mscorlib" + mc:Ignorable="d" + d:DesignHeight="400" d:DesignWidth="600"> + + + + + + + - + \ No newline at end of file diff --git a/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml.cs b/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml.cs index f253cd9..1e2e61e 100644 --- a/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml.cs +++ b/LANCommander.Playnite.Extension/PlayniteSettingsView.xaml.cs @@ -1,4 +1,6 @@ -using System; +using LANCommander.SDK.Models; +using Playnite.SDK; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,9 +19,31 @@ namespace LANCommander.Playnite.Extension { public partial class PlayniteSettingsView : UserControl { - public PlayniteSettingsView() - { + private PlayniteLibraryPlugin Plugin; + public PlayniteSettingsView(PlayniteLibraryPlugin plugin) + { + Plugin = plugin; + + InitializeComponent(); + + var settings = Plugin.GetSettings(false) as PlayniteSettingsViewModel; + + if (Plugin.LANCommander.ValidateToken(new AuthToken() + { + AccessToken = settings.AccessToken, + RefreshToken = settings.RefreshToken, + })) + { + var authenticateButton = FindName("AuthenticateButton") as Button; + + authenticateButton.Visibility = Visibility.Hidden; + } + } + + private void AuthenticateButton_Click(object sender, RoutedEventArgs e) + { + Plugin.ShowAuthenticationWindow(); } } } diff --git a/LANCommander.SDK/Models/AuthRequest.cs b/LANCommander.SDK/Models/AuthRequest.cs new file mode 100644 index 0000000..de00e4c --- /dev/null +++ b/LANCommander.SDK/Models/AuthRequest.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LANCommander.SDK.Models +{ + public class AuthRequest + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/LANCommander.SDK/Models/AuthResponse.cs b/LANCommander.SDK/Models/AuthResponse.cs new file mode 100644 index 0000000..8f5334c --- /dev/null +++ b/LANCommander.SDK/Models/AuthResponse.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LANCommander.SDK.Models +{ + public class AuthResponse + { + public string AccessToken { get; set; } + public string RefreshToken { get; set; } + public DateTime Expiration { get; set; } + } +} diff --git a/LANCommander.SDK/Models/AuthToken.cs b/LANCommander.SDK/Models/AuthToken.cs new file mode 100644 index 0000000..4f5f97b --- /dev/null +++ b/LANCommander.SDK/Models/AuthToken.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LANCommander.SDK.Models +{ + public class AuthToken + { + public string AccessToken { get; set; } + public string RefreshToken { get; set; } + } +}