Adding initial size comparison between eft dir and target dir

This commit is contained in:
Philipp Heenemann 2023-06-18 15:21:50 +02:00
parent 5d8b8851a1
commit 105f402936
2 changed files with 53 additions and 0 deletions

View File

@ -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";
}
}
}

View File

@ -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)