Files
BeWoPlaner/SharedLauncher/Logic/FileController.cs
2022-08-30 09:12:20 +02:00

184 lines
5.8 KiB
C#

using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Extensions;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Update;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BS.SharedLauncher.Logic
{
public static class FileController
{
private static string ZipFormatName => BeWoPathFileConfig.ZipFormatName;
private static string DotBeWoInfo => BeWoPathFileConfig.DotBeWoInfo;
public static string GetDZipPath(string root, int number) => Path.Combine(root, string.Format(ZipFormatName, getFileNumberCountString(number)));
public static string GetDZipInfoPath(string root, int number) => GetDZipPath(root, number) + DotBeWoInfo;
public static void ApplyDZip(Dictionary<string, List<string>> zip2keepfiles, string destinationfolder)
{
foreach (var pair in zip2keepfiles)
{
ApplyDZip(pair.Key, destinationfolder, pair.Value.Contains);
}
}
public static void ApplyDZip(string zipfilepath, string destinationfolder)
=> ApplyDZip(zipfilepath, destinationfolder, (s) => true);
private static void ApplyDZip(string zipfilepath, string destinationfolder, Func<string, bool> isValidFunc)
{
using (ZipArchive archive = ZipFile.OpenRead(zipfilepath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (isValidFunc(entry.Name))
{
entry.ExtractToFile(destinationfolder.Combine(entry.Name), true);
}
}
}
}
public static List<string> GetValidDZipPaths(string path)
{
var res = new List<string>();
var dir = new DirectoryInfo(path);
var files = dir.GetFiles("*.zip");
foreach (var file in files)
{
if (file.FullName.IsDZipValid())
res.Add(file.FullName);
}
return res;
}
public static string GetNewZipPath(string root)
{
int i = 0;
string temp = string.Empty;
do
{
temp = GetDZipPath(root, i++);
} while (temp.IsDZipValidAndDeleteIfNot());
return temp;
}
public static void SaveDZip(string path, byte[] data)
{
File.WriteAllBytes(path, data);
}
public static void SaveInfo<T>(string path, T t)
where T : class
{
File.WriteAllText(path.WithInfoExtension(), XmlConverter.Serialize<T>(t));
}
public static PlanerPackageInfoDC LoadInfo(string filepath)
{
filepath = filepath.WithInfoExtension();
var xml = File.ReadAllText(filepath);
var obj = XmlConverter.Deserialize<PlanerPackageInfoDC>(xml);
return obj;
}
public static bool TryGetPackageInfo(string filepath, out PlanerPackageInfoDC planerPackageInfoDC)
{
planerPackageInfoDC = null;
try
{
planerPackageInfoDC = LoadInfo(filepath);
}
catch (Exception)
{
return false;
}
return true;
}
public static Dictionary<string, PlanerPackageInfoDC> LoadInfos(string rootfolder)
{
var allDZips = GetValidDZipPaths(rootfolder);
if (allDZips is null || !allDZips.Any())
return null;
return allDZips.ToDictionary(dzip => dzip, dzip => LoadInfo(dzip));
// Detailed Debug Section
//var res = new Dictionary<string, PlanerPackageInfoDC>();
//foreach (var dzip in allDZips)
//{
// res.Add(dzip, GetPackageInfo(dzip));
//}
//return res;
}
public static List<PlanerFileInfo> GetPlanerFiles(string path)
{
var res = new List<PlanerFileInfo>();
// Sollte der Path ohne / enden, wird der unterordner mit angegeben
// Deshalb manuell hinzufügen, falls nicht vorhanden
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
path += Path.DirectorySeparatorChar;
getPlanerFilesRecursive(new DirectoryInfo(path), path, ref res);
return res;
}
public static bool IsDZipValidAndDeleteIfNot(this string path)
{
if (path.IsDZipValid())
return true;
new FileInfo(path).Delete();
new FileInfo(path + BeWoPathFileConfig.DotBeWoInfo).Delete();
return false;
}
public static bool IsDZipValid(this string path)
{
return path.Exists() && (path + BeWoPathFileConfig.DotBeWoInfo).Exists();
}
public static string WithInfoExtension(this string path)
{
if (path.EndsWith(DotBeWoInfo))
return path;
return path + DotBeWoInfo;
}
private static void getPlanerFilesRecursive(DirectoryInfo directoryInfo, string relativeTo, ref List<PlanerFileInfo> files)
{
foreach (var item in directoryInfo.GetDirectories())
{
getPlanerFilesRecursive(item, relativeTo, ref files);
}
foreach (var item in directoryInfo.GetFiles())
{
var file = new PlanerFileInfo(item, relativeTo);
files.Add(file);
}
}
private static string getFileNumberCountString(int i)
{
if (i == 0)
return string.Empty;
return $"({i})";
}
}
}