SettingsViewModel.cs 19 KB

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