Files
BeWoPlaner/BeWoLauncher/Utils/Update/UpdateFileManager.cs
2022-08-30 09:12:20 +02:00

158 lines
4.8 KiB
C#

using BeWoLauncher.ServiceProxy;
using BS.SharedLauncher;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Exceptions;
using BS.SharedLauncher.Extensions;
using BS.SharedLauncher.Information;
using BS.SharedLauncher.Logic;
using BS.SharedLauncher.Update;
using BS.SharedLauncher.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
namespace BeWoLauncher.Utils.Update
{
public class UpdateFileManager
{
public DownloadPlanerRequestDC Request { get; set; }
public DownloadPlanerResponseDC Response { get; set; }
public string DownloadZipPath { get; set; }
public string DownloadFolder => BeWoPaths.GetDownloadPath();
public string ApplyFolder => BeWoPaths.GetApplyPath();
public UpdateFileManager()
{
Request = null;
Response = null;
}
public void StartUpdate(bool skipLock)
{
CreateUpdateRequest();
GetUpdateResponse();
if (!Response.HasToDo())
{
return;
}
if (Response.HasToDoNewZip())
{
StartDownload(skipLock);
}
StartApply(skipLock);
DownloadFolder.ClearFolder();
}
private void CreateUpdateRequest()
{
Request = new DownloadPlanerRequestDC();
Request.PlanerRootFolderExists = DownloadFolder.Exists();
if (Request.DownloadFolderExists = DownloadFolder.Exists())
if (!(Request.DownloadFolderIsEmpty = DownloadFolder.IsFolderEmpty()))
Request.DownloadedPackages = FileController.LoadInfos(DownloadFolder);
if (Request.ApplyFolderExists = ApplyFolder.Exists())
if (!(Request.ApplyFolderIsEmpty = ApplyFolder.IsFolderEmpty()))
if (FileController.TryGetPackageInfo(BeWoPaths.GetApplyPackagePath(), out var planer))
Request.ApplyPackage = planer;
}
private void GetUpdateResponse()
{
Response = LauncherServiceFacade.DoDownloadBeWoServiceSync(x => x.Download2(Request));
}
private void StartDownload(bool skiplock)
{
DownloadFolder.CreateFolder();
DownloadFolder.Lock(skiplock);
DownloadZipPath = FileController.GetNewZipPath(BeWoPaths.GetDownloadPath());
FileController.SaveDZip(DownloadZipPath, Response.NewZipData);
FileController.SaveInfo(DownloadZipPath, Response.NewZipInfo);
FileController.SaveInfo(BeWoPaths.GetDownloadPackagePath(), Response.LatestApplicationInfo);
Response.NewZipData = null;
DownloadFolder.Unlock();
}
private void StartApply(bool skiplock)
{
ApplyFolder.CreateFolder();
ApplyFolder.Lock(skiplock);
if (Response.HasToDoDownload2Keep())
{
FileController.ApplyDZip(Response.DownloadFilesToKeep, ApplyFolder);
}
if (Response.HasToDoNewZip())
{
FileController.ApplyDZip(DownloadZipPath, ApplyFolder);
}
if (Response.HasToRemoveFiles)
{
}
// Überschreibe alle lokalen Dateien mit dem Update
//private void ApplyDZip(string path, PlanerPackageInfoDC planer)
//{
//using (ZipArchive archive = ZipFile.OpenRead(path))
//{
// foreach (ZipArchiveEntry entry in archive.Entries)
// {
// var planerFileInfo = PlanerPackage.FindPlanerFile(entry.Name);
// entry.ExtractToFile(ApplyPath.Combine(entry.Name), true);
// }
//}
//}
//// Entferne alte Dateien
//// Überprüfe mit neuer Version Xml
//var applyFiles = Directory.EnumerateFileSystemEntries(ApplyPath);
//foreach (var file in applyFiles)
//{
// var relative = PathUtils.MakeRelativePath(ApplyPath, file);
// if (PlanerPackage.FirstOrDefaultPlanerFile(relative) is object)
// continue;
// if (relative == BeWoPaths.GetLockFileName())
// continue;
// File.Delete(file);
//}
//// Überschreibe Update Version
//File.WriteAllText(BeWoPaths.GetVersionXmlPath(), PlanerVersionXml);
////UnlockApply();
///
throw new NotImplementedException();
}
// RE Auslagern
}
}