SettingsViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using DynamicData;
  5. using LibHac.Tools.FsSystem;
  6. using Ryujinx.Audio.Backends.OpenAL;
  7. using Ryujinx.Audio.Backends.SDL2;
  8. using Ryujinx.Audio.Backends.SoundIo;
  9. using Ryujinx.Ava.Common.Locale;
  10. using Ryujinx.Ava.UI.Helpers;
  11. using Ryujinx.Ava.UI.Windows;
  12. using Ryujinx.Common.Configuration;
  13. using Ryujinx.Common.Configuration.Hid;
  14. using Ryujinx.Common.GraphicsDriver;
  15. using Ryujinx.Common.Logging;
  16. using Ryujinx.Graphics.Vulkan;
  17. using Ryujinx.HLE.FileSystem;
  18. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  19. using Ryujinx.Ui.Common.Configuration;
  20. using Ryujinx.Ui.Common.Configuration.System;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Collections.ObjectModel;
  24. using System.Linq;
  25. using System.Runtime.InteropServices;
  26. using System.Net.NetworkInformation;
  27. using TimeZone = Ryujinx.Ava.UI.Models.TimeZone;
  28. namespace Ryujinx.Ava.UI.ViewModels
  29. {
  30. public class SettingsViewModel : BaseModel
  31. {
  32. private readonly VirtualFileSystem _virtualFileSystem;
  33. private readonly ContentManager _contentManager;
  34. private TimeZoneContentManager _timeZoneContentManager;
  35. private readonly List<string> _validTzRegions;
  36. private readonly Dictionary<string, string> _networkInterfaces;
  37. private float _customResolutionScale;
  38. private int _resolutionScale;
  39. private int _graphicsBackendMultithreadingIndex;
  40. private float _volume;
  41. private bool _isVulkanAvailable = true;
  42. private bool _directoryChanged;
  43. private List<string> _gpuIds = new();
  44. private KeyboardHotkeys _keyboardHotkeys;
  45. private int _graphicsBackendIndex;
  46. private string _customThemePath;
  47. private int _scalingFilter;
  48. private int _scalingFilterLevel;
  49. public event Action CloseWindow;
  50. public event Action SaveSettingsEvent;
  51. private int _networkInterfaceIndex;
  52. public int ResolutionScale
  53. {
  54. get => _resolutionScale;
  55. set
  56. {
  57. _resolutionScale = value;
  58. OnPropertyChanged(nameof(CustomResolutionScale));
  59. OnPropertyChanged(nameof(IsCustomResolutionScaleActive));
  60. }
  61. }
  62. public int GraphicsBackendMultithreadingIndex
  63. {
  64. get => _graphicsBackendMultithreadingIndex;
  65. set
  66. {
  67. _graphicsBackendMultithreadingIndex = value;
  68. if (_graphicsBackendMultithreadingIndex != (int)ConfigurationState.Instance.Graphics.BackendThreading.Value)
  69. {
  70. Dispatcher.UIThread.Post(async () =>
  71. {
  72. await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogSettingsBackendThreadingWarningMessage],
  73. "",
  74. "",
  75. LocaleManager.Instance[LocaleKeys.InputDialogOk],
  76. LocaleManager.Instance[LocaleKeys.DialogSettingsBackendThreadingWarningTitle]);
  77. });
  78. }
  79. OnPropertyChanged();
  80. }
  81. }
  82. public float CustomResolutionScale
  83. {
  84. get => _customResolutionScale;
  85. set
  86. {
  87. _customResolutionScale = MathF.Round(value, 1);
  88. OnPropertyChanged();
  89. }
  90. }
  91. public bool IsVulkanAvailable
  92. {
  93. get => _isVulkanAvailable;
  94. set
  95. {
  96. _isVulkanAvailable = value;
  97. OnPropertyChanged();
  98. }
  99. }
  100. public bool IsOpenGLAvailable => !OperatingSystem.IsMacOS();
  101. public bool IsHypervisorAvailable => OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64;
  102. public bool DirectoryChanged
  103. {
  104. get => _directoryChanged;
  105. set
  106. {
  107. _directoryChanged = value;
  108. OnPropertyChanged();
  109. }
  110. }
  111. public bool IsMacOS => OperatingSystem.IsMacOS();
  112. public bool EnableDiscordIntegration { get; set; }
  113. public bool CheckUpdatesOnStart { get; set; }
  114. public bool ShowConfirmExit { get; set; }
  115. public bool HideCursorOnIdle { get; set; }
  116. public bool EnableDockedMode { get; set; }
  117. public bool EnableKeyboard { get; set; }
  118. public bool EnableMouse { get; set; }
  119. public bool EnableVsync { get; set; }
  120. public bool EnablePptc { get; set; }
  121. public bool EnableInternetAccess { get; set; }
  122. public bool EnableFsIntegrityChecks { get; set; }
  123. public bool IgnoreMissingServices { get; set; }
  124. public bool ExpandDramSize { get; set; }
  125. public bool EnableShaderCache { get; set; }
  126. public bool EnableTextureRecompression { get; set; }
  127. public bool EnableMacroHLE { get; set; }
  128. public bool EnableFileLog { get; set; }
  129. public bool EnableStub { get; set; }
  130. public bool EnableInfo { get; set; }
  131. public bool EnableWarn { get; set; }
  132. public bool EnableError { get; set; }
  133. public bool EnableTrace { get; set; }
  134. public bool EnableGuest { get; set; }
  135. public bool EnableFsAccessLog { get; set; }
  136. public bool EnableDebug { get; set; }
  137. public bool IsOpenAlEnabled { get; set; }
  138. public bool IsSoundIoEnabled { get; set; }
  139. public bool IsSDL2Enabled { get; set; }
  140. public bool EnableCustomTheme { get; set; }
  141. public bool IsCustomResolutionScaleActive => _resolutionScale == 4;
  142. public bool IsScalingFilterActive => _scalingFilter == (int)Ryujinx.Common.Configuration.ScalingFilter.Fsr;
  143. public bool IsVulkanSelected => GraphicsBackendIndex == 0;
  144. public bool UseHypervisor { get; set; }
  145. public string TimeZone { get; set; }
  146. public string ShaderDumpPath { get; set; }
  147. public string CustomThemePath
  148. {
  149. get
  150. {
  151. return _customThemePath;
  152. }
  153. set
  154. {
  155. _customThemePath = value;
  156. OnPropertyChanged();
  157. }
  158. }
  159. public int Language { get; set; }
  160. public int Region { get; set; }
  161. public int FsGlobalAccessLogMode { get; set; }
  162. public int AudioBackend { get; set; }
  163. public int MaxAnisotropy { get; set; }
  164. public int AspectRatio { get; set; }
  165. public int AntiAliasingEffect { get; set; }
  166. public string ScalingFilterLevelText => ScalingFilterLevel.ToString("0");
  167. public int ScalingFilterLevel
  168. {
  169. get => _scalingFilterLevel;
  170. set
  171. {
  172. _scalingFilterLevel = value;
  173. OnPropertyChanged();
  174. OnPropertyChanged(nameof(ScalingFilterLevelText));
  175. }
  176. }
  177. public int OpenglDebugLevel { get; set; }
  178. public int MemoryMode { get; set; }
  179. public int BaseStyleIndex { get; set; }
  180. public int GraphicsBackendIndex
  181. {
  182. get => _graphicsBackendIndex;
  183. set
  184. {
  185. _graphicsBackendIndex = value;
  186. OnPropertyChanged();
  187. OnPropertyChanged(nameof(IsVulkanSelected));
  188. }
  189. }
  190. public int ScalingFilter
  191. {
  192. get => _scalingFilter;
  193. set
  194. {
  195. _scalingFilter = value;
  196. OnPropertyChanged();
  197. OnPropertyChanged(nameof(IsScalingFilterActive));
  198. }
  199. }
  200. public int PreferredGpuIndex { get; set; }
  201. public float Volume
  202. {
  203. get => _volume;
  204. set
  205. {
  206. _volume = value;
  207. ConfigurationState.Instance.System.AudioVolume.Value = _volume / 100;
  208. OnPropertyChanged();
  209. }
  210. }
  211. public DateTimeOffset DateOffset { get; set; }
  212. public TimeSpan TimeOffset { get; set; }
  213. internal AvaloniaList<TimeZone> TimeZones { get; set; }
  214. public AvaloniaList<string> GameDirectories { get; set; }
  215. public ObservableCollection<ComboBoxItem> AvailableGpus { get; set; }
  216. public AvaloniaList<string> NetworkInterfaceList
  217. {
  218. get => new AvaloniaList<string>(_networkInterfaces.Keys);
  219. }
  220. public KeyboardHotkeys KeyboardHotkeys
  221. {
  222. get => _keyboardHotkeys;
  223. set
  224. {
  225. _keyboardHotkeys = value;
  226. OnPropertyChanged();
  227. }
  228. }
  229. public int NetworkInterfaceIndex
  230. {
  231. get => _networkInterfaceIndex;
  232. set
  233. {
  234. _networkInterfaceIndex = value != -1 ? value : 0;
  235. ConfigurationState.Instance.Multiplayer.LanInterfaceId.Value = _networkInterfaces[NetworkInterfaceList[_networkInterfaceIndex]];
  236. }
  237. }
  238. public SettingsViewModel(VirtualFileSystem virtualFileSystem, ContentManager contentManager) : this()
  239. {
  240. _virtualFileSystem = virtualFileSystem;
  241. _contentManager = contentManager;
  242. if (Program.PreviewerDetached)
  243. {
  244. LoadTimeZones();
  245. }
  246. }
  247. public SettingsViewModel()
  248. {
  249. GameDirectories = new AvaloniaList<string>();
  250. TimeZones = new AvaloniaList<TimeZone>();
  251. AvailableGpus = new ObservableCollection<ComboBoxItem>();
  252. _validTzRegions = new List<string>();
  253. _networkInterfaces = new Dictionary<string, string>();
  254. CheckSoundBackends();
  255. PopulateNetworkInterfaces();
  256. if (Program.PreviewerDetached)
  257. {
  258. LoadAvailableGpus();
  259. LoadCurrentConfiguration();
  260. }
  261. }
  262. public void CheckSoundBackends()
  263. {
  264. IsOpenAlEnabled = OpenALHardwareDeviceDriver.IsSupported;
  265. IsSoundIoEnabled = SoundIoHardwareDeviceDriver.IsSupported;
  266. IsSDL2Enabled = SDL2HardwareDeviceDriver.IsSupported;
  267. }
  268. private void LoadAvailableGpus()
  269. {
  270. _gpuIds = new List<string>();
  271. List<string> names = new();
  272. var devices = VulkanRenderer.GetPhysicalDevices();
  273. if (devices.Length == 0)
  274. {
  275. IsVulkanAvailable = false;
  276. GraphicsBackendIndex = 1;
  277. }
  278. else
  279. {
  280. foreach (var device in devices)
  281. {
  282. _gpuIds.Add(device.Id);
  283. names.Add($"{device.Name} {(device.IsDiscrete ? "(dGPU)" : "")}");
  284. }
  285. }
  286. AvailableGpus.Clear();
  287. AvailableGpus.AddRange(names.Select(x => new ComboBoxItem { Content = x }));
  288. }
  289. public void LoadTimeZones()
  290. {
  291. _timeZoneContentManager = new TimeZoneContentManager();
  292. _timeZoneContentManager.InitializeInstance(_virtualFileSystem, _contentManager, IntegrityCheckLevel.None);
  293. foreach ((int offset, string location, string abbr) in _timeZoneContentManager.ParseTzOffsets())
  294. {
  295. int hours = Math.DivRem(offset, 3600, out int seconds);
  296. int minutes = Math.Abs(seconds) / 60;
  297. string abbr2 = abbr.StartsWith('+') || abbr.StartsWith('-') ? string.Empty : abbr;
  298. TimeZones.Add(new TimeZone($"UTC{hours:+0#;-0#;+00}:{minutes:D2}", location, abbr2));
  299. _validTzRegions.Add(location);
  300. }
  301. }
  302. private void PopulateNetworkInterfaces()
  303. {
  304. _networkInterfaces.Clear();
  305. _networkInterfaces.Add(LocaleManager.Instance[LocaleKeys.NetworkInterfaceDefault], "0");
  306. foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
  307. {
  308. _networkInterfaces.Add(networkInterface.Name, networkInterface.Id);
  309. }
  310. }
  311. public void ValidateAndSetTimeZone(string location)
  312. {
  313. if (_validTzRegions.Contains(location))
  314. {
  315. TimeZone = location;
  316. }
  317. }
  318. public void LoadCurrentConfiguration()
  319. {
  320. ConfigurationState config = ConfigurationState.Instance;
  321. // User Interface
  322. EnableDiscordIntegration = config.EnableDiscordIntegration;
  323. CheckUpdatesOnStart = config.CheckUpdatesOnStart;
  324. ShowConfirmExit = config.ShowConfirmExit;
  325. HideCursorOnIdle = config.HideCursorOnIdle;
  326. GameDirectories.Clear();
  327. GameDirectories.AddRange(config.Ui.GameDirs.Value);
  328. EnableCustomTheme = config.Ui.EnableCustomTheme;
  329. CustomThemePath = config.Ui.CustomThemePath;
  330. BaseStyleIndex = config.Ui.BaseStyle == "Light" ? 0 : 1;
  331. // Input
  332. EnableDockedMode = config.System.EnableDockedMode;
  333. EnableKeyboard = config.Hid.EnableKeyboard;
  334. EnableMouse = config.Hid.EnableMouse;
  335. // Keyboard Hotkeys
  336. KeyboardHotkeys = config.Hid.Hotkeys.Value;
  337. // System
  338. Region = (int)config.System.Region.Value;
  339. Language = (int)config.System.Language.Value;
  340. TimeZone = config.System.TimeZone;
  341. DateTime dateTimeOffset = DateTime.Now.AddSeconds(config.System.SystemTimeOffset);
  342. DateOffset = dateTimeOffset.Date;
  343. TimeOffset = dateTimeOffset.TimeOfDay;
  344. EnableVsync = config.Graphics.EnableVsync;
  345. EnableFsIntegrityChecks = config.System.EnableFsIntegrityChecks;
  346. ExpandDramSize = config.System.ExpandRam;
  347. IgnoreMissingServices = config.System.IgnoreMissingServices;
  348. // CPU
  349. EnablePptc = config.System.EnablePtc;
  350. MemoryMode = (int)config.System.MemoryManagerMode.Value;
  351. UseHypervisor = config.System.UseHypervisor;
  352. // Graphics
  353. GraphicsBackendIndex = (int)config.Graphics.GraphicsBackend.Value;
  354. PreferredGpuIndex = _gpuIds.Contains(config.Graphics.PreferredGpu) ? _gpuIds.IndexOf(config.Graphics.PreferredGpu) : 0;
  355. EnableShaderCache = config.Graphics.EnableShaderCache;
  356. EnableTextureRecompression = config.Graphics.EnableTextureRecompression;
  357. EnableMacroHLE = config.Graphics.EnableMacroHLE;
  358. ResolutionScale = config.Graphics.ResScale == -1 ? 4 : config.Graphics.ResScale - 1;
  359. CustomResolutionScale = config.Graphics.ResScaleCustom;
  360. MaxAnisotropy = config.Graphics.MaxAnisotropy == -1 ? 0 : (int)(MathF.Log2(config.Graphics.MaxAnisotropy));
  361. AspectRatio = (int)config.Graphics.AspectRatio.Value;
  362. GraphicsBackendMultithreadingIndex = (int)config.Graphics.BackendThreading.Value;
  363. ShaderDumpPath = config.Graphics.ShadersDumpPath;
  364. AntiAliasingEffect = (int)config.Graphics.AntiAliasing.Value;
  365. ScalingFilter = (int)config.Graphics.ScalingFilter.Value;
  366. ScalingFilterLevel = config.Graphics.ScalingFilterLevel.Value;
  367. // Audio
  368. AudioBackend = (int)config.System.AudioBackend.Value;
  369. Volume = config.System.AudioVolume * 100;
  370. // Network
  371. EnableInternetAccess = config.System.EnableInternetAccess;
  372. // Logging
  373. EnableFileLog = config.Logger.EnableFileLog;
  374. EnableStub = config.Logger.EnableStub;
  375. EnableInfo = config.Logger.EnableInfo;
  376. EnableWarn = config.Logger.EnableWarn;
  377. EnableError = config.Logger.EnableError;
  378. EnableTrace = config.Logger.EnableTrace;
  379. EnableGuest = config.Logger.EnableGuest;
  380. EnableDebug = config.Logger.EnableDebug;
  381. EnableFsAccessLog = config.Logger.EnableFsAccessLog;
  382. FsGlobalAccessLogMode = config.System.FsGlobalAccessLogMode;
  383. OpenglDebugLevel = (int)config.Logger.GraphicsDebugLevel.Value;
  384. NetworkInterfaceIndex = _networkInterfaces.Values.ToList().IndexOf(config.Multiplayer.LanInterfaceId.Value);
  385. }
  386. public void SaveSettings()
  387. {
  388. ConfigurationState config = ConfigurationState.Instance;
  389. // User Interface
  390. config.EnableDiscordIntegration.Value = EnableDiscordIntegration;
  391. config.CheckUpdatesOnStart.Value = CheckUpdatesOnStart;
  392. config.ShowConfirmExit.Value = ShowConfirmExit;
  393. config.HideCursorOnIdle.Value = HideCursorOnIdle;
  394. if (_directoryChanged)
  395. {
  396. List<string> gameDirs = new(GameDirectories);
  397. config.Ui.GameDirs.Value = gameDirs;
  398. }
  399. config.Ui.EnableCustomTheme.Value = EnableCustomTheme;
  400. config.Ui.CustomThemePath.Value = CustomThemePath;
  401. config.Ui.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark";
  402. // Input
  403. config.System.EnableDockedMode.Value = EnableDockedMode;
  404. config.Hid.EnableKeyboard.Value = EnableKeyboard;
  405. config.Hid.EnableMouse.Value = EnableMouse;
  406. // Keyboard Hotkeys
  407. config.Hid.Hotkeys.Value = KeyboardHotkeys;
  408. // System
  409. config.System.Region.Value = (Region)Region;
  410. config.System.Language.Value = (Language)Language;
  411. if (_validTzRegions.Contains(TimeZone))
  412. {
  413. config.System.TimeZone.Value = TimeZone;
  414. }
  415. TimeSpan systemTimeOffset = DateOffset - DateTime.Now;
  416. config.System.SystemTimeOffset.Value = systemTimeOffset.Seconds;
  417. config.Graphics.EnableVsync.Value = EnableVsync;
  418. config.System.EnableFsIntegrityChecks.Value = EnableFsIntegrityChecks;
  419. config.System.ExpandRam.Value = ExpandDramSize;
  420. config.System.IgnoreMissingServices.Value = IgnoreMissingServices;
  421. // CPU
  422. config.System.EnablePtc.Value = EnablePptc;
  423. config.System.MemoryManagerMode.Value = (MemoryManagerMode)MemoryMode;
  424. config.System.UseHypervisor.Value = UseHypervisor;
  425. // Graphics
  426. config.Graphics.GraphicsBackend.Value = (GraphicsBackend)GraphicsBackendIndex;
  427. config.Graphics.PreferredGpu.Value = _gpuIds.ElementAtOrDefault(PreferredGpuIndex);
  428. config.Graphics.EnableShaderCache.Value = EnableShaderCache;
  429. config.Graphics.EnableTextureRecompression.Value = EnableTextureRecompression;
  430. config.Graphics.EnableMacroHLE.Value = EnableMacroHLE;
  431. config.Graphics.ResScale.Value = ResolutionScale == 4 ? -1 : ResolutionScale + 1;
  432. config.Graphics.ResScaleCustom.Value = CustomResolutionScale;
  433. config.Graphics.MaxAnisotropy.Value = MaxAnisotropy == 0 ? -1 : MathF.Pow(2, MaxAnisotropy);
  434. config.Graphics.AspectRatio.Value = (AspectRatio)AspectRatio;
  435. config.Graphics.AntiAliasing.Value = (AntiAliasing)AntiAliasingEffect;
  436. config.Graphics.ScalingFilter.Value = (ScalingFilter)ScalingFilter;
  437. config.Graphics.ScalingFilterLevel.Value = ScalingFilterLevel;
  438. if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex)
  439. {
  440. DriverUtilities.ToggleOGLThreading(GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off);
  441. }
  442. config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;
  443. config.Graphics.ShadersDumpPath.Value = ShaderDumpPath;
  444. // Audio
  445. AudioBackend audioBackend = (AudioBackend)AudioBackend;
  446. if (audioBackend != config.System.AudioBackend.Value)
  447. {
  448. config.System.AudioBackend.Value = audioBackend;
  449. Logger.Info?.Print(LogClass.Application, $"AudioBackend toggled to: {audioBackend}");
  450. }
  451. config.System.AudioVolume.Value = Volume / 100;
  452. // Network
  453. config.System.EnableInternetAccess.Value = EnableInternetAccess;
  454. // Logging
  455. config.Logger.EnableFileLog.Value = EnableFileLog;
  456. config.Logger.EnableStub.Value = EnableStub;
  457. config.Logger.EnableInfo.Value = EnableInfo;
  458. config.Logger.EnableWarn.Value = EnableWarn;
  459. config.Logger.EnableError.Value = EnableError;
  460. config.Logger.EnableTrace.Value = EnableTrace;
  461. config.Logger.EnableGuest.Value = EnableGuest;
  462. config.Logger.EnableDebug.Value = EnableDebug;
  463. config.Logger.EnableFsAccessLog.Value = EnableFsAccessLog;
  464. config.System.FsGlobalAccessLogMode.Value = FsGlobalAccessLogMode;
  465. config.Logger.GraphicsDebugLevel.Value = (GraphicsDebugLevel)OpenglDebugLevel;
  466. config.Multiplayer.LanInterfaceId.Value = _networkInterfaces[NetworkInterfaceList[NetworkInterfaceIndex]];
  467. config.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  468. MainWindow.UpdateGraphicsConfig();
  469. SaveSettingsEvent?.Invoke();
  470. _directoryChanged = false;
  471. }
  472. public void RevertIfNotSaved()
  473. {
  474. Program.ReloadConfig();
  475. }
  476. public void ApplyButton()
  477. {
  478. SaveSettings();
  479. }
  480. public void OkButton()
  481. {
  482. SaveSettings();
  483. CloseWindow?.Invoke();
  484. }
  485. public void CancelButton()
  486. {
  487. RevertIfNotSaved();
  488. CloseWindow?.Invoke();
  489. }
  490. }
  491. }