From 7c97a3db5768c7ee29416674188b536a308ebd3c Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 12 Nov 2023 01:50:34 -0600 Subject: [PATCH] Include download speed in progress dialog --- .../InstallController.cs | 28 ++++++++++++++++--- .../LANCommander.PlaynitePlugin.csproj | 3 ++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/LANCommander.Playnite.Extension/InstallController.cs b/LANCommander.Playnite.Extension/InstallController.cs index 954b9e3..7c7bb86 100644 --- a/LANCommander.Playnite.Extension/InstallController.cs +++ b/LANCommander.Playnite.Extension/InstallController.cs @@ -5,6 +5,7 @@ using Playnite.SDK; using Playnite.SDK.Models; using Playnite.SDK.Plugins; using System; +using System.Diagnostics; using System.Linq; namespace LANCommander.PlaynitePlugin @@ -40,13 +41,30 @@ namespace LANCommander.PlaynitePlugin { var gameManager = new GameManager(Plugin.LANCommanderClient, Plugin.Settings.InstallDirectory); + Stopwatch stopwatch = new Stopwatch(); + + stopwatch.Start(); + + var lastTotalSize = 0d; + var speed = 0d; + gameManager.OnArchiveExtractionProgress += (long pos, long len) => { - var percent = Math.Ceiling((pos / (decimal)len) * 100); + if (stopwatch.ElapsedMilliseconds > 500) + { + var percent = Math.Ceiling((pos / (decimal)len) * 100); - progress.ProgressMaxValue = len; - progress.CurrentProgressValue = pos; - progress.Text = $"Downloading {Game.Name} ({percent}%)"; + progress.ProgressMaxValue = len; + progress.CurrentProgressValue = pos; + + speed = (double)(progress.CurrentProgressValue - lastTotalSize) / (stopwatch.ElapsedMilliseconds / 1000d); + + progress.Text = $"Downloading {Game.Name} ({percent}%) | {ByteSizeLib.ByteSize.FromBytes(speed).ToString("#.#")}/s"; + + lastTotalSize = pos; + + stopwatch.Restart(); + } }; gameManager.OnArchiveEntryExtractionProgress += (object sender, ArchiveEntryExtractionProgressArgs e) => @@ -60,6 +78,8 @@ namespace LANCommander.PlaynitePlugin }; installDirectory = gameManager.Install(gameId); + + stopwatch.Stop(); }, new GlobalProgressOptions($"Preparing to download {Game.Name}") { diff --git a/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj b/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj index e8bed07..38dff6c 100644 --- a/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj +++ b/LANCommander.Playnite.Extension/LANCommander.PlaynitePlugin.csproj @@ -34,6 +34,9 @@ ..\packages\rix0rrr.BeaconLib.1.0.2\lib\net40\BeaconLib.dll + + ..\packages\ByteSize.2.1.1\lib\net45\ByteSize.dll + ..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll