SettingsViewModel.cs 18 KB

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