MainWindow.cs 52 KB

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