From 8711bca159bc9cf0f43a1c1deb3de83647603f3e Mon Sep 17 00:00:00 2001
From: "waffle.lord" <waffle.lord@hotmail.com>
Date: Tue, 7 May 2024 14:19:03 -0400
Subject: [PATCH] add clear cache metadata button

---
 .../Dialogs/WhyCacheThoughDialog.axaml        | 20 +++++----
 .../Dialogs/WhyCacheThoughDialog.axaml.cs     | 42 +++++++++++++++++++
 SPTInstaller/SPTInstaller.csproj              |  4 +-
 3 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml b/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml
index fa63d81..bf70a76 100644
--- a/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml
+++ b/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml
@@ -6,7 +6,7 @@
              xmlns:dialogHost="clr-namespace:DialogHostAvalonia;assembly=DialogHost.Avalonia"
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="SPTInstaller.CustomControls.Dialogs.WhyCacheThoughDialog">
-    <Grid RowDefinitions="AUTO,AUTO,AUTO,*,AUTO" ColumnDefinitions="*,AUTO, AUTO"
+    <Grid RowDefinitions="AUTO,AUTO,AUTO,*,AUTO" ColumnDefinitions="*,AUTO"
           Background="{StaticResource AKI_Background_Light}">
         <Label Content="What is the installer cache for?" FontSize="20"
                Foreground="{StaticResource AKI_Brush_Yellow}" />
@@ -16,7 +16,6 @@ The installer cache is used to ensure you don't re-download large files that you
     - You are low on space
     or
     - You are not planning on installing SPT again any time soon
-
 If possible, you should leave the cache in place to avoid uneccessary, lengthy downloads.
 It also helps us prevent extra traffic to our limited download mirrors. Every bit helps <Span Foreground="red"
                 FontSize="25">♥️</Span>
@@ -25,7 +24,6 @@ It also helps us prevent extra traffic to our limited download mirrors. Every bi
         <Button Grid.Row="3" Grid.ColumnSpan="2"
                 Content="{Binding Source={x:Static helpers:DownloadCacheHelper.CachePath}}"
                 Classes="link"
-                Margin="0 10"
                 IsVisible="{Binding CacheExists, RelativeSource={RelativeSource AncestorType=UserControl}}"
                 Command="{Binding OpenCacheFolder, RelativeSource={RelativeSource AncestorType=UserControl}}" />
         <Label Grid.Row="3" Content="No cache folder exists"
@@ -34,11 +32,17 @@ It also helps us prevent extra traffic to our limited download mirrors. Every bi
         <Label Grid.Row="4"
                Content="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=AdditionalInfo}"
                Foreground="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=AdditionalInfoColor}" />
+        
+        <StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Spacing="10">
+            <Button Content="Move Downloaded Patcher"
+                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=MoveDownloadsPatcherToCache}" />
+            
+            <Button Content="Clear Metadata Cache" 
+                    Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ClearCachedMetaData}"
+                    />
 
-        <Button Grid.Row="4" Grid.Column="1" Content="Move Downloaded Patcher" Margin="0 0 10 0"
-                Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=MoveDownloadsPatcherToCache}" />
-
-        <Button Grid.Row="4" Grid.Column="2" Content="Close" Classes="yellow"
-                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=dialogHost:DialogHost}, Path=CloseDialogCommand}" />
+            <Button Content="Close" Classes="yellow"
+                    Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=dialogHost:DialogHost}, Path=CloseDialogCommand}" />
+        </StackPanel>
     </Grid>
 </UserControl>
\ No newline at end of file
diff --git a/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml.cs b/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml.cs
index 4440593..4bc4f94 100644
--- a/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml.cs
+++ b/SPTInstaller/CustomControls/Dialogs/WhyCacheThoughDialog.axaml.cs
@@ -3,7 +3,9 @@ using System.Diagnostics;
 using System.Linq;
 using Avalonia;
 using Avalonia.Controls;
+using Avalonia.Media;
 using Serilog;
+using Color = System.Drawing.Color;
 
 namespace SPTInstaller.CustomControls.Dialogs;
 
@@ -54,6 +56,46 @@ public partial class WhyCacheThoughDialog : UserControl
         });
     }
     
+    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);
+    }
+    
     public void MoveDownloadsPatcherToCache()
     {
         switch (_movePatcherState)
diff --git a/SPTInstaller/SPTInstaller.csproj b/SPTInstaller/SPTInstaller.csproj
index 40bd3d9..ca0fba1 100644
--- a/SPTInstaller/SPTInstaller.csproj
+++ b/SPTInstaller/SPTInstaller.csproj
@@ -9,8 +9,8 @@
         <PackageIcon>icon.ico</PackageIcon>
         <ApplicationIcon>Assets\icon.ico</ApplicationIcon>
         <Configurations>Debug;Release;TEST</Configurations>
-        <AssemblyVersion>2.64</AssemblyVersion>
-        <FileVersion>2.64</FileVersion>
+        <AssemblyVersion>2.66</AssemblyVersion>
+        <FileVersion>2.66</FileVersion>
         <Company>SPT-AKI</Company>
     </PropertyGroup>