MainWindow.cs 50 KB

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