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";
Plugin = plugin;
}
public override void Install(InstallActionArgs args)
@ -88,7 +87,7 @@ namespace LANCommander.PlaynitePlugin
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 =>
{

View File

@ -32,7 +32,13 @@ namespace LANCommander.PlaynitePlugin
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)
@ -100,52 +106,6 @@ namespace LANCommander.PlaynitePlugin
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()
{
var window = PlayniteApi.Dialogs.CreateWindow(new WindowCreationOptions()
@ -188,6 +148,7 @@ namespace LANCommander.PlaynitePlugin
var manifest = deserializer.Deserialize<GameManifest>(manifestContents);
#region Actions
if (game.GameActions == null)
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;
foreach (var existingAction in game.GameActions)
if (action.Name == existingAction.Name)
game.GameActions.Remove(existingAction);
game.GameActions.AddMissing(new PN.SDK.Models.GameAction()
{
Name = action.Name,
Arguments = action.Arguments,
Path = PlayniteApi.ExpandGameVariables(game, action.Path),
WorkingDir = action.WorkingDirectory.Replace('/', Path.DirectorySeparatorChar) ?? game.InstallDirectory,
Path = PlayniteApi.ExpandGameVariables(game, action.Path?.Replace('/', Path.DirectorySeparatorChar)),
WorkingDir = action.WorkingDirectory?.Replace('/', Path.DirectorySeparatorChar) ?? game.InstallDirectory,
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);
}

View File

@ -15,43 +15,45 @@ namespace LANCommander.PlaynitePlugin
public string ServerAddress { get; set; } = String.Empty;
public string AccessToken { 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)
{
Plugin = plugin;
ServerAddress = Plugin.Settings.ServerAddress;
AccessToken = Plugin.Settings.AccessToken;
RefreshToken = Plugin.Settings.RefreshToken;
var settings = Plugin.LoadPluginSettings<PlayniteSettingsViewModel>();
ServerAddress = settings.ServerAddress;
AccessToken = settings.AccessToken;
RefreshToken = settings.RefreshToken;
InstallDirectory = settings.InstallDirectory;
}
public void BeginEdit()
{
}
public void CancelEdit()
{
}
public void EndEdit()
{
Plugin.Settings.ServerAddress = ServerAddress;
Plugin.SaveSettings();
Plugin.SavePluginSettings(this);
}
public bool VerifySettings(out List<string> errors)
{
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,17 +3,41 @@
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"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:playniteplugin="clr-namespace:LANCommander.PlaynitePlugin" d:DataContext="{d:DesignInstance Type=playniteplugin:PlayniteSettingsViewModel}"
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="600">
<d:DesignerProperties.DesignStyle>
<Style TargetType="UserControl">
<Setter Property="Background" Value="White" />
</Style>
</d:DesignerProperties.DesignStyle>
<StackPanel Margin="20">
<TextBlock Text="LANCommander Server URL"/>
<TextBox Text="{Binding ServerUrl}"/>
<Button Click="AuthenticateButton_Click" Name="AuthenticateButton">Authenticate</Button>
</StackPanel>
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</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>

View File

@ -20,35 +20,55 @@ namespace LANCommander.PlaynitePlugin
public partial class PlayniteSettingsView : UserControl
{
private PlayniteLibraryPlugin Plugin;
private PlayniteSettingsViewModel Settings;
public PlayniteSettingsView(PlayniteLibraryPlugin plugin)
{
Plugin = plugin;
this.Plugin = plugin;
this.Settings = plugin.Settings;
InitializeComponent();
DataContext = this;
UpdateAuthenticationButtonVisibility();
}
private void UpdateAuthenticationButtonVisibility()
{
try
PART_AuthenticateLabel.Content = "Checking authentication status...";
PART_AuthenticationButton.IsEnabled = false;
PART_AuthenticateLabel.Content = Settings.InstallDirectory;
var token = new AuthToken()
{
if (Plugin.LANCommander.ValidateToken(new AuthToken()
{
AccessToken = Plugin.Settings.AccessToken,
RefreshToken = Plugin.Settings.RefreshToken,
}))
{
var authenticateButton = FindName("AuthenticateButton") as Button;
AccessToken = Plugin.Settings.AccessToken,
RefreshToken = Plugin.Settings.RefreshToken,
};
authenticateButton.Visibility = Visibility.Hidden;
}
}
catch
{
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 (Exception ex)
{
}
}
});
}
private void AuthenticateButton_Click(object sender, RoutedEventArgs e)
@ -62,5 +82,16 @@ namespace LANCommander.PlaynitePlugin
{
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?
Context.Password = String.Empty;
Plugin.SaveSettings();
Plugin.SavePluginSettings(Plugin.Settings);
Window.GetWindow(this).Close();
}