Skip to content

Commit

Permalink
Reorganize constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jun 7, 2024
1 parent 407d47e commit 84e0ab5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 74 deletions.
47 changes: 45 additions & 2 deletions src/Etc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace LudusaviPlaynite
{
public static class Etc
{
private static Regex homeDir = new Regex("^~");
public static Version RECOMMENDED_APP_VERSION = new Version(0, 24, 0);
private static Regex HOME_DIR = new Regex("^~");

public static bool IsOnSteam(Game game)
{
Expand All @@ -29,7 +30,7 @@ public static bool IsOnPc(Game game)

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

public static bool OpenDir(string path)
Expand All @@ -45,4 +46,46 @@ public static bool OpenDir(string path)
}
}
}

public static class Tags
{
private const string PREFIX = "[Ludusavi] ";

public const string LEGACY_SKIP = "ludusavi-skip";
public const string SKIP = PREFIX + "Skip";

public const string GAME_BACKUP = PREFIX + "Game: backup";
public const string GAME_NO_BACKUP = PREFIX + "Game: no backup";

public const string GAME_BACKUP_AND_RESTORE = PREFIX + "Game: backup and restore";
public const string GAME_NO_RESTORE = PREFIX + "Game: no restore";

public const string PLATFORM_BACKUP = PREFIX + "Platform: backup";
public const string PLATFORM_NO_BACKUP = PREFIX + "Platform: no backup";

public const string PLATFORM_BACKUP_AND_RESTORE = PREFIX + "Platform: backup and restore";
public const string PLATFORM_NO_RESTORE = PREFIX + "Platform: no restore";

public const string BACKED_UP = PREFIX + "Backed up";
public const string UNKNOWN_SAVE_DATA = PREFIX + "Unknown save data";

// Format: {new tag, {conflicting tags}}
public static Dictionary<string, string[]> CONFLICTS = new Dictionary<string, string[]> {
{SKIP, new string[] {}},
{GAME_BACKUP, new string[] {SKIP, GAME_NO_BACKUP}},
{GAME_NO_BACKUP, new string[] {GAME_BACKUP, GAME_BACKUP_AND_RESTORE}},
{GAME_BACKUP_AND_RESTORE, new string[] {SKIP, GAME_BACKUP, GAME_NO_BACKUP, GAME_NO_RESTORE}},
{GAME_NO_RESTORE, new string[] {GAME_BACKUP_AND_RESTORE}},
{PLATFORM_BACKUP, new string[] {SKIP, PLATFORM_NO_BACKUP}},
{PLATFORM_NO_BACKUP, new string[] {PLATFORM_BACKUP, PLATFORM_BACKUP_AND_RESTORE}},
{PLATFORM_BACKUP_AND_RESTORE, new string[] {SKIP, PLATFORM_BACKUP, PLATFORM_NO_BACKUP, PLATFORM_NO_RESTORE}},
{PLATFORM_NO_RESTORE, new string[] {PLATFORM_BACKUP_AND_RESTORE}},
};

// Format: {(new tag, conflicting tag), conflict replacement}
public static Dictionary<(string, string), string> REPLACEMENTS = new Dictionary<(string, string), string> {
{(GAME_NO_RESTORE, GAME_BACKUP_AND_RESTORE), GAME_BACKUP},
{(PLATFORM_NO_RESTORE, PLATFORM_BACKUP_AND_RESTORE), PLATFORM_BACKUP},
};
}
}
100 changes: 30 additions & 70 deletions src/LudusaviPlaynite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,6 @@ namespace LudusaviPlaynite
{
public class LudusaviPlaynite : GenericPlugin
{
private readonly Version RECOMMENDED_APP_VERSION = new Version(0, 24, 0);

private const string TAG_PREFIX = "[Ludusavi] ";

private const string LEGACY_TAG_SKIP = "ludusavi-skip";
private const string TAG_SKIP = TAG_PREFIX + "Skip";

private const string TAG_GAME_BACKUP = TAG_PREFIX + "Game: backup";
private const string TAG_GAME_NO_BACKUP = TAG_PREFIX + "Game: no backup";

private const string TAG_GAME_BACKUP_AND_RESTORE = TAG_PREFIX + "Game: backup and restore";
private const string TAG_GAME_NO_RESTORE = TAG_PREFIX + "Game: no restore";

private const string TAG_PLATFORM_BACKUP = TAG_PREFIX + "Platform: backup";
private const string TAG_PLATFORM_NO_BACKUP = TAG_PREFIX + "Platform: no backup";

private const string TAG_PLATFORM_BACKUP_AND_RESTORE = TAG_PREFIX + "Platform: backup and restore";
private const string TAG_PLATFORM_NO_RESTORE = TAG_PREFIX + "Platform: no restore";

public const string TAG_BACKED_UP = TAG_PREFIX + "Backed up";
public const string TAG_UNKNOWN_SAVE_DATA = TAG_PREFIX + "Unknown save data";

// Format: {new tag, {conflicting tags}}
private readonly Dictionary<string, string[]> TAGS_AND_CONFLICTS = new Dictionary<string, string[]> {
{TAG_SKIP, new string[] {}},
{TAG_GAME_BACKUP, new string[] {TAG_SKIP, TAG_GAME_NO_BACKUP}},
{TAG_GAME_NO_BACKUP, new string[] {TAG_GAME_BACKUP, TAG_GAME_BACKUP_AND_RESTORE}},
{TAG_GAME_BACKUP_AND_RESTORE, new string[] {TAG_SKIP, TAG_GAME_BACKUP, TAG_GAME_NO_BACKUP, TAG_GAME_NO_RESTORE}},
{TAG_GAME_NO_RESTORE, new string[] {TAG_GAME_BACKUP_AND_RESTORE}},
{TAG_PLATFORM_BACKUP, new string[] {TAG_SKIP, TAG_PLATFORM_NO_BACKUP}},
{TAG_PLATFORM_NO_BACKUP, new string[] {TAG_PLATFORM_BACKUP, TAG_PLATFORM_BACKUP_AND_RESTORE}},
{TAG_PLATFORM_BACKUP_AND_RESTORE, new string[] {TAG_SKIP, TAG_PLATFORM_BACKUP, TAG_PLATFORM_NO_BACKUP, TAG_PLATFORM_NO_RESTORE}},
{TAG_PLATFORM_NO_RESTORE, new string[] {TAG_PLATFORM_BACKUP_AND_RESTORE}},
};
// Format: {(new tag, conflicting tag), conflict replacement}
private readonly Dictionary<(string, string), string> TAG_REPLACEMENTS = new Dictionary<(string, string), string> {
{(TAG_GAME_NO_RESTORE, TAG_GAME_BACKUP_AND_RESTORE), TAG_GAME_BACKUP},
{(TAG_PLATFORM_NO_RESTORE, TAG_PLATFORM_BACKUP_AND_RESTORE), TAG_PLATFORM_BACKUP},
};

private static readonly ILogger logger = LogManager.GetLogger();
public LudusaviPlayniteSettings settings { get; set; }
public override Guid Id { get; } = Guid.Parse("72e2de43-d859-44d8-914e-4277741c8208");
Expand Down Expand Up @@ -321,7 +281,7 @@ public override IEnumerable<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs
}
}

foreach (var entry in TAGS_AND_CONFLICTS)
foreach (var entry in Tags.CONFLICTS)
{
var candidate = entry.Key;
var conflicts = entry.Value;
Expand All @@ -347,7 +307,7 @@ await Task.Run(() =>
{
var removed = RemoveTag(game, conflict);
string replacement;
if (removed && TAG_REPLACEMENTS.TryGetValue((candidate, conflict), out replacement))
if (removed && Tags.REPLACEMENTS.TryGetValue((candidate, conflict), out replacement))
{
AddTag(game, replacement);
}
Expand Down Expand Up @@ -395,11 +355,11 @@ public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
if (!settings.MigratedTags)
{
var oldTag = PlayniteApi.Database.Tags.FirstOrDefault(x => x.Name == LEGACY_TAG_SKIP);
var newTagExists = PlayniteApi.Database.Tags.Any(x => x.Name == TAG_SKIP);
var oldTag = PlayniteApi.Database.Tags.FirstOrDefault(x => x.Name == Tags.LEGACY_SKIP);
var newTagExists = PlayniteApi.Database.Tags.Any(x => x.Name == Tags.SKIP);
if (oldTag != null && !newTagExists)
{
oldTag.Name = TAG_SKIP;
oldTag.Name = Tags.SKIP;
PlayniteApi.Database.Tags.Update(oldTag);
}
settings.MigratedTags = true;
Expand All @@ -413,10 +373,10 @@ public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
RefreshLudusaviBackups();
RefreshLudusaviGames();

if (appVersion.version < RECOMMENDED_APP_VERSION && new Version(settings.SuggestedUpgradeTo) < RECOMMENDED_APP_VERSION)
if (appVersion.version < Etc.RECOMMENDED_APP_VERSION && new Version(settings.SuggestedUpgradeTo) < Etc.RECOMMENDED_APP_VERSION)
{
NotifyInfo(
translator.UpgradePrompt(RECOMMENDED_APP_VERSION.ToString()),
translator.UpgradePrompt(Etc.RECOMMENDED_APP_VERSION.ToString()),
() =>
{
try
Expand All @@ -427,7 +387,7 @@ public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{ }
}
);
settings.SuggestedUpgradeTo = RECOMMENDED_APP_VERSION.ToString();
settings.SuggestedUpgradeTo = Etc.RECOMMENDED_APP_VERSION.ToString();
SavePluginSettings(settings);
}
});
Expand Down Expand Up @@ -853,7 +813,7 @@ private Choice AskUser(string message)

private bool ShouldSkipGame(Game game)
{
return HasTag(game, TAG_SKIP);
return HasTag(game, Tags.SKIP);
}

string GetTitleId(Game game)
Expand Down Expand Up @@ -1034,21 +994,21 @@ private void InitiateOperationSync(Game game, Operation operation, OperationTimi
switch (criteria)
{
case BackupCriteria.Game:
UpdateTagsForChoice(game, choice, TAG_GAME_BACKUP, TAG_GAME_NO_BACKUP);
UpdateTagsForChoice(game, choice, Tags.GAME_BACKUP, Tags.GAME_NO_BACKUP);
break;
case BackupCriteria.Platform:
UpdateTagsForChoice(game, choice, TAG_PLATFORM_BACKUP, TAG_PLATFORM_NO_BACKUP);
UpdateTagsForChoice(game, choice, Tags.PLATFORM_BACKUP, Tags.PLATFORM_NO_BACKUP);
break;
}
break;
case Operation.Restore:
switch (criteria)
{
case BackupCriteria.Game:
UpdateTagsForChoice(game, choice, TAG_GAME_BACKUP_AND_RESTORE, TAG_GAME_NO_RESTORE, TAG_GAME_BACKUP);
UpdateTagsForChoice(game, choice, Tags.GAME_BACKUP_AND_RESTORE, Tags.GAME_NO_RESTORE, Tags.GAME_BACKUP);
break;
case BackupCriteria.Platform:
UpdateTagsForChoice(game, choice, TAG_PLATFORM_BACKUP_AND_RESTORE, TAG_PLATFORM_NO_RESTORE, TAG_PLATFORM_BACKUP);
UpdateTagsForChoice(game, choice, Tags.PLATFORM_BACKUP_AND_RESTORE, Tags.PLATFORM_NO_RESTORE, Tags.PLATFORM_BACKUP);
break;
}
break;
Expand Down Expand Up @@ -1410,11 +1370,11 @@ private void TagGamesWithBackups()
{
if (IsBackedUp(game))
{
AddTag(game, TAG_BACKED_UP);
AddTag(game, Tags.BACKED_UP);
}
else
{
RemoveTag(game, TAG_BACKED_UP);
RemoveTag(game, Tags.BACKED_UP);
}
}
}
Expand All @@ -1425,11 +1385,11 @@ private void TagGamesWithUnknownSaveData()
{
if (!GameHasKnownSaveData(game))
{
AddTag(game, TAG_UNKNOWN_SAVE_DATA);
AddTag(game, Tags.UNKNOWN_SAVE_DATA);
}
else
{
RemoveTag(game, TAG_UNKNOWN_SAVE_DATA);
RemoveTag(game, Tags.UNKNOWN_SAVE_DATA);
}
}
}
Expand All @@ -1441,11 +1401,11 @@ private PlayPreferences GetPlayPreferences(Game game)
return new PlayPreferences();
}

var gameBackupDo = (settings.DoBackupOnGameStopped || HasTag(game, TAG_GAME_BACKUP) || HasTag(game, TAG_GAME_BACKUP_AND_RESTORE))
&& !HasTag(game, TAG_GAME_NO_BACKUP)
&& (Etc.IsOnPc(game) || !settings.OnlyBackupOnGameStoppedIfPc || HasTag(game, TAG_GAME_BACKUP) || HasTag(game, TAG_GAME_BACKUP_AND_RESTORE));
var platformBackupDo = (settings.DoPlatformBackupOnNonPcGameStopped || HasTag(game, TAG_PLATFORM_BACKUP) || HasTag(game, TAG_PLATFORM_BACKUP_AND_RESTORE))
&& !HasTag(game, TAG_PLATFORM_NO_BACKUP)
var gameBackupDo = (settings.DoBackupOnGameStopped || HasTag(game, Tags.GAME_BACKUP) || HasTag(game, Tags.GAME_BACKUP_AND_RESTORE))
&& !HasTag(game, Tags.GAME_NO_BACKUP)
&& (Etc.IsOnPc(game) || !settings.OnlyBackupOnGameStoppedIfPc || HasTag(game, Tags.GAME_BACKUP) || HasTag(game, Tags.GAME_BACKUP_AND_RESTORE));
var platformBackupDo = (settings.DoPlatformBackupOnNonPcGameStopped || HasTag(game, Tags.PLATFORM_BACKUP) || HasTag(game, Tags.PLATFORM_BACKUP_AND_RESTORE))
&& !HasTag(game, Tags.PLATFORM_NO_BACKUP)
&& !Etc.IsOnPc(game)
&& GetGamePlatform(game) != null;

Expand All @@ -1456,29 +1416,29 @@ private PlayPreferences GetPlayPreferences(Game game)
Backup = new OperationPreference
{
Do = gameBackupDo,
Ask = settings.AskBackupOnGameStopped && !HasTag(game, TAG_GAME_BACKUP) && !HasTag(game, TAG_GAME_BACKUP_AND_RESTORE),
Ask = settings.AskBackupOnGameStopped && !HasTag(game, Tags.GAME_BACKUP) && !HasTag(game, Tags.GAME_BACKUP_AND_RESTORE),
},
Restore = new OperationPreference
{
Do = gameBackupDo
&& (settings.DoRestoreOnGameStarting || HasTag(game, TAG_GAME_BACKUP_AND_RESTORE))
&& !HasTag(game, TAG_GAME_NO_RESTORE),
Ask = settings.AskBackupOnGameStopped && !HasTag(game, TAG_GAME_BACKUP_AND_RESTORE),
&& (settings.DoRestoreOnGameStarting || HasTag(game, Tags.GAME_BACKUP_AND_RESTORE))
&& !HasTag(game, Tags.GAME_NO_RESTORE),
Ask = settings.AskBackupOnGameStopped && !HasTag(game, Tags.GAME_BACKUP_AND_RESTORE),
},
},
Platform = new OperationPreferences
{
Backup = new OperationPreference
{
Do = platformBackupDo,
Ask = settings.AskPlatformBackupOnNonPcGameStopped && !HasTag(game, TAG_PLATFORM_BACKUP) && !HasTag(game, TAG_PLATFORM_BACKUP_AND_RESTORE),
Ask = settings.AskPlatformBackupOnNonPcGameStopped && !HasTag(game, Tags.PLATFORM_BACKUP) && !HasTag(game, Tags.PLATFORM_BACKUP_AND_RESTORE),
},
Restore = new OperationPreference
{
Do = platformBackupDo
&& (settings.DoPlatformRestoreOnNonPcGameStarting || HasTag(game, TAG_PLATFORM_BACKUP_AND_RESTORE))
&& !HasTag(game, TAG_PLATFORM_NO_RESTORE),
Ask = settings.AskPlatformBackupOnNonPcGameStopped && !HasTag(game, TAG_PLATFORM_BACKUP_AND_RESTORE),
&& (settings.DoPlatformRestoreOnNonPcGameStarting || HasTag(game, Tags.PLATFORM_BACKUP_AND_RESTORE))
&& !HasTag(game, Tags.PLATFORM_NO_RESTORE),
Ask = settings.AskPlatformBackupOnNonPcGameStopped && !HasTag(game, Tags.PLATFORM_BACKUP_AND_RESTORE),
},
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public string TagGamesWithBackups_Label()
return Translate(
"config-tag-games-with-backups",
new FluentArgs() {
{TAG, (FluentString)LudusaviPlaynite.TAG_BACKED_UP},
{TAG, (FluentString)Tags.BACKED_UP},
}
);
}
Expand All @@ -610,7 +610,7 @@ public string TagGamesWithUnknownSaveData_Label()
return Translate(
"config-tag-games-with-unknown-save-data",
new FluentArgs() {
{TAG, (FluentString)LudusaviPlaynite.TAG_UNKNOWN_SAVE_DATA},
{TAG, (FluentString)Tags.UNKNOWN_SAVE_DATA},
}
);
}
Expand Down

0 comments on commit 84e0ab5

Please sign in to comment.