Skip to content

Commit

Permalink
App: Show disk usage
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jun 16, 2024
1 parent 6ac424f commit 94cf12e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Launcher/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<Label FontSize="18" VerticalAlignment="Center">Not Installed</Label>
</Border>
<Button FontSize="18" Name="DownloadLocationButton" >DownloadLocation</Button>
<StackPanel Orientation="Vertical" Name="StorageSpaceStackPanel">

</StackPanel>
</StackPanel>
<ListBox HorizontalAlignment="Center" Name="NotInstalledListBox" Background="Transparent">
<ListBox.ItemsPanel>
Expand Down
28 changes: 28 additions & 0 deletions Launcher/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
using Avalonia.Media.Imaging;
using Launcher.Extensions;
using Launcher.Forms;
using LauncherGamePlugin;
using LauncherGamePlugin.Commands;
using LauncherGamePlugin.Enums;
using LauncherGamePlugin.Extensions;

namespace Launcher.Views;

Expand Down Expand Up @@ -42,6 +44,7 @@ public MainView()
NotInstalledListBox.SelectionChanged += (_, _) => MonitorListBox(NotInstalledListBox);
SearchBox.KeyUp += (_, _) => ApplySearch();
OnUpdateView += GenerateNewMenuItems;
OnUpdateView += UpdateDriveStats;
}

public void ApplySearch()
Expand Down Expand Up @@ -172,4 +175,29 @@ public async void OnHidePluginSideBar()
_app.SidebarState = PluginSideBar.IsVisible;
UpdateView();
}

private void UpdateDriveStats()
{
StorageSpaceStackPanel.Children.Clear();
try
{
var driveInfo = new DriveInfo(_app.GameDir);
StorageSpaceStackPanel.Children.Add(new Label()
{
FontSize = 16,
Content = $"{driveInfo.Name}: {driveInfo.AvailableFreeSpace.ReadableSize()} free"
});

var percentage = ((driveInfo.TotalSize - driveInfo.AvailableFreeSpace) / (double)driveInfo.TotalSize) * 100;

StorageSpaceStackPanel.Children.Add(new ProgressBar()
{
Value = percentage,
});
}
catch (Exception e)
{
_app.Logger.Log($"Unable to fetch drive statistics: {e.Message}", LogType.Warn);
}
}
}
14 changes: 7 additions & 7 deletions LauncherGamePlugin/Extensions/LongExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

public static class LongExtensions
{
private static readonly string[] gameSizes = { "B", "KB", "MB", "GB" };
private static readonly string[] gameSizes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };

public static string ReadableSize(this long size)
{
int type = 0;
double bytesLeft = size;
while (bytesLeft >= 1024)
if (size <= 0)
{
type++;
bytesLeft /= 1024;
return "0 B";
}

return $"{bytesLeft:0.00} {gameSizes[type]}";
var i = (int)Math.Floor(Math.Log(size, 1024));
var p = Math.Pow(1024, i);
var s = Math.Round(size / p, 2);
return $"{s} {gameSizes[i]}";
}
}
1 change: 1 addition & 0 deletions RemoteDownloaderPlugin/Game/GameDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private async Task DownloadPc(IApp app, PcEntry entry)

try
{
// TODO: Fix security vuln, zips can have backwards paths
using HttpResponseMessage response = await client.GetAsync(entry.Url, HttpCompletionOption.ResponseHeadersRead, _cts.Token);
response.EnsureSuccessStatusCode();
await using var responseStream = await response.Content.ReadAsStreamAsync();
Expand Down

0 comments on commit 94cf12e

Please sign in to comment.