MainWindow.cs 49 KB

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