Skip to content

Commit

Permalink
Fix: Cancelling metadata download stuck on on long timeout doesn't wo…
Browse files Browse the repository at this point in the history
…rk properly
  • Loading branch information
JosefNemec committed Sep 11, 2023
1 parent b47d8ce commit b1e1ade
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Windows;
using Playnite.Common.Media.Icons;
using Playnite.Emulators;
using System.Threading;

namespace Playnite.DesktopApp.ViewModels
{
Expand Down Expand Up @@ -740,7 +741,7 @@ private void UpdatePlatformsCollection()
{
string addNewFile(string path, Guid parent)
{
var newPath = database.AddFile(path, parent, true);
var newPath = database.AddFile(path, parent, true, CancellationToken.None);
if (Paths.AreEqual(Path.GetDirectoryName(path), PlaynitePaths.TempPath))
{
File.Delete(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ private void OnFileDropped(DragEventArgs args)

if (!icoPath.IsNullOrEmpty())
{
game.Icon = Database.AddFile(icoPath, game.Id, true);
game.Icon = Database.AddFile(icoPath, game.Id, true, CancellationToken.None);
}

Database.Games.Add(game);
Expand Down
7 changes: 4 additions & 3 deletions source/Playnite.DesktopApp/ViewModels/GameEditViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Diagnostics;
using Playnite.SDK.Exceptions;
using Playnite.Scripting.PowerShell;
using System.Threading;

namespace Playnite.DesktopApp.ViewModels
{
Expand Down Expand Up @@ -822,7 +823,7 @@ List<Guid> consolidateIds(SelectableDbItemList selectionList, List<Guid> origina
}
else if (File.Exists(EditingGame.Icon))
{
game.Icon = database.AddFile(EditingGame.Icon, game.Id, true);
game.Icon = database.AddFile(EditingGame.Icon, game.Id, true, CancellationToken.None);
}
}

Expand All @@ -834,7 +835,7 @@ List<Guid> consolidateIds(SelectableDbItemList selectionList, List<Guid> origina
}
else if (File.Exists(EditingGame.CoverImage))
{
game.CoverImage = database.AddFile(EditingGame.CoverImage, game.Id, true);
game.CoverImage = database.AddFile(EditingGame.CoverImage, game.Id, true, CancellationToken.None);
}
}

Expand All @@ -850,7 +851,7 @@ List<Guid> consolidateIds(SelectableDbItemList selectionList, List<Guid> origina
}
else if (File.Exists(EditingGame.BackgroundImage))
{
game.BackgroundImage = database.AddFile(EditingGame.BackgroundImage, game.Id, true);
game.BackgroundImage = database.AddFile(EditingGame.BackgroundImage, game.Id, true, CancellationToken.None);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Playnite.DesktopApp.ViewModels
Expand Down Expand Up @@ -124,7 +125,7 @@ public void ConfirmDialog()
{
string addNewFile(string path, Guid parent)
{
var newPath = database.AddFile(path, parent, true);
var newPath = database.AddFile(path, parent, true, CancellationToken.None);
if (Paths.AreEqual(Path.GetDirectoryName(path), PlaynitePaths.TempPath))
{
File.Delete(path);
Expand Down
3 changes: 2 additions & 1 deletion source/Playnite/API/DatabaseAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Playnite.API
Expand Down Expand Up @@ -59,7 +60,7 @@ public string AddFile(string path, Guid parentId)
throw new FileNotFoundException("Cannot add file to database, file not found.");
}

return database.AddFile(path, parentId, false);
return database.AddFile(path, parentId, false, CancellationToken.None);
}

public void SaveFile(string id, string path)
Expand Down
20 changes: 10 additions & 10 deletions source/Playnite/Database/GameDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public interface IGameDatabaseMain : IGameDatabase
void OpenDatabase();
string GetFileStoragePath(Guid parentId);
string GetFullFilePath(string dbPath);
string AddFile(MetadataFile file, Guid parentId, bool isImage);
string AddFile(string path, Guid parentId, bool isImage);
string AddFile(MetadataFile file, Guid parentId, bool isImage, CancellationToken cancelToken);
string AddFile(string path, Guid parentId, bool isImage, CancellationToken cancelToken);
void RemoveFile(string dbPath);
BitmapImage GetFileAsImage(string dbPath, BitmapLoadProperties loadProperties = null);
void CopyFile(string dbPath, string targetPath);
Expand Down Expand Up @@ -793,7 +793,7 @@ public string GetFullFilePath(string dbPath)
return Path.Combine(FilesDirectoryPath, dbPath);
}

public string AddFile(MetadataFile file, Guid parentId, bool isImage)
public string AddFile(MetadataFile file, Guid parentId, bool isImage, CancellationToken cancelToken)
{
if (!file.HasImageData)
{
Expand All @@ -804,7 +804,7 @@ public string AddFile(MetadataFile file, Guid parentId, bool isImage)
string localPath = null;
try
{
localPath = file.GetLocalFile(CancellationToken.None);
localPath = file.GetLocalFile(cancelToken);
}
catch (Exception e)
{
Expand All @@ -816,7 +816,7 @@ public string AddFile(MetadataFile file, Guid parentId, bool isImage)
return null;
}

var finalFile = AddFile(localPath, parentId, isImage);
var finalFile = AddFile(localPath, parentId, isImage, cancelToken);
if (localPath.StartsWith(PlaynitePaths.TempPath))
{
FileSystem.DeleteFile(localPath);
Expand All @@ -825,7 +825,7 @@ public string AddFile(MetadataFile file, Guid parentId, bool isImage)
return finalFile;
}

public string AddFile(string path, Guid parentId, bool isImage)
public string AddFile(string path, Guid parentId, bool isImage, CancellationToken cancelToken)
{
CheckDbState();
var targetDir = Path.Combine(FilesDirectoryPath, parentId.ToString());
Expand All @@ -838,7 +838,7 @@ public string AddFile(string path, Guid parentId, bool isImage)
var extension = Path.GetExtension(new Uri(path).AbsolutePath);
var fileName = Guid.NewGuid().ToString() + extension;
var downPath = Path.Combine(targetDir, fileName);
HttpDownloader.DownloadFile(path, downPath);
HttpDownloader.DownloadFile(path, downPath, cancelToken);
if (isImage)
{
var converted = Images.ConvertToCompatibleFormat(downPath, Path.Combine(targetDir, Path.GetFileNameWithoutExtension(fileName)));
Expand Down Expand Up @@ -1165,17 +1165,17 @@ public Game ImportGame(GameMetadata game, Guid pluginId)

if (game.Icon != null)
{
toAdd.Icon = AddFile(game.Icon, toAdd.Id, true);
toAdd.Icon = AddFile(game.Icon, toAdd.Id, true, CancellationToken.None);
}

if (game.CoverImage != null)
{
toAdd.CoverImage = AddFile(game.CoverImage, toAdd.Id, true);
toAdd.CoverImage = AddFile(game.CoverImage, toAdd.Id, true, CancellationToken.None);
}

if (game.BackgroundImage != null)
{
toAdd.BackgroundImage = AddFile(game.BackgroundImage, toAdd.Id, true);
toAdd.BackgroundImage = AddFile(game.BackgroundImage, toAdd.Id, true, CancellationToken.None);
}

toAdd.IncludeLibraryPluginAction = true;
Expand Down
4 changes: 2 additions & 2 deletions source/Playnite/Database/InMemoryGameDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public string GetFullFilePath(string dbPath)
throw new NotImplementedException();
}

public string AddFile(MetadataFile file, Guid parentId, bool isImage)
public string AddFile(MetadataFile file, Guid parentId, bool isImage, CancellationToken cancelToken)
{
throw new NotImplementedException();
}

public string AddFile(string path, Guid parentId, bool isImage)
public string AddFile(string path, Guid parentId, bool isImage, CancellationToken cancelToken)
{
throw new NotImplementedException();
}
Expand Down
8 changes: 4 additions & 4 deletions source/Playnite/Metadata/MetadataDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ public Task DownloadMetadataAsync(
{
if (playniteSettings.DownloadBackgroundsImmediately && gameData.BackgroundImage.HasImageData)
{
game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true);
game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true, cancelToken);
}
else if (!playniteSettings.DownloadBackgroundsImmediately &&
!gameData.BackgroundImage.Path.IsNullOrEmpty())
Expand All @@ -559,7 +559,7 @@ public Task DownloadMetadataAsync(
}
else if (gameData.BackgroundImage.HasImageData)
{
game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true);
game.BackgroundImage = database.AddFile(gameData.BackgroundImage, game.Id, true, cancelToken);
}
}
}
Expand All @@ -573,7 +573,7 @@ public Task DownloadMetadataAsync(
gameData = ProcessField(game, settings.CoverImage, MetadataField.CoverImage, existingStoreData, existingPluginData, cancelToken);
if (gameData?.CoverImage != null)
{
game.CoverImage = database.AddFile(gameData.CoverImage, game.Id, true);
game.CoverImage = database.AddFile(gameData.CoverImage, game.Id, true, cancelToken);
}
}
}
Expand All @@ -586,7 +586,7 @@ public Task DownloadMetadataAsync(
gameData = ProcessField(game, settings.Icon, MetadataField.Icon, existingStoreData, existingPluginData, cancelToken);
if (gameData?.Icon != null)
{
game.Icon = database.AddFile(gameData.Icon, game.Id, true);
game.Icon = database.AddFile(gameData.Icon, game.Id, true, cancelToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Playnite.DesktopApp.Tests.ViewModels
Expand All @@ -30,9 +31,9 @@ public void ImageReplaceTest()
Name = "Test Game"
};

var origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
var origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
var origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
game.Icon = origIcon;
game.CoverImage = origImage;
game.BackgroundImage = origBackground;
Expand Down Expand Up @@ -78,9 +79,9 @@ public void ImageReplaceMultiTest()
Name = "Test Game"
};

var origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
var origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
var origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
var origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
game.Icon = origIcon;
game.CoverImage = origImage;
game.BackgroundImage = origBackground;
Expand All @@ -92,9 +93,9 @@ public void ImageReplaceMultiTest()
Name = "Test Game 2"
};

origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true);
origIcon = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
origImage = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
origBackground = db.AddFile(PlayniteTests.GenerateFakeFile(), game.Id, true, CancellationToken.None);
game.Icon = origIcon;
game.CoverImage = origImage;
game.BackgroundImage = origBackground;
Expand Down

0 comments on commit b1e1ade

Please sign in to comment.