Merge pull request 'master' (#17) from CWX/SPT-AKI-Installer:master into master

Reviewed-on: waffle.lord/SPT-AKI-Installer#17
This commit is contained in:
IsWaffle 2023-11-09 14:00:35 +00:00
commit f837e02ec0
5 changed files with 47 additions and 6 deletions

View File

@ -3,6 +3,7 @@
<component name="AvaloniaProject"> <component name="AvaloniaProject">
<option name="projectPerEditor"> <option name="projectPerEditor">
<map> <map>
<entry key="SPTInstaller/App.axaml" value="SPTInstaller/SPTInstaller.csproj" />
<entry key="SPTInstaller/CustomControls/DetailedPreCheckItem.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/PreCheckItem.axaml" value="SPTInstaller/SPTInstaller.csproj" />
<entry key="SPTInstaller/CustomControls/SPTInstallButton.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 System.Linq;
using Serilog;
namespace SPTInstaller.Helpers; namespace SPTInstaller.Helpers;
@ -8,11 +9,23 @@ public static class DirectorySizeHelper
// https://stackoverflow.com/a/14488941 // https://stackoverflow.com/a/14488941
static readonly string[] SizeSuffixes = static readonly string[] SizeSuffixes =
{ "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
public static string SizeSuffix(Int64 value, int decimalPlaces = 1) public static string SizeSuffix(Int64 value, int decimalPlaces = 1)
{ {
if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); } if (decimalPlaces < 0)
if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); } {
if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 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. // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
int mag = (int)Math.Log(value, 1024); int mag = (int)Math.Log(value, 1024);
@ -34,5 +47,21 @@ public static class DirectorySizeHelper
SizeSuffixes[mag]); 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); var cacheSize = DirectorySizeHelper.GetSizeOfDirectory(cacheDir);
if (cacheSize == -1)
{
return "An error occurred while getting the cache size :(";
}
if (cacheSize == 0) if (cacheSize == 0)
return "Empty"; return "Empty";

View File

@ -28,6 +28,12 @@ public class FreeSpacePreCheck : PreCheckBase
var installTargetDirectoryInfo = new DirectoryInfo(_internalData.TargetInstallPath); var installTargetDirectoryInfo = new DirectoryInfo(_internalData.TargetInstallPath);
var eftSourceDirSize = DirectorySizeHelper.GetSizeOfDirectory(eftSourceDirectoryInfo); 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 availableSize = DriveInfo.GetDrives().FirstOrDefault(d => d.Name.ToLower() == installTargetDirectoryInfo.Root.Name.ToLower())?.AvailableFreeSpace ?? 0;
var availableSpaceMessage = $"Available Space: {DirectorySizeHelper.SizeSuffix(availableSize, 2)}"; var availableSpaceMessage = $"Available Space: {DirectorySizeHelper.SizeSuffix(availableSize, 2)}";

View File

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