XCITrimmerViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. using Avalonia.Collections;
  2. using DynamicData;
  3. using Gommon;
  4. using Avalonia.Threading;
  5. using Ryujinx.Ava.Common;
  6. using Ryujinx.Ava.Common.Locale;
  7. using Ryujinx.Ava.UI.Helpers;
  8. using Ryujinx.Common.Utilities;
  9. using Ryujinx.UI.App.Common;
  10. using Ryujinx.UI.Common.Models;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading;
  14. using static Ryujinx.Common.Utilities.XCIFileTrimmer;
  15. namespace Ryujinx.Ava.UI.ViewModels
  16. {
  17. public class XCITrimmerViewModel : BaseModel
  18. {
  19. private const long _bytesPerMB = 1024 * 1024;
  20. private enum ProcessingMode
  21. {
  22. Trimming,
  23. Untrimming
  24. }
  25. public enum SortField
  26. {
  27. Name,
  28. Saved
  29. }
  30. private const string _FileExtXCI = "XCI";
  31. private readonly Ryujinx.Common.Logging.XCIFileTrimmerLog _logger;
  32. private readonly ApplicationLibrary _applicationLibrary;
  33. private Optional<XCITrimmerFileModel> _processingApplication = null;
  34. private AvaloniaList<XCITrimmerFileModel> _allXCIFiles = new();
  35. private AvaloniaList<XCITrimmerFileModel> _selectedXCIFiles = new();
  36. private AvaloniaList<XCITrimmerFileModel> _displayedXCIFiles = new();
  37. private MainWindowViewModel _mainWindowViewModel;
  38. private CancellationTokenSource _cancellationTokenSource;
  39. private string _search;
  40. private ProcessingMode _processingMode;
  41. private SortField _sortField = SortField.Name;
  42. private bool _sortAscending = true;
  43. public XCITrimmerViewModel(MainWindowViewModel mainWindowViewModel)
  44. {
  45. _logger = new XCIFileTrimmerWindowLog(this);
  46. _mainWindowViewModel = mainWindowViewModel;
  47. _applicationLibrary = _mainWindowViewModel.ApplicationLibrary;
  48. LoadXCIApplications();
  49. }
  50. private void LoadXCIApplications()
  51. {
  52. var apps = _applicationLibrary.Applications.Items
  53. .Where(app => app.FileExtension == _FileExtXCI);
  54. foreach (var xciApp in apps)
  55. AddOrUpdateXCITrimmerFile(CreateXCITrimmerFile(xciApp.Path));
  56. ApplicationsChanged();
  57. }
  58. private XCITrimmerFileModel CreateXCITrimmerFile(
  59. string path,
  60. OperationOutcome operationOutcome = OperationOutcome.Undetermined)
  61. {
  62. var xciApp = _applicationLibrary.Applications.Items.First(app => app.FileExtension == _FileExtXCI && app.Path == path);
  63. return XCITrimmerFileModel.FromApplicationData(xciApp, _logger) with { ProcessingOutcome = operationOutcome };
  64. }
  65. private bool AddOrUpdateXCITrimmerFile(XCITrimmerFileModel xci, bool suppressChanged = true, bool autoSelect = true)
  66. {
  67. bool replaced = _allXCIFiles.ReplaceWith(xci);
  68. _displayedXCIFiles.ReplaceWith(xci, Filter(xci));
  69. _selectedXCIFiles.ReplaceWith(xci, xci.Trimmable && autoSelect);
  70. if (!suppressChanged)
  71. ApplicationsChanged();
  72. return replaced;
  73. }
  74. private void FilteringChanged()
  75. {
  76. OnPropertyChanged(nameof(Search));
  77. SortAndFilter();
  78. }
  79. private void SortingChanged()
  80. {
  81. OnPropertyChanged(nameof(IsSortedByName));
  82. OnPropertyChanged(nameof(IsSortedBySaved));
  83. OnPropertyChanged(nameof(SortingAscending));
  84. OnPropertyChanged(nameof(SortingField));
  85. OnPropertyChanged(nameof(SortingFieldName));
  86. SortAndFilter();
  87. }
  88. private void DisplayedChanged()
  89. {
  90. OnPropertyChanged(nameof(Status));
  91. OnPropertyChanged(nameof(DisplayedXCIFiles));
  92. OnPropertyChanged(nameof(SelectedDisplayedXCIFiles));
  93. }
  94. private void ApplicationsChanged()
  95. {
  96. OnPropertyChanged(nameof(AllXCIFiles));
  97. OnPropertyChanged(nameof(Status));
  98. OnPropertyChanged(nameof(PotentialSavings));
  99. OnPropertyChanged(nameof(ActualSavings));
  100. OnPropertyChanged(nameof(CanTrim));
  101. OnPropertyChanged(nameof(CanUntrim));
  102. DisplayedChanged();
  103. SortAndFilter();
  104. }
  105. private void SelectionChanged(bool displayedChanged = true)
  106. {
  107. OnPropertyChanged(nameof(Status));
  108. OnPropertyChanged(nameof(CanTrim));
  109. OnPropertyChanged(nameof(CanUntrim));
  110. OnPropertyChanged(nameof(SelectedXCIFiles));
  111. if (displayedChanged)
  112. OnPropertyChanged(nameof(SelectedDisplayedXCIFiles));
  113. }
  114. private void ProcessingChanged()
  115. {
  116. OnPropertyChanged(nameof(Processing));
  117. OnPropertyChanged(nameof(Cancel));
  118. OnPropertyChanged(nameof(Status));
  119. OnPropertyChanged(nameof(CanTrim));
  120. OnPropertyChanged(nameof(CanUntrim));
  121. }
  122. private IEnumerable<XCITrimmerFileModel> GetSelectedDisplayedXCIFiles()
  123. {
  124. return _displayedXCIFiles.Where(xci => _selectedXCIFiles.Contains(xci));
  125. }
  126. private void PerformOperation(ProcessingMode processingMode)
  127. {
  128. if (Processing)
  129. {
  130. return;
  131. }
  132. _processingMode = processingMode;
  133. Processing = true;
  134. var cancellationToken = _cancellationTokenSource.Token;
  135. Thread XCIFileTrimThread = new(() =>
  136. {
  137. var toProcess = Sort(SelectedXCIFiles
  138. .Where(xci =>
  139. (processingMode == ProcessingMode.Untrimming && xci.Untrimmable) ||
  140. (processingMode == ProcessingMode.Trimming && xci.Trimmable)
  141. )).ToList();
  142. var viewsSaved = DisplayedXCIFiles.ToList();
  143. Dispatcher.UIThread.Post(() =>
  144. {
  145. _selectedXCIFiles.Clear();
  146. _displayedXCIFiles.Clear();
  147. _displayedXCIFiles.AddRange(toProcess);
  148. });
  149. try
  150. {
  151. foreach (var xciApp in toProcess)
  152. {
  153. if (cancellationToken.IsCancellationRequested)
  154. break;
  155. var trimmer = new XCIFileTrimmer(xciApp.Path, _logger);
  156. Dispatcher.UIThread.Post(() =>
  157. {
  158. ProcessingApplication = xciApp;
  159. });
  160. var outcome = OperationOutcome.Undetermined;
  161. try
  162. {
  163. if (cancellationToken.IsCancellationRequested)
  164. break;
  165. switch (processingMode)
  166. {
  167. case ProcessingMode.Trimming:
  168. outcome = trimmer.Trim(cancellationToken);
  169. break;
  170. case ProcessingMode.Untrimming:
  171. outcome = trimmer.Untrim(cancellationToken);
  172. break;
  173. }
  174. if (outcome == OperationOutcome.Cancelled)
  175. outcome = OperationOutcome.Undetermined;
  176. }
  177. finally
  178. {
  179. Dispatcher.UIThread.Post(() =>
  180. {
  181. ProcessingApplication = CreateXCITrimmerFile(xciApp.Path);
  182. AddOrUpdateXCITrimmerFile(ProcessingApplication, false, false);
  183. ProcessingApplication = null;
  184. });
  185. }
  186. }
  187. }
  188. finally
  189. {
  190. Dispatcher.UIThread.Post(() =>
  191. {
  192. _displayedXCIFiles.AddOrReplaceMatching(_allXCIFiles, viewsSaved);
  193. _selectedXCIFiles.AddOrReplaceMatching(_allXCIFiles, toProcess);
  194. Processing = false;
  195. ApplicationsChanged();
  196. });
  197. }
  198. })
  199. {
  200. Name = "GUI.XCIFilesTrimmerThread",
  201. IsBackground = true,
  202. };
  203. XCIFileTrimThread.Start();
  204. }
  205. private bool Filter<T>(T arg)
  206. {
  207. if (arg is XCITrimmerFileModel content)
  208. {
  209. return string.IsNullOrWhiteSpace(_search)
  210. || content.Name.ToLower().Contains(_search.ToLower())
  211. || content.Path.ToLower().Contains(_search.ToLower());
  212. }
  213. return false;
  214. }
  215. private class CompareXCITrimmerFiles : IComparer<XCITrimmerFileModel>
  216. {
  217. private XCITrimmerViewModel _viewModel;
  218. public CompareXCITrimmerFiles(XCITrimmerViewModel ViewModel)
  219. {
  220. _viewModel = ViewModel;
  221. }
  222. public int Compare(XCITrimmerFileModel x, XCITrimmerFileModel y)
  223. {
  224. int result = 0;
  225. switch (_viewModel.SortingField)
  226. {
  227. case SortField.Name:
  228. result = x.Name.CompareTo(y.Name);
  229. break;
  230. case SortField.Saved:
  231. result = x.PotentialSavingsB.CompareTo(y.PotentialSavingsB);
  232. break;
  233. }
  234. if (!_viewModel.SortingAscending)
  235. result = -result;
  236. if (result == 0)
  237. result = x.Path.CompareTo(y.Path);
  238. return result;
  239. }
  240. }
  241. private IOrderedEnumerable<XCITrimmerFileModel> Sort(IEnumerable<XCITrimmerFileModel> list)
  242. {
  243. return list
  244. .OrderBy(xci => xci, new CompareXCITrimmerFiles(this))
  245. .ThenBy(it => it.Path);
  246. }
  247. public void TrimSelected()
  248. {
  249. PerformOperation(ProcessingMode.Trimming);
  250. }
  251. public void UntrimSelected()
  252. {
  253. PerformOperation(ProcessingMode.Untrimming);
  254. }
  255. public void SetProgress(int current, int maximum)
  256. {
  257. if (_processingApplication != null)
  258. {
  259. int percentageProgress = 100 * current / maximum;
  260. if (!ProcessingApplication.HasValue || (ProcessingApplication.Value.PercentageProgress != percentageProgress))
  261. ProcessingApplication = ProcessingApplication.Value with { PercentageProgress = percentageProgress };
  262. }
  263. }
  264. public void SelectDisplayed()
  265. {
  266. SelectedXCIFiles.AddRange(DisplayedXCIFiles);
  267. SelectionChanged();
  268. }
  269. public void DeselectDisplayed()
  270. {
  271. SelectedXCIFiles.RemoveMany(DisplayedXCIFiles);
  272. SelectionChanged();
  273. }
  274. public void Select(XCITrimmerFileModel model)
  275. {
  276. bool selectionChanged = !SelectedXCIFiles.Contains(model);
  277. bool displayedSelectionChanged = !SelectedDisplayedXCIFiles.Contains(model);
  278. SelectedXCIFiles.ReplaceOrAdd(model, model);
  279. if (selectionChanged)
  280. SelectionChanged(displayedSelectionChanged);
  281. }
  282. public void Deselect(XCITrimmerFileModel model)
  283. {
  284. bool displayedSelectionChanged = !SelectedDisplayedXCIFiles.Contains(model);
  285. if (SelectedXCIFiles.Remove(model))
  286. SelectionChanged(displayedSelectionChanged);
  287. }
  288. public void SortAndFilter()
  289. {
  290. if (Processing)
  291. return;
  292. Sort(AllXCIFiles)
  293. .AsObservableChangeSet()
  294. .Filter(Filter)
  295. .Bind(out var view).AsObservableList();
  296. _displayedXCIFiles.Clear();
  297. _displayedXCIFiles.AddRange(view);
  298. DisplayedChanged();
  299. }
  300. public Optional<XCITrimmerFileModel> ProcessingApplication
  301. {
  302. get => _processingApplication;
  303. set
  304. {
  305. if (!value.HasValue && _processingApplication.HasValue)
  306. value = _processingApplication.Value with { PercentageProgress = null };
  307. if (value.HasValue)
  308. _displayedXCIFiles.ReplaceWith(value.Value);
  309. _processingApplication = value;
  310. OnPropertyChanged();
  311. }
  312. }
  313. public bool Processing
  314. {
  315. get => _cancellationTokenSource != null;
  316. private set
  317. {
  318. if (value && !Processing)
  319. {
  320. _cancellationTokenSource = new CancellationTokenSource();
  321. }
  322. else if (!value && Processing)
  323. {
  324. _cancellationTokenSource.Dispose();
  325. _cancellationTokenSource = null;
  326. }
  327. ProcessingChanged();
  328. }
  329. }
  330. public bool Cancel
  331. {
  332. get => _cancellationTokenSource != null && _cancellationTokenSource.IsCancellationRequested;
  333. set
  334. {
  335. if (value)
  336. {
  337. if (!Processing)
  338. return;
  339. _cancellationTokenSource.Cancel();
  340. }
  341. ProcessingChanged();
  342. }
  343. }
  344. public string Status
  345. {
  346. get
  347. {
  348. if (Processing)
  349. {
  350. return _processingMode switch
  351. {
  352. ProcessingMode.Trimming => string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerTitleStatusTrimming], DisplayedXCIFiles.Count),
  353. ProcessingMode.Untrimming => string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerTitleStatusUntrimming], DisplayedXCIFiles.Count),
  354. _ => string.Empty
  355. };
  356. }
  357. else
  358. {
  359. return string.IsNullOrEmpty(Search) ?
  360. string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerTitleStatusCount], SelectedXCIFiles.Count, AllXCIFiles.Count) :
  361. string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerTitleStatusCountWithFilter], SelectedXCIFiles.Count, AllXCIFiles.Count, DisplayedXCIFiles.Count);
  362. }
  363. }
  364. }
  365. public string Search
  366. {
  367. get => _search;
  368. set
  369. {
  370. _search = value;
  371. FilteringChanged();
  372. }
  373. }
  374. public SortField SortingField
  375. {
  376. get => _sortField;
  377. set
  378. {
  379. _sortField = value;
  380. SortingChanged();
  381. }
  382. }
  383. public string SortingFieldName
  384. {
  385. get
  386. {
  387. return SortingField switch
  388. {
  389. SortField.Name => LocaleManager.Instance[LocaleKeys.XCITrimmerSortName],
  390. SortField.Saved => LocaleManager.Instance[LocaleKeys.XCITrimmerSortSaved],
  391. _ => string.Empty,
  392. };
  393. }
  394. }
  395. public bool SortingAscending
  396. {
  397. get => _sortAscending;
  398. set
  399. {
  400. _sortAscending = value;
  401. SortingChanged();
  402. }
  403. }
  404. public bool IsSortedByName
  405. {
  406. get => _sortField == SortField.Name;
  407. }
  408. public bool IsSortedBySaved
  409. {
  410. get => _sortField == SortField.Saved;
  411. }
  412. public AvaloniaList<XCITrimmerFileModel> SelectedXCIFiles
  413. {
  414. get => _selectedXCIFiles;
  415. set
  416. {
  417. _selectedXCIFiles = value;
  418. SelectionChanged();
  419. }
  420. }
  421. public AvaloniaList<XCITrimmerFileModel> AllXCIFiles
  422. {
  423. get => _allXCIFiles;
  424. }
  425. public AvaloniaList<XCITrimmerFileModel> DisplayedXCIFiles
  426. {
  427. get => _displayedXCIFiles;
  428. }
  429. public string PotentialSavings
  430. {
  431. get
  432. {
  433. return string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerSavingsMb], AllXCIFiles.Sum(xci => xci.PotentialSavingsB / _bytesPerMB));
  434. }
  435. }
  436. public string ActualSavings
  437. {
  438. get
  439. {
  440. return string.Format(LocaleManager.Instance[LocaleKeys.XCITrimmerSavingsMb], AllXCIFiles.Sum(xci => xci.CurrentSavingsB / _bytesPerMB));
  441. }
  442. }
  443. public IEnumerable<XCITrimmerFileModel> SelectedDisplayedXCIFiles
  444. {
  445. get
  446. {
  447. return GetSelectedDisplayedXCIFiles().ToList();
  448. }
  449. }
  450. public bool CanTrim
  451. {
  452. get
  453. {
  454. return !Processing && _selectedXCIFiles.Any(xci => xci.Trimmable);
  455. }
  456. }
  457. public bool CanUntrim
  458. {
  459. get
  460. {
  461. return !Processing && _selectedXCIFiles.Any(xci => xci.Untrimmable);
  462. }
  463. }
  464. }
  465. }