Skip to content

Commit

Permalink
#71: Add game menu option to open backup directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jun 7, 2024
1 parent e30e62b commit 67f7045
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* In a game's context menu, backup comments are now displayed,
if any have been added in Ludusavi.
If a backup is from another OS than Windows, that is also indicated.
* In a game's context menu, you can now open that game's backup directory.
This requires Ludusavi v0.24.0 or newer.
* Fixed:
* In some cases where Playnite and Ludusavi used different names for a game,
the game's context menu would not list its backups.
This now works properly in more cases if you use Ludusavi 0.24.0 or newer.
This now works properly in more cases if you use Ludusavi v0.24.0 or newer.
* Changed:
* The recommended version of Ludusavi is now 0.24.0. You can download the latest release here:
* The recommended version of Ludusavi is now v0.24.0. You can download the latest release here:
https://github.com/mtkennerly/ludusavi/releases
* In a game's context menu, backups are now sorted newest to oldest.

Expand Down
2 changes: 2 additions & 0 deletions lang/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,5 @@ unrecognized-game = Ludusavi does not recognize {$game}
look-up-as-other-title = Look up with another title
look-up-as-normal-title = Look up with default title
open-backup-directory = Open backup directory
22 changes: 22 additions & 0 deletions src/Etc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
using System.Collections.Generic;
using System.Linq;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace LudusaviPlaynite
{
public static class Etc
{
private static Regex homeDir = new Regex("^~");

public static bool IsOnSteam(Game game)
{
return game.Source?.Name == "Steam"
Expand All @@ -22,5 +26,23 @@ public static bool IsOnPc(Game game)
|| game.Platforms.Any(x => pcSpecs.Contains(x.SpecificationId))
|| game.Platforms.Any(x => pcNames.Contains(x.Name));
}

public static string NormalizePath(string path)
{
return homeDir.Replace(path, Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).Replace("/", "\\");
}

public static bool OpenDir(string path)
{
try
{
Process.Start(NormalizePath(path));
return true;
}
catch
{
return false;
}
}
}
}
37 changes: 36 additions & 1 deletion src/LudusaviPlaynite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class LudusaviPlaynite : GenericPlugin
private LudusaviVersion appVersion { get; set; } = new LudusaviVersion(new Version(0, 0, 0));
private Dictionary<string, string> titles { get; set; } = new Dictionary<string, string>();
private Dictionary<string, List<ApiBackup>> backups { get; set; } = new Dictionary<string, List<ApiBackup>>();
private Dictionary<string, string> backupPaths { get; set; } = new Dictionary<string, string>();
private List<string> manifestGames { get; set; } = new List<string>();
private List<string> manifestGamesWithSaveDataByTitle { get; set; } = new List<string>();
private List<int> manifestGamesWithSaveDataBySteamId { get; set; } = new List<int>();
Expand Down Expand Up @@ -226,6 +227,28 @@ public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs
);
}

if (menuArgs.Games.Count == 1)
{
var backupPath = GetBackupPath(menuArgs.Games[0]);
if (backupPath != null)
{
items.Add(
new GameMenuItem
{
Description = translator.OpenBackupDirectory(),
MenuSection = translator.Ludusavi(),
Action = args =>
{
if (!Etc.OpenDir(backupPath))
{
ShowError(this.translator.CannotOpenFolder());
}
}
}
);
}
}

if (menuArgs.Games.Count == 1)
{
var title = menuArgs.Games[0].Name;
Expand Down Expand Up @@ -500,7 +523,14 @@ public void RefreshLudusaviBackups()
var (code, response) = InvokeLudusavi(new Invocation(Mode.Backups).PathIf(settings.BackupPath, settings.OverrideBackupPath));
if (response?.Games != null)
{
this.backups = response?.Games.ToDictionary(pair => pair.Key, pair => pair.Value.Backups);
foreach (var pair in response?.Games)
{
this.backups.Add(pair.Key, pair.Value.Backups);
if (!string.IsNullOrEmpty(pair.Value.BackupPath))
{
this.backupPaths.Add(pair.Key, pair.Value.BackupPath);
}
}
}

if (this.settings.TagGamesWithBackups)
Expand Down Expand Up @@ -1527,6 +1557,11 @@ private List<ApiBackup> GetBackups(Game game)
return ret;
}

private string GetBackupPath(Game game)
{
return GetDictValue(this.backupPaths, GetTitle(game) ?? GetGameNameWithAlt(game), null);
}

private V GetDictValue<K, V>(Dictionary<K, V> dict, K key, V fallback)
{
if (dict == null || key == null)
Expand Down
12 changes: 1 addition & 11 deletions src/LudusaviPlayniteSettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public partial class LudusaviPlayniteSettingsView : UserControl
{
private LudusaviPlaynite plugin;
private Translator translator;
private Regex homeDir = new Regex("^~");

public LudusaviPlayniteSettingsView(LudusaviPlaynite plugin, Translator translator)
{
Expand All @@ -34,11 +33,6 @@ public LudusaviPlayniteSettingsView(LudusaviPlaynite plugin, Translator translat
this.translator = translator;
}

private string NormalizePath(string path)
{
return homeDir.Replace(path, Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).Replace("/", "\\");
}

public void OnBrowseExecutablePath(object sender, RoutedEventArgs e)
{
var choice = this.plugin.PlayniteApi.Dialogs.SelectFile(translator.SelectFileExecutableFilter());
Expand All @@ -59,11 +53,7 @@ public void OnBrowseBackupPath(object sender, RoutedEventArgs e)

public void OnOpenBackupPath(object sender, RoutedEventArgs e)
{
try
{
Process.Start(NormalizePath(plugin.settings.BackupPath));
}
catch
if (!Etc.OpenDir(plugin.settings.BackupPath))
{
this.plugin.ShowError(this.translator.CannotOpenFolder());
}
Expand Down
2 changes: 2 additions & 0 deletions src/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ public struct ApiGame
public Dictionary<string, ApiRegistry> Registry;
[JsonProperty("backups")]
public List<ApiBackup> Backups;
[JsonProperty("backupPath")]
public string BackupPath;
}

public struct ApiResponse
Expand Down
5 changes: 5 additions & 0 deletions src/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,5 +661,10 @@ public string LookUpAsNormalTitle()
{
return Translate("look-up-as-normal-title");
}

public string OpenBackupDirectory()
{
return Translate("open-backup-directory");
}
}
}

0 comments on commit 67f7045

Please sign in to comment.