62 lines
2.5 KiB
C#
Raw Normal View History

2023-07-29 14:26:55 -04:00
using System.Linq;
using System.Threading.Tasks;
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
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-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-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
2023-07-29 14:26:55 -04:00
var eftSourceDirSize = DirectorySizeHelper.GetSizeOfDirectory(eftSourceDirectoryInfo);
2024-05-01 10:31:55 -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";
2023-07-29 14:26:55 -04:00
if (eftSourceDirSize > availableSize)
{
2024-05-01 10:31:55 -04:00
return PreCheckResult.FromError(
$"Not enough free space on {installTargetDirectoryInfo.Root.Name} to install SPT\n\n{availableSpaceMessage}\n{requiredSpaceMessage}");
2023-07-29 14:26:55 -04:00
}
2024-05-01 10:31:55 -04:00
return PreCheckResult.FromSuccess(
$"There is enough space available on {installTargetDirectoryInfo.Root.Name} to install SPT.\n\n{availableSpaceMessage}\n{requiredSpaceMessage}");
2023-07-29 14:26:55 -04:00
}
catch (Exception ex)
{
return PreCheckResult.FromException(ex);
}
}
}