MainWindowViewModel.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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 string TitleName { get; internal set; }
  77. public MainWindowViewModel(MainWindow owner) : this()
  78. {
  79. _owner = owner;
  80. }
  81. public MainWindowViewModel()
  82. {
  83. Applications = new ObservableCollection<ApplicationData>();
  84. Applications.ToObservableChangeSet()
  85. .Filter(Filter)
  86. .Sort(GetComparer())
  87. .Bind(out _appsObservableList).AsObservableList();
  88. if (Program.PreviewerDetached)
  89. {
  90. LoadConfigurableHotKeys();
  91. Volume = ConfigurationState.Instance.System.AudioVolume;
  92. }
  93. }
  94. public void Initialize()
  95. {
  96. _owner.ApplicationLibrary.ApplicationCountUpdated += ApplicationLibrary_ApplicationCountUpdated;
  97. _owner.ApplicationLibrary.ApplicationAdded += ApplicationLibrary_ApplicationAdded;
  98. Ptc.PtcStateChanged -= ProgressHandler;
  99. Ptc.PtcStateChanged += ProgressHandler;
  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 Thickness GridItemPadding => ShowNames ? new Thickness() : new Thickness(5);
  367. public bool ShowMenuAndStatusBar
  368. {
  369. get => _showMenuAndStatusBar;
  370. set
  371. {
  372. _showMenuAndStatusBar = value;
  373. OnPropertyChanged();
  374. }
  375. }
  376. public bool IsLoadingIndeterminate
  377. {
  378. get => _isLoadingIndeterminate;
  379. set
  380. {
  381. _isLoadingIndeterminate = value;
  382. OnPropertyChanged();
  383. }
  384. }
  385. public bool ShowContent
  386. {
  387. get => _showContent;
  388. set
  389. {
  390. _showContent = value;
  391. OnPropertyChanged();
  392. }
  393. }
  394. public bool IsAppletMenuActive
  395. {
  396. get => _isAppletMenuActive && EnableNonGameRunningControls;
  397. set
  398. {
  399. _isAppletMenuActive = value;
  400. OnPropertyChanged();
  401. }
  402. }
  403. public bool IsGrid => Glyph == Glyph.Grid;
  404. public bool IsList => Glyph == Glyph.List;
  405. internal void Sort(bool isAscending)
  406. {
  407. IsAscending = isAscending;
  408. RefreshView();
  409. }
  410. internal void Sort(ApplicationSort sort)
  411. {
  412. SortMode = sort;
  413. RefreshView();
  414. }
  415. private IComparer<ApplicationData> GetComparer()
  416. {
  417. return SortMode switch
  418. {
  419. ApplicationSort.LastPlayed => new Models.Generic.LastPlayedSortComparer(IsAscending),
  420. ApplicationSort.FileSize => new Models.Generic.FileSizeSortComparer(IsAscending),
  421. ApplicationSort.TotalTimePlayed => new Models.Generic.TimePlayedSortComparer(IsAscending),
  422. ApplicationSort.Title => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.TitleName)
  423. : SortExpressionComparer<ApplicationData>.Descending(app => app.TitleName),
  424. ApplicationSort.Favorite => !IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Favorite)
  425. : SortExpressionComparer<ApplicationData>.Descending(app => app.Favorite),
  426. ApplicationSort.Developer => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Developer)
  427. : SortExpressionComparer<ApplicationData>.Descending(app => app.Developer),
  428. ApplicationSort.FileType => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.FileExtension)
  429. : SortExpressionComparer<ApplicationData>.Descending(app => app.FileExtension),
  430. ApplicationSort.Path => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Path)
  431. : SortExpressionComparer<ApplicationData>.Descending(app => app.Path),
  432. _ => null,
  433. };
  434. }
  435. private void RefreshView()
  436. {
  437. RefreshGrid();
  438. }
  439. private void RefreshGrid()
  440. {
  441. Applications.ToObservableChangeSet()
  442. .Filter(Filter)
  443. .Sort(GetComparer())
  444. .Bind(out _appsObservableList).AsObservableList();
  445. OnPropertyChanged(nameof(AppsObservableList));
  446. }
  447. public bool StartGamesInFullscreen
  448. {
  449. get => ConfigurationState.Instance.Ui.StartFullscreen;
  450. set
  451. {
  452. ConfigurationState.Instance.Ui.StartFullscreen.Value = value;
  453. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  454. OnPropertyChanged();
  455. }
  456. }
  457. public bool ShowConsole
  458. {
  459. get => ConfigurationState.Instance.Ui.ShowConsole;
  460. set
  461. {
  462. ConfigurationState.Instance.Ui.ShowConsole.Value = value;
  463. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  464. OnPropertyChanged();
  465. }
  466. }
  467. public bool ShowConsoleVisible
  468. {
  469. get => ConsoleHelper.SetConsoleWindowStateSupported;
  470. }
  471. public ObservableCollection<ApplicationData> Applications
  472. {
  473. get => _applications;
  474. set
  475. {
  476. _applications = value;
  477. OnPropertyChanged();
  478. }
  479. }
  480. public Glyph Glyph
  481. {
  482. get => (Glyph)ConfigurationState.Instance.Ui.GameListViewMode.Value;
  483. set
  484. {
  485. ConfigurationState.Instance.Ui.GameListViewMode.Value = (int)value;
  486. OnPropertyChanged();
  487. OnPropertyChanged(nameof(IsGrid));
  488. OnPropertyChanged(nameof(IsList));
  489. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  490. }
  491. }
  492. public bool ShowNames
  493. {
  494. get => ConfigurationState.Instance.Ui.ShowNames && ConfigurationState.Instance.Ui.GridSize > 1; set
  495. {
  496. ConfigurationState.Instance.Ui.ShowNames.Value = value;
  497. OnPropertyChanged();
  498. OnPropertyChanged(nameof(GridItemPadding));
  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["GameListHeaderApplication"],
  529. ApplicationSort.Developer => LocaleManager.Instance["GameListHeaderDeveloper"],
  530. ApplicationSort.LastPlayed => LocaleManager.Instance["GameListHeaderLastPlayed"],
  531. ApplicationSort.TotalTimePlayed => LocaleManager.Instance["GameListHeaderTimePlayed"],
  532. ApplicationSort.FileType => LocaleManager.Instance["GameListHeaderFileExtension"],
  533. ApplicationSort.FileSize => LocaleManager.Instance["GameListHeaderFileSize"],
  534. ApplicationSort.Path => LocaleManager.Instance["GameListHeaderPath"],
  535. ApplicationSort.Favorite => LocaleManager.Instance["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. OnPropertyChanged(nameof(GridItemPadding));
  597. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  598. }
  599. }
  600. public async void OpenAmiiboWindow()
  601. {
  602. if (!_isAmiiboRequested)
  603. {
  604. return;
  605. }
  606. if (_owner.AppHost.Device.System.SearchingForAmiibo(out int deviceId))
  607. {
  608. string titleId = _owner.AppHost.Device.Application.TitleIdText.ToUpper();
  609. AmiiboWindow window = new(_showAll, _lastScannedAmiiboId, titleId);
  610. await window.ShowDialog(_owner);
  611. if (window.IsScanned)
  612. {
  613. _showAll = window.ViewModel.ShowAllAmiibo;
  614. _lastScannedAmiiboId = window.ScannedAmiibo.GetId();
  615. _owner.AppHost.Device.System.ScanAmiibo(deviceId, _lastScannedAmiiboId, window.ViewModel.UseRandomUuid);
  616. }
  617. }
  618. }
  619. public void HandleShaderProgress(Switch emulationContext)
  620. {
  621. emulationContext.Gpu.ShaderCacheStateChanged -= ProgressHandler;
  622. emulationContext.Gpu.ShaderCacheStateChanged += ProgressHandler;
  623. }
  624. private bool Filter(object arg)
  625. {
  626. if (arg is ApplicationData app)
  627. {
  628. return string.IsNullOrWhiteSpace(_searchText) || app.TitleName.ToLower().Contains(_searchText.ToLower());
  629. }
  630. return false;
  631. }
  632. private void ApplicationLibrary_ApplicationAdded(object sender, ApplicationAddedEventArgs e)
  633. {
  634. AddApplication(e.AppData);
  635. }
  636. private void ApplicationLibrary_ApplicationCountUpdated(object sender, ApplicationCountUpdatedEventArgs e)
  637. {
  638. StatusBarProgressValue = e.NumAppsLoaded;
  639. StatusBarProgressMaximum = e.NumAppsFound;
  640. LocaleManager.Instance.UpdateDynamicValue("StatusBarGamesLoaded", StatusBarProgressValue, StatusBarProgressMaximum);
  641. Dispatcher.UIThread.Post(() =>
  642. {
  643. if (e.NumAppsFound == 0)
  644. {
  645. _owner.LoadProgressBar.IsVisible = false;
  646. }
  647. });
  648. }
  649. public void AddApplication(ApplicationData applicationData)
  650. {
  651. Dispatcher.UIThread.InvokeAsync(() =>
  652. {
  653. Applications.Add(applicationData);
  654. });
  655. }
  656. public async void LoadApplications()
  657. {
  658. await Dispatcher.UIThread.InvokeAsync(() =>
  659. {
  660. Applications.Clear();
  661. _owner.LoadProgressBar.IsVisible = true;
  662. StatusBarProgressMaximum = 0;
  663. StatusBarProgressValue = 0;
  664. LocaleManager.Instance.UpdateDynamicValue("StatusBarGamesLoaded", 0, 0);
  665. });
  666. ReloadGameList();
  667. }
  668. private void ReloadGameList()
  669. {
  670. if (_isLoading)
  671. {
  672. return;
  673. }
  674. _isLoading = true;
  675. Thread thread = new(() =>
  676. {
  677. _owner.ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs.Value, ConfigurationState.Instance.System.Language);
  678. _isLoading = false;
  679. })
  680. { Name = "GUI.AppListLoadThread", Priority = ThreadPriority.AboveNormal };
  681. thread.Start();
  682. }
  683. public async void OpenFile()
  684. {
  685. OpenFileDialog dialog = new()
  686. {
  687. Title = LocaleManager.Instance["OpenFileDialogTitle"]
  688. };
  689. dialog.Filters.Add(new FileDialogFilter
  690. {
  691. Name = LocaleManager.Instance["AllSupportedFormats"],
  692. Extensions =
  693. {
  694. "nsp",
  695. "pfs0",
  696. "xci",
  697. "nca",
  698. "nro",
  699. "nso"
  700. }
  701. });
  702. dialog.Filters.Add(new FileDialogFilter { Name = "NSP", Extensions = { "nsp" } });
  703. dialog.Filters.Add(new FileDialogFilter { Name = "PFS0", Extensions = { "pfs0" } });
  704. dialog.Filters.Add(new FileDialogFilter { Name = "XCI", Extensions = { "xci" } });
  705. dialog.Filters.Add(new FileDialogFilter { Name = "NCA", Extensions = { "nca" } });
  706. dialog.Filters.Add(new FileDialogFilter { Name = "NRO", Extensions = { "nro" } });
  707. dialog.Filters.Add(new FileDialogFilter { Name = "NSO", Extensions = { "nso" } });
  708. string[] files = await dialog.ShowAsync(_owner);
  709. if (files != null && files.Length > 0)
  710. {
  711. _owner.LoadApplication(files[0]);
  712. }
  713. }
  714. public async void OpenFolder()
  715. {
  716. OpenFolderDialog dialog = new()
  717. {
  718. Title = LocaleManager.Instance["OpenFolderDialogTitle"]
  719. };
  720. string folder = await dialog.ShowAsync(_owner);
  721. if (!string.IsNullOrWhiteSpace(folder) && Directory.Exists(folder))
  722. {
  723. _owner.LoadApplication(folder);
  724. }
  725. }
  726. public void LoadConfigurableHotKeys()
  727. {
  728. if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.ShowUi, out var showUiKey))
  729. {
  730. ShowUiKey = new KeyGesture(showUiKey, KeyModifiers.None);
  731. }
  732. if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Screenshot, out var screenshotKey))
  733. {
  734. ScreenshotKey = new KeyGesture(screenshotKey, KeyModifiers.None);
  735. }
  736. if (AvaloniaMappingHelper.TryGetAvaKey((Ryujinx.Input.Key)ConfigurationState.Instance.Hid.Hotkeys.Value.Pause, out var pauseKey))
  737. {
  738. PauseKey = new KeyGesture(pauseKey, KeyModifiers.None);
  739. }
  740. }
  741. public void TakeScreenshot()
  742. {
  743. _owner.AppHost.ScreenshotRequested = true;
  744. }
  745. public void HideUi()
  746. {
  747. ShowMenuAndStatusBar = false;
  748. }
  749. public void SetListMode()
  750. {
  751. Glyph = Glyph.List;
  752. }
  753. public void SetGridMode()
  754. {
  755. Glyph = Glyph.Grid;
  756. }
  757. public void OpenMiiApplet()
  758. {
  759. string contentPath = _owner.ContentManager.GetInstalledContentPath(0x0100000000001009, StorageId.BuiltInSystem, NcaContentType.Program);
  760. if (!string.IsNullOrWhiteSpace(contentPath))
  761. {
  762. _owner.LoadApplication(contentPath, false, "Mii Applet");
  763. }
  764. }
  765. public static void OpenRyujinxFolder()
  766. {
  767. OpenHelper.OpenFolder(AppDataManager.BaseDirPath);
  768. }
  769. public static void OpenLogsFolder()
  770. {
  771. string logPath = Path.Combine(ReleaseInformations.GetBaseApplicationDirectory(), "Logs");
  772. new DirectoryInfo(logPath).Create();
  773. OpenHelper.OpenFolder(logPath);
  774. }
  775. public void ToggleFullscreen()
  776. {
  777. if (Environment.TickCount64 - _lastFullscreenToggle < HotKeyPressDelayMs)
  778. {
  779. return;
  780. }
  781. _lastFullscreenToggle = Environment.TickCount64;
  782. if (_owner.WindowState == WindowState.FullScreen)
  783. {
  784. _owner.WindowState = WindowState.Normal;
  785. if (IsGameRunning)
  786. {
  787. ShowMenuAndStatusBar = true;
  788. }
  789. }
  790. else
  791. {
  792. _owner.WindowState = WindowState.FullScreen;
  793. if (IsGameRunning)
  794. {
  795. ShowMenuAndStatusBar = false;
  796. }
  797. }
  798. OnPropertyChanged(nameof(IsFullScreen));
  799. }
  800. public bool IsFullScreen => _owner.WindowState == WindowState.FullScreen;
  801. public void ToggleDockMode()
  802. {
  803. if (IsGameRunning)
  804. {
  805. ConfigurationState.Instance.System.EnableDockedMode.Value = !ConfigurationState.Instance.System.EnableDockedMode.Value;
  806. }
  807. }
  808. public async void ExitCurrentState()
  809. {
  810. if (_owner.WindowState == WindowState.FullScreen)
  811. {
  812. ToggleFullscreen();
  813. }
  814. else if (IsGameRunning)
  815. {
  816. await Task.Delay(100);
  817. _owner.AppHost?.ShowExitPrompt();
  818. }
  819. }
  820. public async void OpenSettings()
  821. {
  822. _owner.SettingsWindow = new(_owner.VirtualFileSystem, _owner.ContentManager);
  823. await _owner.SettingsWindow.ShowDialog(_owner);
  824. LoadConfigurableHotKeys();
  825. }
  826. public async void ManageProfiles()
  827. {
  828. await NavigationDialogHost.Show(_owner.AccountManager, _owner.ContentManager, _owner.VirtualFileSystem);
  829. }
  830. public async void OpenAboutWindow()
  831. {
  832. await new AboutWindow().ShowDialog(_owner);
  833. }
  834. public void ChangeLanguage(object obj)
  835. {
  836. LocaleManager.Instance.LoadDefaultLanguage();
  837. LocaleManager.Instance.LoadLanguage((string)obj);
  838. }
  839. private void ProgressHandler<T>(T state, int current, int total) where T : Enum
  840. {
  841. try
  842. {
  843. ProgressMaximum = total;
  844. ProgressValue = current;
  845. switch (state)
  846. {
  847. case PtcLoadingState ptcState:
  848. CacheLoadStatus = $"{current} / {total}";
  849. switch (ptcState)
  850. {
  851. case PtcLoadingState.Start:
  852. case PtcLoadingState.Loading:
  853. LoadHeading = LocaleManager.Instance["CompilingPPTC"];
  854. IsLoadingIndeterminate = false;
  855. break;
  856. case PtcLoadingState.Loaded:
  857. LoadHeading = string.Format(LocaleManager.Instance["LoadingHeading"], TitleName);
  858. IsLoadingIndeterminate = true;
  859. CacheLoadStatus = "";
  860. break;
  861. }
  862. break;
  863. case ShaderCacheLoadingState shaderCacheState:
  864. CacheLoadStatus = $"{current} / {total}";
  865. switch (shaderCacheState)
  866. {
  867. case ShaderCacheLoadingState.Start:
  868. case ShaderCacheLoadingState.Loading:
  869. LoadHeading = LocaleManager.Instance["CompilingShaders"];
  870. IsLoadingIndeterminate = false;
  871. break;
  872. case ShaderCacheLoadingState.Loaded:
  873. LoadHeading = string.Format(LocaleManager.Instance["LoadingHeading"], TitleName);
  874. IsLoadingIndeterminate = true;
  875. CacheLoadStatus = "";
  876. break;
  877. }
  878. break;
  879. default:
  880. throw new ArgumentException($"Unknown Progress Handler type {typeof(T)}");
  881. }
  882. }
  883. catch (Exception) { }
  884. }
  885. public void OpenUserSaveDirectory()
  886. {
  887. ApplicationData selection = SelectedApplication;
  888. if (selection != null)
  889. {
  890. Task.Run(() =>
  891. {
  892. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  893. {
  894. Dispatcher.UIThread.Post(async () =>
  895. {
  896. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogRyujinxErrorMessage"], LocaleManager.Instance["DialogInvalidTitleIdErrorMessage"]);
  897. });
  898. return;
  899. }
  900. UserId userId = new((ulong)_owner.AccountManager.LastOpenedUser.UserId.High, (ulong)_owner.AccountManager.LastOpenedUser.UserId.Low);
  901. SaveDataFilter saveDataFilter = SaveDataFilter.Make(titleIdNumber, saveType: default, userId, saveDataId: default, index: default);
  902. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  903. });
  904. }
  905. }
  906. public void ToggleFavorite()
  907. {
  908. ApplicationData selection = SelectedApplication;
  909. if (selection != null)
  910. {
  911. selection.Favorite = !selection.Favorite;
  912. _owner.ApplicationLibrary.LoadAndSaveMetaData(selection.TitleId, appMetadata =>
  913. {
  914. appMetadata.Favorite = selection.Favorite;
  915. });
  916. RefreshView();
  917. }
  918. }
  919. public void OpenModsDirectory()
  920. {
  921. ApplicationData selection = SelectedApplication;
  922. if (selection != null)
  923. {
  924. string modsBasePath = _owner.VirtualFileSystem.ModLoader.GetModsBasePath();
  925. string titleModsPath = _owner.VirtualFileSystem.ModLoader.GetTitleDir(modsBasePath, selection.TitleId);
  926. OpenHelper.OpenFolder(titleModsPath);
  927. }
  928. }
  929. public void OpenSdModsDirectory()
  930. {
  931. ApplicationData selection = SelectedApplication;
  932. if (selection != null)
  933. {
  934. string sdModsBasePath = _owner.VirtualFileSystem.ModLoader.GetSdModsBasePath();
  935. string titleModsPath = _owner.VirtualFileSystem.ModLoader.GetTitleDir(sdModsBasePath, selection.TitleId);
  936. OpenHelper.OpenFolder(titleModsPath);
  937. }
  938. }
  939. public void OpenPtcDirectory()
  940. {
  941. ApplicationData selection = SelectedApplication;
  942. if (selection != null)
  943. {
  944. string ptcDir = Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu");
  945. string mainPath = Path.Combine(ptcDir, "0");
  946. string backupPath = Path.Combine(ptcDir, "1");
  947. if (!Directory.Exists(ptcDir))
  948. {
  949. Directory.CreateDirectory(ptcDir);
  950. Directory.CreateDirectory(mainPath);
  951. Directory.CreateDirectory(backupPath);
  952. }
  953. OpenHelper.OpenFolder(ptcDir);
  954. }
  955. }
  956. public async void PurgePtcCache()
  957. {
  958. ApplicationData selection = SelectedApplication;
  959. if (selection != null)
  960. {
  961. DirectoryInfo mainDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu", "0"));
  962. DirectoryInfo backupDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "cpu", "1"));
  963. // FIXME: Found a way to reproduce the bold effect on the title name (fork?).
  964. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance["DialogWarning"],
  965. string.Format(LocaleManager.Instance["DialogPPTCDeletionMessage"], selection.TitleName),
  966. LocaleManager.Instance["InputDialogYes"],
  967. LocaleManager.Instance["InputDialogNo"],
  968. LocaleManager.Instance["RyujinxConfirm"]);
  969. List<FileInfo> cacheFiles = new();
  970. if (mainDir.Exists)
  971. {
  972. cacheFiles.AddRange(mainDir.EnumerateFiles("*.cache"));
  973. }
  974. if (backupDir.Exists)
  975. {
  976. cacheFiles.AddRange(backupDir.EnumerateFiles("*.cache"));
  977. }
  978. if (cacheFiles.Count > 0 && result == UserResult.Yes)
  979. {
  980. foreach (FileInfo file in cacheFiles)
  981. {
  982. try
  983. {
  984. file.Delete();
  985. }
  986. catch (Exception e)
  987. {
  988. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogPPTCDeletionErrorMessage"], file.Name, e));
  989. }
  990. }
  991. }
  992. }
  993. }
  994. public void OpenShaderCacheDirectory()
  995. {
  996. ApplicationData selection = SelectedApplication;
  997. if (selection != null)
  998. {
  999. string shaderCacheDir = Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "shader");
  1000. if (!Directory.Exists(shaderCacheDir))
  1001. {
  1002. Directory.CreateDirectory(shaderCacheDir);
  1003. }
  1004. OpenHelper.OpenFolder(shaderCacheDir);
  1005. }
  1006. }
  1007. public void SimulateWakeUpMessage()
  1008. {
  1009. _owner.AppHost.Device.System.SimulateWakeUpMessage();
  1010. }
  1011. public async void PurgeShaderCache()
  1012. {
  1013. ApplicationData selection = SelectedApplication;
  1014. if (selection != null)
  1015. {
  1016. DirectoryInfo shaderCacheDir = new(Path.Combine(AppDataManager.GamesDirPath, selection.TitleId, "cache", "shader"));
  1017. // FIXME: Found a way to reproduce the bold effect on the title name (fork?).
  1018. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(LocaleManager.Instance["DialogWarning"],
  1019. string.Format(LocaleManager.Instance["DialogShaderDeletionMessage"], selection.TitleName),
  1020. LocaleManager.Instance["InputDialogYes"],
  1021. LocaleManager.Instance["InputDialogNo"],
  1022. LocaleManager.Instance["RyujinxConfirm"]);
  1023. List<DirectoryInfo> oldCacheDirectories = new();
  1024. List<FileInfo> newCacheFiles = new();
  1025. if (shaderCacheDir.Exists)
  1026. {
  1027. oldCacheDirectories.AddRange(shaderCacheDir.EnumerateDirectories("*"));
  1028. newCacheFiles.AddRange(shaderCacheDir.GetFiles("*.toc"));
  1029. newCacheFiles.AddRange(shaderCacheDir.GetFiles("*.data"));
  1030. }
  1031. if ((oldCacheDirectories.Count > 0 || newCacheFiles.Count > 0) && result == UserResult.Yes)
  1032. {
  1033. foreach (DirectoryInfo directory in oldCacheDirectories)
  1034. {
  1035. try
  1036. {
  1037. directory.Delete(true);
  1038. }
  1039. catch (Exception e)
  1040. {
  1041. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogPPTCDeletionErrorMessage"], directory.Name, e));
  1042. }
  1043. }
  1044. }
  1045. foreach (FileInfo file in newCacheFiles)
  1046. {
  1047. try
  1048. {
  1049. file.Delete();
  1050. }
  1051. catch (Exception e)
  1052. {
  1053. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["ShaderCachePurgeError"], file.Name, e));
  1054. }
  1055. }
  1056. }
  1057. }
  1058. public async void CheckForUpdates()
  1059. {
  1060. if (Updater.CanUpdate(true, _owner))
  1061. {
  1062. await Updater.BeginParse(_owner, true);
  1063. }
  1064. }
  1065. public async void OpenTitleUpdateManager()
  1066. {
  1067. ApplicationData selection = SelectedApplication;
  1068. if (selection != null)
  1069. {
  1070. await new TitleUpdateWindow(_owner.VirtualFileSystem, ulong.Parse(selection.TitleId, NumberStyles.HexNumber), selection.TitleName).ShowDialog(_owner);
  1071. }
  1072. }
  1073. public async void OpenDownloadableContentManager()
  1074. {
  1075. ApplicationData selection = SelectedApplication;
  1076. if (selection != null)
  1077. {
  1078. await new DownloadableContentManagerWindow(_owner.VirtualFileSystem, ulong.Parse(selection.TitleId, NumberStyles.HexNumber), selection.TitleName).ShowDialog(_owner);
  1079. }
  1080. }
  1081. public async void OpenCheatManager()
  1082. {
  1083. ApplicationData selection = SelectedApplication;
  1084. if (selection != null)
  1085. {
  1086. await new CheatWindow(_owner.VirtualFileSystem, selection.TitleId, selection.TitleName).ShowDialog(_owner);
  1087. }
  1088. }
  1089. public async void OpenCheatManagerForCurrentApp()
  1090. {
  1091. if (!IsGameRunning)
  1092. {
  1093. return;
  1094. }
  1095. ApplicationLoader application = _owner.AppHost.Device.Application;
  1096. if (application != null)
  1097. {
  1098. await new CheatWindow(_owner.VirtualFileSystem, application.TitleIdText, application.TitleName).ShowDialog(_owner);
  1099. _owner.AppHost.Device.EnableCheats();
  1100. }
  1101. }
  1102. public void OpenDeviceSaveDirectory()
  1103. {
  1104. ApplicationData selection = SelectedApplication;
  1105. if (selection != null)
  1106. {
  1107. Task.Run(() =>
  1108. {
  1109. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  1110. {
  1111. Dispatcher.UIThread.Post(async () =>
  1112. {
  1113. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogRyujinxErrorMessage"], LocaleManager.Instance["DialogInvalidTitleIdErrorMessage"]);
  1114. });
  1115. return;
  1116. }
  1117. var saveDataFilter = SaveDataFilter.Make(titleIdNumber, SaveDataType.Device, userId: default, saveDataId: default, index: default);
  1118. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  1119. });
  1120. }
  1121. }
  1122. public void OpenBcatSaveDirectory()
  1123. {
  1124. ApplicationData selection = SelectedApplication;
  1125. if (selection != null)
  1126. {
  1127. Task.Run(() =>
  1128. {
  1129. if (!ulong.TryParse(selection.TitleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNumber))
  1130. {
  1131. Dispatcher.UIThread.Post(async () =>
  1132. {
  1133. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogRyujinxErrorMessage"], LocaleManager.Instance["DialogInvalidTitleIdErrorMessage"]);
  1134. });
  1135. return;
  1136. }
  1137. var saveDataFilter = SaveDataFilter.Make(titleIdNumber, SaveDataType.Bcat, userId: default, saveDataId: default, index: default);
  1138. OpenSaveDirectory(in saveDataFilter, selection, titleIdNumber);
  1139. });
  1140. }
  1141. }
  1142. private void OpenSaveDirectory(in SaveDataFilter filter, ApplicationData data, ulong titleId)
  1143. {
  1144. ApplicationHelper.OpenSaveDir(in filter, titleId, data.ControlHolder, data.TitleName);
  1145. }
  1146. private async void ExtractLogo()
  1147. {
  1148. var selection = SelectedApplication;
  1149. if (selection != null)
  1150. {
  1151. await ApplicationHelper.ExtractSection(NcaSectionType.Logo, selection.Path);
  1152. }
  1153. }
  1154. private async void ExtractRomFs()
  1155. {
  1156. var selection = SelectedApplication;
  1157. if (selection != null)
  1158. {
  1159. await ApplicationHelper.ExtractSection(NcaSectionType.Data, selection.Path);
  1160. }
  1161. }
  1162. private async void ExtractExeFs()
  1163. {
  1164. var selection = SelectedApplication;
  1165. if (selection != null)
  1166. {
  1167. await ApplicationHelper.ExtractSection(NcaSectionType.Code, selection.Path);
  1168. }
  1169. }
  1170. public void CloseWindow()
  1171. {
  1172. _owner.Close();
  1173. }
  1174. private async Task HandleFirmwareInstallation(string filename)
  1175. {
  1176. try
  1177. {
  1178. SystemVersion firmwareVersion = _owner.ContentManager.VerifyFirmwarePackage(filename);
  1179. if (firmwareVersion == null)
  1180. {
  1181. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogFirmwareInstallerFirmwareNotFoundErrorMessage"], filename));
  1182. return;
  1183. }
  1184. string dialogTitle = string.Format(LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallTitle"], firmwareVersion.VersionString);
  1185. SystemVersion currentVersion = _owner.ContentManager.GetCurrentFirmwareVersion();
  1186. string dialogMessage = string.Format(LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallMessage"], firmwareVersion.VersionString);
  1187. if (currentVersion != null)
  1188. {
  1189. dialogMessage += string.Format(LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallSubMessage"], currentVersion.VersionString);
  1190. }
  1191. dialogMessage += LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallConfirmMessage"];
  1192. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
  1193. dialogTitle,
  1194. dialogMessage,
  1195. LocaleManager.Instance["InputDialogYes"],
  1196. LocaleManager.Instance["InputDialogNo"],
  1197. LocaleManager.Instance["RyujinxConfirm"]);
  1198. UpdateWaitWindow waitingDialog = ContentDialogHelper.CreateWaitingDialog(dialogTitle, LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallWaitMessage"]);
  1199. if (result == UserResult.Yes)
  1200. {
  1201. Logger.Info?.Print(LogClass.Application, $"Installing firmware {firmwareVersion.VersionString}");
  1202. Thread thread = new(() =>
  1203. {
  1204. Dispatcher.UIThread.InvokeAsync(delegate
  1205. {
  1206. waitingDialog.Show();
  1207. });
  1208. try
  1209. {
  1210. _owner.ContentManager.InstallFirmware(filename);
  1211. Dispatcher.UIThread.InvokeAsync(async delegate
  1212. {
  1213. waitingDialog.Close();
  1214. string message = string.Format(LocaleManager.Instance["DialogFirmwareInstallerFirmwareInstallSuccessMessage"], firmwareVersion.VersionString);
  1215. await ContentDialogHelper.CreateInfoDialog(dialogTitle, message, LocaleManager.Instance["InputDialogOk"], "", LocaleManager.Instance["RyujinxInfo"]);
  1216. Logger.Info?.Print(LogClass.Application, message);
  1217. // Purge Applet Cache.
  1218. DirectoryInfo miiEditorCacheFolder = new DirectoryInfo(Path.Combine(AppDataManager.GamesDirPath, "0100000000001009", "cache"));
  1219. if (miiEditorCacheFolder.Exists)
  1220. {
  1221. miiEditorCacheFolder.Delete(true);
  1222. }
  1223. });
  1224. }
  1225. catch (Exception ex)
  1226. {
  1227. Dispatcher.UIThread.InvokeAsync(async () =>
  1228. {
  1229. waitingDialog.Close();
  1230. await ContentDialogHelper.CreateErrorDialog(ex.Message);
  1231. });
  1232. }
  1233. finally
  1234. {
  1235. _owner.RefreshFirmwareStatus();
  1236. }
  1237. });
  1238. thread.Name = "GUI.FirmwareInstallerThread";
  1239. thread.Start();
  1240. }
  1241. }
  1242. catch (LibHac.Common.Keys.MissingKeyException ex)
  1243. {
  1244. Logger.Error?.Print(LogClass.Application, ex.ToString());
  1245. Dispatcher.UIThread.Post(async () => await UserErrorDialog.ShowUserErrorDialog(UserError.NoKeys, _owner));
  1246. }
  1247. catch (Exception ex)
  1248. {
  1249. await ContentDialogHelper.CreateErrorDialog(ex.Message);
  1250. }
  1251. }
  1252. public async void InstallFirmwareFromFile()
  1253. {
  1254. OpenFileDialog dialog = new() { AllowMultiple = false };
  1255. dialog.Filters.Add(new FileDialogFilter { Name = LocaleManager.Instance["FileDialogAllTypes"], Extensions = { "xci", "zip" } });
  1256. dialog.Filters.Add(new FileDialogFilter { Name = "XCI", Extensions = { "xci" } });
  1257. dialog.Filters.Add(new FileDialogFilter { Name = "ZIP", Extensions = { "zip" } });
  1258. string[] file = await dialog.ShowAsync(_owner);
  1259. if (file != null && file.Length > 0)
  1260. {
  1261. await HandleFirmwareInstallation(file[0]);
  1262. }
  1263. }
  1264. public async void InstallFirmwareFromFolder()
  1265. {
  1266. OpenFolderDialog dialog = new();
  1267. string folder = await dialog.ShowAsync(_owner);
  1268. if (!string.IsNullOrWhiteSpace(folder))
  1269. {
  1270. await HandleFirmwareInstallation(folder);
  1271. }
  1272. }
  1273. }
  1274. }