MainWindowViewModel.cs 51 KB

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