SettingsViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Threading;
  4. using LibHac.Tools.FsSystem;
  5. using Ryujinx.Audio.Backends.OpenAL;
  6. using Ryujinx.Audio.Backends.SDL2;
  7. using Ryujinx.Audio.Backends.SoundIo;
  8. using Ryujinx.Ava.Common.Locale;
  9. using Ryujinx.Ava.Input;
  10. using Ryujinx.Ava.Ui.Controls;
  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.HLE.FileSystem;
  17. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  18. using Ryujinx.Input;
  19. using Ryujinx.Ui.Common.Configuration;
  20. using Ryujinx.Ui.Common.Configuration.System;
  21. using System;
  22. using System.Collections.Generic;
  23. using TimeZone = Ryujinx.Ava.Ui.Models.TimeZone;
  24. namespace Ryujinx.Ava.Ui.ViewModels
  25. {
  26. internal class SettingsViewModel : BaseModel
  27. {
  28. private readonly VirtualFileSystem _virtualFileSystem;
  29. private readonly ContentManager _contentManager;
  30. private readonly StyleableWindow _owner;
  31. private TimeZoneContentManager _timeZoneContentManager;
  32. private readonly List<string> _validTzRegions;
  33. private float _customResolutionScale;
  34. private int _resolutionScale;
  35. private int _graphicsBackendMultithreadingIndex;
  36. private float _previousVolumeLevel;
  37. private float _volume;
  38. public int ResolutionScale
  39. {
  40. get => _resolutionScale;
  41. set
  42. {
  43. _resolutionScale = value;
  44. OnPropertyChanged(nameof(CustomResolutionScale));
  45. OnPropertyChanged(nameof(IsCustomResolutionScaleActive));
  46. }
  47. }
  48. public int GraphicsBackendMultithreadingIndex
  49. {
  50. get => _graphicsBackendMultithreadingIndex;
  51. set
  52. {
  53. _graphicsBackendMultithreadingIndex = value;
  54. if (_owner != null)
  55. {
  56. if (_graphicsBackendMultithreadingIndex != (int)ConfigurationState.Instance.Graphics.BackendThreading.Value)
  57. {
  58. Dispatcher.UIThread.Post(async () =>
  59. {
  60. await ContentDialogHelper.CreateInfoDialog(_owner,
  61. LocaleManager.Instance["DialogSettingsBackendThreadingWarningMessage"],
  62. "",
  63. "",
  64. LocaleManager.Instance["InputDialogOk"],
  65. LocaleManager.Instance["DialogSettingsBackendThreadingWarningTitle"]);
  66. });
  67. }
  68. }
  69. OnPropertyChanged();
  70. }
  71. }
  72. public float CustomResolutionScale
  73. {
  74. get => _customResolutionScale;
  75. set
  76. {
  77. _customResolutionScale = MathF.Round(value, 1);
  78. OnPropertyChanged();
  79. }
  80. }
  81. public bool EnableDiscordIntegration { get; set; }
  82. public bool CheckUpdatesOnStart { get; set; }
  83. public bool ShowConfirmExit { get; set; }
  84. public bool HideCursorOnIdle { get; set; }
  85. public bool EnableDockedMode { get; set; }
  86. public bool EnableKeyboard { get; set; }
  87. public bool EnableMouse { get; set; }
  88. public bool EnableVsync { get; set; }
  89. public bool EnablePptc { get; set; }
  90. public bool EnableInternetAccess { get; set; }
  91. public bool EnableFsIntegrityChecks { get; set; }
  92. public bool IgnoreMissingServices { get; set; }
  93. public bool ExpandDramSize { get; set; }
  94. public bool EnableShaderCache { get; set; }
  95. public bool EnableFileLog { get; set; }
  96. public bool EnableStub { get; set; }
  97. public bool EnableInfo { get; set; }
  98. public bool EnableWarn { get; set; }
  99. public bool EnableError { get; set; }
  100. public bool EnableTrace { get; set; }
  101. public bool EnableGuest { get; set; }
  102. public bool EnableFsAccessLog { get; set; }
  103. public bool EnableDebug { get; set; }
  104. public bool IsOpenAlEnabled { get; set; }
  105. public bool IsSoundIoEnabled { get; set; }
  106. public bool IsSDL2Enabled { get; set; }
  107. public bool EnableCustomTheme { get; set; }
  108. public bool IsCustomResolutionScaleActive => _resolutionScale == 0;
  109. public string TimeZone { get; set; }
  110. public string ShaderDumpPath { get; set; }
  111. public string CustomThemePath { get; set; }
  112. public int Language { get; set; }
  113. public int Region { get; set; }
  114. public int FsGlobalAccessLogMode { get; set; }
  115. public int AudioBackend { get; set; }
  116. public int MaxAnisotropy { get; set; }
  117. public int AspectRatio { get; set; }
  118. public int OpenglDebugLevel { get; set; }
  119. public int MemoryMode { get; set; }
  120. public int BaseStyleIndex { get; set; }
  121. public float Volume
  122. {
  123. get => _volume;
  124. set
  125. {
  126. _volume = value;
  127. ConfigurationState.Instance.System.AudioVolume.Value = (float)(_volume / 100);
  128. OnPropertyChanged();
  129. }
  130. }
  131. public DateTimeOffset DateOffset { get; set; }
  132. public TimeSpan TimeOffset { get; set; }
  133. public AvaloniaList<TimeZone> TimeZones { get; set; }
  134. public AvaloniaList<string> GameDirectories { get; set; }
  135. private KeyboardHotkeys _keyboardHotkeys;
  136. public KeyboardHotkeys KeyboardHotkeys
  137. {
  138. get => _keyboardHotkeys;
  139. set
  140. {
  141. _keyboardHotkeys = value;
  142. OnPropertyChanged();
  143. }
  144. }
  145. public IGamepadDriver AvaloniaKeyboardDriver { get; }
  146. public SettingsViewModel(VirtualFileSystem virtualFileSystem, ContentManager contentManager, StyleableWindow owner) : this()
  147. {
  148. _virtualFileSystem = virtualFileSystem;
  149. _contentManager = contentManager;
  150. _owner = owner;
  151. if (Program.PreviewerDetached)
  152. {
  153. LoadTimeZones();
  154. AvaloniaKeyboardDriver = new AvaloniaKeyboardDriver(owner);
  155. }
  156. }
  157. public SettingsViewModel()
  158. {
  159. GameDirectories = new AvaloniaList<string>();
  160. TimeZones = new AvaloniaList<TimeZone>();
  161. _validTzRegions = new List<string>();
  162. CheckSoundBackends();
  163. if (Program.PreviewerDetached)
  164. {
  165. LoadCurrentConfiguration();
  166. }
  167. }
  168. public void CheckSoundBackends()
  169. {
  170. IsOpenAlEnabled = OpenALHardwareDeviceDriver.IsSupported;
  171. IsSoundIoEnabled = SoundIoHardwareDeviceDriver.IsSupported;
  172. IsSDL2Enabled = SDL2HardwareDeviceDriver.IsSupported;
  173. }
  174. public void LoadTimeZones()
  175. {
  176. _timeZoneContentManager = new TimeZoneContentManager();
  177. _timeZoneContentManager.InitializeInstance(_virtualFileSystem, _contentManager, IntegrityCheckLevel.None);
  178. foreach ((int offset, string location, string abbr) in _timeZoneContentManager.ParseTzOffsets())
  179. {
  180. int hours = Math.DivRem(offset, 3600, out int seconds);
  181. int minutes = Math.Abs(seconds) / 60;
  182. string abbr2 = abbr.StartsWith('+') || abbr.StartsWith('-') ? string.Empty : abbr;
  183. TimeZones.Add(new TimeZone($"UTC{hours:+0#;-0#;+00}:{minutes:D2}", location, abbr2));
  184. _validTzRegions.Add(location);
  185. }
  186. }
  187. public void ValidateAndSetTimeZone(string location)
  188. {
  189. if (_validTzRegions.Contains(location))
  190. {
  191. TimeZone = location;
  192. OnPropertyChanged(nameof(TimeZone));
  193. }
  194. }
  195. public async void BrowseTheme()
  196. {
  197. var dialog = new OpenFileDialog()
  198. {
  199. Title = LocaleManager.Instance["SettingsSelectThemeFileDialogTitle"],
  200. AllowMultiple = false
  201. };
  202. dialog.Filters.Add(new FileDialogFilter() { Extensions = { "xaml" }, Name = LocaleManager.Instance["SettingsXamlThemeFile"] });
  203. var file = await dialog.ShowAsync(_owner);
  204. if (file != null && file.Length > 0)
  205. {
  206. CustomThemePath = file[0];
  207. OnPropertyChanged(nameof(CustomThemePath));
  208. }
  209. }
  210. public void LoadCurrentConfiguration()
  211. {
  212. ConfigurationState config = ConfigurationState.Instance;
  213. GameDirectories.Clear();
  214. GameDirectories.AddRange(config.Ui.GameDirs.Value);
  215. EnableDiscordIntegration = config.EnableDiscordIntegration;
  216. CheckUpdatesOnStart = config.CheckUpdatesOnStart;
  217. ShowConfirmExit = config.ShowConfirmExit;
  218. HideCursorOnIdle = config.HideCursorOnIdle;
  219. EnableDockedMode = config.System.EnableDockedMode;
  220. EnableKeyboard = config.Hid.EnableKeyboard;
  221. EnableMouse = config.Hid.EnableMouse;
  222. EnableVsync = config.Graphics.EnableVsync;
  223. EnablePptc = config.System.EnablePtc;
  224. EnableInternetAccess = config.System.EnableInternetAccess;
  225. EnableFsIntegrityChecks = config.System.EnableFsIntegrityChecks;
  226. IgnoreMissingServices = config.System.IgnoreMissingServices;
  227. ExpandDramSize = config.System.ExpandRam;
  228. EnableShaderCache = config.Graphics.EnableShaderCache;
  229. EnableFileLog = config.Logger.EnableFileLog;
  230. EnableStub = config.Logger.EnableStub;
  231. EnableInfo = config.Logger.EnableInfo;
  232. EnableWarn = config.Logger.EnableWarn;
  233. EnableError = config.Logger.EnableError;
  234. EnableTrace = config.Logger.EnableTrace;
  235. EnableGuest = config.Logger.EnableGuest;
  236. EnableDebug = config.Logger.EnableDebug;
  237. EnableFsAccessLog = config.Logger.EnableFsAccessLog;
  238. EnableCustomTheme = config.Ui.EnableCustomTheme;
  239. Volume = config.System.AudioVolume * 100;
  240. GraphicsBackendMultithreadingIndex = (int)config.Graphics.BackendThreading.Value;
  241. OpenglDebugLevel = (int)config.Logger.GraphicsDebugLevel.Value;
  242. TimeZone = config.System.TimeZone;
  243. ShaderDumpPath = config.Graphics.ShadersDumpPath;
  244. CustomThemePath = config.Ui.CustomThemePath;
  245. BaseStyleIndex = config.Ui.BaseStyle == "Light" ? 0 : 1;
  246. Language = (int)config.System.Language.Value;
  247. Region = (int)config.System.Region.Value;
  248. FsGlobalAccessLogMode = config.System.FsGlobalAccessLogMode;
  249. AudioBackend = (int)config.System.AudioBackend.Value;
  250. MemoryMode = (int)config.System.MemoryManagerMode.Value;
  251. float anisotropy = config.Graphics.MaxAnisotropy;
  252. MaxAnisotropy = anisotropy == -1 ? 0 : (int)(MathF.Log2(anisotropy));
  253. AspectRatio = (int)config.Graphics.AspectRatio.Value;
  254. int resolution = config.Graphics.ResScale;
  255. ResolutionScale = resolution == -1 ? 0 : resolution;
  256. CustomResolutionScale = config.Graphics.ResScaleCustom;
  257. DateTime dateTimeOffset = DateTime.Now.AddSeconds(config.System.SystemTimeOffset);
  258. DateOffset = dateTimeOffset.Date;
  259. TimeOffset = dateTimeOffset.TimeOfDay;
  260. KeyboardHotkeys = config.Hid.Hotkeys.Value;
  261. _previousVolumeLevel = Volume;
  262. }
  263. public void SaveSettings()
  264. {
  265. List<string> gameDirs = new List<string>(GameDirectories);
  266. ConfigurationState config = ConfigurationState.Instance;
  267. if (_validTzRegions.Contains(TimeZone))
  268. {
  269. config.System.TimeZone.Value = TimeZone;
  270. }
  271. config.Logger.EnableError.Value = EnableError;
  272. config.Logger.EnableTrace.Value = EnableTrace;
  273. config.Logger.EnableWarn.Value = EnableWarn;
  274. config.Logger.EnableInfo.Value = EnableInfo;
  275. config.Logger.EnableStub.Value = EnableStub;
  276. config.Logger.EnableDebug.Value = EnableDebug;
  277. config.Logger.EnableGuest.Value = EnableGuest;
  278. config.Logger.EnableFsAccessLog.Value = EnableFsAccessLog;
  279. config.Logger.EnableFileLog.Value = EnableFileLog;
  280. config.Logger.GraphicsDebugLevel.Value = (GraphicsDebugLevel)OpenglDebugLevel;
  281. config.System.EnableDockedMode.Value = EnableDockedMode;
  282. config.EnableDiscordIntegration.Value = EnableDiscordIntegration;
  283. config.CheckUpdatesOnStart.Value = CheckUpdatesOnStart;
  284. config.ShowConfirmExit.Value = ShowConfirmExit;
  285. config.HideCursorOnIdle.Value = HideCursorOnIdle;
  286. config.Graphics.EnableVsync.Value = EnableVsync;
  287. config.Graphics.EnableShaderCache.Value = EnableShaderCache;
  288. config.System.EnablePtc.Value = EnablePptc;
  289. config.System.EnableInternetAccess.Value = EnableInternetAccess;
  290. config.System.EnableFsIntegrityChecks.Value = EnableFsIntegrityChecks;
  291. config.System.IgnoreMissingServices.Value = IgnoreMissingServices;
  292. config.System.ExpandRam.Value = ExpandDramSize;
  293. config.Hid.EnableKeyboard.Value = EnableKeyboard;
  294. config.Hid.EnableMouse.Value = EnableMouse;
  295. config.Ui.CustomThemePath.Value = CustomThemePath;
  296. config.Ui.EnableCustomTheme.Value = EnableCustomTheme;
  297. config.Ui.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark";
  298. config.System.Language.Value = (Language)Language;
  299. config.System.Region.Value = (Region)Region;
  300. if (ConfigurationState.Instance.Graphics.BackendThreading != (BackendThreading)GraphicsBackendMultithreadingIndex)
  301. {
  302. DriverUtilities.ToggleOGLThreading(GraphicsBackendMultithreadingIndex == (int)BackendThreading.Off);
  303. }
  304. config.Graphics.BackendThreading.Value = (BackendThreading)GraphicsBackendMultithreadingIndex;
  305. TimeSpan systemTimeOffset = DateOffset - DateTime.Now;
  306. config.System.SystemTimeOffset.Value = systemTimeOffset.Seconds;
  307. config.Graphics.ShadersDumpPath.Value = ShaderDumpPath;
  308. config.Ui.GameDirs.Value = gameDirs;
  309. config.System.FsGlobalAccessLogMode.Value = FsGlobalAccessLogMode;
  310. config.System.MemoryManagerMode.Value = (MemoryManagerMode)MemoryMode;
  311. float anisotropy = MaxAnisotropy == 0 ? -1 : MathF.Pow(2, MaxAnisotropy);
  312. config.Graphics.MaxAnisotropy.Value = anisotropy;
  313. config.Graphics.AspectRatio.Value = (AspectRatio)AspectRatio;
  314. config.Graphics.ResScale.Value = ResolutionScale == 0 ? -1 : ResolutionScale;
  315. config.Graphics.ResScaleCustom.Value = CustomResolutionScale;
  316. config.System.AudioVolume.Value = Volume / 100;
  317. AudioBackend audioBackend = (AudioBackend)AudioBackend;
  318. if (audioBackend != config.System.AudioBackend.Value)
  319. {
  320. config.System.AudioBackend.Value = audioBackend;
  321. Logger.Info?.Print(LogClass.Application, $"AudioBackend toggled to: {audioBackend}");
  322. }
  323. config.Hid.Hotkeys.Value = KeyboardHotkeys;
  324. config.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  325. MainWindow.UpdateGraphicsConfig();
  326. _previousVolumeLevel = Volume;
  327. }
  328. public void RevertIfNotSaved()
  329. {
  330. Program.ReloadConfig();
  331. }
  332. }
  333. }