SettingsViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 TimeZone = Ryujinx.Ava.UI.Models.TimeZone;
  26. namespace Ryujinx.Ava.UI.ViewModels
  27. {
  28. public class SettingsViewModel : BaseModel
  29. {
  30. private readonly VirtualFileSystem _virtualFileSystem;
  31. private readonly ContentManager _contentManager;
  32. private TimeZoneContentManager _timeZoneContentManager;
  33. private readonly List<string> _validTzRegions;
  34. private float _customResolutionScale;
  35. private int _resolutionScale;
  36. private int _graphicsBackendMultithreadingIndex;
  37. private float _volume;
  38. private bool _isVulkanAvailable = true;
  39. private bool _directoryChanged;
  40. private List<string> _gpuIds = new();
  41. private KeyboardHotkeys _keyboardHotkeys;
  42. private int _graphicsBackendIndex;
  43. private string _customThemePath;
  44. public event Action CloseWindow;
  45. public event Action SaveSettingsEvent;
  46. public int ResolutionScale
  47. {
  48. get => _resolutionScale;
  49. set
  50. {
  51. _resolutionScale = value;
  52. OnPropertyChanged(nameof(CustomResolutionScale));
  53. OnPropertyChanged(nameof(IsCustomResolutionScaleActive));
  54. }
  55. }
  56. public int GraphicsBackendMultithreadingIndex
  57. {
  58. get => _graphicsBackendMultithreadingIndex;
  59. set
  60. {
  61. _graphicsBackendMultithreadingIndex = value;
  62. if (_graphicsBackendMultithreadingIndex != (int)ConfigurationState.Instance.Graphics.BackendThreading.Value)
  63. {
  64. Dispatcher.UIThread.Post(async () =>
  65. {
  66. await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogSettingsBackendThreadingWarningMessage],
  67. "",
  68. "",
  69. LocaleManager.Instance[LocaleKeys.InputDialogOk],
  70. LocaleManager.Instance[LocaleKeys.DialogSettingsBackendThreadingWarningTitle]);
  71. });
  72. }
  73. OnPropertyChanged();
  74. }
  75. }
  76. public float CustomResolutionScale
  77. {
  78. get => _customResolutionScale;
  79. set
  80. {
  81. _customResolutionScale = MathF.Round(value, 1);
  82. OnPropertyChanged();
  83. }
  84. }
  85. public bool IsVulkanAvailable
  86. {
  87. get => _isVulkanAvailable;
  88. set
  89. {
  90. _isVulkanAvailable = value;
  91. OnPropertyChanged();
  92. }
  93. }
  94. public bool IsOpenGLAvailable => !OperatingSystem.IsMacOS();
  95. public bool DirectoryChanged
  96. {
  97. get => _directoryChanged;
  98. set
  99. {
  100. _directoryChanged = value;
  101. OnPropertyChanged();
  102. }
  103. }
  104. public bool IsMacOS
  105. {
  106. get => OperatingSystem.IsMacOS();
  107. }
  108. public bool EnableDiscordIntegration { get; set; }
  109. public bool CheckUpdatesOnStart { get; set; }
  110. public bool ShowConfirmExit { get; set; }
  111. public bool HideCursorOnIdle { get; set; }
  112. public bool EnableDockedMode { get; set; }
  113. public bool EnableKeyboard { get; set; }
  114. public bool EnableMouse { get; set; }
  115. public bool EnableVsync { get; set; }
  116. public bool EnablePptc { get; set; }
  117. public bool EnableInternetAccess { get; set; }
  118. public bool EnableFsIntegrityChecks { get; set; }
  119. public bool IgnoreMissingServices { get; set; }
  120. public bool ExpandDramSize { get; set; }
  121. public bool EnableShaderCache { get; set; }
  122. public bool EnableTextureRecompression { get; set; }
  123. public bool EnableMacroHLE { get; set; }
  124. public bool EnableFileLog { get; set; }
  125. public bool EnableStub { get; set; }
  126. public bool EnableInfo { get; set; }
  127. public bool EnableWarn { get; set; }
  128. public bool EnableError { get; set; }
  129. public bool EnableTrace { get; set; }
  130. public bool EnableGuest { get; set; }
  131. public bool EnableFsAccessLog { get; set; }
  132. public bool EnableDebug { get; set; }
  133. public bool IsOpenAlEnabled { get; set; }
  134. public bool IsSoundIoEnabled { get; set; }
  135. public bool IsSDL2Enabled { get; set; }
  136. public bool EnableCustomTheme { get; set; }
  137. public bool IsCustomResolutionScaleActive => _resolutionScale == 4;
  138. public bool IsVulkanSelected => GraphicsBackendIndex == 0;
  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. // Graphics
  300. GraphicsBackendIndex = (int)config.Graphics.GraphicsBackend.Value;
  301. PreferredGpuIndex = _gpuIds.Contains(config.Graphics.PreferredGpu) ? _gpuIds.IndexOf(config.Graphics.PreferredGpu) : 0;
  302. EnableShaderCache = config.Graphics.EnableShaderCache;
  303. EnableTextureRecompression = config.Graphics.EnableTextureRecompression;
  304. EnableMacroHLE = config.Graphics.EnableMacroHLE;
  305. ResolutionScale = config.Graphics.ResScale == -1 ? 4 : config.Graphics.ResScale - 1;
  306. CustomResolutionScale = config.Graphics.ResScaleCustom;
  307. MaxAnisotropy = config.Graphics.MaxAnisotropy == -1 ? 0 : (int)(MathF.Log2(config.Graphics.MaxAnisotropy));
  308. AspectRatio = (int)config.Graphics.AspectRatio.Value;
  309. GraphicsBackendMultithreadingIndex = (int)config.Graphics.BackendThreading.Value;
  310. ShaderDumpPath = config.Graphics.ShadersDumpPath;
  311. // Audio
  312. AudioBackend = (int)config.System.AudioBackend.Value;
  313. Volume = config.System.AudioVolume * 100;
  314. // Network
  315. EnableInternetAccess = config.System.EnableInternetAccess;
  316. // Logging
  317. EnableFileLog = config.Logger.EnableFileLog;
  318. EnableStub = config.Logger.EnableStub;
  319. EnableInfo = config.Logger.EnableInfo;
  320. EnableWarn = config.Logger.EnableWarn;
  321. EnableError = config.Logger.EnableError;
  322. EnableTrace = config.Logger.EnableTrace;
  323. EnableGuest = config.Logger.EnableGuest;
  324. EnableDebug = config.Logger.EnableDebug;
  325. EnableFsAccessLog = config.Logger.EnableFsAccessLog;
  326. FsGlobalAccessLogMode = config.System.FsGlobalAccessLogMode;
  327. OpenglDebugLevel = (int)config.Logger.GraphicsDebugLevel.Value;
  328. }
  329. public void SaveSettings()
  330. {
  331. ConfigurationState config = ConfigurationState.Instance;
  332. // User Interface
  333. config.EnableDiscordIntegration.Value = EnableDiscordIntegration;
  334. config.CheckUpdatesOnStart.Value = CheckUpdatesOnStart;
  335. config.ShowConfirmExit.Value = ShowConfirmExit;
  336. config.HideCursorOnIdle.Value = HideCursorOnIdle;
  337. if (_directoryChanged)
  338. {
  339. List<string> gameDirs = new(GameDirectories);
  340. config.Ui.GameDirs.Value = gameDirs;
  341. }
  342. config.Ui.EnableCustomTheme.Value = EnableCustomTheme;
  343. config.Ui.CustomThemePath.Value = CustomThemePath;
  344. config.Ui.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark";
  345. // Input
  346. config.System.EnableDockedMode.Value = EnableDockedMode;
  347. config.Hid.EnableKeyboard.Value = EnableKeyboard;
  348. config.Hid.EnableMouse.Value = EnableMouse;
  349. // Keyboard Hotkeys
  350. config.Hid.Hotkeys.Value = KeyboardHotkeys;
  351. // System
  352. config.System.Region.Value = (Region)Region;
  353. config.System.Language.Value = (Language)Language;
  354. if (_validTzRegions.Contains(TimeZone))
  355. {
  356. config.System.TimeZone.Value = TimeZone;
  357. }
  358. TimeSpan systemTimeOffset = DateOffset - DateTime.Now;
  359. config.System.SystemTimeOffset.Value = systemTimeOffset.Seconds;
  360. config.Graphics.EnableVsync.Value = EnableVsync;
  361. config.System.EnableFsIntegrityChecks.Value = EnableFsIntegrityChecks;
  362. config.System.ExpandRam.Value = ExpandDramSize;
  363. config.System.IgnoreMissingServices.Value = IgnoreMissingServices;
  364. // CPU
  365. config.System.EnablePtc.Value = EnablePptc;
  366. config.System.MemoryManagerMode.Value = (MemoryManagerMode)MemoryMode;
  367. // Graphics
  368. config.Graphics.GraphicsBackend.Value = (GraphicsBackend)GraphicsBackendIndex;
  369. config.Graphics.PreferredGpu.Value = _gpuIds.ElementAtOrDefault(PreferredGpuIndex);
  370. config.Graphics.EnableShaderCache.Value = EnableShaderCache;
  371. config.Graphics.EnableTextureRecompression.Value = EnableTextureRecompression;
  372. config.Graphics.EnableMacroHLE.Value = EnableMacroHLE;
  373. config.Graphics.ResScale.Value = ResolutionScale == 4 ? -1 : ResolutionScale + 1;
  374. config.Graphics.ResScaleCustom.Value = CustomResolutionScale;
  375. config.Graphics.MaxAnisotropy.Value = MaxAnisotropy == 0 ? -1 : MathF.Pow(2, MaxAnisotropy);
  376. config.Graphics.AspectRatio.Value = (AspectRatio)AspectRatio;
  377. if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex)
  378. {
  379. DriverUtilities.ToggleOGLThreading(GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off);
  380. }
  381. config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;
  382. config.Graphics.ShadersDumpPath.Value = ShaderDumpPath;
  383. // Audio
  384. AudioBackend audioBackend = (AudioBackend)AudioBackend;
  385. if (audioBackend != config.System.AudioBackend.Value)
  386. {
  387. config.System.AudioBackend.Value = audioBackend;
  388. Logger.Info?.Print(LogClass.Application, $"AudioBackend toggled to: {audioBackend}");
  389. }
  390. config.System.AudioVolume.Value = Volume / 100;
  391. // Network
  392. config.System.EnableInternetAccess.Value = EnableInternetAccess;
  393. // Logging
  394. config.Logger.EnableFileLog.Value = EnableFileLog;
  395. config.Logger.EnableStub.Value = EnableStub;
  396. config.Logger.EnableInfo.Value = EnableInfo;
  397. config.Logger.EnableWarn.Value = EnableWarn;
  398. config.Logger.EnableError.Value = EnableError;
  399. config.Logger.EnableTrace.Value = EnableTrace;
  400. config.Logger.EnableGuest.Value = EnableGuest;
  401. config.Logger.EnableDebug.Value = EnableDebug;
  402. config.Logger.EnableFsAccessLog.Value = EnableFsAccessLog;
  403. config.System.FsGlobalAccessLogMode.Value = FsGlobalAccessLogMode;
  404. config.Logger.GraphicsDebugLevel.Value = (GraphicsDebugLevel)OpenglDebugLevel;
  405. config.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  406. MainWindow.UpdateGraphicsConfig();
  407. SaveSettingsEvent?.Invoke();
  408. _directoryChanged = false;
  409. }
  410. public void RevertIfNotSaved()
  411. {
  412. Program.ReloadConfig();
  413. }
  414. public void ApplyButton()
  415. {
  416. SaveSettings();
  417. }
  418. public void OkButton()
  419. {
  420. SaveSettings();
  421. CloseWindow?.Invoke();
  422. }
  423. public void CancelButton()
  424. {
  425. RevertIfNotSaved();
  426. CloseWindow?.Invoke();
  427. }
  428. }
  429. }