MainWindow.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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 Simulate_WakeUp_Message_Pressed(object sender, EventArgs args)
  973. {
  974. _emulationContext.System.SimulateWakeUpMessage();
  975. }
  976. private void Update_Pressed(object sender, EventArgs args)
  977. {
  978. if (Updater.CanUpdate(true))
  979. {
  980. _ = Updater.BeginParse(this, true);
  981. }
  982. }
  983. private void About_Pressed(object sender, EventArgs args)
  984. {
  985. AboutWindow aboutWin = new AboutWindow();
  986. aboutWin.Show();
  987. }
  988. private void Fav_Toggled(object sender, EventArgs args)
  989. {
  990. ConfigurationState.Instance.Ui.GuiColumns.FavColumn.Value = _favToggle.Active;
  991. SaveConfig();
  992. UpdateColumns();
  993. }
  994. private void Icon_Toggled(object sender, EventArgs args)
  995. {
  996. ConfigurationState.Instance.Ui.GuiColumns.IconColumn.Value = _iconToggle.Active;
  997. SaveConfig();
  998. UpdateColumns();
  999. }
  1000. private void Title_Toggled(object sender, EventArgs args)
  1001. {
  1002. ConfigurationState.Instance.Ui.GuiColumns.AppColumn.Value = _appToggle.Active;
  1003. SaveConfig();
  1004. UpdateColumns();
  1005. }
  1006. private void Developer_Toggled(object sender, EventArgs args)
  1007. {
  1008. ConfigurationState.Instance.Ui.GuiColumns.DevColumn.Value = _developerToggle.Active;
  1009. SaveConfig();
  1010. UpdateColumns();
  1011. }
  1012. private void Version_Toggled(object sender, EventArgs args)
  1013. {
  1014. ConfigurationState.Instance.Ui.GuiColumns.VersionColumn.Value = _versionToggle.Active;
  1015. SaveConfig();
  1016. UpdateColumns();
  1017. }
  1018. private void TimePlayed_Toggled(object sender, EventArgs args)
  1019. {
  1020. ConfigurationState.Instance.Ui.GuiColumns.TimePlayedColumn.Value = _timePlayedToggle.Active;
  1021. SaveConfig();
  1022. UpdateColumns();
  1023. }
  1024. private void LastPlayed_Toggled(object sender, EventArgs args)
  1025. {
  1026. ConfigurationState.Instance.Ui.GuiColumns.LastPlayedColumn.Value = _lastPlayedToggle.Active;
  1027. SaveConfig();
  1028. UpdateColumns();
  1029. }
  1030. private void FileExt_Toggled(object sender, EventArgs args)
  1031. {
  1032. ConfigurationState.Instance.Ui.GuiColumns.FileExtColumn.Value = _fileExtToggle.Active;
  1033. SaveConfig();
  1034. UpdateColumns();
  1035. }
  1036. private void FileSize_Toggled(object sender, EventArgs args)
  1037. {
  1038. ConfigurationState.Instance.Ui.GuiColumns.FileSizeColumn.Value = _fileSizeToggle.Active;
  1039. SaveConfig();
  1040. UpdateColumns();
  1041. }
  1042. private void Path_Toggled(object sender, EventArgs args)
  1043. {
  1044. ConfigurationState.Instance.Ui.GuiColumns.PathColumn.Value = _pathToggle.Active;
  1045. SaveConfig();
  1046. UpdateColumns();
  1047. }
  1048. private void RefreshList_Pressed(object sender, ButtonReleaseEventArgs args)
  1049. {
  1050. UpdateGameTable();
  1051. }
  1052. private static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  1053. {
  1054. string aValue = model.GetValue(a, 5).ToString();
  1055. string bValue = model.GetValue(b, 5).ToString();
  1056. if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "mins")
  1057. {
  1058. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 60).ToString();
  1059. }
  1060. else if (aValue.Length > 3 && aValue.Substring(aValue.Length - 3) == "hrs")
  1061. {
  1062. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 4)) * 3600).ToString();
  1063. }
  1064. else if (aValue.Length > 4 && aValue.Substring(aValue.Length - 4) == "days")
  1065. {
  1066. aValue = (float.Parse(aValue.Substring(0, aValue.Length - 5)) * 86400).ToString();
  1067. }
  1068. else
  1069. {
  1070. aValue = aValue.Substring(0, aValue.Length - 1);
  1071. }
  1072. if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "mins")
  1073. {
  1074. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 60).ToString();
  1075. }
  1076. else if (bValue.Length > 3 && bValue.Substring(bValue.Length - 3) == "hrs")
  1077. {
  1078. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 4)) * 3600).ToString();
  1079. }
  1080. else if (bValue.Length > 4 && bValue.Substring(bValue.Length - 4) == "days")
  1081. {
  1082. bValue = (float.Parse(bValue.Substring(0, bValue.Length - 5)) * 86400).ToString();
  1083. }
  1084. else
  1085. {
  1086. bValue = bValue.Substring(0, bValue.Length - 1);
  1087. }
  1088. if (float.Parse(aValue) > float.Parse(bValue))
  1089. {
  1090. return -1;
  1091. }
  1092. else if (float.Parse(bValue) > float.Parse(aValue))
  1093. {
  1094. return 1;
  1095. }
  1096. else
  1097. {
  1098. return 0;
  1099. }
  1100. }
  1101. private static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  1102. {
  1103. string aValue = model.GetValue(a, 6).ToString();
  1104. string bValue = model.GetValue(b, 6).ToString();
  1105. if (aValue == "Never")
  1106. {
  1107. aValue = DateTime.UnixEpoch.ToString();
  1108. }
  1109. if (bValue == "Never")
  1110. {
  1111. bValue = DateTime.UnixEpoch.ToString();
  1112. }
  1113. return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
  1114. }
  1115. private static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
  1116. {
  1117. string aValue = model.GetValue(a, 8).ToString();
  1118. string bValue = model.GetValue(b, 8).ToString();
  1119. if (aValue.Substring(aValue.Length - 2) == "GB")
  1120. {
  1121. aValue = (float.Parse(aValue[0..^2]) * 1024).ToString();
  1122. }
  1123. else
  1124. {
  1125. aValue = aValue[0..^2];
  1126. }
  1127. if (bValue.Substring(bValue.Length - 2) == "GB")
  1128. {
  1129. bValue = (float.Parse(bValue[0..^2]) * 1024).ToString();
  1130. }
  1131. else
  1132. {
  1133. bValue = bValue[0..^2];
  1134. }
  1135. if (float.Parse(aValue) > float.Parse(bValue))
  1136. {
  1137. return -1;
  1138. }
  1139. else if (float.Parse(bValue) > float.Parse(aValue))
  1140. {
  1141. return 1;
  1142. }
  1143. else
  1144. {
  1145. return 0;
  1146. }
  1147. }
  1148. }
  1149. }