MainWindow.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. using ARMeilleure.Translation;
  2. using ARMeilleure.Translation.PTC;
  3. using Gtk;
  4. using LibHac.Common;
  5. using LibHac.Ns;
  6. using Ryujinx.Audio;
  7. using Ryujinx.Common.Configuration;
  8. using Ryujinx.Common.Logging;
  9. using Ryujinx.Common.System;
  10. using Ryujinx.Configuration;
  11. using Ryujinx.Graphics.GAL;
  12. using Ryujinx.Graphics.OpenGL;
  13. using Ryujinx.HLE.FileSystem;
  14. using Ryujinx.HLE.FileSystem.Content;
  15. using Ryujinx.HLE.HOS;
  16. using Ryujinx.Ui.Diagnostic;
  17. using System;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Reflection;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using GUI = Gtk.Builder.ObjectAttribute;
  24. namespace Ryujinx.Ui
  25. {
  26. public class MainWindow : Window
  27. {
  28. private static VirtualFileSystem _virtualFileSystem;
  29. private static ContentManager _contentManager;
  30. private static UserChannelPersistence _userChannelPersistence;
  31. private static WindowsMultimediaTimerResolution _windowsMultimediaTimerResolution;
  32. private static HLE.Switch _emulationContext;
  33. private static GlRenderer _glWidget;
  34. private static GtkHostUiHandler _uiHandler;
  35. public static GlRenderer GlWidget => _glWidget;
  36. private static AutoResetEvent _deviceExitStatus = new AutoResetEvent(false);
  37. private static ListStore _tableStore;
  38. private static bool _updatingGameTable;
  39. private static bool _gameLoaded;
  40. private static string _gamePath;
  41. private static bool _ending;
  42. #pragma warning disable CS0169, CS0649, IDE0044
  43. [GUI] public MenuItem ExitMenuItem;
  44. [GUI] public MenuItem UpdateMenuItem;
  45. [GUI] MenuBar _menuBar;
  46. [GUI] Box _footerBox;
  47. [GUI] Box _statusBar;
  48. [GUI] MenuItem _stopEmulation;
  49. [GUI] MenuItem _fullScreen;
  50. [GUI] CheckMenuItem _startFullScreen;
  51. [GUI] CheckMenuItem _favToggle;
  52. [GUI] MenuItem _firmwareInstallDirectory;
  53. [GUI] MenuItem _firmwareInstallFile;
  54. [GUI] Label _fifoStatus;
  55. [GUI] CheckMenuItem _iconToggle;
  56. [GUI] CheckMenuItem _developerToggle;
  57. [GUI] CheckMenuItem _appToggle;
  58. [GUI] CheckMenuItem _timePlayedToggle;
  59. [GUI] CheckMenuItem _versionToggle;
  60. [GUI] CheckMenuItem _lastPlayedToggle;
  61. [GUI] CheckMenuItem _fileExtToggle;
  62. [GUI] CheckMenuItem _pathToggle;
  63. [GUI] CheckMenuItem _fileSizeToggle;
  64. [GUI] Label _dockedMode;
  65. [GUI] Label _gameStatus;
  66. [GUI] TreeView _gameTable;
  67. [GUI] TreeSelection _gameTableSelection;
  68. [GUI] ScrolledWindow _gameTableWindow;
  69. [GUI] Label _gpuName;
  70. [GUI] Label _progressLabel;
  71. [GUI] Label _firmwareVersionLabel;
  72. [GUI] LevelBar _progressBar;
  73. [GUI] Box _viewBox;
  74. [GUI] Label _vSyncStatus;
  75. [GUI] Box _listStatusBox;
  76. #pragma warning restore CS0649, IDE0044, CS0169
  77. public MainWindow() : this(new Builder("Ryujinx.Ui.MainWindow.glade")) { }
  78. private MainWindow(Builder builder) : base(builder.GetObject("_mainWin").Handle)
  79. {
  80. builder.Autoconnect(this);
  81. int monitorWidth = Display.PrimaryMonitor.Geometry.Width * Display.PrimaryMonitor.ScaleFactor;
  82. int monitorHeight = Display.PrimaryMonitor.Geometry.Height * Display.PrimaryMonitor.ScaleFactor;
  83. this.DefaultWidth = monitorWidth < 1280 ? monitorWidth : 1280;
  84. this.DefaultHeight = monitorHeight < 760 ? monitorHeight : 760;
  85. this.WindowStateEvent += MainWindow_WindowStateEvent;
  86. this.DeleteEvent += Window_Close;
  87. _fullScreen.Activated += FullScreen_Toggled;
  88. this.Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png");
  89. this.Title = $"Ryujinx {Program.Version}";
  90. ApplicationLibrary.ApplicationAdded += Application_Added;
  91. ApplicationLibrary.ApplicationCountUpdated += ApplicationCount_Updated;
  92. GlRenderer.StatusUpdatedEvent += Update_StatusBar;
  93. _gameTable.ButtonReleaseEvent += Row_Clicked;
  94. // First we check that a migration isn't needed. (because VirtualFileSystem will create the new directory otherwise)
  95. bool continueWithStartup = Migration.PromptIfMigrationNeededForStartup(this, out bool migrationNeeded);
  96. if (!continueWithStartup)
  97. {
  98. End(null);
  99. }
  100. _virtualFileSystem = VirtualFileSystem.CreateInstance();
  101. _userChannelPersistence = new UserChannelPersistence();
  102. _contentManager = new ContentManager(_virtualFileSystem);
  103. if (migrationNeeded)
  104. {
  105. bool migrationSuccessful = Migration.DoMigrationForStartup(this, _virtualFileSystem);
  106. if (!migrationSuccessful)
  107. {
  108. End(null);
  109. }
  110. }
  111. // Make sure that everything is loaded.
  112. _virtualFileSystem.Reload();
  113. ApplyTheme();
  114. if (ConfigurationState.Instance.Ui.StartFullscreen)
  115. {
  116. _startFullScreen.Active = true;
  117. }
  118. _stopEmulation.Sensitive = false;
  119. if (ConfigurationState.Instance.Ui.GuiColumns.FavColumn) _favToggle.Active = true;
  120. if (ConfigurationState.Instance.Ui.GuiColumns.IconColumn) _iconToggle.Active = true;
  121. if (ConfigurationState.Instance.Ui.GuiColumns.AppColumn) _appToggle.Active = true;
  122. if (ConfigurationState.Instance.Ui.GuiColumns.DevColumn) _developerToggle.Active = true;
  123. if (ConfigurationState.Instance.Ui.GuiColumns.VersionColumn) _versionToggle.Active = true;
  124. if (ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn) _timePlayedToggle.Active = true;
  125. if (ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn) _lastPlayedToggle.Active = true;
  126. if (ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn) _fileExtToggle.Active = true;
  127. if (ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn) _fileSizeToggle.Active = true;
  128. if (ConfigurationState.Instance.Ui.GuiColumns.PathColumn) _pathToggle.Active = true;
  129. _gameTable.Model = _tableStore = new ListStore(
  130. typeof(bool),
  131. typeof(Gdk.Pixbuf),
  132. typeof(string),
  133. typeof(string),
  134. typeof(string),
  135. typeof(string),
  136. typeof(string),
  137. typeof(string),
  138. typeof(string),
  139. typeof(string),
  140. typeof(BlitStruct<ApplicationControlProperty>));
  141. _tableStore.SetSortFunc(5, TimePlayedSort);
  142. _tableStore.SetSortFunc(6, LastPlayedSort);
  143. _tableStore.SetSortFunc(8, FileSizeSort);
  144. int columnId = ConfigurationState.Instance.Ui.ColumnSort.SortColumnId;
  145. bool ascending = ConfigurationState.Instance.Ui.ColumnSort.SortAscending;
  146. _tableStore.SetSortColumnId(columnId, ascending ? SortType.Ascending : SortType.Descending);
  147. _gameTable.EnableSearch = true;
  148. _gameTable.SearchColumn = 2;
  149. UpdateColumns();
  150. UpdateGameTable();
  151. ConfigurationState.Instance.Ui.GameDirs.Event += (sender, args) =>
  152. {
  153. if (args.OldValue != args.NewValue)
  154. {
  155. UpdateGameTable();
  156. }
  157. };
  158. Task.Run(RefreshFirmwareLabel);
  159. _statusBar.Hide();
  160. _uiHandler = new GtkHostUiHandler(this);
  161. _gamePath = null;
  162. }
  163. private void MainWindow_WindowStateEvent(object o, WindowStateEventArgs args)
  164. {
  165. _fullScreen.Label = args.Event.NewWindowState.HasFlag(Gdk.WindowState.Fullscreen) ? "Exit Fullscreen" : "Enter Fullscreen";
  166. }
  167. internal static void ApplyTheme()
  168. {
  169. if (!ConfigurationState.Instance.Ui.EnableCustomTheme)
  170. {
  171. return;
  172. }
  173. if (File.Exists(ConfigurationState.Instance.Ui.CustomThemePath) && (System.IO.Path.GetExtension(ConfigurationState.Instance.Ui.CustomThemePath) == ".css"))
  174. {
  175. CssProvider cssProvider = new CssProvider();
  176. cssProvider.LoadFromPath(ConfigurationState.Instance.Ui.CustomThemePath);
  177. StyleContext.AddProviderForScreen(Gdk.Screen.Default, cssProvider, 800);
  178. }
  179. else
  180. {
  181. Logger.Warning?.Print(LogClass.Application, $"The \"custom_theme_path\" section in \"Config.json\" contains an invalid path: \"{ConfigurationState.Instance.Ui.CustomThemePath}\".");
  182. }
  183. }
  184. private void UpdateColumns()
  185. {
  186. foreach (TreeViewColumn column in _gameTable.Columns)
  187. {
  188. _gameTable.RemoveColumn(column);
  189. }
  190. CellRendererToggle favToggle = new CellRendererToggle();
  191. favToggle.Toggled += FavToggle_Toggled;
  192. if (ConfigurationState.Instance.Ui.GuiColumns.FavColumn) _gameTable.AppendColumn("Fav", favToggle, "active", 0);
  193. if (ConfigurationState.Instance.Ui.GuiColumns.IconColumn) _gameTable.AppendColumn("Icon", new CellRendererPixbuf(), "pixbuf", 1);
  194. if (ConfigurationState.Instance.Ui.GuiColumns.AppColumn) _gameTable.AppendColumn("Application", new CellRendererText(), "text", 2);
  195. if (ConfigurationState.Instance.Ui.GuiColumns.DevColumn) _gameTable.AppendColumn("Developer", new CellRendererText(), "text", 3);
  196. if (ConfigurationState.Instance.Ui.GuiColumns.VersionColumn) _gameTable.AppendColumn("Version", new CellRendererText(), "text", 4);
  197. if (ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn) _gameTable.AppendColumn("Time Played", new CellRendererText(), "text", 5);
  198. if (ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn) _gameTable.AppendColumn("Last Played", new CellRendererText(), "text", 6);
  199. if (ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn) _gameTable.AppendColumn("File Ext", new CellRendererText(), "text", 7);
  200. if (ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn) _gameTable.AppendColumn("File Size", new CellRendererText(), "text", 8);
  201. if (ConfigurationState.Instance.Ui.GuiColumns.PathColumn) _gameTable.AppendColumn("Path", new CellRendererText(), "text", 9);
  202. foreach (TreeViewColumn column in _gameTable.Columns)
  203. {
  204. switch (column.Title)
  205. {
  206. case "Fav":
  207. column.SortColumnId = 0;
  208. column.Clicked += Column_Clicked;
  209. break;
  210. case "Application":
  211. column.SortColumnId = 2;
  212. column.Clicked += Column_Clicked;
  213. break;
  214. case "Developer":
  215. column.SortColumnId = 3;
  216. column.Clicked += Column_Clicked;
  217. break;
  218. case "Version":
  219. column.SortColumnId = 4;
  220. column.Clicked += Column_Clicked;
  221. break;
  222. case "Time Played":
  223. column.SortColumnId = 5;
  224. column.Clicked += Column_Clicked;
  225. break;
  226. case "Last Played":
  227. column.SortColumnId = 6;
  228. column.Clicked += Column_Clicked;
  229. break;
  230. case "File Ext":
  231. column.SortColumnId = 7;
  232. column.Clicked += Column_Clicked;
  233. break;
  234. case "File Size":
  235. column.SortColumnId = 8;
  236. column.Clicked += Column_Clicked;
  237. break;
  238. case "Path":
  239. column.SortColumnId = 9;
  240. column.Clicked += Column_Clicked;
  241. break;
  242. }
  243. }
  244. }
  245. private HLE.Switch InitializeSwitchInstance()
  246. {
  247. _virtualFileSystem.Reload();
  248. HLE.Switch instance = new HLE.Switch(_virtualFileSystem, _contentManager, _userChannelPersistence, InitializeRenderer(), InitializeAudioEngine())
  249. {
  250. UiHandler = _uiHandler
  251. };
  252. instance.Initialize();
  253. return instance;
  254. }
  255. internal static void UpdateGameTable()
  256. {
  257. if (_updatingGameTable || _gameLoaded)
  258. {
  259. return;
  260. }
  261. _updatingGameTable = true;
  262. _tableStore.Clear();
  263. Thread applicationLibraryThread = new Thread(() =>
  264. {
  265. ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs,
  266. _virtualFileSystem, ConfigurationState.Instance.System.Language);
  267. _updatingGameTable = false;
  268. });
  269. applicationLibraryThread.Name = "GUI.ApplicationLibraryThread";
  270. applicationLibraryThread.IsBackground = true;
  271. applicationLibraryThread.Start();
  272. }
  273. internal void LoadApplication(string path)
  274. {
  275. if (_gameLoaded)
  276. {
  277. GtkDialog.CreateInfoDialog("Ryujinx", "A game has already been loaded", "Please close it first and try again.");
  278. }
  279. else
  280. {
  281. #if !DEBUG
  282. if (ConfigurationState.Instance.Logger.EnableDebug.Value)
  283. {
  284. MessageDialog debugWarningDialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Warning, ButtonsType.YesNo, null)
  285. {
  286. Title = "Ryujinx - Warning",
  287. Text = "You have debug logging enabled, which is designed to be used by developers only.",
  288. SecondaryText = "For optimal performance, it's recommended to disable debug logging. Would you like to disable debug logging now?"
  289. };
  290. if (debugWarningDialog.Run() == (int)ResponseType.Yes)
  291. {
  292. ConfigurationState.Instance.Logger.EnableDebug.Value = false;
  293. SaveConfig();
  294. }
  295. debugWarningDialog.Dispose();
  296. }
  297. if (!string.IsNullOrWhiteSpace(ConfigurationState.Instance.Graphics.ShadersDumpPath.Value))
  298. {
  299. MessageDialog shadersDumpWarningDialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Warning, ButtonsType.YesNo, null)
  300. {
  301. Title = "Ryujinx - Warning",
  302. Text = "You have shader dumping enabled, which is designed to be used by developers only.",
  303. SecondaryText = "For optimal performance, it's recommended to disable shader dumping. Would you like to disable shader dumping now?"
  304. };
  305. if (shadersDumpWarningDialog.Run() == (int)ResponseType.Yes)
  306. {
  307. ConfigurationState.Instance.Graphics.ShadersDumpPath.Value = "";
  308. SaveConfig();
  309. }
  310. shadersDumpWarningDialog.Dispose();
  311. }
  312. #endif
  313. Logger.RestartTime();
  314. HLE.Switch device = InitializeSwitchInstance();
  315. UpdateGraphicsConfig();
  316. SystemVersion firmwareVersion = _contentManager.GetCurrentFirmwareVersion();
  317. bool isDirectory = Directory.Exists(path);
  318. if (!SetupValidator.CanStartApplication(_contentManager, path, out UserError userError))
  319. {
  320. if (SetupValidator.CanFixStartApplication(_contentManager, path, userError, out firmwareVersion))
  321. {
  322. if (userError == UserError.NoFirmware)
  323. {
  324. MessageDialog shouldInstallFirmwareDialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.YesNo, null)
  325. {
  326. Title = "Ryujinx - Info",
  327. Text = "No Firmware Installed",
  328. SecondaryText = $"Would you like to install the firmware embedded in this game? (Firmware {firmwareVersion.VersionString})"
  329. };
  330. if (shouldInstallFirmwareDialog.Run() != (int)ResponseType.Yes)
  331. {
  332. shouldInstallFirmwareDialog.Dispose();
  333. UserErrorDialog.CreateUserErrorDialog(userError);
  334. device.Dispose();
  335. return;
  336. }
  337. else
  338. {
  339. shouldInstallFirmwareDialog.Dispose();
  340. }
  341. }
  342. if (!SetupValidator.TryFixStartApplication(_contentManager, path, userError, out _))
  343. {
  344. UserErrorDialog.CreateUserErrorDialog(userError);
  345. device.Dispose();
  346. return;
  347. }
  348. // Tell the user that we installed a firmware for them.
  349. if (userError == UserError.NoFirmware)
  350. {
  351. firmwareVersion = _contentManager.GetCurrentFirmwareVersion();
  352. RefreshFirmwareLabel();
  353. GtkDialog.CreateInfoDialog("Ryujinx - Info", $"Firmware {firmwareVersion.VersionString} was installed",
  354. $"No installed firmware was found but Ryujinx was able to install firmware {firmwareVersion.VersionString} from the provided game.\nThe emulator will now start.");
  355. }
  356. }
  357. else
  358. {
  359. UserErrorDialog.CreateUserErrorDialog(userError);
  360. device.Dispose();
  361. return;
  362. }
  363. }
  364. Logger.Notice.Print(LogClass.Application, $"Using Firmware Version: {firmwareVersion?.VersionString}");
  365. if (Directory.Exists(path))
  366. {
  367. string[] romFsFiles = Directory.GetFiles(path, "*.istorage");
  368. if (romFsFiles.Length == 0)
  369. {
  370. romFsFiles = Directory.GetFiles(path, "*.romfs");
  371. }
  372. if (romFsFiles.Length > 0)
  373. {
  374. Logger.Info?.Print(LogClass.Application, "Loading as cart with RomFS.");
  375. device.LoadCart(path, romFsFiles[0]);
  376. }
  377. else
  378. {
  379. Logger.Info?.Print(LogClass.Application, "Loading as cart WITHOUT RomFS.");
  380. device.LoadCart(path);
  381. }
  382. }
  383. else if (File.Exists(path))
  384. {
  385. switch (System.IO.Path.GetExtension(path).ToLowerInvariant())
  386. {
  387. case ".xci":
  388. Logger.Info?.Print(LogClass.Application, "Loading as XCI.");
  389. device.LoadXci(path);
  390. break;
  391. case ".nca":
  392. Logger.Info?.Print(LogClass.Application, "Loading as NCA.");
  393. device.LoadNca(path);
  394. break;
  395. case ".nsp":
  396. case ".pfs0":
  397. Logger.Info?.Print(LogClass.Application, "Loading as NSP.");
  398. device.LoadNsp(path);
  399. break;
  400. default:
  401. Logger.Info?.Print(LogClass.Application, "Loading as homebrew.");
  402. try
  403. {
  404. device.LoadProgram(path);
  405. }
  406. catch (ArgumentOutOfRangeException)
  407. {
  408. Logger.Error?.Print(LogClass.Application, "The file which you have specified is unsupported by Ryujinx.");
  409. }
  410. break;
  411. }
  412. }
  413. else
  414. {
  415. Logger.Warning?.Print(LogClass.Application, "Please specify a valid XCI/NCA/NSP/PFS0/NRO file.");
  416. device.Dispose();
  417. return;
  418. }
  419. _emulationContext = device;
  420. _gamePath = path;
  421. _deviceExitStatus.Reset();
  422. Translator.IsReadyForTranslation.Reset();
  423. #if MACOS_BUILD
  424. CreateGameWindow(device);
  425. #else
  426. Thread windowThread = new Thread(() =>
  427. {
  428. CreateGameWindow(device);
  429. })
  430. {
  431. Name = "GUI.WindowThread"
  432. };
  433. windowThread.Start();
  434. #endif
  435. _gameLoaded = true;
  436. _stopEmulation.Sensitive = true;
  437. _firmwareInstallFile.Sensitive = false;
  438. _firmwareInstallDirectory.Sensitive = false;
  439. DiscordIntegrationModule.SwitchToPlayingState(device.Application.TitleIdText, device.Application.TitleName);
  440. ApplicationLibrary.LoadAndSaveMetaData(device.Application.TitleIdText, appMetadata =>
  441. {
  442. appMetadata.LastPlayed = DateTime.UtcNow.ToString();
  443. });
  444. }
  445. }
  446. private void CreateGameWindow(HLE.Switch device)
  447. {
  448. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  449. {
  450. _windowsMultimediaTimerResolution = new WindowsMultimediaTimerResolution(1);
  451. }
  452. _glWidget = new GlRenderer(_emulationContext, ConfigurationState.Instance.Logger.GraphicsDebugLevel);
  453. Application.Invoke(delegate
  454. {
  455. _viewBox.Remove(_gameTableWindow);
  456. _glWidget.Expand = true;
  457. _viewBox.Child = _glWidget;
  458. _glWidget.ShowAll();
  459. EditFooterForGameRender();
  460. if (this.Window.State.HasFlag(Gdk.WindowState.Fullscreen))
  461. {
  462. ToggleExtraWidgets(false);
  463. }
  464. else if (ConfigurationState.Instance.Ui.StartFullscreen.Value)
  465. {
  466. FullScreen_Toggled(null, null);
  467. }
  468. });
  469. _glWidget.WaitEvent.WaitOne();
  470. _glWidget.Start();
  471. Ptc.Close();
  472. PtcProfiler.Stop();
  473. device.Dispose();
  474. _deviceExitStatus.Set();
  475. // NOTE: Everything that is here will not be executed when you close the UI.
  476. Application.Invoke(delegate
  477. {
  478. if (this.Window.State.HasFlag(Gdk.WindowState.Fullscreen))
  479. {
  480. ToggleExtraWidgets(true);
  481. }
  482. _viewBox.Remove(_glWidget);
  483. _glWidget.Exit();
  484. if(_glWidget.Window != this.Window && _glWidget.Window != null)
  485. {
  486. _glWidget.Window.Dispose();
  487. }
  488. _glWidget.Dispose();
  489. _windowsMultimediaTimerResolution?.Dispose();
  490. _windowsMultimediaTimerResolution = null;
  491. _viewBox.Add(_gameTableWindow);
  492. _gameTableWindow.Expand = true;
  493. this.Window.Title = $"Ryujinx {Program.Version}";
  494. _emulationContext = null;
  495. _gameLoaded = false;
  496. _glWidget = null;
  497. DiscordIntegrationModule.SwitchToMainMenu();
  498. RecreateFooterForMenu();
  499. UpdateColumns();
  500. UpdateGameTable();
  501. Task.Run(RefreshFirmwareLabel);
  502. Task.Run(HandleRelaunch);
  503. _stopEmulation.Sensitive = false;
  504. _firmwareInstallFile.Sensitive = true;
  505. _firmwareInstallDirectory.Sensitive = true;
  506. });
  507. }
  508. private void RecreateFooterForMenu()
  509. {
  510. _listStatusBox.Show();
  511. _statusBar.Hide();
  512. }
  513. private void EditFooterForGameRender()
  514. {
  515. _listStatusBox.Hide();
  516. _statusBar.Show();
  517. }
  518. public void ToggleExtraWidgets(bool show)
  519. {
  520. if (_glWidget != null)
  521. {
  522. if (show)
  523. {
  524. _menuBar.ShowAll();
  525. _footerBox.Show();
  526. _statusBar.Show();
  527. }
  528. else
  529. {
  530. _menuBar.Hide();
  531. _footerBox.Hide();
  532. }
  533. }
  534. }
  535. private static void UpdateGameMetadata(string titleId)
  536. {
  537. if (_gameLoaded)
  538. {
  539. ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
  540. {
  541. DateTime lastPlayedDateTime = DateTime.Parse(appMetadata.LastPlayed);
  542. double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds;
  543. appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero);
  544. });
  545. }
  546. }
  547. public static void UpdateGraphicsConfig()
  548. {
  549. int resScale = ConfigurationState.Instance.Graphics.ResScale;
  550. float resScaleCustom = ConfigurationState.Instance.Graphics.ResScaleCustom;
  551. Graphics.Gpu.GraphicsConfig.ResScale = (resScale == -1) ? resScaleCustom : resScale;
  552. Graphics.Gpu.GraphicsConfig.MaxAnisotropy = ConfigurationState.Instance.Graphics.MaxAnisotropy;
  553. Graphics.Gpu.GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
  554. Graphics.Gpu.GraphicsConfig.EnableShaderCache = ConfigurationState.Instance.Graphics.EnableShaderCache;
  555. }
  556. public static void SaveConfig()
  557. {
  558. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  559. }
  560. private void End(HLE.Switch device)
  561. {
  562. if (_ending)
  563. {
  564. return;
  565. }
  566. _ending = true;
  567. if (device != null)
  568. {
  569. UpdateGameMetadata(device.Application.TitleIdText);
  570. if (_glWidget != null)
  571. {
  572. // We tell the widget that we are exiting
  573. _glWidget.Exit();
  574. // Wait for the other thread to dispose the HLE context before exiting.
  575. _deviceExitStatus.WaitOne();
  576. }
  577. }
  578. Dispose();
  579. DiscordIntegrationModule.Exit();
  580. Ptc.Dispose();
  581. PtcProfiler.Dispose();
  582. Logger.Shutdown();
  583. Application.Quit();
  584. }
  585. private static IRenderer InitializeRenderer()
  586. {
  587. return new Renderer();
  588. }
  589. private static IAalOutput InitializeAudioEngine()
  590. {
  591. if (ConfigurationState.Instance.System.AudioBackend.Value == AudioBackend.SoundIo)
  592. {
  593. if (SoundIoAudioOut.IsSupported)
  594. {
  595. return new SoundIoAudioOut();
  596. }
  597. else
  598. {
  599. Logger.Warning?.Print(LogClass.Audio, "SoundIO is not supported, falling back to dummy audio out.");
  600. }
  601. }
  602. else if (ConfigurationState.Instance.System.AudioBackend.Value == AudioBackend.OpenAl)
  603. {
  604. if (OpenALAudioOut.IsSupported)
  605. {
  606. return new OpenALAudioOut();
  607. }
  608. else
  609. {
  610. Logger.Warning?.Print(LogClass.Audio, "OpenAL is not supported, trying to fall back to SoundIO.");
  611. if (SoundIoAudioOut.IsSupported)
  612. {
  613. Logger.Warning?.Print(LogClass.Audio, "Found SoundIO, changing configuration.");
  614. ConfigurationState.Instance.System.AudioBackend.Value = AudioBackend.SoundIo;
  615. SaveConfig();
  616. return new SoundIoAudioOut();
  617. }
  618. else
  619. {
  620. Logger.Warning?.Print(LogClass.Audio, "SoundIO is not supported, falling back to dummy audio out.");
  621. }
  622. }
  623. }
  624. return new DummyAudioOut();
  625. }
  626. //Events
  627. private void Application_Added(object sender, ApplicationAddedEventArgs args)
  628. {
  629. Application.Invoke(delegate
  630. {
  631. _tableStore.AppendValues(
  632. args.AppData.Favorite,
  633. new Gdk.Pixbuf(args.AppData.Icon, 75, 75),
  634. $"{args.AppData.TitleName}\n{args.AppData.TitleId.ToUpper()}",
  635. args.AppData.Developer,
  636. args.AppData.Version,
  637. args.AppData.TimePlayed,
  638. args.AppData.LastPlayed,
  639. args.AppData.FileExtension,
  640. args.AppData.FileSize,
  641. args.AppData.Path,
  642. args.AppData.ControlHolder);
  643. });
  644. }
  645. private void ApplicationCount_Updated(object sender, ApplicationCountUpdatedEventArgs args)
  646. {
  647. Application.Invoke(delegate
  648. {
  649. _progressLabel.Text = $"{args.NumAppsLoaded}/{args.NumAppsFound} Games Loaded";
  650. float barValue = 0;
  651. if (args.NumAppsFound != 0)
  652. {
  653. barValue = (float)args.NumAppsLoaded / args.NumAppsFound;
  654. }
  655. _progressBar.Value = barValue;
  656. if (args.NumAppsLoaded == args.NumAppsFound) // Reset the vertical scrollbar to the top when titles finish loading
  657. {
  658. _gameTableWindow.Vadjustment.Value = 0;
  659. }
  660. });
  661. }
  662. private void Update_StatusBar(object sender, StatusUpdatedEventArgs args)
  663. {
  664. Application.Invoke(delegate
  665. {
  666. _gameStatus.Text = args.GameStatus;
  667. _fifoStatus.Text = args.FifoStatus;
  668. _gpuName.Text = args.GpuName;
  669. _dockedMode.Text = args.DockedMode;
  670. if (args.VSyncEnabled)
  671. {
  672. _vSyncStatus.Attributes = new Pango.AttrList();
  673. _vSyncStatus.Attributes.Insert(new Pango.AttrForeground(11822, 60138, 51657));
  674. }
  675. else
  676. {
  677. _vSyncStatus.Attributes = new Pango.AttrList();
  678. _vSyncStatus.Attributes.Insert(new Pango.AttrForeground(ushort.MaxValue, 17733, 21588));
  679. }
  680. });
  681. }
  682. private void FavToggle_Toggled(object sender, ToggledArgs args)
  683. {
  684. _tableStore.GetIter(out TreeIter treeIter, new TreePath(args.Path));
  685. string titleId = _tableStore.GetValue(treeIter, 2).ToString().Split("\n")[1].ToLower();
  686. bool newToggleValue = !(bool)_tableStore.GetValue(treeIter, 0);
  687. _tableStore.SetValue(treeIter, 0, newToggleValue);
  688. ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
  689. {
  690. appMetadata.Favorite = newToggleValue;
  691. });
  692. }
  693. private void Column_Clicked(object sender, EventArgs args)
  694. {
  695. TreeViewColumn column = (TreeViewColumn)sender;
  696. ConfigurationState.Instance.Ui.ColumnSort.SortColumnId.Value = column.SortColumnId;
  697. ConfigurationState.Instance.Ui.ColumnSort.SortAscending.Value = column.SortOrder == SortType.Ascending;
  698. SaveConfig();
  699. }
  700. private void Row_Activated(object sender, RowActivatedArgs args)
  701. {
  702. _gameTableSelection.GetSelected(out TreeIter treeIter);
  703. string path = (string)_tableStore.GetValue(treeIter, 9);
  704. LoadApplication(path);
  705. }
  706. private void VSyncStatus_Clicked(object sender, ButtonReleaseEventArgs args)
  707. {
  708. _emulationContext.EnableDeviceVsync = !_emulationContext.EnableDeviceVsync;
  709. }
  710. private void DockedMode_Clicked(object sender, ButtonReleaseEventArgs args)
  711. {
  712. ConfigurationState.Instance.System.EnableDockedMode.Value = !ConfigurationState.Instance.System.EnableDockedMode.Value;
  713. }
  714. private void Row_Clicked(object sender, ButtonReleaseEventArgs args)
  715. {
  716. if (args.Event.Button != 3) return;
  717. _gameTableSelection.GetSelected(out TreeIter treeIter);
  718. if (treeIter.UserData == IntPtr.Zero) return;
  719. BlitStruct<ApplicationControlProperty> controlData = (BlitStruct<ApplicationControlProperty>)_tableStore.GetValue(treeIter, 10);
  720. GameTableContextMenu contextMenu = new GameTableContextMenu(_tableStore, controlData, treeIter, _virtualFileSystem);
  721. contextMenu.ShowAll();
  722. contextMenu.PopupAtPointer(null);
  723. }
  724. private void Load_Application_File(object sender, EventArgs args)
  725. {
  726. FileChooserDialog fileChooser = new FileChooserDialog("Choose the file to open", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  727. fileChooser.Filter = new FileFilter();
  728. fileChooser.Filter.AddPattern("*.nsp" );
  729. fileChooser.Filter.AddPattern("*.pfs0");
  730. fileChooser.Filter.AddPattern("*.xci" );
  731. fileChooser.Filter.AddPattern("*.nca" );
  732. fileChooser.Filter.AddPattern("*.nro" );
  733. fileChooser.Filter.AddPattern("*.nso" );
  734. if (fileChooser.Run() == (int)ResponseType.Accept)
  735. {
  736. LoadApplication(fileChooser.Filename);
  737. }
  738. fileChooser.Dispose();
  739. }
  740. private void Load_Application_Folder(object sender, EventArgs args)
  741. {
  742. FileChooserDialog fileChooser = new FileChooserDialog("Choose the folder to open", this, FileChooserAction.SelectFolder, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept);
  743. if (fileChooser.Run() == (int)ResponseType.Accept)
  744. {
  745. LoadApplication(fileChooser.Filename);
  746. }
  747. fileChooser.Dispose();
  748. }
  749. private void Open_Ryu_Folder(object sender, EventArgs args)
  750. {
  751. Process.Start(new ProcessStartInfo
  752. {
  753. FileName = AppDataManager.BaseDirPath,
  754. UseShellExecute = true,
  755. Verb = "open"
  756. });
  757. }
  758. private void OpenLogsFolder_Pressed(object sender, EventArgs args)
  759. {
  760. string logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
  761. DirectoryInfo directory = new DirectoryInfo(logPath);
  762. directory.Create();
  763. Process.Start(new ProcessStartInfo
  764. {
  765. FileName = logPath,
  766. UseShellExecute = true,
  767. Verb = "open"
  768. });
  769. }
  770. private void Exit_Pressed(object sender, EventArgs args)
  771. {
  772. if (!_gameLoaded || GtkDialog.CreateExitDialog())
  773. {
  774. End(_emulationContext);
  775. }
  776. }
  777. private void Window_Close(object sender, DeleteEventArgs args)
  778. {
  779. if (!_gameLoaded || GtkDialog.CreateExitDialog())
  780. {
  781. End(_emulationContext);
  782. }
  783. else
  784. {
  785. args.RetVal = true;
  786. }
  787. }
  788. private void StopEmulation_Pressed(object sender, EventArgs args)
  789. {
  790. _glWidget?.Exit();
  791. }
  792. private void Installer_File_Pressed(object o, EventArgs args)
  793. {
  794. FileChooserDialog fileChooser = new FileChooserDialog("Choose the firmware file to open",
  795. this,
  796. FileChooserAction.Open,
  797. "Cancel",
  798. ResponseType.Cancel,
  799. "Open",
  800. ResponseType.Accept);
  801. fileChooser.Filter = new FileFilter();
  802. fileChooser.Filter.AddPattern("*.zip");
  803. fileChooser.Filter.AddPattern("*.xci");
  804. HandleInstallerDialog(fileChooser);
  805. }
  806. private void Installer_Directory_Pressed(object o, EventArgs args)
  807. {
  808. FileChooserDialog directoryChooser = new FileChooserDialog("Choose the firmware directory to open",
  809. this,
  810. FileChooserAction.SelectFolder,
  811. "Cancel",
  812. ResponseType.Cancel,
  813. "Open",
  814. ResponseType.Accept);
  815. HandleInstallerDialog(directoryChooser);
  816. }
  817. private void RefreshFirmwareLabel()
  818. {
  819. SystemVersion currentFirmware = _contentManager.GetCurrentFirmwareVersion();
  820. GLib.Idle.Add(new GLib.IdleHandler(() =>
  821. {
  822. _firmwareVersionLabel.Text = currentFirmware != null ? currentFirmware.VersionString : "0.0.0";
  823. return false;
  824. }));
  825. }
  826. private void HandleRelaunch()
  827. {
  828. if (_userChannelPersistence.PreviousIndex != -1 && _userChannelPersistence.ShouldRestart)
  829. {
  830. _userChannelPersistence.ShouldRestart = false;
  831. LoadApplication(_gamePath);
  832. }
  833. else
  834. {
  835. // otherwise, clear state.
  836. _userChannelPersistence = new UserChannelPersistence();
  837. _gamePath = null;
  838. }
  839. }
  840. private void HandleInstallerDialog(FileChooserDialog fileChooser)
  841. {
  842. if (fileChooser.Run() == (int)ResponseType.Accept)
  843. {
  844. MessageDialog dialog = null;
  845. try
  846. {
  847. string filename = fileChooser.Filename;
  848. fileChooser.Dispose();
  849. SystemVersion firmwareVersion = _contentManager.VerifyFirmwarePackage(filename);
  850. if (firmwareVersion == null)
  851. {
  852. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  853. dialog.Text = "Firmware not found.";
  854. dialog.SecondaryText = $"A valid system firmware was not found in {filename}.";
  855. Logger.Error?.Print(LogClass.Application, $"A valid system firmware was not found in {filename}.");
  856. dialog.Run();
  857. dialog.Hide();
  858. dialog.Dispose();
  859. return;
  860. }
  861. SystemVersion currentVersion = _contentManager.GetCurrentFirmwareVersion();
  862. string dialogMessage = $"System version {firmwareVersion.VersionString} will be installed.";
  863. if (currentVersion != null)
  864. {
  865. dialogMessage += $"This will replace the current system version {currentVersion.VersionString}. ";
  866. }
  867. dialogMessage += "Do you want to continue?";
  868. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, false, "");
  869. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  870. dialog.SecondaryText = dialogMessage;
  871. int response = dialog.Run();
  872. dialog.Dispose();
  873. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.None, false, "");
  874. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  875. dialog.SecondaryText = "Installing firmware...";
  876. if (response == (int)ResponseType.Yes)
  877. {
  878. Logger.Info?.Print(LogClass.Application, $"Installing firmware {firmwareVersion.VersionString}");
  879. Thread thread = new Thread(() =>
  880. {
  881. GLib.Idle.Add(new GLib.IdleHandler(() =>
  882. {
  883. dialog.Run();
  884. return false;
  885. }));
  886. try
  887. {
  888. _contentManager.InstallFirmware(filename);
  889. GLib.Idle.Add(new GLib.IdleHandler(() =>
  890. {
  891. dialog.Dispose();
  892. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  893. dialog.Text = $"Install Firmware {firmwareVersion.VersionString}";
  894. dialog.SecondaryText = $"System version {firmwareVersion.VersionString} successfully installed.";
  895. Logger.Info?.Print(LogClass.Application, $"System version {firmwareVersion.VersionString} successfully installed.");
  896. dialog.Run();
  897. dialog.Dispose();
  898. return false;
  899. }));
  900. }
  901. catch (Exception ex)
  902. {
  903. GLib.Idle.Add(new GLib.IdleHandler(() =>
  904. {
  905. dialog.Dispose();
  906. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  907. dialog.Text = $"Install Firmware {firmwareVersion.VersionString} Failed.";
  908. dialog.SecondaryText = $"An error occured while installing system version {firmwareVersion.VersionString}." +
  909. " Please check logs for more info.";
  910. Logger.Error?.Print(LogClass.Application, ex.Message);
  911. dialog.Run();
  912. dialog.Dispose();
  913. return false;
  914. }));
  915. }
  916. finally
  917. {
  918. RefreshFirmwareLabel();
  919. }
  920. });
  921. thread.Name = "GUI.FirmwareInstallerThread";
  922. thread.Start();
  923. }
  924. else
  925. {
  926. dialog.Dispose();
  927. }
  928. }
  929. catch (Exception ex)
  930. {
  931. if (dialog != null)
  932. {
  933. dialog.Dispose();
  934. }
  935. dialog = new MessageDialog(this, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, false, "");
  936. dialog.Text = "Parsing Firmware Failed.";
  937. dialog.SecondaryText = "An error occured while parsing firmware. Please check the logs for more info.";
  938. Logger.Error?.Print(LogClass.Application, ex.Message);
  939. dialog.Run();
  940. dialog.Dispose();
  941. }
  942. }
  943. else
  944. {
  945. fileChooser.Dispose();
  946. }
  947. }
  948. private void FullScreen_Toggled(object sender, EventArgs args)
  949. {
  950. bool fullScreenToggled = this.Window.State.HasFlag(Gdk.WindowState.Fullscreen);
  951. if (!fullScreenToggled)
  952. {
  953. Fullscreen();
  954. ToggleExtraWidgets(false);
  955. }
  956. else
  957. {
  958. Unfullscreen();
  959. ToggleExtraWidgets(true);
  960. }
  961. }
  962. private void StartFullScreen_Toggled(object sender, EventArgs args)
  963. {
  964. ConfigurationState.Instance.Ui.StartFullscreen.Value = _startFullScreen.Active;
  965. SaveConfig();
  966. }
  967. private void Settings_Pressed(object sender, EventArgs args)
  968. {
  969. SettingsWindow settingsWin = new SettingsWindow(_virtualFileSystem, _contentManager);
  970. settingsWin.Show();
  971. }
  972. private void Update_Pressed(object sender, EventArgs args)
  973. {
  974. if (Updater.CanUpdate(true))
  975. {
  976. _ = Updater.BeginParse(this, true);
  977. }
  978. }
  979. private void About_Pressed(object sender, EventArgs args)
  980. {
  981. AboutWindow aboutWin = new AboutWindow();
  982. aboutWin.Show();
  983. }
  984. private void Fav_Toggled(object sender, EventArgs args)
  985. {
  986. ConfigurationState.Instance.Ui.GuiColumns.FavColumn.Value = _favToggle.Active;
  987. SaveConfig();
  988. UpdateColumns();
  989. }
  990. private void Icon_Toggled(object sender, EventArgs args)
  991. {
  992. ConfigurationState.Instance.Ui.GuiColumns.IconColumn.Value = _iconToggle.Active;
  993. SaveConfig();
  994. UpdateColumns();
  995. }
  996. private void Title_Toggled(object sender, EventArgs args)
  997. {
  998. ConfigurationState.Instance.Ui.GuiColumns.AppColumn.Value = _appToggle.Active;
  999. SaveConfig();
  1000. UpdateColumns();
  1001. }
  1002. private void Developer_Toggled(object sender, EventArgs args)
  1003. {
  1004. ConfigurationState.Instance.Ui.GuiColumns.DevColumn.Value = _developerToggle.Active;
  1005. SaveConfig();
  1006. UpdateColumns();
  1007. }
  1008. private void Version_Toggled(object sender, EventArgs args)
  1009. {
  1010. ConfigurationState.Instance.Ui.GuiColumns.VersionColumn.Value = _versionToggle.Active;
  1011. SaveConfig();
  1012. UpdateColumns();
  1013. }
  1014. private void TimePlayed_Toggled(object sender, EventArgs args)
  1015. {
  1016. ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn.Value = _timePlayedToggle.Active;
  1017. SaveConfig();
  1018. UpdateColumns();
  1019. }
  1020. private void LastPlayed_Toggled(object sender, EventArgs args)
  1021. {
  1022. ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn.Value = _lastPlayedToggle.Active;
  1023. SaveConfig();
  1024. UpdateColumns();
  1025. }
  1026. private void FileExt_Toggled(object sender, EventArgs args)
  1027. {
  1028. ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn.Value = _fileExtToggle.Active;
  1029. SaveConfig();
  1030. UpdateColumns();
  1031. }
  1032. private void FileSize_Toggled(object sender, EventArgs args)
  1033. {
  1034. ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn.Value = _fileSizeToggle.Active;
  1035. SaveConfig();
  1036. UpdateColumns();
  1037. }
  1038. private void Path_Toggled(object sender, EventArgs args)
  1039. {
  1040. ConfigurationState.Instance.Ui.GuiColumns.PathColumn.Value = _pathToggle.Active;
  1041. SaveConfig();
  1042. UpdateColumns();
  1043. }
  1044. private void RefreshList_Pressed(object sender, ButtonReleaseEventArgs args)
  1045. {
  1046. UpdateGameTable();
  1047. }
  1048. private static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  1049. {
  1050. string aValue = model.GetValue(a, 5).ToString();
  1051. string bValue = model.GetValue(b, 5).ToString();
  1052. if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "mins")
  1053. {
  1054. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 60).ToString();
  1055. }
  1056. else if (aValue.Length > 3 && aValue.Substring(aValue.Length - 3) == "hrs")
  1057. {
  1058. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 4)) * 3600).ToString();
  1059. }
  1060. else if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "days")
  1061. {
  1062. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 86400).ToString();
  1063. }
  1064. else
  1065. {
  1066. aValue = aValue.Substring(0, aValue.Length - 1);
  1067. }
  1068. if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "mins")
  1069. {
  1070. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 60).ToString();
  1071. }
  1072. else if (bValue.Length > 3 && bValue.Substring(bValue.Length - 3) == "hrs")
  1073. {
  1074. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 4)) * 3600).ToString();
  1075. }
  1076. else if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "days")
  1077. {
  1078. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 86400).ToString();
  1079. }
  1080. else
  1081. {
  1082. bValue = bValue.Substring(0, bValue.Length - 1);
  1083. }
  1084. if (float.Parse(aValue) > float.Parse(bValue))
  1085. {
  1086. return -1;
  1087. }
  1088. else if (float.Parse(bValue) > float.Parse(aValue))
  1089. {
  1090. return 1;
  1091. }
  1092. else
  1093. {
  1094. return 0;
  1095. }
  1096. }
  1097. private static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  1098. {
  1099. string aValue = model.GetValue(a, 6).ToString();
  1100. string bValue = model.GetValue(b, 6).ToString();
  1101. if (aValue == "Never")
  1102. {
  1103. aValue = DateTime.UnixEpoch.ToString();
  1104. }
  1105. if (bValue == "Never")
  1106. {
  1107. bValue = DateTime.UnixEpoch.ToString();
  1108. }
  1109. return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
  1110. }
  1111. private static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
  1112. {
  1113. string aValue = model.GetValue(a, 8).ToString();
  1114. string bValue = model.GetValue(b, 8).ToString();
  1115. if (aValue.Substring(aValue.Length - 2) == "GB")
  1116. {
  1117. aValue = (float.Parse(aValue[0..^2]) * 1024).ToString();
  1118. }
  1119. else
  1120. {
  1121. aValue = aValue[0..^2];
  1122. }
  1123. if (bValue.Substring(bValue.Length - 2) == "GB")
  1124. {
  1125. bValue = (float.Parse(bValue[0..^2]) * 1024).ToString();
  1126. }
  1127. else
  1128. {
  1129. bValue = bValue[0..^2];
  1130. }
  1131. if (float.Parse(aValue) > float.Parse(bValue))
  1132. {
  1133. return -1;
  1134. }
  1135. else if (float.Parse(bValue) > float.Parse(aValue))
  1136. {
  1137. return 1;
  1138. }
  1139. else
  1140. {
  1141. return 0;
  1142. }
  1143. }
  1144. }
  1145. }