From b1e1ade3d999d1a456d5ca103a3da915e3f71d16 Mon Sep 17 00:00:00 2001 From: Josef Nemec Date: Mon, 11 Sep 2023 09:55:48 +0200 Subject: [PATCH] Fix: Cancelling metadata download stuck on on long timeout doesn't work properly --- .../DatabaseFieldsManagerViewModel.cs | 3 ++- .../ViewModels/DesktopAppViewModel.cs | 2 +- .../ViewModels/GameEditViewModel.cs | 7 ++++--- .../ViewModels/ToolsConfigViewModel.cs | 3 ++- source/Playnite/API/DatabaseAPI.cs | 3 ++- source/Playnite/Database/GameDatabase.cs | 20 +++++++++---------- .../Playnite/Database/InMemoryGameDatabase.cs | 4 ++-- .../Playnite/Metadata/MetadataDownloader.cs | 8 ++++---- .../ViewModels/GameEditViewModelTests.cs | 19 +++++++++--------- 9 files changed, 37 insertions(+), 32 deletions(-) diff --git a/source/Playnite.DesktopApp/ViewModels/DatabaseFieldsManagerViewModel.cs b/source/Playnite.DesktopApp/ViewModels/DatabaseFieldsManagerViewModel.cs index 5db2421c6..515f8d995 100644 --- a/source/Playnite.DesktopApp/ViewModels/DatabaseFieldsManagerViewModel.cs +++ b/source/Playnite.DesktopApp/ViewModels/DatabaseFieldsManagerViewModel.cs @@ -16,6 +16,7 @@ using System.Windows; using Playnite.Common.Media.Icons; using Playnite.Emulators; +using System.Threading; namespace Playnite.DesktopApp.ViewModels { @@ -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); diff --git a/source/Playnite.DesktopApp/ViewModels/DesktopAppViewModel.cs b/source/Playnite.DesktopApp/ViewModels/DesktopAppViewModel.cs index 51551950e..918f87377 100644 --- a/source/Playnite.DesktopApp/ViewModels/DesktopAppViewModel.cs +++ b/source/Playnite.DesktopApp/ViewModels/DesktopAppViewModel.cs @@ -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); diff --git a/source/Playnite.DesktopApp/ViewModels/GameEditViewModel.cs b/source/Playnite.DesktopApp/ViewModels/GameEditViewModel.cs index db3442168..c73153c76 100644 --- a/source/Playnite.DesktopApp/ViewModels/GameEditViewModel.cs +++ b/source/Playnite.DesktopApp/ViewModels/GameEditViewModel.cs @@ -27,6 +27,7 @@ using System.Diagnostics; using Playnite.SDK.Exceptions; using Playnite.Scripting.PowerShell; +using System.Threading; namespace Playnite.DesktopApp.ViewModels { @@ -822,7 +823,7 @@ List consolidateIds(SelectableDbItemList selectionList, List 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); } } @@ -834,7 +835,7 @@ List consolidateIds(SelectableDbItemList selectionList, List 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); } } @@ -850,7 +851,7 @@ List consolidateIds(SelectableDbItemList selectionList, List 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); } } diff --git a/source/Playnite.DesktopApp/ViewModels/ToolsConfigViewModel.cs b/source/Playnite.DesktopApp/ViewModels/ToolsConfigViewModel.cs index bb3a03d87..eb037b5c8 100644 --- a/source/Playnite.DesktopApp/ViewModels/ToolsConfigViewModel.cs +++ b/source/Playnite.DesktopApp/ViewModels/ToolsConfigViewModel.cs @@ -11,6 +11,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Playnite.DesktopApp.ViewModels @@ -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); diff --git a/source/Playnite/API/DatabaseAPI.cs b/source/Playnite/API/DatabaseAPI.cs index 9f7da8444..0a4f9251b 100644 --- a/source/Playnite/API/DatabaseAPI.cs +++ b/source/Playnite/API/DatabaseAPI.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Playnite.API @@ -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) diff --git a/source/Playnite/Database/GameDatabase.cs b/source/Playnite/Database/GameDatabase.cs index 2c661e64f..9d3422b84 100644 --- a/source/Playnite/Database/GameDatabase.cs +++ b/source/Playnite/Database/GameDatabase.cs @@ -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); @@ -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) { @@ -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) { @@ -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); @@ -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()); @@ -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))); @@ -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; diff --git a/source/Playnite/Database/InMemoryGameDatabase.cs b/source/Playnite/Database/InMemoryGameDatabase.cs index 8a335e021..a6e639513 100644 --- a/source/Playnite/Database/InMemoryGameDatabase.cs +++ b/source/Playnite/Database/InMemoryGameDatabase.cs @@ -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(); } diff --git a/source/Playnite/Metadata/MetadataDownloader.cs b/source/Playnite/Metadata/MetadataDownloader.cs index b1516d0a5..53c8e30f5 100644 --- a/source/Playnite/Metadata/MetadataDownloader.cs +++ b/source/Playnite/Metadata/MetadataDownloader.cs @@ -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()) @@ -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); } } } @@ -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); } } } @@ -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); } } } diff --git a/source/Tests/Playnite.DesktopApp.Tests/ViewModels/GameEditViewModelTests.cs b/source/Tests/Playnite.DesktopApp.Tests/ViewModels/GameEditViewModelTests.cs index ff20aabe3..33b433728 100644 --- a/source/Tests/Playnite.DesktopApp.Tests/ViewModels/GameEditViewModelTests.cs +++ b/source/Tests/Playnite.DesktopApp.Tests/ViewModels/GameEditViewModelTests.cs @@ -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 @@ -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; @@ -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; @@ -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;