Skip to content

Commit

Permalink
Add cloud sync to per game settings - part 2
Browse files Browse the repository at this point in the history
* Manual sync
* Improve appearance
  • Loading branch information
hawkeye116477 committed Jan 10, 2024
1 parent 433655e commit 2d9ebec
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 31 deletions.
16 changes: 16 additions & 0 deletions src/Enums/CloudSyncAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LegendaryLibraryNS.Enums
{
public enum CloudSyncAction
{
Download,
Upload,
ForceDownload,
ForceUpload
}
}
25 changes: 21 additions & 4 deletions src/LegendaryCloud.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CliWrap;
using CliWrap.EventStream;
using LegendaryLibraryNS.Enums;
using LegendaryLibraryNS.Models;
using Playnite.Common;
using Playnite.SDK;
Expand Down Expand Up @@ -123,7 +124,7 @@ public static string CalculateGameSavesPath(string gameName, string gameID, stri
}


public static void SyncGameSaves(string gameName, string gameID, string gameInstallDir, bool download)
public static void SyncGameSaves(string gameName, string gameID, string gameInstallDir, CloudSyncAction cloudSyncAction, bool manualSync = false)
{
var cloudSyncEnabled = LegendaryLibrary.GetSettings().SyncGameSaves;
var gamesSettings = LegendaryGameSettingsView.LoadSavedGamesSettings();
Expand All @@ -136,10 +137,14 @@ public static void SyncGameSaves(string gameName, string gameID, string gameInst
{
cloudSyncEnabled = (bool)gameSettings.AutoSyncSaves;
}
if (manualSync)
{
cloudSyncEnabled = true;
}
if (cloudSyncEnabled)
{
var cloudSaveFolder = CalculateGameSavesPath(gameName, gameID, gameInstallDir);
if (gameSettings?.CloudSaveFolder != "")
if (!gameSettings.CloudSaveFolder.IsNullOrEmpty())
{
cloudSaveFolder = gameSettings.CloudSaveFolder;
}
Expand All @@ -154,14 +159,26 @@ public static void SyncGameSaves(string gameName, string gameID, string gameInst
{
a.ProgressMaxValue = 100;
a.CurrentProgressValue = 0;
var cloudArgs = new List<string>();
cloudArgs.AddRange(new[] { "-y", "sync-saves", gameID });
var skippedActivity = "--skip-upload";
if (download == false)
if (cloudSyncAction == CloudSyncAction.Upload || cloudSyncAction == CloudSyncAction.ForceUpload)
{
skippedActivity = "--skip-download";
}
cloudArgs.Add(skippedActivity);
if (cloudSyncAction == CloudSyncAction.ForceDownload)
{
cloudArgs.Add("--force-download");
}
else if (cloudSyncAction == CloudSyncAction.ForceUpload)
{
cloudArgs.Add("--force-upload");
}
cloudArgs.AddRange(new[] { "--save-path", cloudSaveFolder });
var cmd = Cli.Wrap(LegendaryLauncher.ClientExecPath)
.WithEnvironmentVariables(LegendaryLauncher.DefaultEnvironmentVariables)
.WithArguments(new[] { "-y", "sync-saves", gameID, skippedActivity, "--save-path", cloudSaveFolder });
.WithArguments(cloudArgs);
await foreach (var cmdEvent in cmd.ListenAsync())
{
switch (cmdEvent)
Expand Down
56 changes: 32 additions & 24 deletions src/LegendaryGameSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,38 @@
</TabItem>
<TabItem Header="{DynamicResource LOCLegendaryCloudSaves}">
<StackPanel Margin="10">
<Grid x:Name="CloudSavesGrid" Visibility="Visible">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<CheckBox Name="AutoSyncChk" Content="{DynamicResource LOCLegendaryAutoSyncSaves}" Margin="0,0,0,5" Grid.Column="0" Grid.Row="0" Click="AutoSyncChk_Click"></CheckBox>
<TextBlock Text="{DynamicResource LOCLegendarySavePath}" Grid.Column="0" Grid.Row="1"
Margin="0,0,0,5" VerticalAlignment="Center" HorizontalAlignment="Left">
</TextBlock>
<TextBox x:Name="SelectedSavePathTxt"
Grid.Row="1" Grid.Column="1" Margin="0,0,0,5" VerticalAlignment="Center"/>
<Button Name="ChooseSavePathBtn"
Margin="5,0,0,5" Content="&#xec5b;" FontFamily="{DynamicResource FontIcoFont}" Grid.Row="1" Grid.Column="2"
ToolTip="{DynamicResource LOCLegendary3P_PlayniteSelectDirectoryTooltip}" VerticalAlignment="Center" Click="ChooseSavePathBtn_Click"/>
<Button Name="CalculatePathBtn" Margin="5,0,0,5" Grid.Row="2" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Left" Content="{DynamicResource LOCLegendaryCalculatePath}" FontFamily="{DynamicResource FontIcoFont}" Click="CalculatePathBtn_Click"/>
</Grid>
<StackPanel x:Name="CloudSavesSP">
<CheckBox Name="AutoSyncChk" Content="{DynamicResource LOCLegendaryAutoSyncSaves}" Margin="0,0,0,5" Click="AutoSyncChk_Click"></CheckBox>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Text="{DynamicResource LOCLegendarySavePath}" Grid.Column="0" Grid.Row="1"
Margin="0,0,10,5" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<TextBox x:Name="SelectedSavePathTxt" Grid.Row="1" Grid.Column="1" Margin="0,0,0,5" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<Button Name="ChooseSavePathBtn"
Margin="5,0,0,5" Content="&#xec5b;" FontFamily="{DynamicResource FontIcoFont}" Grid.Row="1" Grid.Column="2"
ToolTip="{DynamicResource LOCLegendary3P_PlayniteSelectDirectoryTooltip}" VerticalAlignment="Center" Click="ChooseSavePathBtn_Click"/>
<Button Name="CalculatePathBtn" Margin="5,0,0,5" Grid.Row="1" Grid.Column="3"
VerticalAlignment="Center" HorizontalAlignment="Left"
Content="&#xef85;" ToolTip="{DynamicResource LOCLegendaryCalculatePath}" FontFamily="{DynamicResource FontIcoFont}" Click="CalculatePathBtn_Click"/>
</Grid>
<StackPanel Orientation="Horizontal" Margin="0,10,0,5">
<TextBlock Text="{DynamicResource LOCLegendaryManualSyncSaves}" Margin="0,0,10,0" VerticalAlignment="Center"/>
<ComboBox x:Name="ManualSyncSavesCBo" DisplayMemberPath="Value" SelectedValuePath="Key" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Margin="0,0,10,0" HorizontalAlignment="Left"/>
<Button x:Name="SyncBtn" Content="{DynamicResource LOCLegendarySync}" HorizontalAlignment="Left" Margin="0" Click="SyncBtn_Click"></Button>
</StackPanel>
</StackPanel>
<TextBlock Text="{DynamicResource LOCLegendaryCloudSavesNotSupported}" Name="CloudSavesNotSupportedTB" Visibility="Collapsed"></TextBlock>
</StackPanel>
</TabItem>
Expand Down
25 changes: 24 additions & 1 deletion src/LegendaryGameSettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Playnite.Common;
using Playnite.SDK;
using Playnite.SDK.Models;
using LegendaryLibraryNS.Enums;

namespace LegendaryLibraryNS
{
Expand Down Expand Up @@ -143,10 +144,22 @@ private void LegendaryGameSettingsViewUC_Loaded(object sender, RoutedEventArgs e
EnableOfflineModeChk.IsEnabled = true;
}
}
var cloudSyncActions = new Dictionary<CloudSyncAction, string>
{
{ CloudSyncAction.Download, ResourceProvider.GetString(LOC.LegendaryDownload) },
{ CloudSyncAction.Upload, ResourceProvider.GetString(LOC.LegendaryUpload) },
{ CloudSyncAction.ForceDownload, ResourceProvider.GetString(LOC.LegendaryForceDownload) },
{ CloudSyncAction.ForceUpload, ResourceProvider.GetString(LOC.LegendaryForceUpload) }
};
ManualSyncSavesCBo.ItemsSource = cloudSyncActions;
ManualSyncSavesCBo.SelectedIndex = 0;



cloudPath = LegendaryCloud.CalculateGameSavesPath(Game.Name, Game.GameId, Game.InstallDirectory);
if (cloudPath.IsNullOrEmpty())
{
CloudSavesGrid.Visibility = Visibility.Collapsed;
CloudSavesSP.Visibility = Visibility.Collapsed;
CloudSavesNotSupportedTB.Visibility = Visibility.Visible;
}
}
Expand Down Expand Up @@ -187,5 +200,15 @@ private void AutoSyncChk_Click(object sender, RoutedEventArgs e)
playniteAPI.Dialogs.ShowMessage(ResourceProvider.GetString(LOC.LegendarySyncGameSavesWarn), "", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}

private void SyncBtn_Click(object sender, RoutedEventArgs e)
{
var result = playniteAPI.Dialogs.ShowMessage(ResourceProvider.GetString(LOC.LegendaryCloudSaveConfirm), ResourceProvider.GetString(LOC.LegendaryCloudSaves), MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
CloudSyncAction selectedCloudSyncAction = (CloudSyncAction)ManualSyncSavesCBo.SelectedValue;
LegendaryCloud.SyncGameSaves(Game.Name, GameID, Game.InstallDirectory, selectedCloudSyncAction, true);
}
}
}
}
4 changes: 2 additions & 2 deletions src/LegendaryLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ public void MigrateOnlineGames()

public override void OnGameStarting(OnGameStartingEventArgs args)
{
LegendaryCloud.SyncGameSaves(args.Game.Name, args.Game.GameId, args.Game.InstallDirectory, true);
LegendaryCloud.SyncGameSaves(args.Game.Name, args.Game.GameId, args.Game.InstallDirectory, CloudSyncAction.Download);
}

public override void OnGameStopped(OnGameStoppedEventArgs args)
{
LegendaryCloud.SyncGameSaves(args.Game.Name, args.Game.GameId, args.Game.InstallDirectory, false);
LegendaryCloud.SyncGameSaves(args.Game.Name, args.Game.GameId, args.Game.InstallDirectory, CloudSyncAction.Upload);
}

public override IEnumerable<SidebarItem> GetSidebarItems()
Expand Down
1 change: 1 addition & 0 deletions src/LegendaryLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<ItemGroup>
<Compile Include="Converters\DownloadStatusEnumToStringConverter.cs" />
<Compile Include="Enums\ClearCacheTime.cs" />
<Compile Include="Enums\CloudSyncAction.cs" />
<Compile Include="Enums\DownloadAction.cs" />
<Compile Include="Enums\DownloadCompleteAction.cs" />
<Compile Include="Enums\DownloadStatus.cs" />
Expand Down
7 changes: 7 additions & 0 deletions src/Localization/en_US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@
<sys:String x:Key="LOCLegendaryAutoSyncSaves">Sync saves to the cloud</sys:String>
<sys:String x:Key="LOCLegendaryCalculatePath">Calculate path</sys:String>
<sys:String x:Key="LOCLegendaryCloudSavesNotSupported">This game doesn't support cloud saves.</sys:String>
<sys:String x:Key="LOCLegendaryManualSyncSaves">Manual sync saves</sys:String>
<sys:String x:Key="LOCLegendarySync">Sync</sys:String>
<sys:String x:Key="LOCLegendaryDownload">Download</sys:String>
<sys:String x:Key="LOCLegendaryUpload">Upload</sys:String>
<sys:String x:Key="LOCLegendaryForceDownload">Force download</sys:String>
<sys:String x:Key="LOCLegendaryForceUpload">Force upload</sys:String>
<sys:String x:Key="LOCLegendaryCloudSaveConfirm">Cloud saves feature is experimental. It's recommended to backup game saves in case something goes wrong. Are you sure you want to continue?</sys:String>
</ResourceDictionary>
28 changes: 28 additions & 0 deletions src/LocalizationKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,33 @@ public static class LOC
/// This game doesn't support cloud saves.
/// </summary>
public const string LegendaryCloudSavesNotSupported = "LOCLegendaryCloudSavesNotSupported";
/// <summary>
/// Manual sync saves
/// </summary>
public const string LegendaryManualSyncSaves = "LOCLegendaryManualSyncSaves";
/// <summary>
/// Sync
/// </summary>
public const string LegendarySync = "LOCLegendarySync";
/// <summary>
/// Download
/// </summary>
public const string LegendaryDownload = "LOCLegendaryDownload";
/// <summary>
/// Upload
/// </summary>
public const string LegendaryUpload = "LOCLegendaryUpload";
/// <summary>
/// Force download
/// </summary>
public const string LegendaryForceDownload = "LOCLegendaryForceDownload";
/// <summary>
/// Force upload
/// </summary>
public const string LegendaryForceUpload = "LOCLegendaryForceUpload";
/// <summary>
/// Cloud saves feature is experimental. It's recommended to backup game saves in case something goes wrong. Are you sure you want to continue?
/// </summary>
public const string LegendaryCloudSaveConfirm = "LOCLegendaryCloudSaveConfirm";
}
}

0 comments on commit 2d9ebec

Please sign in to comment.