173 lines
4.5 KiB
C#
173 lines
4.5 KiB
C#
using BeWoLauncher.Logic.Utils;
|
|
using BeWoLauncher.Logic.Utils.Download;
|
|
using BS.SharedLauncher.Extensions;
|
|
using BS.SharedLauncher.Information;
|
|
using BS.SharedLauncher.Logic;
|
|
using BS.SharedLauncher.Utils;
|
|
using System;
|
|
using System.IO;
|
|
using System.Windows;
|
|
|
|
namespace BeWoLauncher.Logic.Controller
|
|
{
|
|
public static class DownloadController
|
|
{
|
|
public static DownloadManager Manager { get; set; }
|
|
|
|
public static long FreeSpace { get; set; }
|
|
public static long NeededSpace => Manager.UpdatePlanResponse.DownloadSize;
|
|
|
|
static DownloadController()
|
|
{
|
|
|
|
}
|
|
|
|
public static void Start(string logger_path = null)
|
|
{
|
|
Manager = new DownloadManager();
|
|
|
|
if (!string.IsNullOrEmpty(logger_path))
|
|
{
|
|
GenericTextLogger.Init(logger_path);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns>Error string - null if successful</returns>
|
|
public static string TryLoadInstallPlan()
|
|
{
|
|
GenericTextLogger.PostImportantInfo("Starting Install Process");
|
|
|
|
if (!CheckConnection())
|
|
return "Download server is down";
|
|
|
|
try
|
|
{
|
|
Manager.GetUpdatePlan();
|
|
|
|
var response = Manager.UpdatePlanResponse;
|
|
|
|
if (response.Successful)
|
|
{
|
|
var client_version = response.CurrentPackage.Version;
|
|
|
|
GenericTextLogger.Post($"Anfrage wurde erfolgreich verarbeitet. Server gibt Version {client_version} vor.");
|
|
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return $"Server gibt Fehler zurück. Fehler: {response.Error}";
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return e.ToString();
|
|
}
|
|
}
|
|
|
|
public static string TryLoadUpdatePlan(out bool can_quick_start)
|
|
{
|
|
GenericTextLogger.PostImportantInfo($"Starting Update Process");
|
|
|
|
can_quick_start = false;
|
|
|
|
if (!CheckConnection())
|
|
{
|
|
if (DoesPlanerExeExists())
|
|
{
|
|
can_quick_start = true;
|
|
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return "Download server is down & quickstart not possible";
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
Manager.GetUpdatePlan();
|
|
|
|
var response = Manager.UpdatePlanResponse;
|
|
|
|
if (response.Successful)
|
|
{
|
|
var client_version = response.CurrentPackage.Version;
|
|
|
|
GenericTextLogger.Post($"Anfrage wurde erfolgreich verarbeitet. Server gibt Version {client_version} vor.");
|
|
|
|
if (!response.HasToDo())
|
|
can_quick_start = true;
|
|
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
return $"Server gibt Fehler zurück. Fehler: {response.Error}";
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return e.ToString();
|
|
}
|
|
}
|
|
|
|
public static string TryLoadUninstallPlan()
|
|
{
|
|
try
|
|
{
|
|
Manager.GetDeletePlan();
|
|
return null;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return e.ToString();
|
|
}
|
|
}
|
|
|
|
public static bool DoesPlanerExeExists()
|
|
{
|
|
try
|
|
{
|
|
return File.Exists(LauncherPaths.GetPlanerExePath());
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
}
|
|
|
|
public static string UpdateDiskSpace(string path)
|
|
{
|
|
FileInfo f = new FileInfo(path);
|
|
|
|
string drive = System.IO.Path.GetPathRoot(f.FullName);
|
|
FreeSpace = NonspecificTools.GetTotalFreeSpace(drive);
|
|
|
|
if (FreeSpace >= 0)
|
|
return NonspecificTools.SizeSuffix((ulong)FreeSpace);
|
|
else
|
|
return "error";
|
|
}
|
|
|
|
private static bool CheckConnection()
|
|
{
|
|
GenericTextLogger.Post("Checking download server connection...");
|
|
var check = Manager.CheckForConnection();
|
|
if (check)
|
|
GenericTextLogger.Post("Download server is up");
|
|
else
|
|
GenericTextLogger.Post("Download server is down");
|
|
|
|
return check;
|
|
}
|
|
}
|
|
}
|