SettingsViewModel.cs 20 KB

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