Refactor settings to properly handle install directory.

dashboard
Pat Hartl 2023-01-11 20:48:39 -06:00
parent 9ff4575626
commit c16f61befd
6 changed files with 162 additions and 87 deletions

View File

@ -22,7 +22,6 @@ namespace LANCommander.PlaynitePlugin
{ {
Name = "Install using LANCommander"; Name = "Install using LANCommander";
Plugin = plugin; Plugin = plugin;
} }
public override void Install(InstallActionArgs args) public override void Install(InstallActionArgs args)
@ -88,7 +87,7 @@ namespace LANCommander.PlaynitePlugin
private string Extract(LANCommander.SDK.Models.Game game, string archivePath) private string Extract(LANCommander.SDK.Models.Game game, string archivePath)
{ {
var destination = $"C:\\Games\\{game.Title.SanitizeFilename()}"; var destination = Path.Combine(Plugin.Settings.InstallDirectory, game.Title.SanitizeFilename());
Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress => Plugin.PlayniteApi.Dialogs.ActivateGlobalProgress(progress =>
{ {

View File

@ -32,7 +32,13 @@ namespace LANCommander.PlaynitePlugin
HasSettings = true, HasSettings = true,
}; };
LoadSettings(); Settings = new PlayniteSettingsViewModel(this);
LANCommander = new LANCommanderClient(Settings.ServerAddress);
LANCommander.Token = new SDK.Models.AuthToken()
{
AccessToken = Settings.AccessToken,
RefreshToken = Settings.RefreshToken,
};
} }
public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args) public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
@ -100,52 +106,6 @@ namespace LANCommander.PlaynitePlugin
return new PlayniteSettingsView(this); return new PlayniteSettingsView(this);
} }
public void LoadSettings()
{
Settings = LoadPluginSettings<PlayniteSettingsViewModel>();
try
{
if (LANCommander == null)
LANCommander = new LANCommanderClient(Settings.ServerAddress);
LANCommander.Client.BaseUrl = new Uri(Settings.ServerAddress);
var token = new SDK.Models.AuthToken()
{
AccessToken = Settings.AccessToken,
RefreshToken = Settings.RefreshToken,
};
LANCommander.Token = token;
}
catch
{
}
}
public void SaveSettings()
{
SavePluginSettings(Settings);
if (LANCommander == null)
LANCommander = new LANCommanderClient(Settings.ServerAddress);
if (Settings.ServerAddress != LANCommander.Client.BaseUrl.ToString())
{
LANCommander.Client.BaseUrl = new Uri(Settings.ServerAddress);
var token = new SDK.Models.AuthToken()
{
AccessToken = Settings.AccessToken,
RefreshToken = Settings.RefreshToken,
};
LANCommander.Token = token;
}
}
public System.Windows.Window ShowAuthenticationWindow() public System.Windows.Window ShowAuthenticationWindow()
{ {
var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions() var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions()
@ -188,6 +148,7 @@ namespace LANCommander.PlaynitePlugin
var manifest = deserializer.Deserialize<GameManifest>(manifestContents); var manifest = deserializer.Deserialize<GameManifest>(manifestContents);
#region Actions
if (game.GameActions == null) if (game.GameActions == null)
game.GameActions = new System.Collections.ObjectModel.ObservableCollection<PN.SDK.Models.GameAction>(); game.GameActions = new System.Collections.ObjectModel.ObservableCollection<PN.SDK.Models.GameAction>();
@ -195,15 +156,73 @@ namespace LANCommander.PlaynitePlugin
{ {
bool isFirstAction = !manifest.Actions.Any(a => a.IsPrimaryAction) && manifest.Actions.First().Name == action.Name; bool isFirstAction = !manifest.Actions.Any(a => a.IsPrimaryAction) && manifest.Actions.First().Name == action.Name;
foreach (var existingAction in game.GameActions)
if (action.Name == existingAction.Name)
game.GameActions.Remove(existingAction);
game.GameActions.AddMissing(new PN.SDK.Models.GameAction() game.GameActions.AddMissing(new PN.SDK.Models.GameAction()
{ {
Name = action.Name, Name = action.Name,
Arguments = action.Arguments, Arguments = action.Arguments,
Path = PlayniteApi.ExpandGameVariables(game, action.Path), Path = PlayniteApi.ExpandGameVariables(game, action.Path?.Replace('/', Path.DirectorySeparatorChar)),
WorkingDir = action.WorkingDirectory.Replace('/', Path.DirectorySeparatorChar) ?? game.InstallDirectory, WorkingDir = action.WorkingDirectory?.Replace('/', Path.DirectorySeparatorChar) ?? game.InstallDirectory,
IsPlayAction = action.IsPrimaryAction || isFirstAction IsPlayAction = action.IsPrimaryAction || isFirstAction
}); });
} }
#endregion
#region Features
var singlePlayerFeature = PlayniteApi.Database.Features.FirstOrDefault(f => f.Name == "Single Player");
if (manifest.LanMultiplayer != null)
{
var multiplayerInfo = manifest.LanMultiplayer;
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
string featureName = $"LAN Multiplayer {playerCount}";
if (PlayniteApi.Database.Features.Any(f => f.Name == featureName))
{
game.Features.Add(PlayniteApi.Database.Features.FirstOrDefault(f => f.Name == featureName));
}
else
{
PlayniteApi.Database.Features.Add(new PN.SDK.Models.GameFeature()
{
Name = featureName
});
game.Features.Add(new PN.SDK.Models.GameFeature()
{
Name = $"LAN Multiplayer {playerCount}"
});
}
}
if (manifest.LocalMultiplayer != null)
{
var multiplayerInfo = manifest.LocalMultiplayer;
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
game.Features.Add(new PN.SDK.Models.GameFeature()
{
Name = $"Local Multiplayer {playerCount}"
});
}
if (manifest.OnlineMultiplayer != null)
{
var multiplayerInfo = manifest.OnlineMultiplayer;
string playerCount = multiplayerInfo.MinPlayers == multiplayerInfo.MaxPlayers ? $"({multiplayerInfo.MinPlayers} players)" : $"({multiplayerInfo.MinPlayers} - {multiplayerInfo.MaxPlayers} players)";
game.Features.Add(new PN.SDK.Models.GameFeature()
{
Name = $"Online Multiplayer {playerCount}"
});
}
#endregion
PlayniteApi.Database.Games.Update(game); PlayniteApi.Database.Games.Update(game);
} }

View File

@ -15,19 +15,20 @@ namespace LANCommander.PlaynitePlugin
public string ServerAddress { get; set; } = String.Empty; public string ServerAddress { get; set; } = String.Empty;
public string AccessToken { get; set; } = String.Empty; public string AccessToken { get; set; } = String.Empty;
public string RefreshToken { get; set; } = String.Empty; public string RefreshToken { get; set; } = String.Empty;
public string InstallDirectory { get; set; } = String.Empty;
public PlayniteSettingsViewModel() public PlayniteSettingsViewModel() { }
{
}
public PlayniteSettingsViewModel(PlayniteLibraryPlugin plugin) public PlayniteSettingsViewModel(PlayniteLibraryPlugin plugin)
{ {
Plugin = plugin; Plugin = plugin;
ServerAddress = Plugin.Settings.ServerAddress; var settings = Plugin.LoadPluginSettings<PlayniteSettingsViewModel>();
AccessToken = Plugin.Settings.AccessToken;
RefreshToken = Plugin.Settings.RefreshToken; ServerAddress = settings.ServerAddress;
AccessToken = settings.AccessToken;
RefreshToken = settings.RefreshToken;
InstallDirectory = settings.InstallDirectory;
} }
public void BeginEdit() public void BeginEdit()
@ -42,16 +43,17 @@ namespace LANCommander.PlaynitePlugin
public void EndEdit() public void EndEdit()
{ {
Plugin.Settings.ServerAddress = ServerAddress; Plugin.SavePluginSettings(this);
Plugin.SaveSettings();
} }
public bool VerifySettings(out List<string> errors) public bool VerifySettings(out List<string> errors)
{ {
errors = new List<string>(); errors = new List<string>();
return true; if (String.IsNullOrWhiteSpace(InstallDirectory))
errors.Add("An install directory needs to be set!");
return errors.Count == 0;
} }
} }
} }

View File

@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:playniteplugin="clr-namespace:LANCommander.PlaynitePlugin" d:DataContext="{d:DesignInstance Type=playniteplugin:PlayniteSettingsViewModel}"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="600"> d:DesignHeight="400" d:DesignWidth="600">
<d:DesignerProperties.DesignStyle> <d:DesignerProperties.DesignStyle>
@ -11,9 +11,33 @@
<Setter Property="Background" Value="White" /> <Setter Property="Background" Value="White" />
</Style> </Style>
</d:DesignerProperties.DesignStyle> </d:DesignerProperties.DesignStyle>
<StackPanel Margin="20"> <Grid Margin="20">
<TextBlock Text="LANCommander Server URL"/> <Grid.RowDefinitions>
<TextBox Text="{Binding ServerUrl}"/> <RowDefinition Height="auto" />
<Button Click="AuthenticateButton_Click" Name="AuthenticateButton">Authenticate</Button> <RowDefinition Height="auto" />
</StackPanel> </Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Install Directory" />
<TextBox Name="PART_InstallDirectory" Grid.Column="2" Text="{Binding InstallDirectory}" />
<Button Grid.Column="4" Content="Browse" Click="SelectInstallDirectory_Click" VerticalAlignment="Center" Grid.ColumnSpan="2" />
</Grid>
<Grid Height="40" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Name="PART_AuthenticationButton" Content="Authenticate" Grid.Column="0" Click="AuthenticateButton_Click" VerticalAlignment="Center" />
<Label Name="PART_AuthenticateLabel" Margin="20,0,0,0" VerticalAlignment="Center" Grid.Column="1" />
</Grid>
</Grid>
</UserControl> </UserControl>

View File

@ -20,35 +20,55 @@ namespace LANCommander.PlaynitePlugin
public partial class PlayniteSettingsView : UserControl public partial class PlayniteSettingsView : UserControl
{ {
private PlayniteLibraryPlugin Plugin; private PlayniteLibraryPlugin Plugin;
private PlayniteSettingsViewModel Settings;
public PlayniteSettingsView(PlayniteLibraryPlugin plugin) public PlayniteSettingsView(PlayniteLibraryPlugin plugin)
{ {
Plugin = plugin; this.Plugin = plugin;
this.Settings = plugin.Settings;
InitializeComponent(); InitializeComponent();
DataContext = this;
UpdateAuthenticationButtonVisibility(); UpdateAuthenticationButtonVisibility();
} }
private void UpdateAuthenticationButtonVisibility() private void UpdateAuthenticationButtonVisibility()
{ {
try PART_AuthenticateLabel.Content = "Checking authentication status...";
{ PART_AuthenticationButton.IsEnabled = false;
if (Plugin.LANCommander.ValidateToken(new AuthToken() PART_AuthenticateLabel.Content = Settings.InstallDirectory;
var token = new AuthToken()
{ {
AccessToken = Plugin.Settings.AccessToken, AccessToken = Plugin.Settings.AccessToken,
RefreshToken = Plugin.Settings.RefreshToken, RefreshToken = Plugin.Settings.RefreshToken,
})) };
{
var authenticateButton = FindName("AuthenticateButton") as Button;
authenticateButton.Visibility = Visibility.Hidden; var task = Task.Run(() => Plugin.LANCommander.ValidateToken(token))
.ContinueWith(antecedent =>
{
try
{
Dispatcher.Invoke(new Action(() =>
{
if (antecedent.Result == true)
{
PART_AuthenticateLabel.Content = "Authentication failed!";
PART_AuthenticationButton.IsEnabled = true;
} }
else
{
PART_AuthenticateLabel.Content = "Connection established!";
PART_AuthenticationButton.IsEnabled = false;
} }
catch }));
}
catch (Exception ex)
{ {
} }
});
} }
private void AuthenticateButton_Click(object sender, RoutedEventArgs e) private void AuthenticateButton_Click(object sender, RoutedEventArgs e)
@ -62,5 +82,16 @@ namespace LANCommander.PlaynitePlugin
{ {
UpdateAuthenticationButtonVisibility(); UpdateAuthenticationButtonVisibility();
} }
private void SelectInstallDirectory_Click(object sender, RoutedEventArgs e)
{
var selectedDirectory = Plugin.PlayniteApi.Dialogs.SelectFolder();
if (!String.IsNullOrWhiteSpace(selectedDirectory))
{
PART_InstallDirectory.Text = selectedDirectory;
Plugin.Settings.InstallDirectory = selectedDirectory;
}
}
} }
} }

View File

@ -68,7 +68,7 @@ namespace LANCommander.PlaynitePlugin.Views
// Probably unneeded, but why not be more secure? // Probably unneeded, but why not be more secure?
Context.Password = String.Empty; Context.Password = String.Empty;
Plugin.SaveSettings(); Plugin.SavePluginSettings(Plugin.Settings);
Window.GetWindow(this).Close(); Window.GetWindow(this).Close();
} }