MainWindow.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. using Gtk;
  2. using JsonPrettyPrinterPlus;
  3. using Ryujinx.Audio;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.Configuration;
  6. using Ryujinx.Graphics.OpenGL;
  7. using Ryujinx.HLE.FileSystem;
  8. using Ryujinx.Profiler;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using Utf8Json;
  17. using Utf8Json.Resolvers;
  18. using GUI = Gtk.Builder.ObjectAttribute;
  19. namespace Ryujinx.Ui
  20. {
  21. public class MainWindow : Window
  22. {
  23. private static HLE.Switch _device;
  24. private static Renderer _renderer;
  25. private static IAalOutput _audioOut;
  26. private static GlScreen _screen;
  27. private static ListStore _tableStore;
  28. private static bool _updatingGameTable;
  29. private static bool _gameLoaded;
  30. private static bool _ending;
  31. private static TreeView _treeView;
  32. #pragma warning disable CS0649
  33. #pragma warning disable IDE0044
  34. [GUI] Window _mainWin;
  35. [GUI] CheckMenuItem _fullScreen;
  36. [GUI] MenuItem _stopEmulation;
  37. [GUI] CheckMenuItem _favToggle;
  38. [GUI] MenuItem _firmwareInstallFile;
  39. [GUI] MenuItem _firmwareInstallDirectory;
  40. [GUI] CheckMenuItem _iconToggle;
  41. [GUI] CheckMenuItem _appToggle;
  42. [GUI] CheckMenuItem _developerToggle;
  43. [GUI] CheckMenuItem _versionToggle;
  44. [GUI] CheckMenuItem _timePlayedToggle;
  45. [GUI] CheckMenuItem _lastPlayedToggle;
  46. [GUI] CheckMenuItem _fileExtToggle;
  47. [GUI] CheckMenuItem _fileSizeToggle;
  48. [GUI] CheckMenuItem _pathToggle;
  49. [GUI] TreeView _gameTable;
  50. [GUI] TreeSelection _gameTableSelection;
  51. [GUI] Label _progressLabel;
  52. [GUI] Label _firmwareVersionLabel;
  53. [GUI] LevelBar _progressBar;
  54. #pragma warning restore CS0649
  55. #pragma warning restore IDE0044
  56. public MainWindow() : this(new Builder("Ryujinx.Ui.MainWindow.glade")) { }
  57. private MainWindow(Builder builder) : base(builder.GetObject("_mainWin").Handle)
  58. {
  59. builder.Autoconnect(this);
  60. DeleteEvent += Window_Close;
  61. ApplicationLibrary.ApplicationAdded += Application_Added;
  62. _gameTable.ButtonReleaseEvent += Row_Clicked;
  63. bool continueWithStartup = Migration.PromptIfMigrationNeededForStartup(this, out bool migrationNeeded);
  64. if (!continueWithStartup)
  65. {
  66. End();
  67. }
  68. _renderer = new Renderer();
  69. _audioOut = InitializeAudioEngine();
  70. // TODO: Initialization and dispose of HLE.Switch when starting/stoping emulation.
  71. _device = InitializeSwitchInstance();
  72. if (migrationNeeded)
  73. {
  74. bool migrationSuccessful = Migration.DoMigrationForStartup(this, _device);
  75. if (!migrationSuccessful)
  76. {
  77. End();
  78. }
  79. }
  80. _treeView = _gameTable;
  81. ApplyTheme();
  82. _mainWin.Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png");
  83. _stopEmulation.Sensitive = false;
  84. if (ConfigurationState.Instance.Ui.GuiColumns.FavColumn) _favToggle.Active = true;
  85. if (ConfigurationState.Instance.Ui.GuiColumns.IconColumn) _iconToggle.Active = true;
  86. if (ConfigurationState.Instance.Ui.GuiColumns.AppColumn) _appToggle.Active = true;
  87. if (ConfigurationState.Instance.Ui.GuiColumns.DevColumn) _developerToggle.Active = true;
  88. if (ConfigurationState.Instance.Ui.GuiColumns.VersionColumn) _versionToggle.Active = true;
  89. if (ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn) _timePlayedToggle.Active = true;
  90. if (ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn) _lastPlayedToggle.Active = true;
  91. if (ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn) _fileExtToggle.Active = true;
  92. if (ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn) _fileSizeToggle.Active = true;
  93. if (ConfigurationState.Instance.Ui.GuiColumns.PathColumn) _pathToggle.Active = true;
  94. _gameTable.Model = _tableStore = new ListStore(
  95. typeof(bool),
  96. typeof(Gdk.Pixbuf),
  97. typeof(string),
  98. typeof(string),
  99. typeof(string),
  100. typeof(string),
  101. typeof(string),
  102. typeof(string),
  103. typeof(string),
  104. typeof(string));
  105. _tableStore.SetSortFunc(5, TimePlayedSort);
  106. _tableStore.SetSortFunc(6, LastPlayedSort);
  107. _tableStore.SetSortFunc(8, FileSizeSort);
  108. _tableStore.SetSortColumnId(0, SortType.Descending);
  109. UpdateColumns();
  110. #pragma warning disable CS4014
  111. UpdateGameTable();
  112. #pragma warning restore CS4014
  113. Task.Run(RefreshFirmwareLabel);
  114. }
  115. internal static void ApplyTheme()
  116. {
  117. if (!ConfigurationState.Instance.Ui.EnableCustomTheme)
  118. {
  119. return;
  120. }
  121. if (File.Exists(ConfigurationState.Instance.Ui.CustomThemePath) && (System.IO.Path.GetExtension(ConfigurationState.Instance.Ui.CustomThemePath) == ".css"))
  122. {
  123. CssProvider cssProvider = new CssProvider();
  124. cssProvider.LoadFromPath(ConfigurationState.Instance.Ui.CustomThemePath);
  125. StyleContext.AddProviderForScreen(Gdk.Screen.Default, cssProvider, 800);
  126. }
  127. else
  128. {
  129. Logger.PrintWarning(LogClass.Application, $"The \"custom_theme_path\" section in \"Config.json\" contains an invalid path: \"{ConfigurationState.Instance.Ui.CustomThemePath}\".");
  130. }
  131. }
  132. private void UpdateColumns()
  133. {
  134. foreach (TreeViewColumn column in _gameTable.Columns)
  135. {
  136. _gameTable.RemoveColumn(column);
  137. }
  138. CellRendererToggle favToggle = new CellRendererToggle();
  139. favToggle.Toggled += FavToggle_Toggled;
  140. if (ConfigurationState.Instance.Ui.GuiColumns.FavColumn) _gameTable.AppendColumn("Fav", favToggle, "active", 0);
  141. if (ConfigurationState.Instance.Ui.GuiColumns.IconColumn) _gameTable.AppendColumn("Icon", new CellRendererPixbuf(), "pixbuf", 1);
  142. if (ConfigurationState.Instance.Ui.GuiColumns.AppColumn) _gameTable.AppendColumn("Application", new CellRendererText(), "text", 2);
  143. if (ConfigurationState.Instance.Ui.GuiColumns.DevColumn) _gameTable.AppendColumn("Developer", new CellRendererText(), "text", 3);
  144. if (ConfigurationState.Instance.Ui.GuiColumns.VersionColumn) _gameTable.AppendColumn("Version", new CellRendererText(), "text", 4);
  145. if (ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn) _gameTable.AppendColumn("Time Played", new CellRendererText(), "text", 5);
  146. if (ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn) _gameTable.AppendColumn("Last Played", new CellRendererText(), "text", 6);
  147. if (ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn) _gameTable.AppendColumn("File Ext", new CellRendererText(), "text", 7);
  148. if (ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn) _gameTable.AppendColumn("File Size", new CellRendererText(), "text", 8);
  149. if (ConfigurationState.Instance.Ui.GuiColumns.PathColumn) _gameTable.AppendColumn("Path", new CellRendererText(), "text", 9);
  150. foreach (TreeViewColumn column in _gameTable.Columns)
  151. {
  152. if (column.Title == "Fav" && ConfigurationState.Instance.Ui.GuiColumns.FavColumn) column.SortColumnId = 0;
  153. else if (column.Title == "Application" && ConfigurationState.Instance.Ui.GuiColumns.AppColumn) column.SortColumnId = 2;
  154. else if (column.Title == "Developer" && ConfigurationState.Instance.Ui.GuiColumns.DevColumn) column.SortColumnId = 3;
  155. else if (column.Title == "Version" && ConfigurationState.Instance.Ui.GuiColumns.VersionColumn) column.SortColumnId = 4;
  156. else if (column.Title == "Time Played" && ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn) column.SortColumnId = 5;
  157. else if (column.Title == "Last Played" && ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn) column.SortColumnId = 6;
  158. else if (column.Title == "File Ext" && ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn) column.SortColumnId = 7;
  159. else if (column.Title == "File Size" && ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn) column.SortColumnId = 8;
  160. else if (column.Title == "Path" && ConfigurationState.Instance.Ui.GuiColumns.PathColumn) column.SortColumnId = 9;
  161. }
  162. }
  163. private HLE.Switch InitializeSwitchInstance()
  164. {
  165. HLE.Switch instance = new HLE.Switch(_renderer, _audioOut);
  166. instance.Initialize();
  167. return instance;
  168. }
  169. internal static async Task UpdateGameTable()
  170. {
  171. if (_updatingGameTable)
  172. {
  173. return;
  174. }
  175. _updatingGameTable = true;
  176. _tableStore.Clear();
  177. await Task.Run(() => ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs,
  178. _device.System.KeySet, _device.System.State.DesiredTitleLanguage, _device.System.FsClient,
  179. _device.FileSystem));
  180. _updatingGameTable = false;
  181. }
  182. internal void LoadApplication(string path)
  183. {
  184. if (_gameLoaded)
  185. {
  186. GtkDialog.CreateErrorDialog("A game has already been loaded. Please close the emulator and try again");
  187. }
  188. else
  189. {
  190. Logger.RestartTime();
  191. // TODO: Move this somewhere else + reloadable?
  192. Ryujinx.Graphics.Gpu.GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
  193. if (Directory.Exists(path))
  194. {
  195. string[] romFsFiles = Directory.GetFiles(path, "*.istorage");
  196. if (romFsFiles.Length == 0)
  197. {
  198. romFsFiles = Directory.GetFiles(path, "*.romfs");
  199. }
  200. if (romFsFiles.Length > 0)
  201. {
  202. Logger.PrintInfo(LogClass.Application, "Loading as cart with RomFS.");
  203. _device.LoadCart(path, romFsFiles[0]);
  204. }
  205. else
  206. {
  207. Logger.PrintInfo(LogClass.Application, "Loading as cart WITHOUT RomFS.");
  208. _device.LoadCart(path);
  209. }
  210. }
  211. else if (File.Exists(path))
  212. {
  213. switch (System.IO.Path.GetExtension(path).ToLowerInvariant())
  214. {
  215. case ".xci":
  216. Logger.PrintInfo(LogClass.Application, "Loading as XCI.");
  217. _device.LoadXci(path);
  218. break;
  219. case ".nca":
  220. Logger.PrintInfo(LogClass.Application, "Loading as NCA.");
  221. _device.LoadNca(path);
  222. break;
  223. case ".nsp":
  224. case ".pfs0":
  225. Logger.PrintInfo(LogClass.Application, "Loading as NSP.");
  226. _device.LoadNsp(path);
  227. break;
  228. default:
  229. Logger.PrintInfo(LogClass.Application, "Loading as homebrew.");
  230. try
  231. {
  232. _device.LoadProgram(path);
  233. }
  234. catch (ArgumentOutOfRangeException)
  235. {
  236. Logger.PrintError(LogClass.Application, "The file which you have specified is unsupported by Ryujinx.");
  237. }
  238. break;
  239. }
  240. }
  241. else
  242. {
  243. Logger.PrintWarning(LogClass.Application, "Please specify a valid XCI/NCA/NSP/PFS0/NRO file.");
  244. End();
  245. }
  246. #if MACOS_BUILD
  247. CreateGameWindow();
  248. #else
  249. new Thread(CreateGameWindow).Start();
  250. #endif
  251. _gameLoaded = true;
  252. _stopEmulation.Sensitive = true;
  253. _firmwareInstallFile.Sensitive = false;
  254. _firmwareInstallDirectory.Sensitive = false;
  255. DiscordIntegrationModule.SwitchToPlayingState(_device.System.TitleIdText, _device.System.TitleName);
  256. ApplicationLibrary.LoadAndSaveMetaData(_device.System.TitleIdText, appMetadata =>
  257. {
  258. appMetadata.LastPlayed = DateTime.UtcNow.ToString();
  259. });
  260. }
  261. }
  262. private static void CreateGameWindow()
  263. {
  264. _device.Hid.InitializePrimaryController(ConfigurationState.Instance.Hid.ControllerType);
  265. using (_screen = new GlScreen(_device, _renderer))
  266. {
  267. _screen.MainLoop();
  268. End();
  269. }
  270. }
  271. private static void End()
  272. {
  273. if (_ending)
  274. {
  275. return;
  276. }
  277. _ending = true;
  278. if (_gameLoaded)
  279. {
  280. ApplicationLibrary.LoadAndSaveMetaData(_device.System.TitleIdText, appMetadata =>
  281. {
  282. DateTime lastPlayedDateTime = DateTime.Parse(appMetadata.LastPlayed);
  283. double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds;
  284. appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero);
  285. });
  286. }
  287. Profile.FinishProfiling();
  288. _device?.Dispose();
  289. _audioOut?.Dispose();
  290. Logger.Shutdown();
  291. Environment.Exit(0);
  292. }
  293. /// <summary>
  294. /// Picks an <see cref="IAalOutput"/> audio output renderer supported on this machine
  295. /// </summary>
  296. /// <returns>An <see cref="IAalOutput"/> supported by this machine</returns>
  297. private static IAalOutput InitializeAudioEngine()
  298. {
  299. if (OpenALAudioOut.IsSupported)
  300. {
  301. return new OpenALAudioOut();
  302. }
  303. else if (SoundIoAudioOut.IsSupported)
  304. {
  305. return new SoundIoAudioOut();
  306. }
  307. else
  308. {
  309. return new DummyAudioOut();
  310. }
  311. }
  312. //Events
  313. private void Application_Added(object sender, ApplicationAddedEventArgs args)
  314. {
  315. Application.Invoke(delegate
  316. {
  317. _tableStore.AppendValues(
  318. args.AppData.Favorite,
  319. new Gdk.Pixbuf(args.AppData.Icon, 75, 75),
  320. $"{args.AppData.TitleName}\n{args.AppData.TitleId.ToUpper()}",
  321. args.AppData.Developer,
  322. args.AppData.Version,
  323. args.AppData.TimePlayed,
  324. args.AppData.LastPlayed,
  325. args.AppData.FileExtension,
  326. args.AppData.FileSize,
  327. args.AppData.Path);
  328. _progressLabel.Text = $"{args.NumAppsLoaded}/{args.NumAppsFound} Games Loaded";
  329. _progressBar.Value = (float)args.NumAppsLoaded / args.NumAppsFound;
  330. });
  331. }
  332. private void FavToggle_Toggled(object sender, ToggledArgs args)
  333. {
  334. _tableStore.GetIter(out TreeIter treeIter, new TreePath(args.Path));
  335. string titleId = _tableStore.GetValue(treeIter, 2).ToString().Split("\n")[1].ToLower();
  336. bool newToggleValue = !(bool)_tableStore.GetValue(treeIter, 0);
  337. _tableStore.SetValue(treeIter, 0, newToggleValue);
  338. ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
  339. {
  340. appMetadata.Favorite = newToggleValue;
  341. });
  342. }
  343. private void Row_Activated(object sender, RowActivatedArgs args)
  344. {
  345. _gameTableSelection.GetSelected(out TreeIter treeIter);
  346. string path = (string)_tableStore.GetValue(treeIter, 9);
  347. LoadApplication(path);
  348. }
  349. private void Row_Clicked(object sender, ButtonReleaseEventArgs args)
  350. {
  351. if (args.Event.Button != 3) return;
  352. _gameTableSelection.GetSelected(out TreeIter treeIter);
  353. if (treeIter.UserData == IntPtr.Zero) return;
  354. GameTableContextMenu contextMenu = new GameTableContextMenu(_tableStore, treeIter, _device.System.FsClient);
  355. contextMenu.ShowAll();
  356. contextMenu.PopupAtPointer(null);
  357. }
  358. private void Load_Application_File(object sender, EventArgs args)
  359. {
  360. FileChooserDialog fileChooser = new FileChooserDialog("Choose the file to open", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  361. fileChooser.Filter = new FileFilter();
  362. fileChooser.Filter.AddPattern("*.nsp" );
  363. fileChooser.Filter.AddPattern("*.pfs0");
  364. fileChooser.Filter.AddPattern("*.xci" );
  365. fileChooser.Filter.AddPattern("*.nca" );
  366. fileChooser.Filter.AddPattern("*.nro" );
  367. fileChooser.Filter.AddPattern("*.nso" );
  368. if (fileChooser.Run() == (int)ResponseType.Accept)
  369. {
  370. LoadApplication(fileChooser.Filename);
  371. }
  372. fileChooser.Dispose();
  373. }
  374. private void Load_Application_Folder(object sender, EventArgs args)
  375. {
  376. FileChooserDialog fileChooser = new FileChooserDialog("Choose the folder to open", this, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  377. if (fileChooser.Run() == (int)ResponseType.Accept)
  378. {
  379. LoadApplication(fileChooser.Filename);
  380. }
  381. fileChooser.Dispose();
  382. }
  383. private void Open_Ryu_Folder(object sender, EventArgs args)
  384. {
  385. Process.Start(new ProcessStartInfo()
  386. {
  387. FileName = new VirtualFileSystem().GetBasePath(),
  388. UseShellExecute = true,
  389. Verb = "open"
  390. });
  391. }
  392. private void Exit_Pressed(object sender, EventArgs args)
  393. {
  394. _screen?.Exit();
  395. End();
  396. }
  397. private void Window_Close(object sender, DeleteEventArgs args)
  398. {
  399. _screen?.Exit();
  400. End();
  401. }
  402. private void StopEmulation_Pressed(object sender, EventArgs args)
  403. {
  404. // TODO: Write logic to kill running game
  405. _gameLoaded = false;
  406. }
  407. private void Installer_File_Pressed(object o, EventArgs args)
  408. {
  409. FileChooserDialog fileChooser = new FileChooserDialog("Choose the firmware file to open",
  410. this,
  411. FileChooserAction.Open,
  412. "Cancel",
  413. ResponseType.Cancel,
  414. "Open",
  415. ResponseType.Accept);
  416. fileChooser.Filter = new FileFilter();
  417. fileChooser.Filter.AddPattern("*.zip");
  418. fileChooser.Filter.AddPattern("*.xci");
  419. HandleInstallerDialog(fileChooser);
  420. }
  421. private void Installer_Directory_Pressed(object o, EventArgs args)
  422. {
  423. FileChooserDialog directoryChooser = new FileChooserDialog("Choose the firmware directory to open",
  424. this,
  425. FileChooserAction.SelectFolder,
  426. "Cancel",
  427. ResponseType.Cancel,
  428. "Open",
  429. ResponseType.Accept);
  430. HandleInstallerDialog(directoryChooser);
  431. }
  432. private void RefreshFirmwareLabel()
  433. {
  434. var currentFirmware = _device.System.GetCurrentFirmwareVersion();
  435. GLib.Idle.Add(new GLib.IdleHandler(() =>
  436. {
  437. _firmwareVersionLabel.Text = currentFirmware != null ? currentFirmware.VersionString : "0.0.0";
  438. return false;
  439. }));
  440. }
  441. private void HandleInstallerDialog(FileChooserDialog fileChooser)
  442. {
  443. if (fileChooser.Run() == (int)ResponseType.Accept)
  444. {
  445. MessageDialog dialog = null;
  446. try
  447. {
  448. string filename = fileChooser.Filename;
  449. fileChooser.Dispose();
  450. var firmwareVersion = _device.System.VerifyFirmwarePackage(filename);
  451. if (firmwareVersion == null)
  452. {
  453. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  454. dialog.Text = "Firmware not found.";
  455. dialog.SecondaryText = $"A valid system firmware was not found in {filename}.";
  456. Logger.PrintError(LogClass.Application, $"A valid system firmware was not found in {filename}.");
  457. dialog.Run();
  458. dialog.Hide();
  459. dialog.Dispose();
  460. return;
  461. }
  462. var currentVersion = _device.System.GetCurrentFirmwareVersion();
  463. string dialogMessage = $"System version {firmwareVersion.VersionString} will be installed.";
  464. if (currentVersion != null)
  465. {
  466. dialogMessage += $"This will replace the current system version {currentVersion.VersionString}. ";
  467. }
  468. dialogMessage += "Do you want to continue?";
  469. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, false, "");
  470. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  471. dialog.SecondaryText = dialogMessage;
  472. int response = dialog.Run();
  473. dialog.Dispose();
  474. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.None, false, "");
  475. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  476. dialog.SecondaryText = "Installing firmware...";
  477. if (response == (int)ResponseType.Yes)
  478. {
  479. Logger.PrintInfo(LogClass.Application, $"Installing firmware {firmwareVersion.VersionString}");
  480. Thread thread = new Thread(() =>
  481. {
  482. GLib.Idle.Add(new GLib.IdleHandler(() =>
  483. {
  484. dialog.Run();
  485. return false;
  486. }));
  487. try
  488. {
  489. _device.System.InstallFirmware(filename);
  490. GLib.Idle.Add(new GLib.IdleHandler(() =>
  491. {
  492. dialog.Dispose();
  493. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  494. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  495. dialog.SecondaryText = $"System version {firmwareVersion.VersionString} successfully installed.";
  496. Logger.PrintInfo(LogClass.Application, $"System version {firmwareVersion.VersionString} successfully installed.");
  497. dialog.Run();
  498. dialog.Dispose();
  499. return false;
  500. }));
  501. }
  502. catch (Exception ex)
  503. {
  504. GLib.Idle.Add(new GLib.IdleHandler(() =>
  505. {
  506. dialog.Dispose();
  507. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  508. dialog.Text = $"Install Firmware {firmwareVersion.VersionString} Failed.";
  509. dialog.SecondaryText = $"An error occured while installing system version {firmwareVersion.VersionString}." +
  510. " Please check logs for more info.";
  511. Logger.PrintError(LogClass.Application, ex.Message);
  512. dialog.Run();
  513. dialog.Dispose();
  514. return false;
  515. }));
  516. }
  517. finally
  518. {
  519. RefreshFirmwareLabel();
  520. }
  521. });
  522. thread.Name = "GUI.FirmwareInstallerThread";
  523. thread.Start();
  524. }
  525. else
  526. {
  527. dialog.Dispose();
  528. }
  529. }
  530. catch (Exception ex)
  531. {
  532. if (dialog != null)
  533. {
  534. dialog.Dispose();
  535. }
  536. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  537. dialog.Text = "Parsing Firmware Failed.";
  538. dialog.SecondaryText = "An error occured while parsing firmware. Please check the logs for more info.";
  539. Logger.PrintError(LogClass.Application, ex.Message);
  540. dialog.Run();
  541. dialog.Dispose();
  542. }
  543. }
  544. else
  545. {
  546. fileChooser.Dispose();
  547. }
  548. }
  549. private void FullScreen_Toggled(object o, EventArgs args)
  550. {
  551. if (_fullScreen.Active)
  552. {
  553. Fullscreen();
  554. }
  555. else
  556. {
  557. Unfullscreen();
  558. }
  559. }
  560. private void Settings_Pressed(object sender, EventArgs args)
  561. {
  562. SwitchSettings settingsWin = new SwitchSettings();
  563. settingsWin.Show();
  564. }
  565. private void Update_Pressed(object sender, EventArgs args)
  566. {
  567. string ryuUpdater = System.IO.Path.Combine(new VirtualFileSystem().GetBasePath(), "RyuUpdater.exe");
  568. try
  569. {
  570. Process.Start(new ProcessStartInfo(ryuUpdater, "/U") { UseShellExecute = true });
  571. }
  572. catch(System.ComponentModel.Win32Exception)
  573. {
  574. GtkDialog.CreateErrorDialog("Update canceled by user or updater was not found");
  575. }
  576. }
  577. private void About_Pressed(object sender, EventArgs args)
  578. {
  579. AboutWindow aboutWin = new AboutWindow();
  580. aboutWin.Show();
  581. }
  582. private void Fav_Toggled(object sender, EventArgs args)
  583. {
  584. ConfigurationState.Instance.Ui.GuiColumns.FavColumn.Value = _favToggle.Active;
  585. SaveConfig();
  586. UpdateColumns();
  587. }
  588. private void Icon_Toggled(object sender, EventArgs args)
  589. {
  590. ConfigurationState.Instance.Ui.GuiColumns.IconColumn.Value = _iconToggle.Active;
  591. SaveConfig();
  592. UpdateColumns();
  593. }
  594. private void Title_Toggled(object sender, EventArgs args)
  595. {
  596. ConfigurationState.Instance.Ui.GuiColumns.AppColumn.Value = _appToggle.Active;
  597. SaveConfig();
  598. UpdateColumns();
  599. }
  600. private void Developer_Toggled(object sender, EventArgs args)
  601. {
  602. ConfigurationState.Instance.Ui.GuiColumns.DevColumn.Value = _developerToggle.Active;
  603. SaveConfig();
  604. UpdateColumns();
  605. }
  606. private void Version_Toggled(object sender, EventArgs args)
  607. {
  608. ConfigurationState.Instance.Ui.GuiColumns.VersionColumn.Value = _versionToggle.Active;
  609. SaveConfig();
  610. UpdateColumns();
  611. }
  612. private void TimePlayed_Toggled(object sender, EventArgs args)
  613. {
  614. ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn.Value = _timePlayedToggle.Active;
  615. SaveConfig();
  616. UpdateColumns();
  617. }
  618. private void LastPlayed_Toggled(object sender, EventArgs args)
  619. {
  620. ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn.Value = _lastPlayedToggle.Active;
  621. SaveConfig();
  622. UpdateColumns();
  623. }
  624. private void FileExt_Toggled(object sender, EventArgs args)
  625. {
  626. ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn.Value = _fileExtToggle.Active;
  627. SaveConfig();
  628. UpdateColumns();
  629. }
  630. private void FileSize_Toggled(object sender, EventArgs args)
  631. {
  632. ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn.Value = _fileSizeToggle.Active;
  633. SaveConfig();
  634. UpdateColumns();
  635. }
  636. private void Path_Toggled(object sender, EventArgs args)
  637. {
  638. ConfigurationState.Instance.Ui.GuiColumns.PathColumn.Value = _pathToggle.Active;
  639. SaveConfig();
  640. UpdateColumns();
  641. }
  642. private void RefreshList_Pressed(object sender, ButtonReleaseEventArgs args)
  643. {
  644. #pragma warning disable CS4014
  645. UpdateGameTable();
  646. #pragma warning restore CS4014
  647. }
  648. private static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  649. {
  650. string aValue = model.GetValue(a, 5).ToString();
  651. string bValue = model.GetValue(b, 5).ToString();
  652. if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "mins")
  653. {
  654. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 60).ToString();
  655. }
  656. else if (aValue.Length > 3 && aValue.Substring(aValue.Length - 3) == "hrs")
  657. {
  658. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 4)) * 3600).ToString();
  659. }
  660. else if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "days")
  661. {
  662. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 86400).ToString();
  663. }
  664. else
  665. {
  666. aValue = aValue.Substring(0, aValue.Length - 1);
  667. }
  668. if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "mins")
  669. {
  670. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 60).ToString();
  671. }
  672. else if (bValue.Length > 3 && bValue.Substring(bValue.Length - 3) == "hrs")
  673. {
  674. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 4)) * 3600).ToString();
  675. }
  676. else if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "days")
  677. {
  678. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 86400).ToString();
  679. }
  680. else
  681. {
  682. bValue = bValue.Substring(0, bValue.Length - 1);
  683. }
  684. if (float.Parse(aValue) > float.Parse(bValue))
  685. {
  686. return -1;
  687. }
  688. else if (float.Parse(bValue) > float.Parse(aValue))
  689. {
  690. return 1;
  691. }
  692. else
  693. {
  694. return 0;
  695. }
  696. }
  697. private static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  698. {
  699. string aValue = model.GetValue(a, 6).ToString();
  700. string bValue = model.GetValue(b, 6).ToString();
  701. if (aValue == "Never")
  702. {
  703. aValue = DateTime.UnixEpoch.ToString();
  704. }
  705. if (bValue == "Never")
  706. {
  707. bValue = DateTime.UnixEpoch.ToString();
  708. }
  709. return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
  710. }
  711. private static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
  712. {
  713. string aValue = model.GetValue(a, 8).ToString();
  714. string bValue = model.GetValue(b, 8).ToString();
  715. if (aValue.Substring(aValue.Length - 2) == "GB")
  716. {
  717. aValue = (float.Parse(aValue[0..^2]) * 1024).ToString();
  718. }
  719. else
  720. {
  721. aValue = aValue[0..^2];
  722. }
  723. if (bValue.Substring(bValue.Length - 2) == "GB")
  724. {
  725. bValue = (float.Parse(bValue[0..^2]) * 1024).ToString();
  726. }
  727. else
  728. {
  729. bValue = bValue[0..^2];
  730. }
  731. if (float.Parse(aValue) > float.Parse(bValue))
  732. {
  733. return -1;
  734. }
  735. else if (float.Parse(bValue) > float.Parse(aValue))
  736. {
  737. return 1;
  738. }
  739. else
  740. {
  741. return 0;
  742. }
  743. }
  744. public static void SaveConfig()
  745. {
  746. ConfigurationState.Instance.ToFileFormat().SaveConfig(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.json"));
  747. }
  748. }
  749. }