SettingsViewModel.cs 16 KB

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