Adding initial size comparison between eft dir and target dir
This commit is contained in:
parent
5d8b8851a1
commit
105f402936
46
SPTInstaller/Helpers/DirectorySizeHelper.cs
Normal file
46
SPTInstaller/Helpers/DirectorySizeHelper.cs
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user