MainWindow.cs 36 KB

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