add a try-catch to dir size calculation, other stuff

This commit is contained in:
IsWaffle 2023-10-20 22:26:13 -04:00
parent 74d48b90b1
commit 6c2f545743
5 changed files with 47 additions and 6 deletions

View File

@ -3,6 +3,7 @@
<component name="AvaloniaProject">
<option name="projectPerEditor">
<map>
<entry key="SPTInstaller/App.axaml" value="SPTInstaller/SPTInstaller.csproj" />
<entry key="SPTInstaller/CustomControls/DetailedPreCheckItem.axaml" value="SPTInstaller/SPTInstaller.csproj" />
<entry key="SPTInstaller/CustomControls/PreCheckItem.axaml" value="SPTInstaller/SPTInstaller.csproj" />
<entry key="SPTInstaller/CustomControls/SPTInstallButton.axaml" value="SPTInstaller/SPTInstaller.csproj" />

View File

@ -1,4 +1,5 @@
using System.Linq;
using Serilog;
namespace SPTInstaller.Helpers;
@ -8,11 +9,23 @@ public static class DirectorySizeHelper
// https://stackoverflow.com/a/14488941
static readonly string[] SizeSuffixes =
{ "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
public static string SizeSuffix(Int64 value, int decimalPlaces = 1)
{
if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
if (decimalPlaces < 0)
{
throw new ArgumentOutOfRangeException("decimalPlaces");
}
if (value < 0)
{
return "-" + SizeSuffix(-value, decimalPlaces);
}
if (value == 0)
{
return string.Format("{0:n" + decimalPlaces + "} bytes", 0);
}
// mag is 0 for bytes, 1 for KB, 2, for MB, etc.
int mag = (int)Math.Log(value, 1024);
@ -34,5 +47,21 @@ public static class DirectorySizeHelper
SizeSuffixes[mag]);
}
public static long GetSizeOfDirectory(DirectoryInfo sourceDir) => sourceDir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
/// <summary>
/// Gets the size of a directory in bytes
/// </summary>
/// <param name="sourceDir">The directory to get the size of</param>
/// <returns>the size of the <paramref name="sourceDir"/> in bytes or -1 if an error occurred</returns>
public static long GetSizeOfDirectory(DirectoryInfo sourceDir)
{
try
{
return sourceDir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length);
}
catch (Exception ex)
{
Log.Error(ex, "Something went wrong calculating dir size");
return -1;
}
}
}

View File

@ -20,6 +20,11 @@ public static class DownloadCacheHelper
var cacheSize = DirectorySizeHelper.GetSizeOfDirectory(cacheDir);
if (cacheSize == -1)
{
return "An error occurred while getting the cache size :(";
}
if (cacheSize == 0)
return "Empty";

View File

@ -28,6 +28,12 @@ public class FreeSpacePreCheck : PreCheckBase
var installTargetDirectoryInfo = new DirectoryInfo(_internalData.TargetInstallPath);
var eftSourceDirSize = DirectorySizeHelper.GetSizeOfDirectory(eftSourceDirectoryInfo);
if (eftSourceDirSize == -1)
{
return PreCheckResult.FromError("An error occurred while getting the EFT source directory size");
}
var availableSize = DriveInfo.GetDrives().FirstOrDefault(d => d.Name.ToLower() == installTargetDirectoryInfo.Root.Name.ToLower())?.AvailableFreeSpace ?? 0;
var availableSpaceMessage = $"Available Space: {DirectorySizeHelper.SizeSuffix(availableSize, 2)}";

View File

@ -9,8 +9,8 @@
<PackageIcon>icon.ico</PackageIcon>
<ApplicationIcon>Assets\icon.ico</ApplicationIcon>
<Configurations>Debug;Release;TEST</Configurations>
<AssemblyVersion>2.17</AssemblyVersion>
<FileVersion>2.17</FileVersion>
<AssemblyVersion>2.18</AssemblyVersion>
<FileVersion>2.18</FileVersion>
<Company>SPT-AKI</Company>
</PropertyGroup>