MainWindow.cs 38 KB

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