MainWindow.axaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. NotificationHelper.SetNotificationManager(this);
  86. }
  87. private void IsActiveChanged(bool obj)
  88. {
  89. ViewModel.IsActive = obj;
  90. }
  91. public void LoadGameList()
  92. {
  93. if (_isLoading)
  94. {
  95. return;
  96. }
  97. _isLoading = true;
  98. LoadApplications();
  99. _isLoading = false;
  100. }
  101. protected override void HandleScalingChanged(double scale)
  102. {
  103. Program.DesktopScaleFactor = scale;
  104. base.HandleScalingChanged(scale);
  105. }
  106. public void AddApplication(ApplicationData applicationData)
  107. {
  108. Dispatcher.UIThread.InvokeAsync(() =>
  109. {
  110. ViewModel.Applications.Add(applicationData);
  111. });
  112. }
  113. private void ApplicationLibrary_ApplicationAdded(object sender, ApplicationAddedEventArgs e)
  114. {
  115. AddApplication(e.AppData);
  116. }
  117. private void ApplicationLibrary_ApplicationCountUpdated(object sender, ApplicationCountUpdatedEventArgs e)
  118. {
  119. LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.StatusBarGamesLoaded, e.NumAppsLoaded, e.NumAppsFound);
  120. Dispatcher.UIThread.Post(() =>
  121. {
  122. ViewModel.StatusBarProgressValue = e.NumAppsLoaded;
  123. ViewModel.StatusBarProgressMaximum = e.NumAppsFound;
  124. if (e.NumAppsFound == 0)
  125. {
  126. StatusBarView.LoadProgressBar.IsVisible = false;
  127. }
  128. if (e.NumAppsLoaded == e.NumAppsFound)
  129. {
  130. StatusBarView.LoadProgressBar.IsVisible = false;
  131. }
  132. });
  133. }
  134. public void Application_Opened(object sender, ApplicationOpenedEventArgs args)
  135. {
  136. if (args.Application != null)
  137. {
  138. ViewModel.SelectedIcon = args.Application.Icon;
  139. string path = new FileInfo(args.Application.Path).FullName;
  140. ViewModel.LoadApplication(path);
  141. }
  142. args.Handled = true;
  143. }
  144. internal static void DeferLoadApplication(string launchPathArg, bool startFullscreenArg)
  145. {
  146. _deferLoad = true;
  147. _launchPath = launchPathArg;
  148. _startFullscreen = startFullscreenArg;
  149. }
  150. public void SwitchToGameControl(bool startFullscreen = false)
  151. {
  152. ViewModel.ShowLoadProgress = false;
  153. ViewModel.ShowContent = true;
  154. ViewModel.IsLoadingIndeterminate = false;
  155. Dispatcher.UIThread.InvokeAsync(() =>
  156. {
  157. if (startFullscreen && ViewModel.WindowState != WindowState.FullScreen)
  158. {
  159. ViewModel.ToggleFullscreen();
  160. }
  161. });
  162. }
  163. public void ShowLoading(bool startFullscreen = false)
  164. {
  165. ViewModel.ShowContent = false;
  166. ViewModel.ShowLoadProgress = true;
  167. ViewModel.IsLoadingIndeterminate = true;
  168. Dispatcher.UIThread.InvokeAsync(() =>
  169. {
  170. if (startFullscreen && ViewModel.WindowState != WindowState.FullScreen)
  171. {
  172. ViewModel.ToggleFullscreen();
  173. }
  174. });
  175. }
  176. protected override void HandleWindowStateChanged(WindowState state)
  177. {
  178. ViewModel.WindowState = state;
  179. if (state != WindowState.Minimized)
  180. {
  181. Renderer.Start();
  182. }
  183. }
  184. private void Initialize()
  185. {
  186. _userChannelPersistence = new UserChannelPersistence();
  187. VirtualFileSystem = VirtualFileSystem.CreateInstance();
  188. LibHacHorizonManager = new LibHacHorizonManager();
  189. ContentManager = new ContentManager(VirtualFileSystem);
  190. LibHacHorizonManager.InitializeFsServer(VirtualFileSystem);
  191. LibHacHorizonManager.InitializeArpServer();
  192. LibHacHorizonManager.InitializeBcatServer();
  193. LibHacHorizonManager.InitializeSystemClients();
  194. ApplicationLibrary = new ApplicationLibrary(VirtualFileSystem);
  195. // Save data created before we supported extra data in directory save data will not work properly if
  196. // given empty extra data. Luckily some of that extra data can be created using the data from the
  197. // save data indexer, which should be enough to check access permissions for user saves.
  198. // Every single save data's extra data will be checked and fixed if needed each time the emulator is opened.
  199. // Consider removing this at some point in the future when we don't need to worry about old saves.
  200. VirtualFileSystem.FixExtraData(LibHacHorizonManager.RyujinxClient);
  201. AccountManager = new AccountManager(LibHacHorizonManager.RyujinxClient, CommandLineState.Profile);
  202. VirtualFileSystem.ReloadKeySet();
  203. ApplicationHelper.Initialize(VirtualFileSystem, AccountManager, LibHacHorizonManager.RyujinxClient, this);
  204. }
  205. protected void CheckLaunchState()
  206. {
  207. if (ShowKeyErrorOnLoad)
  208. {
  209. ShowKeyErrorOnLoad = false;
  210. Dispatcher.UIThread.Post(async () => await
  211. UserErrorDialog.ShowUserErrorDialog(UserError.NoKeys, this));
  212. }
  213. if (_deferLoad)
  214. {
  215. _deferLoad = false;
  216. ViewModel.LoadApplication(_launchPath, _startFullscreen);
  217. }
  218. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false))
  219. {
  220. Updater.BeginParse(this, false).ContinueWith(task =>
  221. {
  222. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  223. }, TaskContinuationOptions.OnlyOnFaulted);
  224. }
  225. }
  226. private void Load()
  227. {
  228. StatusBarView.VolumeStatus.Click += VolumeStatus_CheckedChanged;
  229. GameGrid.ApplicationOpened += Application_Opened;
  230. GameGrid.DataContext = ViewModel;
  231. GameList.ApplicationOpened += Application_Opened;
  232. GameList.DataContext = ViewModel;
  233. LoadHotKeys();
  234. }
  235. protected override void OnOpened(EventArgs e)
  236. {
  237. base.OnOpened(e);
  238. CheckLaunchState();
  239. }
  240. private void SetMainContent(Control content = null)
  241. {
  242. if (content == null)
  243. {
  244. content = GameLibrary;
  245. }
  246. if (MainContent.Content != content)
  247. {
  248. MainContent.Content = content;
  249. }
  250. }
  251. public static void UpdateGraphicsConfig()
  252. {
  253. GraphicsConfig.ResScale = ConfigurationState.Instance.Graphics.ResScale == -1 ? ConfigurationState.Instance.Graphics.ResScaleCustom : ConfigurationState.Instance.Graphics.ResScale;
  254. GraphicsConfig.MaxAnisotropy = ConfigurationState.Instance.Graphics.MaxAnisotropy;
  255. GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
  256. GraphicsConfig.EnableShaderCache = ConfigurationState.Instance.Graphics.EnableShaderCache;
  257. GraphicsConfig.EnableTextureRecompression = ConfigurationState.Instance.Graphics.EnableTextureRecompression;
  258. GraphicsConfig.EnableMacroHLE = ConfigurationState.Instance.Graphics.EnableMacroHLE;
  259. }
  260. public void LoadHotKeys()
  261. {
  262. HotKeyManager.SetHotKey(FullscreenHotKey, new KeyGesture(Key.Enter, KeyModifiers.Alt));
  263. HotKeyManager.SetHotKey(FullscreenHotKey2, new KeyGesture(Key.F11));
  264. HotKeyManager.SetHotKey(FullscreenHotKeyMacOS, new KeyGesture(Key.F, KeyModifiers.Control | KeyModifiers.Meta));
  265. HotKeyManager.SetHotKey(DockToggleHotKey, new KeyGesture(Key.F9));
  266. HotKeyManager.SetHotKey(ExitHotKey, new KeyGesture(Key.Escape));
  267. }
  268. private void VolumeStatus_CheckedChanged(object sender, SplitButtonClickEventArgs e)
  269. {
  270. var volumeSplitButton = sender as ToggleSplitButton;
  271. if (ViewModel.IsGameRunning)
  272. {
  273. if (!volumeSplitButton.IsChecked)
  274. {
  275. ViewModel.AppHost.Device.SetVolume(ConfigurationState.Instance.System.AudioVolume);
  276. }
  277. else
  278. {
  279. ViewModel.AppHost.Device.SetVolume(0);
  280. }
  281. ViewModel.Volume = ViewModel.AppHost.Device.GetVolume();
  282. }
  283. }
  284. protected override void OnClosing(CancelEventArgs e)
  285. {
  286. if (!ViewModel.IsClosing && ViewModel.AppHost != null && ConfigurationState.Instance.ShowConfirmExit)
  287. {
  288. e.Cancel = true;
  289. ConfirmExit();
  290. return;
  291. }
  292. ViewModel.IsClosing = true;
  293. if (ViewModel.AppHost != null)
  294. {
  295. ViewModel.AppHost.AppExit -= ViewModel.AppHost_AppExit;
  296. ViewModel.AppHost.AppExit += (sender, e) =>
  297. {
  298. ViewModel.AppHost = null;
  299. Dispatcher.UIThread.Post(() =>
  300. {
  301. MainContent = null;
  302. Close();
  303. });
  304. };
  305. ViewModel.AppHost?.Stop();
  306. e.Cancel = true;
  307. return;
  308. }
  309. ApplicationLibrary.CancelLoading();
  310. InputManager.Dispose();
  311. Program.Exit();
  312. base.OnClosing(e);
  313. }
  314. private void ConfirmExit()
  315. {
  316. Dispatcher.UIThread.InvokeAsync(async () =>
  317. {
  318. ViewModel.IsClosing = await ContentDialogHelper.CreateExitDialog();
  319. if (ViewModel.IsClosing)
  320. {
  321. Close();
  322. }
  323. });
  324. }
  325. public async void LoadApplications()
  326. {
  327. await Dispatcher.UIThread.InvokeAsync(() =>
  328. {
  329. ViewModel.Applications.Clear();
  330. StatusBarView.LoadProgressBar.IsVisible = true;
  331. ViewModel.StatusBarProgressMaximum = 0;
  332. ViewModel.StatusBarProgressValue = 0;
  333. LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.StatusBarGamesLoaded, 0, 0);
  334. });
  335. ReloadGameList();
  336. }
  337. private void ReloadGameList()
  338. {
  339. if (_isLoading)
  340. {
  341. return;
  342. }
  343. _isLoading = true;
  344. ApplicationLibrary.LoadApplications(ConfigurationState.Instance.Ui.GameDirs.Value, ConfigurationState.Instance.System.Language);
  345. _isLoading = false;
  346. }
  347. }
  348. }