-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
308 lines (263 loc) · 11.8 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using AsaModCleaner.Models;
using AsaModCleaner.Services;
using Microsoft.Extensions.Logging;
namespace AsaModCleaner
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
private readonly GameService _gameService;
private readonly ILogger<MainWindow> _logger;
private readonly Queue<InstalledMod> _modsToCleanQueue = new();
private string? _selectedSortCriterion = "InstallDate"; // Default criterion
private ListSortDirection _selectedSortDirection = ListSortDirection.Ascending; // Default direction
private ObservableCollection<InstalledMod> InstalledMods { get; set; } = [];
private readonly ISettingsService _settingsService;
public MainWindow(GameService gameService, ISettingsService settingsService, ILogger<MainWindow> logger)
{
_gameService = gameService;
_logger = logger;
_settingsService = settingsService;
InitializeComponent();
// Subscribe to the Loaded event
Loaded += MainWindow_Loaded;
}
private void EnableButtons(bool enabled)
{
SelectAllButton.IsEnabled = enabled;
RefreshButton.IsEnabled = enabled;
CleanButton.IsEnabled = enabled;
}
// Loaded Event Handler
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
ApplyWindowSettings();
EnableButtons(false);
// Run the initialization logic in a separate thread to avoid blocking the UI
var success = await Task.Run(() => _gameService.Initialize());
if (success)
{
// Proceed with the follow-up task if initialization was successful
LoadModList();
EnableButtons(true);
}
else
{
// Handle the scenario where initialization failed
MessageBox.Show("Initialization failed. Please ensure Steam is running and ARK is installed.", "Initialization Error", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred during initialization: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ApplyWindowSettings()
{
var settings = _settingsService.LoadWindowSettings();
// Apply position and size if not maximized
if (settings.IsMaximized)
{
WindowState = WindowState.Maximized;
}
else
{
Left = settings.Left;
Top = settings.Top;
Width = settings.Width;
Height = settings.Height;
}
}
// Refresh Button Click Event Handler
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
LoadModList();
ScrollToTop(ModList);
}
private void SelectAllButton_Click(object sender, RoutedEventArgs e)
{
foreach (var mod in InstalledMods)
{
mod.IsSelected = true; // Set each mod's IsSelected property to true
}
}
// Clean Button Click Event Handler
private void CleanButton_Click(object sender, RoutedEventArgs e)
{
// Get all selected mods from the InstalledMods collection
var selectedMods = InstalledMods.Where(mod => mod.IsSelected).ToList();
if (selectedMods.Count == 0)
{
MessageBox.Show("Please select at least one mod to clean.");
return;
}
// Add selected mods to the cleaning queue
foreach (var mod in selectedMods)
{
_modsToCleanQueue.Enqueue(mod);
}
// Start processing the cleaning queue
ProcessCleaningQueue();
}
private async void ProcessCleaningQueue()
{
try
{
// Show the progress bar and reset its value
CleaningProgressBar.Visibility = Visibility.Visible;
CleaningProgressBar.Minimum = 0;
CleaningProgressBar.Maximum = _modsToCleanQueue.Count;
CleaningProgressBar.Value = 0;
var totalMods = _modsToCleanQueue.Count;
var processedMods = 0;
var installDir = _gameService.GetArkInstallDir();
var modPath = _gameService.GetModsPath(installDir!);
while (_modsToCleanQueue.Count > 0)
{
// Dequeue the next mod from the queue
var modToClean = _modsToCleanQueue.Dequeue();
try
{
// Find the folder corresponding to PathOnDisk in modPath recursively
if (!string.IsNullOrWhiteSpace(modToClean.PathOnDisk) && Directory.Exists(modPath))
{
var folderToDelete = FindFolderRecursively(modPath, modToClean.PathOnDisk);
if (!string.IsNullOrEmpty(folderToDelete) && Directory.Exists(folderToDelete))
{
// Delete the folder and its contents
Directory.Delete(folderToDelete, true);
}
}
// Remove the mod from the ObservableCollection
InstalledMods.Remove(modToClean);
// Save changes to the library
var changedLibrary = new Library { InstalledMods = InstalledMods.ToList() };
_gameService.SaveModLibraryChanges(changedLibrary, installDir!);
// Update the progress bar
processedMods++;
CleaningProgressBar.Value = processedMods;
// Optional: Update status or show feedback
StatusLabel.Text =
$"{modToClean.Details?.Name} cleaned successfully. ({processedMods}/{totalMods})";
}
catch (Exception ex)
{
_logger.LogError($"Failed to delete {modToClean.Details?.Name}: {ex.Message}");
}
// Adding a small delay to visually represent progress
await Task.Delay(100); // Simulate delay to show progress, adjust or remove in real implementation
}
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while processing the cleaning queue: {ex.Message}");
MessageBox.Show($"An error occurred while processing the queue: {ex.Message}", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
// Once the queue is processed, hide the progress bar
CleaningProgressBar.Visibility = Visibility.Collapsed;
StatusLabel.Text = "Cleaning process completed.";
}
}
private string FindFolderRecursively(string rootPath, string targetPath)
{
try
{
return Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories)
.FirstOrDefault(dir => dir.Equals(targetPath, StringComparison.OrdinalIgnoreCase)) ?? string.Empty;
}
catch (Exception ex)
{
_logger.LogError($"Error while searching for folder: {ex.Message}");
return string.Empty;
}
}
private void LoadModList()
{
var installDir = _gameService.GetArkInstallDir();
if (string.IsNullOrWhiteSpace(installDir)) return;
var modLibrary = _gameService.DeserializeModLibrary(installDir);
if (modLibrary?.InstalledMods == null) return;
InstalledMods.Clear();
foreach (var mod in modLibrary.InstalledMods)
{
mod.IsSelected = false; // Reset selections
InstalledMods.Add(mod);
}
ModList.ItemsSource = InstalledMods; // Bind ObservableCollection
StatusLabel.Text = $"{InstalledMods.Count} mods loaded.";
}
private static void ScrollToTop(ListView listView)
{
if (VisualTreeHelper.GetChildrenCount(listView) <= 0) return;
var border = (Border?)VisualTreeHelper.GetChild(listView, 0);
if (border is null || VisualTreeHelper.GetChildrenCount(border) <= 0) return;
var scrollViewer = (ScrollViewer?)VisualTreeHelper.GetChild(border, 0);
scrollViewer?.ScrollToTop(); // Ensure the scrollbar starts at the top
}
private void SortCriteriaDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SortCriteriaDropdown.SelectedItem is not ComboBoxItem selectedItem) return;
_selectedSortCriterion = selectedItem.Tag.ToString();
ApplySorting();
}
private void SortDirectionDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SortDirectionDropdown.SelectedItem is not ComboBoxItem selectedItem) return;
_selectedSortDirection = selectedItem.Tag.ToString() == "Ascending"
? ListSortDirection.Ascending
: ListSortDirection.Descending;
ApplySorting();
}
private void ApplySorting()
{
var collectionView = (CollectionView)CollectionViewSource.GetDefaultView(InstalledMods);
collectionView.SortDescriptions.Clear();
// Apply sorting based on selected criterion and direction
switch (_selectedSortCriterion)
{
case "InstallDate":
collectionView.SortDescriptions.Add(new SortDescription(nameof(InstalledMod.DateInstalled), _selectedSortDirection));
break;
case "Name":
collectionView.SortDescriptions.Add(new SortDescription("Details.Name", _selectedSortDirection));
break;
case "Free":
collectionView.SortDescriptions.Add(new SortDescription(nameof(InstalledMod.Details.PremiumDetails.IsPremium), ListSortDirection.Ascending));
break;
case "Premium":
collectionView.SortDescriptions.Add(new SortDescription(nameof(InstalledMod.Details.PremiumDetails.IsPremium), ListSortDirection.Descending));
break;
case "Author":
collectionView.SortDescriptions.Add(new SortDescription("Details.Authors[0].Name", _selectedSortDirection));
break;
}
}
// Save window settings when the application closes
private void Window_Closing(object sender, CancelEventArgs e)
{
var settings = new WindowSettings
{
Left = Left,
Top = Top,
Width = Width,
Height = Height,
IsMaximized = WindowState == WindowState.Maximized
};
_settingsService.SaveWindowSettings(settings);
}
}
}