MainWindow.cs 38 KB

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