SettingsViewModel.cs 20 KB

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