Files
BeWoPlaner/BeWoLauncher/Logic/Utils/Download/DownloadManager.cs

167 lines
5.5 KiB
C#

using BeWoLauncher.ServiceProxy;
using BS.SharedLauncher;
using BS.SharedLauncher.DataContract;
using BS.SharedLauncher.Enums;
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;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
namespace BeWoLauncher.Logic.Utils.Download
{
public class DownloadManager
{
public DownloadPlanerRequestDC Request { get; set; }
public DownloadPlanerResponseInfoDC ResponseInfo { get; set; }
public DownloadPlanerResponseDC Response { get; set; }
public string DownloadFolder => LauncherPaths.GetDownloadPath();
public string ApplyFolder => LauncherPaths.GetApplyPath();
public DownloadManager()
{
Request = null;
ResponseInfo = null;
Response = null;
}
public void StartDownloadInfo()
{
CreateDownloadRequest();
GetDownloadResponseInfo();
ConnectionInformation.Version = ResponseInfo.Version;
}
public void StartDownload(bool skipLock, Action<int, object> download)
{
GetDownloadResponse();
if (!Response.HasToDo())
{
// Macht das Sinn?
// Ne, wahrsch. Fehler
throw new NotImplementedException("#45419873274");
return;
}
if (Response.HasToDoNewZip())
{
DownloadFolder.CreateFolder();
DownloadFolder.Lock(skipLock);
LauncherPaths.CurrentDownloadZipLocation = FileController.GetNewZipPath(LauncherPaths.GetDownloadPath());
//FileController.SaveDZipStream(LauncherPaths.CurrentDownloadZipLocation, Response.NewZipStream, Response.NewZipData.LongLength, download);
FileController.SaveDZip(LauncherPaths.CurrentDownloadZipLocation, Response.NewZipData);
FileController.SaveInfo(LauncherPaths.CurrentDownloadZipLocation, Response.NewZipInfo);
FileController.SaveInfo(LauncherPaths.GetRootPath().Combine("latest"), Response.LatestApplicationInfo);
Response.NewZipData = null;
DownloadFolder.Unlock();
}
}
public void StartApply(bool skiplock, Action<int, object> apply)
{
ApplyFolder.CreateFolder();
ApplyFolder.Lock(skiplock);
if (Response.HasToDoDownload2Keep())
{
FileController.ApplyDZip(Response.DownloadFilesToKeep, ApplyFolder, apply);
}
if (Response.HasToDoNewZip())
{
FileController.ApplyDZip(LauncherPaths.CurrentDownloadZipLocation, ApplyFolder, apply);
}
}
public void StartCleanup(Action<int, object> feedback)
{
if (Response.HasToRemoveFiles)
{
StartRemoveFiles(feedback, Response.LatestApplicationInfo);
}
DownloadFolder.ClearFolder();
ApplyFolder.Unlock();
Thread.Sleep(1000);
}
public void StartUninstall(Action<int, object> feedback)
{
var t = new PlanerPackageInfoDC()
{
Files = new List<PlanerFileInfoDC>()
};
StartRemoveFiles(feedback, t);
}
private void StartRemoveFiles(Action<int, object> feedback, PlanerPackageInfoDC newPackage)
{
feedback((int)LauncherProgress.DeleteStart, null);
Thread.Sleep(500);
FileController.RemoveUnsuedFiles(newPackage, ApplyFolder, feedback);
}
private void CreateDownloadRequest()
{
Request = new DownloadPlanerRequestDC();
if (Request.PlanerRootFolderExists = LauncherPaths.ActualPlanerLocation.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()))
{
var files = FileController.GetPlanerFiles(ApplyFolder);
if (files is object)
files.ForEach(file => file.LoadHash());
FileController.TryGetPackageInfo(ApplyFolder, out var package);
if (package == null)
package = new PlanerPackageInfoDC();
package.Files = files.ToPlanerFileInfoDCList();
Request.ApplyPackage = package;
}
}
}
private void GetDownloadResponse()
{
Response = LauncherServiceFacade.DoDownloadBeWoServiceSync(x => x.Download2(Request));
}
private void GetDownloadResponseInfo()
{
var t = ConnectionInformation.Tenant;
ResponseInfo = LauncherServiceFacade.DoDownloadBeWoServiceSyncWithException(x => x.DownloadInfo(Request));
}
}
}