From 1ede37c0313c6192572e4b2033a84617e7963439 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 12 Nov 2023 01:04:05 -0600 Subject: [PATCH] Simplify game install cancellation. Cancels now happen silently and don't generate a dialog. --- .../InstallController.cs | 15 +++- LANCommander.SDK/EventArgs.cs | 2 - LANCommander.SDK/GameManager.cs | 74 +++++++++++++------ LANCommander.SDK/RedistributableManager.cs | 2 - 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/LANCommander.Playnite.Extension/InstallController.cs b/LANCommander.Playnite.Extension/InstallController.cs index 8b7e021..6440365 100644 --- a/LANCommander.Playnite.Extension/InstallController.cs +++ b/LANCommander.Playnite.Extension/InstallController.cs @@ -49,9 +49,7 @@ namespace LANCommander.PlaynitePlugin { if (progress.CancelToken != null && progress.CancelToken.IsCancellationRequested) { - e.Reader.Cancel(); - e.Reader.Dispose(); - e.Stream.Dispose(); + gameManager.CancelInstall(); progress.IsIndeterminate = true; } @@ -78,6 +76,17 @@ namespace LANCommander.PlaynitePlugin InvokeOnInstalled(new GameInstalledEventArgs(installInfo)); } + else if (result.Canceled) + { + var game = Plugin.PlayniteApi.Database.Games.Get(Game.Id); + + game.IsInstalling = false; + game.IsInstalled = false; + + Plugin.PlayniteApi.Database.Games.Update(game); + } + else if (result.Error != null) + throw result.Error; } } } diff --git a/LANCommander.SDK/EventArgs.cs b/LANCommander.SDK/EventArgs.cs index ca85135..c0af51a 100644 --- a/LANCommander.SDK/EventArgs.cs +++ b/LANCommander.SDK/EventArgs.cs @@ -14,8 +14,6 @@ namespace LANCommander.SDK public class ArchiveEntryExtractionProgressArgs : EventArgs { - public IReader Reader { get; set; } - public TrackableStream Stream { get; set; } public ReaderProgress Progress { get; set; } public IEntry Entry { get; set; } } diff --git a/LANCommander.SDK/GameManager.cs b/LANCommander.SDK/GameManager.cs index f0d1e03..bd99083 100644 --- a/LANCommander.SDK/GameManager.cs +++ b/LANCommander.SDK/GameManager.cs @@ -25,6 +25,9 @@ namespace LANCommander.SDK public delegate void OnArchiveExtractionProgressHandler(long position, long length); public event OnArchiveExtractionProgressHandler OnArchiveExtractionProgress; + private TrackableStream Stream; + private IReader Reader; + public GameManager(Client client, string defaultInstallDirectory) { Client = client; @@ -61,7 +64,7 @@ namespace LANCommander.SDK if (!result.Success && !result.Canceled) throw new Exception("Could not extract the installer. Retry the install or check your connection"); else if (result.Canceled) - throw new Exception("Game install was canceled"); + return ""; GameManifest manifest = null; @@ -143,41 +146,62 @@ namespace LANCommander.SDK Logger?.LogTrace("Downloading and extracting {Game} to path {Destination}", game.Title, destination); + var extractionResult = new ExtractionResult + { + Canceled = false, + }; + try { Directory.CreateDirectory(destination); - using (var gameStream = Client.StreamGame(game.Id)) - using (var reader = ReaderFactory.Open(gameStream)) + Stream = Client.StreamGame(game.Id); + Reader = ReaderFactory.Open(Stream); + + Stream.OnProgress += (pos, len) => { - gameStream.OnProgress += (pos, len) => - { - OnArchiveExtractionProgress?.Invoke(pos, len); - }; + OnArchiveExtractionProgress?.Invoke(pos, len); + }; - reader.EntryExtractionProgress += (object sender, ReaderExtractionEventArgs e) => + Reader.EntryExtractionProgress += (object sender, ReaderExtractionEventArgs e) => + { + OnArchiveEntryExtractionProgress?.Invoke(this, new ArchiveEntryExtractionProgressArgs { - OnArchiveEntryExtractionProgress?.Invoke(this, new ArchiveEntryExtractionProgressArgs - { - Entry = e.Item, - Progress = e.ReaderProgress, - Reader = reader, - Stream = gameStream - }); - }; + Entry = e.Item, + Progress = e.ReaderProgress, + }); + }; - reader.WriteAllToDirectory(destination, new ExtractionOptions() + while (Reader.MoveToNextEntry()) + { + if (Reader.Cancelled) + break; + + Reader.WriteEntryToDirectory(destination, new ExtractionOptions() { ExtractFullPath = true, - Overwrite = true + Overwrite = true, + PreserveFileTime = true, }); } + + Reader.Dispose(); + Stream.Dispose(); } catch (Exception ex) { - if (false) + if (Reader.Cancelled) { + Logger?.LogTrace("User cancelled the download"); + extractionResult.Canceled = true; + + if (Directory.Exists(destination)) + { + Logger?.LogTrace("Cleaning up orphaned files after cancelled install"); + + Directory.Delete(destination, true); + } } else { @@ -194,11 +218,6 @@ namespace LANCommander.SDK } } - var extractionResult = new ExtractionResult - { - Canceled = false, - }; - if (!extractionResult.Canceled) { extractionResult.Success = true; @@ -209,5 +228,12 @@ namespace LANCommander.SDK return extractionResult; } + + public void CancelInstall() + { + Reader?.Cancel(); + // Reader?.Dispose(); + // Stream?.Dispose(); + } } } diff --git a/LANCommander.SDK/RedistributableManager.cs b/LANCommander.SDK/RedistributableManager.cs index d714599..fcc87d8 100644 --- a/LANCommander.SDK/RedistributableManager.cs +++ b/LANCommander.SDK/RedistributableManager.cs @@ -126,8 +126,6 @@ namespace LANCommander.SDK { Entry = e.Item, Progress = e.ReaderProgress, - Reader = reader, - Stream = redistributableStream }); };