Skip to content

Commit

Permalink
Itch: Also add time estimate to itch downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jun 9, 2024
1 parent 3170ff8 commit 1f5d322
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
9 changes: 8 additions & 1 deletion ItchIoIntegration/Service/ItchGameDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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();
}
Expand Down
20 changes: 20 additions & 0 deletions LauncherGamePlugin/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,24 @@ public static Platform GuessPlatformBasedOnString(string? path)
List<string> 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;
}
}
15 changes: 1 addition & 14 deletions RemoteDownloaderPlugin/Game/GameDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down

0 comments on commit 1f5d322

Please sign in to comment.