Skip to content

Commit

Permalink
Fix some race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Mar 19, 2023
1 parent 34c068f commit 470c923
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"this game requires a matching custom entry" for non-PC games.
However, that assumption isn't always true.
Now, the plugin just adds this message onto the error notification when Ludusavi doesn't recognize a game.
* If you had both game- and platform-based automatic backups enabled after play,
then the game backup might not be done yet when the platform backup was attempted,
resulting in the platform backup aborting.
The plugin now waits until the game backup is done before attempting the platform backup.
* Changed:
* The recommended version of Ludusavi is now 0.16.0. You can download the latest release here:
https://github.com/mtkennerly/ludusavi/releases
Expand Down
50 changes: 30 additions & 20 deletions src/LudusaviPlaynite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,12 @@ public override void OnGameStarting(OnGameStartingEventArgs args)

if (prefs.Game.Restore.Do)
{
var _task = InitiateOperation(game, Operation.Restore, OperationTiming.BeforePlay, BackupCriteria.Game);
InitiateOperationSync(game, Operation.Restore, OperationTiming.BeforePlay, BackupCriteria.Game);
}

if (prefs.Platform.Restore.Do)
{
var _task = InitiateOperation(game, Operation.Restore, OperationTiming.BeforePlay, BackupCriteria.Platform);
InitiateOperationSync(game, Operation.Restore, OperationTiming.BeforePlay, BackupCriteria.Platform);
}

if (settings.DoBackupDuringPlay)
Expand Down Expand Up @@ -454,15 +454,18 @@ public override void OnGameStopped(OnGameStoppedEventArgs arg)
}
}

if (prefs.Game.Backup.Do)
Task.Run(async () =>
{
var _task = InitiateOperation(game, Operation.Backup, OperationTiming.AfterPlay, BackupCriteria.Game);
}
if (prefs.Game.Backup.Do)
{
await InitiateOperation(game, Operation.Backup, OperationTiming.AfterPlay, BackupCriteria.Game);
}
if (prefs.Platform.Backup.Do)
{
var _task = InitiateOperation(game, Operation.Backup, OperationTiming.AfterPlay, BackupCriteria.Platform);
}
if (prefs.Platform.Backup.Do)
{
await InitiateOperation(game, Operation.Backup, OperationTiming.AfterPlay, BackupCriteria.Platform);
}
});
}

public override ISettings GetSettings(bool firstRunSettings)
Expand Down Expand Up @@ -850,7 +853,7 @@ private string FindGame(Game game, string name, OperationTiming timing, BackupCr
return officialName;
}

private async Task InitiateOperation(Game game, Operation operation, OperationTiming timing, BackupCriteria criteria, ApiBackup? backup = null)
private void InitiateOperationSync(Game game, Operation operation, OperationTiming timing, BackupCriteria criteria, ApiBackup? backup = null)
{
if (game == null)
{
Expand Down Expand Up @@ -943,10 +946,10 @@ private async Task InitiateOperation(Game game, Operation operation, OperationTi
switch (operation)
{
case Operation.Backup:
await Task.Run(() => BackUpOneGame(game, timing, criteria));
BackUpOneGame(game, timing, criteria);
break;
case Operation.Restore:
var error = await Task.Run(() => RestoreOneGame(game, backup, criteria));
var error = RestoreOneGame(game, backup, criteria);
if (timing == OperationTiming.BeforePlay && !String.IsNullOrEmpty(error.Message) && !error.Empty)
{
ShowError(error.Message);
Expand All @@ -955,6 +958,11 @@ private async Task InitiateOperation(Game game, Operation operation, OperationTi
}
}

private async Task InitiateOperation(Game game, Operation operation, OperationTiming timing, BackupCriteria criteria, ApiBackup? backup = null)
{
await Task.Run(() => InitiateOperationSync(game, operation, timing, criteria, backup));
}

private void BackUpOneGame(Game game, OperationTiming timing, BackupCriteria criteria)
{
pendingOperation = true;
Expand Down Expand Up @@ -1071,16 +1079,18 @@ private void BackUpAllGames()
private void BackUpOneGameDuringPlay(Game game)
{
var prefs = GetPlayPreferences(game);

if (prefs.Game.Backup.Do && !prefs.Game.Backup.Ask && settings.DoBackupDuringPlay)
Task.Run(() =>
{
Task.Run(() => BackUpOneGame(game, OperationTiming.DuringPlay, BackupCriteria.Game));
}
if (prefs.Game.Backup.Do && !prefs.Game.Backup.Ask && settings.DoBackupDuringPlay)
{
BackUpOneGame(game, OperationTiming.DuringPlay, BackupCriteria.Game);
}
if (prefs.Platform.Backup.Do && !prefs.Platform.Backup.Ask && settings.DoBackupDuringPlay)
{
Task.Run(() => BackUpOneGame(game, OperationTiming.DuringPlay, BackupCriteria.Platform));
}
if (prefs.Platform.Backup.Do && !prefs.Platform.Backup.Ask && settings.DoBackupDuringPlay)
{
BackUpOneGame(game, OperationTiming.DuringPlay, BackupCriteria.Platform);
}
});
}

private RestorationError RestoreOneGame(Game game, ApiBackup? backup, BackupCriteria criteria)
Expand Down

0 comments on commit 470c923

Please sign in to comment.