diff --git a/SPTInstaller/Helpers/DirectorySizeHelper.cs b/SPTInstaller/Helpers/DirectorySizeHelper.cs new file mode 100644 index 0000000..9aef26b --- /dev/null +++ b/SPTInstaller/Helpers/DirectorySizeHelper.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; +using System.Linq; +using Serilog; +using SPTInstaller.Models; + +namespace SPTInstaller.Helpers +{ + public static class DirectorySizeHelper + { + public static Result CheckAvailableSize(string eftSourceDirPath, string installTargetDirPath) + { + try + { + var eftSourceDirectoryInfo = new DirectoryInfo(eftSourceDirPath); + var installTargetDirectoryInfo = new DirectoryInfo(installTargetDirPath); + + var eftSourceDirSize = GetSizeOfDirectory(eftSourceDirectoryInfo); + var availableSize = DriveInfo.GetDrives().FirstOrDefault(d => d.Name == installTargetDirectoryInfo.Root.Name)?.AvailableFreeSpace ?? 0; + + if (eftSourceDirSize > availableSize) + { + return Result.FromError($"Not enough space on drive {installTargetDirectoryInfo.Root.Name}.\n\nRequired: {FormatFileSize(eftSourceDirSize)}\nAvailable: {FormatFileSize(availableSize)}"); + } + + return Result.FromSuccess(); + } + catch (Exception ex) + { + Log.Error(ex, "Error while checking available size"); + + return Result.FromError(ex.Message); + } + } + + private static long GetSizeOfDirectory(DirectoryInfo sourceDir) => sourceDir.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi => fi.Length); + + private static string FormatFileSize(long bytes) + { + const int unit = 1024; + var exp = (int)(Math.Log(bytes) / Math.Log(unit)); + + return $"{bytes / Math.Pow(unit, exp):F2} {"KMGTPE"[exp - 1]}B"; + } + } +} \ No newline at end of file diff --git a/SPTInstaller/Installer Tasks/IntializationTask.cs b/SPTInstaller/Installer Tasks/IntializationTask.cs index 6fdafc8..1f024f6 100644 --- a/SPTInstaller/Installer Tasks/IntializationTask.cs +++ b/SPTInstaller/Installer Tasks/IntializationTask.cs @@ -3,6 +3,7 @@ using SPTInstaller.Interfaces; using SPTInstaller.Models; using System.IO; using System.Threading.Tasks; +using SPTInstaller.Helpers; namespace SPTInstaller.Installer_Tasks { @@ -28,6 +29,12 @@ namespace SPTInstaller.Installer_Tasks SetStatus(null, $"Installed EFT Game Path: {FileHelper.GetRedactedPath(_data.OriginalGamePath)}"); + var directorySizeCheckResult = DirectorySizeHelper.CheckAvailableSize(_data.OriginalGamePath, _data.TargetInstallPath); + if (!directorySizeCheckResult.Succeeded) + { + return Result.FromError(directorySizeCheckResult.Message); + } + var result = PreCheckHelper.DetectOriginalGameVersion(_data.OriginalGamePath); if (!result.Succeeded)