MainWindowViewModel.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Media;
  5. using Avalonia.Threading;
  6. using DynamicData;
  7. using DynamicData.Binding;
  8. using LibHac.Fs;
  9. using LibHac.FsSystem;
  10. using LibHac.Ncm;
  11. using Ryujinx.Ava.Common;
  12. using Ryujinx.Ava.Common.Locale;
  13. using Ryujinx.Ava.Input;
  14. using Ryujinx.Ava.UI.Controls;
  15. using Ryujinx.Ava.UI.Helpers;
  16. using Ryujinx.Ava.UI.Windows;
  17. using Ryujinx.Common;
  18. using Ryujinx.Common.Configuration;
  19. using Ryujinx.Common.Logging;
  20. using Ryujinx.Cpu;
  21. using Ryujinx.HLE;
  22. using Ryujinx.HLE.FileSystem;
  23. using Ryujinx.HLE.HOS;
  24. using Ryujinx.Modules;
  25. using Ryujinx.Ui.App.Common;
  26. using Ryujinx.Ui.Common;
  27. using Ryujinx.Ui.Common.Configuration;
  28. using Ryujinx.Ui.Common.Helper;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Threading;
  35. using System.Threading.Tasks;
  36. using Path = System.IO.Path;
  37. using ShaderCacheLoadingState = Ryujinx.Graphics.Gpu.Shader.ShaderCacheState;
  38. namespace Ryujinx.Ava.UI.ViewModels
  39. {
  40. internal class MainWindowViewModel : BaseModel
  41. {
  42. private const int HotKeyPressDelayMs = 500;
  43. private readonly MainWindow _owner;
  44. private ObservableCollection<ApplicationData> _applications;
  45. private string _aspectStatusText;
  46. private string _loadHeading;
  47. private string _cacheLoadStatus;
  48. private string _searchText;
  49. private Timer _searchTimer;
  50. private string _dockedStatusText;
  51. private string _fifoStatusText;
  52. private string _gameStatusText;
  53. private string _volumeStatusText;
  54. private string _gpuStatusText;
  55. private bool _isAmiiboRequested;
  56. private bool _isGameRunning;
  57. private bool _isLoading;
  58. private int _progressMaximum;
  59. private int _progressValue;
  60. private long _lastFullscreenToggle = Environment.TickCount64;
  61. private bool _showLoadProgress;
  62. private bool _showMenuAndStatusBar = true;
  63. private bool _showStatusSeparator;
  64. private Brush _progressBarForegroundColor;
  65. private Brush _progressBarBackgroundColor;
  66. private Brush _vsyncColor;
  67. private byte[] _selectedIcon;
  68. private bool _isAppletMenuActive;
  69. private int _statusBarProgressMaximum;
  70. private int _statusBarProgressValue;
  71. private bool _isPaused;
  72. private bool _showContent = true;
  73. private bool _isLoadingIndeterminate = true;
  74. private bool _showAll;
  75. private string _lastScannedAmiiboId;
  76. private ReadOnlyObservableCollection<ApplicationData> _appsObservableList;
  77. public ApplicationLibrary ApplicationLibrary => _owner.ApplicationLibrary;
  78. public string TitleName { get; internal set; }
  79. public MainWindowViewModel(MainWindow owner) : this()
  80. {
  81. _owner = owner;
  82. }
  83. public MainWindowViewModel()
  84. {
  85. Applications = new ObservableCollection<ApplicationData>();
  86. Applications.ToObservableChangeSet()
  87. .Filter(Filter)
  88. .Sort(GetComparer())
  89. .Bind(out _appsObservableList).AsObservableList();
  90. if (Program.PreviewerDetached)
  91. {
  92. LoadConfigurableHotKeys();
  93. Volume = ConfigurationState.Instance.System.AudioVolume;
  94. }
  95. }
  96. public void Initialize()
  97. {
  98. ApplicationLibrary.ApplicationCountUpdated += ApplicationLibrary_ApplicationCountUpdated;
  99. ApplicationLibrary.ApplicationAdded += ApplicationLibrary_ApplicationAdded;
  100. }
  101. public string SearchText
  102. {
  103. get => _searchText;
  104. set
  105. {
  106. _searchText = value;
  107. _searchTimer?.Dispose();
  108. _searchTimer = new Timer(TimerCallback, null, 1000, 0);
  109. }
  110. }
  111. private void TimerCallback(object obj)
  112. {
  113. RefreshView();
  114. _searchTimer.Dispose();
  115. _searchTimer = null;
  116. }
  117. public ReadOnlyObservableCollection<ApplicationData> AppsObservableList
  118. {
  119. get => _appsObservableList;
  120. set
  121. {
  122. _appsObservableList = value;
  123. OnPropertyChanged();
  124. }
  125. }
  126. public bool IsPaused
  127. {
  128. get => _isPaused;
  129. set
  130. {
  131. _isPaused = value;
  132. OnPropertyChanged();
  133. }
  134. }
  135. public bool EnableNonGameRunningControls => !IsGameRunning;
  136. public bool ShowFirmwareStatus => !ShowLoadProgress;
  137. public bool IsGameRunning
  138. {
  139. get => _isGameRunning;
  140. set
  141. {
  142. _isGameRunning = value;
  143. if (!value)
  144. {
  145. ShowMenuAndStatusBar = false;
  146. }
  147. OnPropertyChanged();
  148. OnPropertyChanged(nameof(EnableNonGameRunningControls));
  149. OnPropertyChanged(nameof(ShowFirmwareStatus));
  150. }
  151. }
  152. public bool IsAmiiboRequested
  153. {
  154. get => _isAmiiboRequested && _isGameRunning;
  155. set
  156. {
  157. _isAmiiboRequested = value;
  158. OnPropertyChanged();
  159. }
  160. }
  161. public bool ShowLoadProgress
  162. {
  163. get => _showLoadProgress;
  164. set
  165. {
  166. _showLoadProgress = value;
  167. OnPropertyChanged();
  168. OnPropertyChanged(nameof(ShowFirmwareStatus));
  169. }
  170. }
  171. public string GameStatusText
  172. {
  173. get => _gameStatusText;
  174. set
  175. {
  176. _gameStatusText = value;
  177. OnPropertyChanged();
  178. }
  179. }
  180. private string _showUikey = "F4";
  181. private string _pauseKey = "F5";
  182. private string _screenshotkey = "F8";
  183. private float _volume;
  184. private string _backendText;
  185. public ApplicationData SelectedApplication
  186. {
  187. get
  188. {
  189. return Glyph switch
  190. {
  191. Glyph.List => _owner.GameList.SelectedApplication,
  192. Glyph.Grid => _owner.GameGrid.SelectedApplication,
  193. _ => null,
  194. };
  195. }
  196. }
  197. public string LoadHeading
  198. {
  199. get => _loadHeading;
  200. set
  201. {
  202. _loadHeading = value;
  203. OnPropertyChanged();
  204. }
  205. }
  206. public string CacheLoadStatus
  207. {
  208. get => _cacheLoadStatus;
  209. set
  210. {
  211. _cacheLoadStatus = value;
  212. OnPropertyChanged();
  213. }
  214. }
  215. public Brush ProgressBarBackgroundColor
  216. {
  217. get => _progressBarBackgroundColor;
  218. set
  219. {
  220. _progressBarBackgroundColor = value;
  221. OnPropertyChanged();
  222. }
  223. }
  224. public Brush ProgressBarForegroundColor
  225. {
  226. get => _progressBarForegroundColor;
  227. set
  228. {
  229. _progressBarForegroundColor = value;
  230. OnPropertyChanged();
  231. }
  232. }
  233. public Brush VsyncColor
  234. {
  235. get => _vsyncColor;
  236. set
  237. {
  238. _vsyncColor = value;
  239. OnPropertyChanged();
  240. }
  241. }
  242. public byte[] SelectedIcon
  243. {
  244. get => _selectedIcon;
  245. set
  246. {
  247. _selectedIcon = value;
  248. OnPropertyChanged();
  249. }
  250. }
  251. public int ProgressMaximum
  252. {
  253. get => _progressMaximum;
  254. set
  255. {
  256. _progressMaximum = value;
  257. OnPropertyChanged();
  258. }
  259. }
  260. public int ProgressValue
  261. {
  262. get => _progressValue;
  263. set
  264. {
  265. _progressValue = value;
  266. OnPropertyChanged();
  267. }
  268. }
  269. public int StatusBarProgressMaximum
  270. {
  271. get => _statusBarProgressMaximum;
  272. set
  273. {
  274. _statusBarProgressMaximum = value;
  275. OnPropertyChanged();
  276. }
  277. }
  278. public int StatusBarProgressValue
  279. {
  280. get => _statusBarProgressValue;
  281. set
  282. {
  283. _statusBarProgressValue = value;
  284. OnPropertyChanged();
  285. }
  286. }
  287. public string FifoStatusText
  288. {
  289. get => _fifoStatusText;
  290. set
  291. {
  292. _fifoStatusText = value;
  293. OnPropertyChanged();
  294. }
  295. }
  296. public string GpuNameText
  297. {
  298. get => _gpuStatusText;
  299. set
  300. {
  301. _gpuStatusText = value;
  302. OnPropertyChanged();
  303. }
  304. }
  305. public string BackendText
  306. {
  307. get => _backendText;
  308. set
  309. {
  310. _backendText = value;
  311. OnPropertyChanged();
  312. }
  313. }
  314. public string DockedStatusText
  315. {
  316. get => _dockedStatusText;
  317. set
  318. {
  319. _dockedStatusText = value;
  320. OnPropertyChanged();
  321. }
  322. }
  323. public string AspectRatioStatusText
  324. {
  325. get => _aspectStatusText;
  326. set
  327. {
  328. _aspectStatusText = value;
  329. OnPropertyChanged();
  330. }
  331. }
  332. public string VolumeStatusText
  333. {
  334. get => _volumeStatusText;
  335. set
  336. {
  337. _volumeStatusText = value;
  338. OnPropertyChanged();
  339. }
  340. }
  341. public bool VolumeMuted => _volume == 0;
  342. public float Volume
  343. {
  344. get => _volume;
  345. set
  346. {
  347. _volume = value;
  348. if (_isGameRunning)
  349. {
  350. _owner.AppHost.Device.SetVolume(_volume);
  351. }
  352. OnPropertyChanged(nameof(VolumeStatusText));
  353. OnPropertyChanged(nameof(VolumeMuted));
  354. OnPropertyChanged();
  355. }
  356. }
  357. public bool ShowStatusSeparator
  358. {
  359. get => _showStatusSeparator;
  360. set
  361. {
  362. _showStatusSeparator = value;
  363. OnPropertyChanged();
  364. }
  365. }
  366. public bool ShowMenuAndStatusBar
  367. {
  368. get => _showMenuAndStatusBar;
  369. set
  370. {
  371. _showMenuAndStatusBar = value;
  372. OnPropertyChanged();
  373. }
  374. }
  375. public bool IsLoadingIndeterminate
  376. {
  377. get => _isLoadingIndeterminate;
  378. set
  379. {
  380. _isLoadingIndeterminate = value;
  381. OnPropertyChanged();
  382. }
  383. }
  384. public bool ShowContent
  385. {
  386. get => _showContent;
  387. set
  388. {
  389. _showContent = value;
  390. OnPropertyChanged();
  391. }
  392. }
  393. public bool IsAppletMenuActive
  394. {
  395. get => _isAppletMenuActive && EnableNonGameRunningControls;
  396. set
  397. {
  398. _isAppletMenuActive = value;
  399. OnPropertyChanged();
  400. }
  401. }
  402. public bool IsGrid => Glyph == Glyph.Grid;
  403. public bool IsList => Glyph == Glyph.List;
  404. internal void Sort(bool isAscending)
  405. {
  406. IsAscending = isAscending;
  407. RefreshView();
  408. }
  409. internal void Sort(ApplicationSort sort)
  410. {
  411. SortMode = sort;
  412. RefreshView();
  413. }
  414. private IComparer<ApplicationData> GetComparer()
  415. {
  416. return SortMode switch
  417. {
  418. ApplicationSort.LastPlayed => new Models.Generic.LastPlayedSortComparer(IsAscending),
  419. ApplicationSort.FileSize => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.FileSizeBytes)
  420. : SortExpressionComparer<ApplicationData>.Descending(app => app.FileSizeBytes),
  421. ApplicationSort.TotalTimePlayed => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.TimePlayedNum)
  422. : SortExpressionComparer<ApplicationData>.Descending(app => app.TimePlayedNum),
  423. ApplicationSort.Title => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.TitleName)
  424. : SortExpressionComparer<ApplicationData>.Descending(app => app.TitleName),
  425. ApplicationSort.Favorite => !IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Favorite)
  426. : SortExpressionComparer<ApplicationData>.Descending(app => app.Favorite),
  427. ApplicationSort.Developer => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Developer)
  428. : SortExpressionComparer<ApplicationData>.Descending(app => app.Developer),
  429. ApplicationSort.FileType => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.FileExtension)
  430. : SortExpressionComparer<ApplicationData>.Descending(app => app.FileExtension),
  431. ApplicationSort.Path => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Path)
  432. : SortExpressionComparer<ApplicationData>.Descending(app => app.Path),
  433. _ => null,
  434. };
  435. }
  436. private void RefreshView()
  437. {
  438. RefreshGrid();
  439. }
  440. private void RefreshGrid()
  441. {
  442. Applications.ToObservableChangeSet()
  443. .Filter(Filter)
  444. .Sort(GetComparer())
  445. .Bind(out _appsObservableList).AsObservableList();
  446. OnPropertyChanged(nameof(AppsObservableList));
  447. }
  448. public bool StartGamesInFullscreen
  449. {
  450. get => ConfigurationState.Instance.Ui.StartFullscreen;
  451. set
  452. {
  453. ConfigurationState.Instance.Ui.StartFullscreen.Value = value;
  454. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  455. OnPropertyChanged();
  456. }
  457. }
  458. public bool ShowConsole
  459. {
  460. get => ConfigurationState.Instance.Ui.ShowConsole;
  461. set
  462. {
  463. ConfigurationState.Instance.Ui.ShowConsole.Value = value;
  464. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  465. OnPropertyChanged();
  466. }
  467. }
  468. public bool ShowConsoleVisible
  469. {
  470. get => ConsoleHelper.SetConsoleWindowStateSupported;
  471. }
  472. public ObservableCollection<ApplicationData> Applications
  473. {
  474. get => _applications;
  475. set
  476. {
  477. _applications = value;
  478. OnPropertyChanged();
  479. }
  480. }
  481. public Glyph Glyph
  482. {
  483. get => (Glyph)ConfigurationState.Instance.Ui.GameListViewMode.Value;
  484. set
  485. {
  486. ConfigurationState.Instance.Ui.GameListViewMode.Value = (int)value;
  487. OnPropertyChanged();
  488. OnPropertyChanged(nameof(IsGrid));
  489. OnPropertyChanged(nameof(IsList));
  490. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  491. }
  492. }
  493. public bool ShowNames
  494. {
  495. get => ConfigurationState.Instance.Ui.ShowNames && ConfigurationState.Instance.Ui.GridSize > 1; set
  496. {
  497. ConfigurationState.Instance.Ui.ShowNames.Value = value;
  498. OnPropertyChanged();
  499. OnPropertyChanged(nameof(GridSizeScale));
  500. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  501. }
  502. }
  503. internal ApplicationSort SortMode
  504. {
  505. get => (ApplicationSort)ConfigurationState.Instance.Ui.ApplicationSort.Value;
  506. private set
  507. {
  508. ConfigurationState.Instance.Ui.ApplicationSort.Value = (int)value;
  509. OnPropertyChanged();
  510. OnPropertyChanged(nameof(SortName));
  511. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  512. }
  513. }
  514. public bool IsSortedByFavorite => SortMode == ApplicationSort.Favorite;
  515. public bool IsSortedByTitle => SortMode == ApplicationSort.Title;
  516. public bool IsSortedByDeveloper => SortMode == ApplicationSort.Developer;
  517. public bool IsSortedByLastPlayed => SortMode == ApplicationSort.LastPlayed;
  518. public bool IsSortedByTimePlayed => SortMode == ApplicationSort.TotalTimePlayed;
  519. public bool IsSortedByType => SortMode == ApplicationSort.FileType;
  520. public bool IsSortedBySize => SortMode == ApplicationSort.FileSize;
  521. public bool IsSortedByPath => SortMode == ApplicationSort.Path;
  522. public string SortName
  523. {
  524. get
  525. {
  526. return SortMode switch
  527. {
  528. ApplicationSort.Title => LocaleManager.Instance[LocaleKeys.GameListHeaderApplication],
  529. ApplicationSort.Developer => LocaleManager.Instance[LocaleKeys.GameListHeaderDeveloper],
  530. ApplicationSort.LastPlayed => LocaleManager.Instance[LocaleKeys.GameListHeaderLastPlayed],
  531. ApplicationSort.TotalTimePlayed => LocaleManager.Instance[LocaleKeys.GameListHeaderTimePlayed],
  532. ApplicationSort.FileType => LocaleManager.Instance[LocaleKeys.GameListHeaderFileExtension],
  533. ApplicationSort.FileSize => LocaleManager.Instance[LocaleKeys.GameListHeaderFileSize],
  534. ApplicationSort.Path => LocaleManager.Instance[LocaleKeys.GameListHeaderPath],
  535. ApplicationSort.Favorite => LocaleManager.Instance[LocaleKeys.CommonFavorite],
  536. _ => string.Empty,
  537. };
  538. }
  539. }
  540. public bool IsAscending
  541. {
  542. get => ConfigurationState.Instance.Ui.IsAscendingOrder;
  543. private set
  544. {
  545. ConfigurationState.Instance.Ui.IsAscendingOrder.Value = value;
  546. OnPropertyChanged();
  547. OnPropertyChanged(nameof(SortMode));
  548. OnPropertyChanged(nameof(SortName));
  549. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  550. }
  551. }
  552. public KeyGesture ShowUiKey
  553. {
  554. get => KeyGesture.Parse(_showUikey); set
  555. {
  556. _showUikey = value.ToString();
  557. OnPropertyChanged();
  558. }
  559. }
  560. public KeyGesture ScreenshotKey
  561. {
  562. get => KeyGesture.Parse(_screenshotkey); set
  563. {
  564. _screenshotkey = value.ToString();
  565. OnPropertyChanged();
  566. }
  567. }
  568. public KeyGesture PauseKey
  569. {
  570. get => KeyGesture.Parse(_pauseKey); set
  571. {
  572. _pauseKey = value.ToString();
  573. OnPropertyChanged();
  574. }
  575. }
  576. public bool IsGridSmall => ConfigurationState.Instance.Ui.GridSize == 1;
  577. public bool IsGridMedium => ConfigurationState.Instance.Ui.GridSize == 2;
  578. public bool IsGridLarge => ConfigurationState.Instance.Ui.GridSize == 3;
  579. public bool IsGridHuge => ConfigurationState.Instance.Ui.GridSize == 4;
  580. public int GridSizeScale
  581. {
  582. get => ConfigurationState.Instance.Ui.GridSize;
  583. set
  584. {
  585. ConfigurationState.Instance.Ui.GridSize.Value = value;
  586. if (value < 2)
  587. {
  588. ShowNames = false;
  589. }
  590. OnPropertyChanged();
  591. OnPropertyChanged(nameof(IsGridSmall));
  592. OnPropertyChanged(nameof(IsGridMedium));
  593. OnPropertyChanged(nameof(IsGridLarge));
  594. OnPropertyChanged(nameof(IsGridHuge));
  595. OnPropertyChanged(nameof(ShowNames));
  596. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  597. }
  598. }
  599. public async void OpenAmiiboWindow()
  600. {
  601. if (!_isAmiiboRequested)
  602. {
  603. return;
  604. }
  605. if (_owner.AppHost.Device.System.SearchingForAmiibo(out int deviceId))
  606. {
  607. string titleId = _owner.AppHost.Device.Application.TitleIdText.ToUpper();
  608. AmiiboWindow window = new(_showAll, _lastScannedAmiiboId, titleId);
  609. await window.ShowDialog(_owner);
  610. if (window.IsScanned)
  611. {
  612. _showAll = window.ViewModel.ShowAllAmiibo;
  613. _lastScannedAmiiboId = window.ScannedAmiibo.GetId();
  614. _owner.AppHost.Device.System.ScanAmiibo(deviceId, _lastScannedAmiiboId, window.ViewModel.UseRandomUuid);
  615. }
  616. }
  617. }
  618. public void SetUiProgressHandlers(Switch emulationContext)
  619. {
  620. if (emulationContext.Application.DiskCacheLoadState != null)
  621. {
  622. emulationContext.Application.DiskCacheLoadState.StateChanged -= ProgressHandler;
  623. emulationContext.Application.DiskCacheLoadState.StateChanged += ProgressHandler;
  624. }
  625. emulationContext.Gpu.ShaderCacheStateChanged -= ProgressHandler;
  626. emulationContext.Gpu.ShaderCacheStateChanged += ProgressHandler;
  627. }
  628. private bool Filter(object arg)
  629. {
  630. if (arg is ApplicationData app)
  631. {
  632. return string.IsNullOrWhiteSpace(_searchText) || app.TitleName.ToLower().Contains(_searchText.ToLower());
  633. }
  634. return false;
  635. }
  636. private void ApplicationLibrary_ApplicationAdded(object sender, ApplicationAddedEventArgs e)
  637. {
  638. AddApplication(e.AppData);
  639. }
  640. private void ApplicationLibrary_ApplicationCountUpdated(object sender, ApplicationCountUpdatedEventArgs e)
  641. {
  642. StatusBarProgressValue = e.NumAppsLoaded;
  643. StatusBarProgressMaximum = e.NumAppsFound;
  644. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarGamesLoaded, StatusBarProgressValue, StatusBarProgressMaximum);
  645. Dispatcher.UIThread.Post(() =>
  646. {
  647. if (e.NumAppsFound == 0)
  648. {
  649. _owner.LoadProgressBar.IsVisible = false;
  650. }
  651. if (e.NumAppsLoaded == e.NumAppsFound)
  652. {
  653. _owner.LoadProgressBar.IsVisible = false;
  654. }
  655. });
  656. }
  657. public void AddApplication(ApplicationData applicationData)
  658. {
  659. Dispatcher.UIThread.InvokeAsync(() =>
  660. {
  661. Applications.Add(applicationData);
  662. });
  663. }
  664. public async void LoadApplications()
  665. {
  666. await Dispatcher.UIThread.InvokeAsync(() =>
  667. {
  668. Applications.Clear();
  669. _owner.LoadProgressBar.IsVisible = true;
  670. StatusBarProgressMaximum = 0;
  671. StatusBarProgressValue = 0;
  672. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarGamesLoaded, 0, 0);
  673. });
  674. ReloadGameList();
  675. }
  676. private void ReloadGameList()
  677. {
  678. if (_isLoading)
  679. {
  680. return;
  681. }
  682. _isLoading = true;
  683. Thread thread = new(() =>
  684. {
  685. ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs.Value, ConfigurationState.Instance.System.Language);
  686. _isLoading = false;
  687. })
  688. { Name = "GUI.AppListLoadThread", Priority = ThreadPriority.AboveNormal };
  689. thread.Start();
  690. }
  691. public async void OpenFile()
  692. {
  693. OpenFileDialog dialog = new()
  694. {
  695. Title = LocaleManager.Instance[LocaleKeys.OpenFileDialogTitle]
  696. };
  697. dialog.Filters.Add(new FileDialogFilter
  698. {
  699. Name = LocaleManager.Instance[LocaleKeys.AllSupportedFormats],
  700. Extensions =
  701. {
  702. "nsp",
  703. "pfs0",
  704. "xci",
  705. "nca",
  706. "nro",
  707. "nso"
  708. }
  709. });
  710. dialog.Filters.Add(new FileDialogFilter { Name = "NSP", Extensions = { "nsp" } });
  711. dialog.Filters.Add(new FileDialogFilter { Name = "PFS0", Extensions = { "pfs0" } });
  712. dialog.Filters.Add(new FileDialogFilter { Name = "XCI", Extensions = { "xci" } });
  713. dialog.Filters.Add(new FileDialogFilter { Name = "NCA", Extensions = { "nca" } });
  714. dialog.Filters.Add(new FileDialogFilter { Name = "NRO", Extensions = { "nro" } });
  715. dialog.Filters.Add(new FileDialogFilter { Name = "NSO", Extensions = { "nso" } });
  716. string[] files = await dialog.ShowAsync(_owner);
  717. if (files != null && files.Length > 0)
  718. {
  719. _owner.LoadApplication(files[0]);
  720. }
  721. }
  722. public async void OpenFolder()
  723. {
  724. OpenFolderDialog dialog = new()
  725. {
  726. Title = LocaleManager.Instance[LocaleKeys.OpenFolderDialogTitle]
  727. };
  728. string folder = await dialog.ShowAsync(_owner);
  729. if (!string.IsNullOrWhiteSpace(folder) && Directory.Exists(folder))
  730. {
  731. _owner.LoadApplication(folder);
  732. }
  733. }
  734. public void LoadConfigurableHotKeys()
  735. {
  736. if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUi, out var showUiKey))
  737. {
  738. ShowUiKey = new KeyGesture(showUiKey, KeyModifiers.None);
  739. }
  740. if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot, out var screenshotKey))
  741. {
  742. ScreenshotKey = new KeyGesture(screenshotKey, KeyModifiers.None);
  743. }
  744. if (AvaloniaKeyboardMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause, out var pauseKey))
  745. {
  746. PauseKey = new KeyGesture(pauseKey, KeyModifiers.None);
  747. }
  748. }
  749. public void TakeScreenshot()
  750. {
  751. _owner.AppHost.ScreenshotRequested = true;
  752. }
  753. public void HideUi()
  754. {
  755. ShowMenuAndStatusBar = false;
  756. }
  757. public void SetListMode()
  758. {
  759. Glyph = Glyph.List;
  760. }
  761. public void SetGridMode()
  762. {
  763. Glyph = Glyph.Grid;
  764. }
  765. public void OpenMiiApplet()
  766. {
  767. string contentPath = _owner.ContentManager.GetInstalledContentPath(0x0100000000001009, StorageId.BuiltInSystem, NcaContentType.Program);
  768. if (!string.IsNullOrWhiteSpace(contentPath))
  769. {
  770. _owner.LoadApplication(contentPath, false, "Mii Applet");
  771. }
  772. }
  773. public static void OpenRyujinxFolder()
  774. {
  775. OpenHelper.OpenFolder(AppDataManager.BaseDirPath);
  776. }
  777. public static void OpenLogsFolder()
  778. {
  779. string logPath = Path.Combine(ReleaseInformations.GetBaseApplicationDirectory(), "Logs");
  780. new DirectoryInfo(logPath).Create();
  781. OpenHelper.OpenFolder(logPath);
  782. }
  783. public void ToggleFullscreen()
  784. {
  785. if (Environment.TickCount64 - _lastFullscreenToggle < HotKeyPressDelayMs)
  786. {
  787. return;
  788. }
  789. _lastFullscreenToggle = Environment.TickCount64;
  790. if (_owner.WindowState == WindowState.FullScreen)
  791. {
  792. _owner.WindowState = WindowState.Normal;
  793. if (IsGameRunning)
  794. {
  795. ShowMenuAndStatusBar = true;
  796. }
  797. }
  798. else
  799. {
  800. _owner.WindowState = WindowState.FullScreen;
  801. if (IsGameRunning)
  802. {
  803. ShowMenuAndStatusBar = false;
  804. }
  805. }
  806. OnPropertyChanged(nameof(IsFullScreen));
  807. }
  808. public bool IsFullScreen => _owner.WindowState == WindowState.FullScreen;
  809. public void ToggleDockMode()
  810. {
  811. if (IsGameRunning)
  812. {
  813. ConfigurationState.Instance.System.EnableDockedMode.Value = !ConfigurationState.Instance.System.EnableDockedMode.Value;
  814. }
  815. }
  816. public async void ExitCurrentState()
  817. {
  818. if (_owner.WindowState == WindowState.FullScreen)
  819. {
  820. ToggleFullscreen();
  821. }
  822. else if (IsGameRunning)
  823. {
  824. await Task.Delay(100);
  825. _owner.AppHost?.ShowExitPrompt();
  826. }
  827. }
  828. public async void OpenSettings()
  829. {
  830. _owner.SettingsWindow = new(_owner.VirtualFileSystem, _owner.ContentManager);
  831. await _owner.SettingsWindow.ShowDialog(_owner);
  832. LoadConfigurableHotKeys();
  833. }
  834. public async void ManageProfiles()
  835. {
  836. await NavigationDialogHost.Show(_owner.AccountManager, _owner.ContentManager, _owner.VirtualFileSystem, _owner.LibHacHorizonManager.RyujinxClient);
  837. }
  838. public async void OpenAboutWindow()
  839. {
  840. await AboutWindow.Show();
  841. }
  842. public void ChangeLanguage(object obj)
  843. {
  844. LocaleManager.Instance.LoadDefaultLanguage();
  845. LocaleManager.Instance.LoadLanguage((string)obj);
  846. }
  847. private void ProgressHandler<T>(T state, int current, int total) where T : Enum
  848. {
  849. try
  850. {
  851. ProgressMaximum = total;
  852. ProgressValue = current;
  853. switch (state)
  854. {
  855. case LoadState ptcState:
  856. CacheLoadStatus = $"{current} / {total}";
  857. switch (ptcState)
  858. {
  859. case LoadState.Unloaded:
  860. case LoadState.Loading:
  861. LoadHeading = LocaleManager.Instance[LocaleKeys.CompilingPPTC];
  862. IsLoadingIndeterminate = false;
  863. break;
  864. case LoadState.Loaded:
  865. LoadHeading = string.Format(LocaleManager.Instance[LocaleKeys.LoadingHeading], TitleName);
  866. IsLoadingIndeterminate = true;
  867. CacheLoadStatus = "";
  868. break;
  869. }
  870. break;
  871. case ShaderCacheLoadingState shaderCacheState:
  872. CacheLoadStatus = $"{current} / {total}";
  873. switch (shaderCacheState)
  874. {
  875. case ShaderCacheLoadingState.Start:
  876. case ShaderCacheLoadingState.Loading:
  877. LoadHeading = LocaleManager.Instance[LocaleKeys.CompilingShaders];
  878. IsLoadingIndeterminate = false;
  879. break;
  880. case ShaderCacheLoadingState.Loaded:
  881. LoadHeading = string.Format(LocaleManager.Instance[LocaleKeys.LoadingHeading], TitleName);
  882. IsLoadingIndeterminate = true;
  883. CacheLoadStatus = "";
  884. break;
  885. }
  886. break;
  887. default:
  888. throw new ArgumentException($"Unknown Progress Handler type {typeof(T)}");
  889. }
  890. }
  891. catch (Exception) { }
  892. }
  893. public void OpenUserSaveDirectory()
  894. {
  895. ApplicationData selection = SelectedApplication;
  896. if (selection != null)
  897. {
  898. Task.Run(() =>
  899. {
  900. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  901. {
  902. Dispatcher.UIThread.Post(async () =>
  903. {
  904. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogRyujinxErrorMessage], LocaleManager.Instance[LocaleKeys.DialogInvalidTitleIdErrorMessage]);
  905. });
  906. return;
  907. }
  908. UserId userId = new((ulong)_owner.AccountManager.LastOpenedUser.UserId.High, (ulong)_owner.AccountManager.LastOpenedUser.UserId.Low);
  909. SaveDataFilter saveDataFilter = SaveDataFilter.Make(titleIdNumber, saveType: default, userId, saveDataId: default, index: default);
  910. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  911. });
  912. }
  913. }
  914. public void ToggleFavorite()
  915. {
  916. ApplicationData selection = SelectedApplication;
  917. if (selection != null)
  918. {
  919. selection.Favorite = !selection.Favorite;
  920. ApplicationLibrary.LoadAndSaveMetaData(selection.TitleId, appMetadata =>
  921. {
  922. appMetadata.Favorite = selection.Favorite;
  923. });
  924. RefreshView();
  925. }
  926. }
  927. public void OpenModsDirectory()
  928. {
  929. ApplicationData selection = SelectedApplication;
  930. if (selection != null)
  931. {
  932. string modsBasePath = _owner.VirtualFileSystem.ModLoader.GetModsBasePath();
  933. string titleModsPath = _owner.VirtualFileSystem.ModLoader.GetTitleDir(modsBasePath, selection.TitleId);
  934. OpenHelper.OpenFolder(titleModsPath);
  935. }
  936. }
  937. public void OpenSdModsDirectory()
  938. {
  939. ApplicationData selection = SelectedApplication;
  940. if (selection != null)
  941. {
  942. string sdModsBasePath = _owner.VirtualFileSystem.ModLoader.GetSdModsBasePath();
  943. string titleModsPath = _owner.VirtualFileSystem.ModLoader.GetTitleDir(sdModsBasePath, selection.TitleId);
  944. OpenHelper.OpenFolder(titleModsPath);
  945. }
  946. }
  947. public void OpenPtcDirectory()
  948. {
  949. ApplicationData selection = SelectedApplication;
  950. if (selection != null)
  951. {
  952. string ptcDir = Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu");
  953. string mainPath = Path.Combine(ptcDir, "0");
  954. string backupPath = Path.Combine(ptcDir, "1");
  955. if (!Directory.Exists(ptcDir))
  956. {
  957. Directory.CreateDirectory(ptcDir);
  958. Directory.CreateDirectory(mainPath);
  959. Directory.CreateDirectory(backupPath);
  960. }
  961. OpenHelper.OpenFolder(ptcDir);
  962. }
  963. }
  964. public async void PurgePtcCache()
  965. {
  966. ApplicationData selection = SelectedApplication;
  967. if (selection != null)
  968. {
  969. DirectoryInfo mainDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu", "0"));
  970. DirectoryInfo backupDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu", "1"));
  971. // FIXME: Found a way to reproduce the bold effect on the title name (fork?).
  972. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance[LocaleKeys.DialogWarning],
  973. string.Format(LocaleManager.Instance[LocaleKeys.DialogPPTCDeletionMessage], selection.TitleName),
  974. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  975. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  976. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  977. List<FileInfo> cacheFiles = new();
  978. if (mainDir.Exists)
  979. {
  980. cacheFiles.AddRange(mainDir.EnumerateFiles("*.cache"));
  981. }
  982. if (backupDir.Exists)
  983. {
  984. cacheFiles.AddRange(backupDir.EnumerateFiles("*.cache"));
  985. }
  986. if (cacheFiles.Count > 0 && result == UserResult.Yes)
  987. {
  988. foreach (FileInfo file in cacheFiles)
  989. {
  990. try
  991. {
  992. file.Delete();
  993. }
  994. catch (Exception e)
  995. {
  996. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogPPTCDeletionErrorMessage], file.Name, e));
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. public void OpenShaderCacheDirectory()
  1003. {
  1004. ApplicationData selection = SelectedApplication;
  1005. if (selection != null)
  1006. {
  1007. string shaderCacheDir = Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "shader");
  1008. if (!Directory.Exists(shaderCacheDir))
  1009. {
  1010. Directory.CreateDirectory(shaderCacheDir);
  1011. }
  1012. OpenHelper.OpenFolder(shaderCacheDir);
  1013. }
  1014. }
  1015. public void SimulateWakeUpMessage()
  1016. {
  1017. _owner.AppHost.Device.System.SimulateWakeUpMessage();
  1018. }
  1019. public async void PurgeShaderCache()
  1020. {
  1021. ApplicationData selection = SelectedApplication;
  1022. if (selection != null)
  1023. {
  1024. DirectoryInfo shaderCacheDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "shader"));
  1025. // FIXME: Found a way to reproduce the bold effect on the title name (fork?).
  1026. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance[LocaleKeys.DialogWarning],
  1027. string.Format(LocaleManager.Instance[LocaleKeys.DialogShaderDeletionMessage], selection.TitleName),
  1028. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  1029. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  1030. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  1031. List<DirectoryInfo> oldCacheDirectories = new();
  1032. List<FileInfo> newCacheFiles = new();
  1033. if (shaderCacheDir.Exists)
  1034. {
  1035. oldCacheDirectories.AddRange(shaderCacheDir.EnumerateDirectories("*"));
  1036. newCacheFiles.AddRange(shaderCacheDir.GetFiles("*.toc"));
  1037. newCacheFiles.AddRange(shaderCacheDir.GetFiles("*.data"));
  1038. }
  1039. if ((oldCacheDirectories.Count > 0 || newCacheFiles.Count > 0) && result == UserResult.Yes)
  1040. {
  1041. foreach (DirectoryInfo directory in oldCacheDirectories)
  1042. {
  1043. try
  1044. {
  1045. directory.Delete(true);
  1046. }
  1047. catch (Exception e)
  1048. {
  1049. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogPPTCDeletionErrorMessage], directory.Name, e));
  1050. }
  1051. }
  1052. }
  1053. foreach (FileInfo file in newCacheFiles)
  1054. {
  1055. try
  1056. {
  1057. file.Delete();
  1058. }
  1059. catch (Exception e)
  1060. {
  1061. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.ShaderCachePurgeError], file.Name, e));
  1062. }
  1063. }
  1064. }
  1065. }
  1066. public async void CheckForUpdates()
  1067. {
  1068. if (Updater.CanUpdate(true, _owner))
  1069. {
  1070. await Updater.BeginParse(_owner, true);
  1071. }
  1072. }
  1073. public async void OpenTitleUpdateManager()
  1074. {
  1075. ApplicationData selection = SelectedApplication;
  1076. if (selection != null)
  1077. {
  1078. await new TitleUpdateWindow(_owner.VirtualFileSystem, ulong.Parse(selection.TitleId, NumberStyles.HexNumber), selection.TitleName).ShowDialog(_owner);
  1079. }
  1080. }
  1081. public async void OpenDownloadableContentManager()
  1082. {
  1083. ApplicationData selection = SelectedApplication;
  1084. if (selection != null)
  1085. {
  1086. await new DownloadableContentManagerWindow(_owner.VirtualFileSystem, ulong.Parse(selection.TitleId, NumberStyles.HexNumber), selection.TitleName).ShowDialog(_owner);
  1087. }
  1088. }
  1089. public async void OpenCheatManager()
  1090. {
  1091. ApplicationData selection = SelectedApplication;
  1092. if (selection != null)
  1093. {
  1094. await new CheatWindow(_owner.VirtualFileSystem, selection.TitleId, selection.TitleName).ShowDialog(_owner);
  1095. }
  1096. }
  1097. public async void OpenCheatManagerForCurrentApp()
  1098. {
  1099. if (!IsGameRunning)
  1100. {
  1101. return;
  1102. }
  1103. ApplicationLoader application = _owner.AppHost.Device.Application;
  1104. if (application != null)
  1105. {
  1106. await new CheatWindow(_owner.VirtualFileSystem, application.TitleIdText, application.TitleName).ShowDialog(_owner);
  1107. _owner.AppHost.Device.EnableCheats();
  1108. }
  1109. }
  1110. public void OpenDeviceSaveDirectory()
  1111. {
  1112. ApplicationData selection = SelectedApplication;
  1113. if (selection != null)
  1114. {
  1115. Task.Run(() =>
  1116. {
  1117. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  1118. {
  1119. Dispatcher.UIThread.Post(async () =>
  1120. {
  1121. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogRyujinxErrorMessage], LocaleManager.Instance[LocaleKeys.DialogInvalidTitleIdErrorMessage]);
  1122. });
  1123. return;
  1124. }
  1125. var saveDataFilter = SaveDataFilter.Make(titleIdNumber, SaveDataType.Device, userId: default, saveDataId: default, index: default);
  1126. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  1127. });
  1128. }
  1129. }
  1130. public void OpenBcatSaveDirectory()
  1131. {
  1132. ApplicationData selection = SelectedApplication;
  1133. if (selection != null)
  1134. {
  1135. Task.Run(() =>
  1136. {
  1137. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  1138. {
  1139. Dispatcher.UIThread.Post(async () =>
  1140. {
  1141. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogRyujinxErrorMessage], LocaleManager.Instance[LocaleKeys.DialogInvalidTitleIdErrorMessage]);
  1142. });
  1143. return;
  1144. }
  1145. var saveDataFilter = SaveDataFilter.Make(titleIdNumber, SaveDataType.Bcat, userId: default, saveDataId: default, index: default);
  1146. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  1147. });
  1148. }
  1149. }
  1150. private void OpenSaveDirectory(in SaveDataFilter filter, ApplicationData data, ulong titleId)
  1151. {
  1152. ApplicationHelper.OpenSaveDir(in filter, titleId, data.ControlHolder, data.TitleName);
  1153. }
  1154. private async void ExtractLogo()
  1155. {
  1156. var selection = SelectedApplication;
  1157. if (selection != null)
  1158. {
  1159. await ApplicationHelper.ExtractSection(NcaSectionType.Logo, selection.Path);
  1160. }
  1161. }
  1162. private async void ExtractRomFs()
  1163. {
  1164. var selection = SelectedApplication;
  1165. if (selection != null)
  1166. {
  1167. await ApplicationHelper.ExtractSection(NcaSectionType.Data, selection.Path);
  1168. }
  1169. }
  1170. private async void ExtractExeFs()
  1171. {
  1172. var selection = SelectedApplication;
  1173. if (selection != null)
  1174. {
  1175. await ApplicationHelper.ExtractSection(NcaSectionType.Code, selection.Path);
  1176. }
  1177. }
  1178. public void CloseWindow()
  1179. {
  1180. _owner.Close();
  1181. }
  1182. private async Task HandleFirmwareInstallation(string filename)
  1183. {
  1184. try
  1185. {
  1186. SystemVersion firmwareVersion = _owner.ContentManager.VerifyFirmwarePackage(filename);
  1187. if (firmwareVersion == null)
  1188. {
  1189. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareNotFoundErrorMessage], filename));
  1190. return;
  1191. }
  1192. string dialogTitle = string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallTitle], firmwareVersion.VersionString);
  1193. SystemVersion currentVersion = _owner.ContentManager.GetCurrentFirmwareVersion();
  1194. string dialogMessage = string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallMessage], firmwareVersion.VersionString);
  1195. if (currentVersion != null)
  1196. {
  1197. dialogMessage += string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallSubMessage], currentVersion.VersionString);
  1198. }
  1199. dialogMessage += LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallConfirmMessage];
  1200. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
  1201. dialogTitle,
  1202. dialogMessage,
  1203. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  1204. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  1205. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  1206. UpdateWaitWindow waitingDialog = ContentDialogHelper.CreateWaitingDialog(dialogTitle, LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallWaitMessage]);
  1207. if (result == UserResult.Yes)
  1208. {
  1209. Logger.Info?.Print(LogClass.Application, $"Installing firmware {firmwareVersion.VersionString}");
  1210. Thread thread = new(() =>
  1211. {
  1212. Dispatcher.UIThread.InvokeAsync(delegate
  1213. {
  1214. waitingDialog.Show();
  1215. });
  1216. try
  1217. {
  1218. _owner.ContentManager.InstallFirmware(filename);
  1219. Dispatcher.UIThread.InvokeAsync(async delegate
  1220. {
  1221. waitingDialog.Close();
  1222. string message = string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallerFirmwareInstallSuccessMessage], firmwareVersion.VersionString);
  1223. await ContentDialogHelper.CreateInfoDialog(dialogTitle, message, LocaleManager.Instance[LocaleKeys.InputDialogOk], "", LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
  1224. Logger.Info?.Print(LogClass.Application, message);
  1225. // Purge Applet Cache.
  1226. DirectoryInfo miiEditorCacheFolder = new DirectoryInfo(Path.Combine(AppDataManager.GamesDirPath, "0100000000001009", "cache"));
  1227. if (miiEditorCacheFolder.Exists)
  1228. {
  1229. miiEditorCacheFolder.Delete(true);
  1230. }
  1231. });
  1232. }
  1233. catch (Exception ex)
  1234. {
  1235. Dispatcher.UIThread.InvokeAsync(async () =>
  1236. {
  1237. waitingDialog.Close();
  1238. await ContentDialogHelper.CreateErrorDialog(ex.Message);
  1239. });
  1240. }
  1241. finally
  1242. {
  1243. _owner.RefreshFirmwareStatus();
  1244. }
  1245. });
  1246. thread.Name = "GUI.FirmwareInstallerThread";
  1247. thread.Start();
  1248. }
  1249. }
  1250. catch (LibHac.Common.Keys.MissingKeyException ex)
  1251. {
  1252. Logger.Error?.Print(LogClass.Application, ex.ToString());
  1253. Dispatcher.UIThread.Post(async () => await UserErrorDialog.ShowUserErrorDialog(UserError.NoKeys, _owner));
  1254. }
  1255. catch (Exception ex)
  1256. {
  1257. await ContentDialogHelper.CreateErrorDialog(ex.Message);
  1258. }
  1259. }
  1260. public async void InstallFirmwareFromFile()
  1261. {
  1262. OpenFileDialog dialog = new() { AllowMultiple = false };
  1263. dialog.Filters.Add(new FileDialogFilter { Name = LocaleManager.Instance[LocaleKeys.FileDialogAllTypes], Extensions = { "xci", "zip" } });
  1264. dialog.Filters.Add(new FileDialogFilter { Name = "XCI", Extensions = { "xci" } });
  1265. dialog.Filters.Add(new FileDialogFilter { Name = "ZIP", Extensions = { "zip" } });
  1266. string[] file = await dialog.ShowAsync(_owner);
  1267. if (file != null && file.Length > 0)
  1268. {
  1269. await HandleFirmwareInstallation(file[0]);
  1270. }
  1271. }
  1272. public async void InstallFirmwareFromFolder()
  1273. {
  1274. OpenFolderDialog dialog = new();
  1275. string folder = await dialog.ShowAsync(_owner);
  1276. if (!string.IsNullOrWhiteSpace(folder))
  1277. {
  1278. await HandleFirmwareInstallation(folder);
  1279. }
  1280. }
  1281. }
  1282. }