Simplify game install cancellation. Cancels now happen silently and don't generate a dialog.

pull/32/head
Pat Hartl 2023-11-12 01:04:05 -06:00
parent bb980cc063
commit 1ede37c031
4 changed files with 62 additions and 31 deletions

View File

@ -49,9 +49,7 @@ namespace LANCommander.PlaynitePlugin
{ {
if (progress.CancelToken != null && progress.CancelToken.IsCancellationRequested) if (progress.CancelToken != null && progress.CancelToken.IsCancellationRequested)
{ {
e.Reader.Cancel(); gameManager.CancelInstall();
e.Reader.Dispose();
e.Stream.Dispose();
progress.IsIndeterminate = true; progress.IsIndeterminate = true;
} }
@ -78,6 +76,17 @@ namespace LANCommander.PlaynitePlugin
InvokeOnInstalled(new GameInstalledEventArgs(installInfo)); 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;
} }
} }
} }

View File

@ -14,8 +14,6 @@ namespace LANCommander.SDK
public class ArchiveEntryExtractionProgressArgs : EventArgs public class ArchiveEntryExtractionProgressArgs : EventArgs
{ {
public IReader Reader { get; set; }
public TrackableStream Stream { get; set; }
public ReaderProgress Progress { get; set; } public ReaderProgress Progress { get; set; }
public IEntry Entry { get; set; } public IEntry Entry { get; set; }
} }

View File

@ -25,6 +25,9 @@ namespace LANCommander.SDK
public delegate void OnArchiveExtractionProgressHandler(long position, long length); public delegate void OnArchiveExtractionProgressHandler(long position, long length);
public event OnArchiveExtractionProgressHandler OnArchiveExtractionProgress; public event OnArchiveExtractionProgressHandler OnArchiveExtractionProgress;
private TrackableStream Stream;
private IReader Reader;
public GameManager(Client client, string defaultInstallDirectory) public GameManager(Client client, string defaultInstallDirectory)
{ {
Client = client; Client = client;
@ -61,7 +64,7 @@ namespace LANCommander.SDK
if (!result.Success && !result.Canceled) if (!result.Success && !result.Canceled)
throw new Exception("Could not extract the installer. Retry the install or check your connection"); throw new Exception("Could not extract the installer. Retry the install or check your connection");
else if (result.Canceled) else if (result.Canceled)
throw new Exception("Game install was canceled"); return "";
GameManifest manifest = null; GameManifest manifest = null;
@ -143,41 +146,62 @@ namespace LANCommander.SDK
Logger?.LogTrace("Downloading and extracting {Game} to path {Destination}", game.Title, destination); Logger?.LogTrace("Downloading and extracting {Game} to path {Destination}", game.Title, destination);
var extractionResult = new ExtractionResult
{
Canceled = false,
};
try try
{ {
Directory.CreateDirectory(destination); Directory.CreateDirectory(destination);
using (var gameStream = Client.StreamGame(game.Id)) Stream = Client.StreamGame(game.Id);
using (var reader = ReaderFactory.Open(gameStream)) 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<IEntry> e) => Reader.EntryExtractionProgress += (object sender, ReaderExtractionEventArgs<IEntry> e) =>
{
OnArchiveEntryExtractionProgress?.Invoke(this, new ArchiveEntryExtractionProgressArgs
{ {
OnArchiveEntryExtractionProgress?.Invoke(this, new ArchiveEntryExtractionProgressArgs Entry = e.Item,
{ Progress = e.ReaderProgress,
Entry = e.Item, });
Progress = e.ReaderProgress, };
Reader = reader,
Stream = gameStream
});
};
reader.WriteAllToDirectory(destination, new ExtractionOptions() while (Reader.MoveToNextEntry())
{
if (Reader.Cancelled)
break;
Reader.WriteEntryToDirectory(destination, new ExtractionOptions()
{ {
ExtractFullPath = true, ExtractFullPath = true,
Overwrite = true Overwrite = true,
PreserveFileTime = true,
}); });
} }
Reader.Dispose();
Stream.Dispose();
} }
catch (Exception ex) 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 else
{ {
@ -194,11 +218,6 @@ namespace LANCommander.SDK
} }
} }
var extractionResult = new ExtractionResult
{
Canceled = false,
};
if (!extractionResult.Canceled) if (!extractionResult.Canceled)
{ {
extractionResult.Success = true; extractionResult.Success = true;
@ -209,5 +228,12 @@ namespace LANCommander.SDK
return extractionResult; return extractionResult;
} }
public void CancelInstall()
{
Reader?.Cancel();
// Reader?.Dispose();
// Stream?.Dispose();
}
} }
} }

View File

@ -126,8 +126,6 @@ namespace LANCommander.SDK
{ {
Entry = e.Item, Entry = e.Item,
Progress = e.ReaderProgress, Progress = e.ReaderProgress,
Reader = reader,
Stream = redistributableStream
}); });
}; };