2022-05-14 12:19:40 +01:00
|
|
|
|
using System;
|
2022-05-13 22:41:15 +01:00
|
|
|
|
using System.IO;
|
2022-05-14 17:03:24 +01:00
|
|
|
|
using Spectre.Console;
|
2022-07-09 13:08:41 -04:00
|
|
|
|
using SPT_AKI_Installer.Aki.Core.Model;
|
2022-05-13 22:41:15 +01:00
|
|
|
|
|
2022-05-14 02:58:38 +01:00
|
|
|
|
namespace SPT_AKI_Installer.Aki.Helper
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
|
|
|
|
public static class FileHelper
|
|
|
|
|
{
|
2022-07-09 13:08:41 -04:00
|
|
|
|
public static GenericResult CopyDirectoryWithProgress(DirectoryInfo sourceDir, DirectoryInfo targetDir, IProgress<double> progress)
|
2022-05-14 17:03:24 +01:00
|
|
|
|
{
|
2022-07-09 13:08:41 -04:00
|
|
|
|
try
|
2022-05-14 17:03:24 +01:00
|
|
|
|
{
|
2022-07-09 13:08:41 -04:00
|
|
|
|
int totalFiles = sourceDir.GetFiles("*.*", SearchOption.AllDirectories).Length;
|
|
|
|
|
int processedFiles = 0;
|
2022-05-14 17:03:24 +01:00
|
|
|
|
|
2022-07-09 13:08:41 -04:00
|
|
|
|
foreach (var dir in sourceDir.GetDirectories("*", SearchOption.AllDirectories))
|
2022-05-14 17:03:24 +01:00
|
|
|
|
{
|
2022-07-09 13:08:41 -04:00
|
|
|
|
Directory.CreateDirectory(dir.FullName.Replace(sourceDir.FullName, targetDir.FullName));
|
2022-05-14 17:03:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-09 13:08:41 -04:00
|
|
|
|
foreach (var file in sourceDir.GetFiles("*.*", SearchOption.AllDirectories))
|
2022-05-14 17:03:24 +01:00
|
|
|
|
{
|
2022-07-09 13:08:41 -04:00
|
|
|
|
File.Copy(file.FullName, file.FullName.Replace(sourceDir.FullName, targetDir.FullName), true);
|
|
|
|
|
processedFiles++;
|
|
|
|
|
|
|
|
|
|
progress.Report((int)Math.Floor(((double)processedFiles / totalFiles) * 100));
|
2022-05-14 17:03:24 +01:00
|
|
|
|
}
|
2022-07-09 13:08:41 -04:00
|
|
|
|
|
|
|
|
|
return GenericResult.FromSuccess();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return GenericResult.FromError(ex.Message);
|
|
|
|
|
}
|
2022-05-14 17:03:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 14:41:44 +01:00
|
|
|
|
public static void DeleteFiles(string filePath, bool allFolders = false)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2022-06-21 19:51:26 +01:00
|
|
|
|
if (File.Exists(filePath) || Directory.Exists(filePath))
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2022-06-21 19:51:26 +01:00
|
|
|
|
if (filePath.Contains('.'))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(filePath, allFolders);
|
|
|
|
|
}
|
2022-05-13 22:41:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 14:41:44 +01:00
|
|
|
|
public static string FindFile(string path, string name)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2022-05-19 14:41:44 +01:00
|
|
|
|
string[] filePaths = Directory.GetFiles(path);
|
|
|
|
|
foreach (string file in filePaths)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2022-06-06 15:55:46 +01:00
|
|
|
|
if (file.Contains(name, StringComparison.OrdinalIgnoreCase))
|
2022-05-19 14:41:44 +01:00
|
|
|
|
{
|
|
|
|
|
return file;
|
|
|
|
|
}
|
2022-05-13 22:41:15 +01:00
|
|
|
|
}
|
2022-05-19 14:41:44 +01:00
|
|
|
|
return null;
|
2022-05-13 22:41:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 14:41:44 +01:00
|
|
|
|
public static string FindFile(string path, string name, string altName)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
|
|
|
|
string[] filePaths = Directory.GetFiles(path);
|
|
|
|
|
foreach (string file in filePaths)
|
|
|
|
|
{
|
2022-05-19 14:41:44 +01:00
|
|
|
|
if (file.Contains(name, StringComparison.OrdinalIgnoreCase) &&
|
|
|
|
|
file.Contains(altName, StringComparison.OrdinalIgnoreCase))
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 12:19:40 +01:00
|
|
|
|
public static bool FindFolder(string patchRef, string targetPath, out DirectoryInfo dir)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2022-05-30 13:04:54 +01:00
|
|
|
|
var dirInfo = new DirectoryInfo(targetPath).GetDirectories();
|
|
|
|
|
string patchInner = null;
|
|
|
|
|
foreach (var file in dirInfo)
|
|
|
|
|
{
|
|
|
|
|
if (file.FullName.Contains("patcher", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
patchInner = file.FullName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var path = new DirectoryInfo(patchInner);
|
2022-05-13 22:41:15 +01:00
|
|
|
|
if (path.Exists)
|
|
|
|
|
{
|
|
|
|
|
dir = path;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
dir = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|