SettingsViewModel.cs 19 KB

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