MainWindow.cs 48 KB

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