From 1f5d3220586b2ef144a503d0be48f57e7f93182b Mon Sep 17 00:00:00 2001 From: suchmememanyskill <38142618+suchmememanyskill@users.noreply.github.com> Date: Sun, 9 Jun 2024 22:25:58 +0200 Subject: [PATCH] Itch: Also add time estimate to itch downloads --- ItchIoIntegration/Service/ItchGameDownload.cs | 9 ++++++++- LauncherGamePlugin/Utils.cs | 20 +++++++++++++++++++ RemoteDownloaderPlugin/Game/GameDownload.cs | 15 +------------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/ItchIoIntegration/Service/ItchGameDownload.cs b/ItchIoIntegration/Service/ItchGameDownload.cs index 0f99427..ae9e6f4 100644 --- a/ItchIoIntegration/Service/ItchGameDownload.cs +++ b/ItchIoIntegration/Service/ItchGameDownload.cs @@ -17,6 +17,8 @@ public class ItchGameDownload : ProgressStatus private readonly CancellationTokenSource _cts = new(); private bool _doneDownloading = false; private int _lastSecond = 0; + + private DateTimeOffset _downloadStart = DateTimeOffset.Now; public ItchGameDownload(string url, string path, string filename) { @@ -32,8 +34,13 @@ private void OnProgressUpdate(object? obj, float progress) _lastSecond = DateTime.Now.Second; + var timeBetweenNowAndStart = DateTimeOffset.Now - _downloadStart; + var totalTime = timeBetweenNowAndStart * (1 / progress); + var estimatedTime = totalTime - timeBetweenNowAndStart; + var estimatedDisplay = Utils.TimeSpanAsTimeEstimate(estimatedTime); + progress *= 100; - Line1 = $"Downloading: {progress:0}%"; + Line1 = $"Downloading: {progress:0}% {estimatedDisplay}"; Percentage = progress; InvokeOnUpdate(); } diff --git a/LauncherGamePlugin/Utils.cs b/LauncherGamePlugin/Utils.cs index f33027c..7c3bebc 100644 --- a/LauncherGamePlugin/Utils.cs +++ b/LauncherGamePlugin/Utils.cs @@ -200,4 +200,24 @@ public static Platform GuessPlatformBasedOnString(string? path) List windowsFileExt = [".exe", ".bat", ".msi", ".cmd"]; return windowsFileExt.Any(path.EndsWith) ? Platform.Windows : Platform.Linux; } + + public static string TimeSpanAsTimeEstimate(TimeSpan estimatedTime) + { + var estimatedDisplay = ""; + + if (estimatedTime.TotalMinutes < 60) + { + estimatedDisplay = $"{estimatedTime.Minutes}m"; + } + else if (estimatedTime.TotalMinutes < 1440) + { + estimatedDisplay = $"{estimatedTime.Hours}h{estimatedTime.Minutes}m"; + } + else + { + estimatedDisplay = $"{estimatedTime.Days}d{estimatedTime.Hours}h{estimatedTime.Minutes}m"; + } + + return estimatedDisplay; + } } \ No newline at end of file diff --git a/RemoteDownloaderPlugin/Game/GameDownload.cs b/RemoteDownloaderPlugin/Game/GameDownload.cs index 02607d3..4b40412 100644 --- a/RemoteDownloaderPlugin/Game/GameDownload.cs +++ b/RemoteDownloaderPlugin/Game/GameDownload.cs @@ -35,20 +35,7 @@ private void OnProgressUpdate(object? obj, float progress) var timeBetweenNowAndStart = DateTimeOffset.Now - _downloadStart; var totalTime = timeBetweenNowAndStart * (1 / progress); var estimatedTime = totalTime - timeBetweenNowAndStart; - var estimatedDisplay = ""; - - if (estimatedTime.TotalMinutes < 60) - { - estimatedDisplay = $"{estimatedTime.Minutes}m"; - } - else if (estimatedTime.TotalMinutes < 1440) - { - estimatedDisplay = $"{estimatedTime.Hours}h{estimatedTime.Minutes}m"; - } - else - { - estimatedDisplay = $"{estimatedTime.Days}d{estimatedTime.Hours}h{estimatedTime.Minutes}m"; - } + var estimatedDisplay = LauncherGamePlugin.Utils.TimeSpanAsTimeEstimate(estimatedTime); progress *= 100; Line1 = $"Downloading: {progress:0}% {estimatedDisplay}";