SPT-AKI-Installer/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml.cs

161 lines
5.3 KiB
C#
Raw Normal View History

2024-03-23 17:45:41 -04:00
using SPTInstaller.Helpers;
2023-08-25 23:46:11 -04:00
using System.Diagnostics;
2024-03-23 17:45:41 -04:00
using System.Linq;
using Avalonia;
using Avalonia.Controls;
2024-05-07 14:19:03 -04:00
using Avalonia.Media;
2024-03-23 17:45:41 -04:00
using Serilog;
2024-05-07 14:19:03 -04:00
using Color = System.Drawing.Color;
2023-08-25 23:46:11 -04:00
namespace SPTInstaller.CustomControls.Dialogs;
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
public partial class WhyCacheThoughDialog : UserControl
{
2024-03-23 17:45:41 -04:00
private int _movePatcherState = 0;
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
private FileInfo? _foundPatcher;
2023-08-25 23:46:11 -04:00
public WhyCacheThoughDialog()
{
InitializeComponent();
}
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
public static readonly StyledProperty<string> AdditionalInfoProperty =
AvaloniaProperty.Register<WhyCacheThoughDialog, string>(nameof(AdditionalInfo));
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
public string AdditionalInfo
{
get => GetValue(AdditionalInfoProperty);
set => SetValue(AdditionalInfoProperty, value);
}
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
public static readonly StyledProperty<string> AdditionalInfoColorProperty =
AvaloniaProperty.Register<WhyCacheThoughDialog, string>(nameof(AdditionalInfoColor));
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
public string AdditionalInfoColor
{
get => GetValue(AdditionalInfoColorProperty);
set => SetValue(AdditionalInfoColorProperty, value);
}
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
public bool CacheExists => Directory.Exists(DownloadCacheHelper.CachePath);
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
public void OpenCacheFolder()
{
if (!CacheExists)
return;
2024-05-01 10:31:55 -04:00
2023-08-25 23:46:11 -04:00
Process.Start(new ProcessStartInfo()
{
2024-05-01 10:31:55 -04:00
FileName = Path.EndsInDirectorySeparator(DownloadCacheHelper.CachePath)
? DownloadCacheHelper.CachePath
: DownloadCacheHelper.CachePath + Path.DirectorySeparatorChar,
2023-08-25 23:46:11 -04:00
UseShellExecute = true,
Verb = "open"
});
}
2024-05-01 10:31:55 -04:00
2024-05-07 14:19:03 -04:00
public void ClearCachedMetaData()
{
var cachedMetadata =
new DirectoryInfo(DownloadCacheHelper.CachePath).GetFiles("*.json", SearchOption.TopDirectoryOnly);
var message = "no cached metadata to remove";
if (cachedMetadata.Length == 0)
{
AdditionalInfo = message;
AdditionalInfoColor = "dodgerblue";
Log.Information(message);
return;
}
var allDeleted = true;
foreach (var file in cachedMetadata)
{
try
{
file.Delete();
file.Refresh();
if (file.Exists)
{
allDeleted = false;
}
}
catch (Exception ex)
{
Log.Error(ex, $"Failed to delete cached metadata file: {file.Name}");
}
}
message = allDeleted ? "cached metadata removed" : "some files could not be removed. Check logs";
AdditionalInfo = message;
AdditionalInfoColor = allDeleted ? "green" : "red";
Log.Information(message);
}
2024-03-23 17:45:41 -04:00
public void MoveDownloadsPatcherToCache()
{
switch (_movePatcherState)
{
case 0:
var downloadsPath =
Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
var downloadsFolder = new DirectoryInfo(downloadsPath);
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
if (!downloadsFolder.Exists)
{
2024-03-25 14:40:29 -04:00
var message = "Could not get downloads folder :(";
Log.Error($"[MV_0] {message}");
AdditionalInfo = message;
2024-03-23 17:45:41 -04:00
AdditionalInfoColor = "red";
_movePatcherState = -1;
return;
}
2024-05-01 10:31:55 -04:00
_foundPatcher = downloadsFolder.GetFiles("Patcher_*").OrderByDescending(p => p.CreationTime)
.FirstOrDefault();
2024-03-23 17:45:41 -04:00
if (_foundPatcher == null || !_foundPatcher.Exists)
{
2024-03-25 14:40:29 -04:00
var message = "Could not find a patcher file in your downloads folder";
Log.Warning($"[MV_0] {message}");
AdditionalInfo = message;
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
AdditionalInfoColor = "red";
return;
}
2024-05-01 10:31:55 -04:00
2024-03-25 14:40:29 -04:00
Log.Information($"[MV_0] Found patcher for move: {_foundPatcher.Name}");
2024-05-01 10:31:55 -04:00
AdditionalInfo =
$"Click again to move the below patcher file to the cache folder\n{_foundPatcher?.Name ?? "-SOMETHING WENT WRONG-"}";
2024-03-23 17:45:41 -04:00
AdditionalInfoColor = "#FFC107";
_movePatcherState = 1;
break;
case 1:
try
{
var cacheFilePath = Path.Join(DownloadCacheHelper.CachePath, "patcher");
_foundPatcher?.MoveTo(cacheFilePath, true);
2024-03-25 14:40:29 -04:00
var message = "Patcher was moved into cache :D";
Log.Information($"[MV_1] {message}");
AdditionalInfo = message;
2024-03-23 17:45:41 -04:00
AdditionalInfoColor = "ForestGreen";
}
catch (Exception ex)
{
AdditionalInfo = "Something went wrong :(";
AdditionalInfoColor = "red";
Log.Error(ex, "Failed to move downloaded patcher file into cache");
}
2024-05-01 10:31:55 -04:00
2024-03-23 17:45:41 -04:00
break;
2024-03-25 14:40:29 -04:00
default:
Log.Error("[MV_ ] Move state is broken :(");
break;
2024-03-23 17:45:41 -04:00
}
}
2024-05-01 10:31:55 -04:00
}