2023-07-29 14:26:55 -04:00
using System.Linq ;
using System.Threading.Tasks ;
2023-07-12 08:32:57 +02:00
using SPTInstaller.Helpers ;
using SPTInstaller.Models ;
namespace SPTInstaller.Installer_Tasks.PreChecks ;
public class FreeSpacePreCheck : PreCheckBase
{
private readonly InternalData _internalData ;
2024-05-01 10:31:55 -04:00
2023-07-12 08:32:57 +02:00
public FreeSpacePreCheck ( InternalData internalData ) : base ( "Free Space" , true )
{
_internalData = internalData ;
}
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
public override async Task < PreCheckResult > CheckOperation ( )
2023-07-12 08:32:57 +02:00
{
2023-07-29 14:26:55 -04:00
if ( _internalData . OriginalGamePath is null )
return PreCheckResult . FromError ( "Could not find EFT game path" ) ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
if ( _internalData . TargetInstallPath is null )
return PreCheckResult . FromError ( "Could not find install target path" ) ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
try
2023-07-12 08:32:57 +02:00
{
2023-07-29 14:26:55 -04:00
var eftSourceDirectoryInfo = new DirectoryInfo ( _internalData . OriginalGamePath ) ;
var installTargetDirectoryInfo = new DirectoryInfo ( _internalData . TargetInstallPath ) ;
2024-05-01 10:31:55 -04:00
2024-05-01 14:17:09 -04:00
var cacheDirectory = new DirectoryInfo ( DownloadCacheHelper . CachePath ) ;
2023-07-29 14:26:55 -04:00
var eftSourceDirSize = DirectorySizeHelper . GetSizeOfDirectory ( eftSourceDirectoryInfo ) ;
2024-05-01 10:31:55 -04:00
2023-10-20 22:26:13 -04:00
if ( eftSourceDirSize = = - 1 )
{
return PreCheckResult . FromError ( "An error occurred while getting the EFT source directory size" ) ;
}
2024-05-01 10:31:55 -04:00
var availableSize = DriveInfo . GetDrives ( )
. FirstOrDefault ( d = > d . Name . ToLower ( ) = = installTargetDirectoryInfo . Root . Name . ToLower ( ) )
? . AvailableFreeSpace ? ? 0 ;
2023-12-29 15:38:41 -05:00
// add 10Gb overhead to game files for potential patches / release files
eftSourceDirSize + = 10000000000 ;
2024-05-01 10:31:55 -04:00
2023-07-29 14:26:55 -04:00
var availableSpaceMessage = $"Available Space: {DirectorySizeHelper.SizeSuffix(availableSize, 2)}" ;
2024-05-01 10:31:55 -04:00
var requiredSpaceMessage =
$"Space Required for EFT Client: {DirectorySizeHelper.SizeSuffix(eftSourceDirSize, 2)} including ~10Gb overhead" ;
2024-05-01 14:17:09 -04:00
var cacheDriveMessage = "" ;
var cacheDriveOK = true ;
// if cache directory is on another drive, check that drive for around 5Gb of required space
if ( cacheDirectory . Root . Name . ToLower ( ) ! = installTargetDirectoryInfo . Root . Name . ToLower ( ) )
{
cacheDriveOK = false ;
var availableCacheDriveSize = DriveInfo . GetDrives ( )
. FirstOrDefault ( d = >
d . Name . ToLower ( ) = = cacheDirectory . Root . Name . ToLower ( ) )
? . AvailableFreeSpace ? ?
0 ;
// check if the drive where the cache is has at least 5Gb of free space. We should only need 2-3Gb
if ( availableCacheDriveSize > 5000000000 )
{
cacheDriveMessage = $"Drive for cache '{cacheDirectory.Root.Name}' has at least 5Gb of space. Available: {DirectorySizeHelper.SizeSuffix(availableCacheDriveSize, 2)}" ;
cacheDriveOK = true ;
}
else
{
cacheDriveMessage = $"Drive for cache '{cacheDirectory.Root.Name}' does NOT have at least 5Gb of space. Available: {DirectorySizeHelper.SizeSuffix(availableCacheDriveSize, 2)}" ;
}
}
2023-07-29 14:26:55 -04:00
if ( eftSourceDirSize > availableSize )
{
2024-05-01 10:31:55 -04:00
return PreCheckResult . FromError (
2024-05-01 14:17:09 -04:00
$"Not enough free space on {installTargetDirectoryInfo.Root.Name} to install SPT\n\n{availableSpaceMessage}\n{requiredSpaceMessage}\n\n{cacheDriveMessage}" ) ;
}
var okGameSpaceMessage =
$"There is enough space available on {installTargetDirectoryInfo.Root.Name} to install SPT.\n\n{availableSpaceMessage}\n{requiredSpaceMessage}\n\n{cacheDriveMessage}" ;
if ( ! cacheDriveOK )
{
return PreCheckResult . FromError ( okGameSpaceMessage ) ;
2023-07-29 14:26:55 -04:00
}
2024-05-01 10:31:55 -04:00
2024-05-01 14:17:09 -04:00
return PreCheckResult . FromSuccess ( okGameSpaceMessage ) ;
2023-07-29 14:26:55 -04:00
}
catch ( Exception ex )
{
return PreCheckResult . FromException ( ex ) ;
}
2023-07-12 08:32:57 +02:00
}
}