diff --git a/LANCommander.Playnite.Extension/DownloadQueueController.cs b/LANCommander.Playnite.Extension/DownloadQueueController.cs index 93c04825..bf7c1af0 100644 --- a/LANCommander.Playnite.Extension/DownloadQueueController.cs +++ b/LANCommander.Playnite.Extension/DownloadQueueController.cs @@ -35,6 +35,9 @@ namespace LANCommander.PlaynitePlugin public delegate void OnInstallFailHandler(Game game); public event OnInstallFailHandler OnInstallFail; + public delegate void OnInstallCancelledHandler(Game game); + public event OnInstallCancelledHandler OnInstallCancelled; + public DownloadQueueController(LANCommanderLibraryPlugin plugin) { Plugin = plugin; @@ -98,7 +101,17 @@ namespace LANCommander.PlaynitePlugin private void ChangeCurrentItemStatus(DownloadQueueItemStatus status) { Plugin.PlayniteApi.MainView.UIDispatcher.Invoke(() => { - DownloadQueue.CurrentItem.Status = status; + switch (status) + { + case DownloadQueueItemStatus.Downloading: + DownloadQueue.CurrentItem.ProgressIndeterminate = false; + DownloadQueue.CurrentItem.Status = status; + break; + default: + DownloadQueue.CurrentItem.ProgressIndeterminate = true; + DownloadQueue.CurrentItem.Status = status; + break; + } }); } @@ -187,6 +200,15 @@ namespace LANCommander.PlaynitePlugin Task.Run(() => Install()); } + public void CancelInstall() + { + if (DownloadQueue.CurrentItem.Status == DownloadQueueItemStatus.Downloading) + { + ChangeCurrentItemStatus(DownloadQueueItemStatus.Canceled); + Plugin.LANCommanderClient.Games.CancelInstall(); + } + } + private void Install() { if (DownloadQueue.CurrentItem == null) @@ -222,6 +244,15 @@ namespace LANCommander.PlaynitePlugin { installDirectory = Plugin.LANCommanderClient.Games.Install(game.Id); } + catch (InstallCanceledException ex) + { + OnInstallCancelled?.Invoke(DownloadQueue.CurrentItem.Game); + Remove(DownloadQueue.CurrentItem); + + Stopwatch.Stop(); + + return; + } catch (InstallException ex) { Plugin.PlayniteApi.Notifications.Add($"InstallFail-{DownloadQueue.CurrentItem.Game.Id}", ex.Message, NotificationType.Error); diff --git a/LANCommander.Playnite.Extension/InstallController.cs b/LANCommander.Playnite.Extension/InstallController.cs index 07514bd2..af23fcc7 100644 --- a/LANCommander.Playnite.Extension/InstallController.cs +++ b/LANCommander.Playnite.Extension/InstallController.cs @@ -56,6 +56,7 @@ namespace LANCommander.PlaynitePlugin Plugin.DownloadQueue.Add(Game); Plugin.DownloadQueue.OnInstallComplete += MarkInstalled; Plugin.DownloadQueue.OnInstallFail += OnInstallFail; + Plugin.DownloadQueue.OnInstallCancelled += OnInstallCancelled; } private void OnInstallFail(Playnite.SDK.Models.Game game) @@ -66,6 +67,16 @@ namespace LANCommander.PlaynitePlugin Plugin.PlayniteApi.Database.Games.Update(game); } + private void OnInstallCancelled(Playnite.SDK.Models.Game game) + { + Plugin.LANCommanderClient.Games.CancelInstall(); + + game.IsInstalling = false; + game.IsInstalled = false; + + Plugin.PlayniteApi.Database.Games.Update(game); + } + public void MarkInstalled(Playnite.SDK.Models.Game game, string installDirectory) { if (game.Id == Game.Id) diff --git a/LANCommander.Playnite.Extension/Models/DownloadQueueItem.cs b/LANCommander.Playnite.Extension/Models/DownloadQueueItem.cs index 9594c247..60259147 100644 --- a/LANCommander.Playnite.Extension/Models/DownloadQueueItem.cs +++ b/LANCommander.Playnite.Extension/Models/DownloadQueueItem.cs @@ -10,7 +10,8 @@ namespace LANCommander.PlaynitePlugin.Models Downloading, InstallingRedistributables, RunningScripts, - DownloadingSaves + DownloadingSaves, + Canceled } public class DownloadQueueItem : ObservableObject @@ -72,6 +73,8 @@ namespace LANCommander.PlaynitePlugin.Models return "Running Scripts"; case DownloadQueueItemStatus.DownloadingSaves: return "Downloading Saves"; + case DownloadQueueItemStatus.Canceled: + return "Canceling"; case DownloadQueueItemStatus.Idle: default: return "Idle"; diff --git a/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml b/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml index a3e8eb78..35416aa2 100644 --- a/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml +++ b/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml @@ -31,7 +31,7 @@ - + @@ -104,6 +104,12 @@ + + + + + + @@ -133,7 +139,7 @@ - + diff --git a/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml.cs b/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml.cs index dc15d55d..053e398c 100644 --- a/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml.cs +++ b/LANCommander.Playnite.Extension/Views/DownloadQueue.xaml.cs @@ -30,6 +30,11 @@ namespace LANCommander.PlaynitePlugin.Views InitializeComponent(); } + private void CancelDownload(object sender, EventArgs e) + { + Plugin.DownloadQueue.CancelInstall(); + } + private void BackToLibrary(object sender, RoutedEventArgs e) { Plugin.PlayniteApi.MainView.SwitchToLibraryView(); diff --git a/LANCommander.SDK/Exceptions/InstallCanceledException.cs b/LANCommander.SDK/Exceptions/InstallCanceledException.cs new file mode 100644 index 00000000..3583b2ff --- /dev/null +++ b/LANCommander.SDK/Exceptions/InstallCanceledException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LANCommander.SDK.Exceptions +{ + public class InstallCanceledException : Exception + { + public InstallCanceledException(string message) : base(message) { } + } +} diff --git a/LANCommander.SDK/Exceptions/InstallException.cs b/LANCommander.SDK/Exceptions/InstallException.cs new file mode 100644 index 00000000..fd52fd41 --- /dev/null +++ b/LANCommander.SDK/Exceptions/InstallException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LANCommander.SDK.Exceptions +{ + public class InstallException : Exception + { + public InstallException(string message) : base(message) { } + } +} diff --git a/LANCommander.SDK/GameService.cs b/LANCommander.SDK/GameService.cs index 7bd8438a..4eb65f28 100644 --- a/LANCommander.SDK/GameService.cs +++ b/LANCommander.SDK/GameService.cs @@ -168,7 +168,7 @@ namespace LANCommander.SDK if (!result.Success && !result.Canceled) throw new InstallException("Could not extract the installer. Retry the install or check your connection"); else if (result.Canceled) - return ""; + throw new InstallCanceledException("Game install was canceled"); game.InstallDirectory = result.Directory;