MainWindow.cs 36 KB

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