MainWindow.axaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Threading;
  5. using FluentAvalonia.UI.Controls;
  6. using Ryujinx.Ava.Common;
  7. using Ryujinx.Ava.Common.Locale;
  8. using Ryujinx.Ava.Input;
  9. using Ryujinx.Ava.UI.Applet;
  10. using Ryujinx.Ava.UI.Helpers;
  11. using Ryujinx.Ava.UI.ViewModels;
  12. using Ryujinx.Common.Logging;
  13. using Ryujinx.Graphics.Gpu;
  14. using Ryujinx.HLE.FileSystem;
  15. using Ryujinx.HLE.HOS;
  16. using Ryujinx.HLE.HOS.Services.Account.Acc;
  17. using Ryujinx.Input.SDL2;
  18. using Ryujinx.Modules;
  19. using Ryujinx.Ui.App.Common;
  20. using Ryujinx.Ui.Common;
  21. using Ryujinx.Ui.Common.Configuration;
  22. using Ryujinx.Ui.Common.Helper;
  23. using System;
  24. using System.ComponentModel;
  25. using System.IO;
  26. using System.Threading.Tasks;
  27. using InputManager = Ryujinx.Input.HLE.InputManager;
  28. namespace Ryujinx.Ava.UI.Windows
  29. {
  30. public partial class MainWindow : StyleableWindow
  31. {
  32. internal static MainWindowViewModel MainWindowViewModel { get; private set; }
  33. private bool _isLoading;
  34. private UserChannelPersistence _userChannelPersistence;
  35. private static bool _deferLoad;
  36. private static string _launchPath;
  37. private static bool _startFullscreen;
  38. internal readonly AvaHostUiHandler UiHandler;
  39. public VirtualFileSystem VirtualFileSystem { get; private set; }
  40. public ContentManager ContentManager { get; private set; }
  41. public AccountManager AccountManager { get; private set; }
  42. public LibHacHorizonManager LibHacHorizonManager { get; private set; }
  43. public InputManager InputManager { get; private set; }
  44. internal MainWindowViewModel ViewModel { get; private set; }
  45. public SettingsWindow SettingsWindow { get; set; }
  46. public static bool ShowKeyErrorOnLoad { get; set; }
  47. public ApplicationLibrary ApplicationLibrary { get; set; }
  48. public MainWindow()
  49. {
  50. ViewModel = new MainWindowViewModel();
  51. MainWindowViewModel = ViewModel;
  52. DataContext = ViewModel;
  53. InitializeComponent();
  54. Load();
  55. UiHandler = new AvaHostUiHandler(this);
  56. ViewModel.Title = $"Ryujinx {Program.Version}";
  57. // NOTE: Height of MenuBar and StatusBar is not usable here, since it would still be 0 at this point.
  58. double barHeight = MenuBar.MinHeight + StatusBarView.StatusBar.MinHeight;
  59. Height = ((Height - barHeight) / Program.WindowScaleFactor) + barHeight;
  60. Width /= Program.WindowScaleFactor;
  61. if (Program.PreviewerDetached)
  62. {
  63. Initialize();
  64. InputManager = new InputManager(new AvaloniaKeyboardDriver(this), new SDL2GamepadDriver());
  65. ViewModel.Initialize(
  66. ContentManager,
  67. ApplicationLibrary,
  68. VirtualFileSystem,
  69. AccountManager,
  70. InputManager,
  71. _userChannelPersistence,
  72. LibHacHorizonManager,
  73. UiHandler,
  74. ShowLoading,
  75. SwitchToGameControl,
  76. SetMainContent,
  77. this);
  78. ViewModel.RefreshFirmwareStatus();
  79. LoadGameList();
  80. this.GetObservable(IsActiveProperty).Subscribe(IsActiveChanged);
  81. }
  82. ApplicationLibrary.ApplicationCountUpdated += ApplicationLibrary_ApplicationCountUpdated;
  83. ApplicationLibrary.ApplicationAdded += ApplicationLibrary_ApplicationAdded;
  84. ViewModel.ReloadGameList += ReloadGameList;
  85. }
  86. private void IsActiveChanged(bool obj)
  87. {
  88. ViewModel.IsActive = obj;
  89. }
  90. public void LoadGameList()
  91. {
  92. if (_isLoading)
  93. {
  94. return;
  95. }
  96. _isLoading = true;
  97. LoadApplications();
  98. _isLoading = false;
  99. }
  100. protected override void HandleScalingChanged(double scale)
  101. {
  102. Program.DesktopScaleFactor = scale;
  103. base.HandleScalingChanged(scale);
  104. }
  105. public void AddApplication(ApplicationData applicationData)
  106. {
  107. Dispatcher.UIThread.InvokeAsync(() =>
  108. {
  109. ViewModel.Applications.Add(applicationData);
  110. });
  111. }
  112. private void ApplicationLibrary_ApplicationAdded(object sender, ApplicationAddedEventArgs e)
  113. {
  114. AddApplication(e.AppData);
  115. }
  116. private void ApplicationLibrary_ApplicationCountUpdated(object sender, ApplicationCountUpdatedEventArgs e)
  117. {
  118. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarGamesLoaded, e.NumAppsLoaded, e.NumAppsFound);
  119. Dispatcher.UIThread.Post(() =>
  120. {
  121. ViewModel.StatusBarProgressValue = e.NumAppsLoaded;
  122. ViewModel.StatusBarProgressMaximum = e.NumAppsFound;
  123. if (e.NumAppsFound == 0)
  124. {
  125. StatusBarView.LoadProgressBar.IsVisible = false;
  126. }
  127. if (e.NumAppsLoaded == e.NumAppsFound)
  128. {
  129. StatusBarView.LoadProgressBar.IsVisible = false;
  130. }
  131. });
  132. }
  133. public void Application_Opened(object sender, ApplicationOpenedEventArgs args)
  134. {
  135. if (args.Application != null)
  136. {
  137. ViewModel.SelectedIcon = args.Application.Icon;
  138. string path = new FileInfo(args.Application.Path).FullName;
  139. ViewModel.LoadApplication(path);
  140. }
  141. args.Handled = true;
  142. }
  143. internal static void DeferLoadApplication(string launchPathArg, bool startFullscreenArg)
  144. {
  145. _deferLoad = true;
  146. _launchPath = launchPathArg;
  147. _startFullscreen = startFullscreenArg;
  148. }
  149. public void SwitchToGameControl(bool startFullscreen = false)
  150. {
  151. ViewModel.ShowLoadProgress = false;
  152. ViewModel.ShowContent = true;
  153. ViewModel.IsLoadingIndeterminate = false;
  154. Dispatcher.UIThread.InvokeAsync(() =>
  155. {
  156. if (startFullscreen && ViewModel.WindowState != WindowState.FullScreen)
  157. {
  158. ViewModel.ToggleFullscreen();
  159. }
  160. });
  161. }
  162. public void ShowLoading(bool startFullscreen = false)
  163. {
  164. ViewModel.ShowContent = false;
  165. ViewModel.ShowLoadProgress = true;
  166. ViewModel.IsLoadingIndeterminate = true;
  167. Dispatcher.UIThread.InvokeAsync(() =>
  168. {
  169. if (startFullscreen && ViewModel.WindowState != WindowState.FullScreen)
  170. {
  171. ViewModel.ToggleFullscreen();
  172. }
  173. });
  174. }
  175. protected override void HandleWindowStateChanged(WindowState state)
  176. {
  177. ViewModel.WindowState = state;
  178. if (state != WindowState.Minimized)
  179. {
  180. Renderer.Start();
  181. }
  182. }
  183. private void Initialize()
  184. {
  185. _userChannelPersistence = new UserChannelPersistence();
  186. VirtualFileSystem = VirtualFileSystem.CreateInstance();
  187. LibHacHorizonManager = new LibHacHorizonManager();
  188. ContentManager = new ContentManager(VirtualFileSystem);
  189. LibHacHorizonManager.InitializeFsServer(VirtualFileSystem);
  190. LibHacHorizonManager.InitializeArpServer();
  191. LibHacHorizonManager.InitializeBcatServer();
  192. LibHacHorizonManager.InitializeSystemClients();
  193. ApplicationLibrary = new ApplicationLibrary(VirtualFileSystem);
  194. // Save data created before we supported extra data in directory save data will not work properly if
  195. // given empty extra data. Luckily some of that extra data can be created using the data from the
  196. // save data indexer, which should be enough to check access permissions for user saves.
  197. // Every single save data's extra data will be checked and fixed if needed each time the emulator is opened.
  198. // Consider removing this at some point in the future when we don't need to worry about old saves.
  199. VirtualFileSystem.FixExtraData(LibHacHorizonManager.RyujinxClient);
  200. AccountManager = new AccountManager(LibHacHorizonManager.RyujinxClient, CommandLineState.Profile);
  201. VirtualFileSystem.ReloadKeySet();
  202. ApplicationHelper.Initialize(VirtualFileSystem, AccountManager, LibHacHorizonManager.RyujinxClient, this);
  203. }
  204. protected void CheckLaunchState()
  205. {
  206. if (ShowKeyErrorOnLoad)
  207. {
  208. ShowKeyErrorOnLoad = false;
  209. Dispatcher.UIThread.Post(async () => await
  210. UserErrorDialog.ShowUserErrorDialog(UserError.NoKeys, this));
  211. }
  212. if (_deferLoad)
  213. {
  214. _deferLoad = false;
  215. ViewModel.LoadApplication(_launchPath, _startFullscreen);
  216. }
  217. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false, this))
  218. {
  219. Updater.BeginParse(this, false).ContinueWith(task =>
  220. {
  221. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  222. }, TaskContinuationOptions.OnlyOnFaulted);
  223. }
  224. }
  225. private void Load()
  226. {
  227. StatusBarView.VolumeStatus.Click += VolumeStatus_CheckedChanged;
  228. GameGrid.ApplicationOpened += Application_Opened;
  229. GameGrid.DataContext = ViewModel;
  230. GameList.ApplicationOpened += Application_Opened;
  231. GameList.DataContext = ViewModel;
  232. LoadHotKeys();
  233. }
  234. protected override void OnOpened(EventArgs e)
  235. {
  236. base.OnOpened(e);
  237. CheckLaunchState();
  238. }
  239. private void SetMainContent(Control content = null)
  240. {
  241. if (content == null)
  242. {
  243. content = GameLibrary;
  244. }
  245. if (MainContent.Content != content)
  246. {
  247. MainContent.Content = content;
  248. }
  249. }
  250. public static void UpdateGraphicsConfig()
  251. {
  252. GraphicsConfig.ResScale = ConfigurationState.Instance.Graphics.ResScale == -1 ? ConfigurationState.Instance.Graphics.ResScaleCustom : ConfigurationState.Instance.Graphics.ResScale;
  253. GraphicsConfig.MaxAnisotropy = ConfigurationState.Instance.Graphics.MaxAnisotropy;
  254. GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
  255. GraphicsConfig.EnableShaderCache = ConfigurationState.Instance.Graphics.EnableShaderCache;
  256. GraphicsConfig.EnableTextureRecompression = ConfigurationState.Instance.Graphics.EnableTextureRecompression;
  257. GraphicsConfig.EnableMacroHLE = ConfigurationState.Instance.Graphics.EnableMacroHLE;
  258. }
  259. public void LoadHotKeys()
  260. {
  261. HotKeyManager.SetHotKey(FullscreenHotKey, new KeyGesture(Key.Enter, KeyModifiers.Alt));
  262. HotKeyManager.SetHotKey(FullscreenHotKey2, new KeyGesture(Key.F11));
  263. HotKeyManager.SetHotKey(DockToggleHotKey, new KeyGesture(Key.F9));
  264. HotKeyManager.SetHotKey(ExitHotKey, new KeyGesture(Key.Escape));
  265. }
  266. private void VolumeStatus_CheckedChanged(object sender, SplitButtonClickEventArgs e)
  267. {
  268. var volumeSplitButton = sender as ToggleSplitButton;
  269. if (ViewModel.IsGameRunning)
  270. {
  271. if (!volumeSplitButton.IsChecked)
  272. {
  273. ViewModel.AppHost.Device.SetVolume(ConfigurationState.Instance.System.AudioVolume);
  274. }
  275. else
  276. {
  277. ViewModel.AppHost.Device.SetVolume(0);
  278. }
  279. ViewModel.Volume = ViewModel.AppHost.Device.GetVolume();
  280. }
  281. }
  282. protected override void OnClosing(CancelEventArgs e)
  283. {
  284. if (!ViewModel.IsClosing && ViewModel.AppHost != null && ConfigurationState.Instance.ShowConfirmExit)
  285. {
  286. e.Cancel = true;
  287. ConfirmExit();
  288. return;
  289. }
  290. ViewModel.IsClosing = true;
  291. if (ViewModel.AppHost != null)
  292. {
  293. ViewModel.AppHost.AppExit -= ViewModel.AppHost_AppExit;
  294. ViewModel.AppHost.AppExit += (sender, e) =>
  295. {
  296. ViewModel.AppHost = null;
  297. Dispatcher.UIThread.Post(() =>
  298. {
  299. MainContent = null;
  300. Close();
  301. });
  302. };
  303. ViewModel.AppHost?.Stop();
  304. e.Cancel = true;
  305. return;
  306. }
  307. ApplicationLibrary.CancelLoading();
  308. InputManager.Dispose();
  309. Program.Exit();
  310. base.OnClosing(e);
  311. }
  312. private void ConfirmExit()
  313. {
  314. Dispatcher.UIThread.InvokeAsync(async () =>
  315. {
  316. ViewModel.IsClosing = await ContentDialogHelper.CreateExitDialog();
  317. if (ViewModel.IsClosing)
  318. {
  319. Close();
  320. }
  321. });
  322. }
  323. public async void LoadApplications()
  324. {
  325. await Dispatcher.UIThread.InvokeAsync(() =>
  326. {
  327. ViewModel.Applications.Clear();
  328. StatusBarView.LoadProgressBar.IsVisible = true;
  329. ViewModel.StatusBarProgressMaximum = 0;
  330. ViewModel.StatusBarProgressValue = 0;
  331. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarGamesLoaded, 0, 0);
  332. });
  333. ReloadGameList();
  334. }
  335. private void ReloadGameList()
  336. {
  337. if (_isLoading)
  338. {
  339. return;
  340. }
  341. _isLoading = true;
  342. ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs.Value, ConfigurationState.Instance.System.Language);
  343. _isLoading = false;
  344. }
  345. }
  346. }