Configuration.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using JsonPrettyPrinterPlus;
  2. using LibHac.Fs;
  3. using OpenTK.Input;
  4. using Ryujinx.Common;
  5. using Ryujinx.Common.Logging;
  6. using Ryujinx.HLE;
  7. using Ryujinx.HLE.HOS.SystemState;
  8. using Ryujinx.HLE.HOS.Services;
  9. using Ryujinx.HLE.Input;
  10. using Ryujinx.UI;
  11. using Ryujinx.UI.Input;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using Utf8Json;
  18. using Utf8Json.Resolvers;
  19. namespace Ryujinx
  20. {
  21. public class Configuration
  22. {
  23. /// <summary>
  24. /// The default configuration instance
  25. /// </summary>
  26. public static Configuration Instance { get; private set; }
  27. /// <summary>
  28. /// Dumps shaders in this local directory
  29. /// </summary>
  30. public string GraphicsShadersDumpPath { get; set; }
  31. /// <summary>
  32. /// Enables printing debug log messages
  33. /// </summary>
  34. public bool LoggingEnableDebug { get; set; }
  35. /// <summary>
  36. /// Enables printing stub log messages
  37. /// </summary>
  38. public bool LoggingEnableStub { get; set; }
  39. /// <summary>
  40. /// Enables printing info log messages
  41. /// </summary>
  42. public bool LoggingEnableInfo { get; set; }
  43. /// <summary>
  44. /// Enables printing warning log messages
  45. /// </summary>
  46. public bool LoggingEnableWarn { get; set; }
  47. /// <summary>
  48. /// Enables printing error log messages
  49. /// </summary>
  50. public bool LoggingEnableError { get; set; }
  51. /// <summary>
  52. /// Enables printing guest log messages
  53. /// </summary>
  54. public bool LoggingEnableGuest { get; set; }
  55. /// <summary>
  56. /// Enables printing FS access log messages
  57. /// </summary>
  58. public bool LoggingEnableFsAccessLog { get; set; }
  59. /// <summary>
  60. /// Controls which log messages are written to the log targets
  61. /// </summary>
  62. public LogClass[] LoggingFilteredClasses { get; set; }
  63. /// <summary>
  64. /// Enables or disables logging to a file on disk
  65. /// </summary>
  66. public bool EnableFileLog { get; set; }
  67. /// <summary>
  68. /// Change System Language
  69. /// </summary>
  70. public SystemLanguage SystemLanguage { get; set; }
  71. /// <summary>
  72. /// Enables or disables Docked Mode
  73. /// </summary>
  74. public bool DockedMode { get; set; }
  75. /// <summary>
  76. /// Enables or disables Discord Rich Presence
  77. /// </summary>
  78. public bool EnableDiscordIntegration { get; set; }
  79. /// <summary>
  80. /// Enables or disables Vertical Sync
  81. /// </summary>
  82. public bool EnableVsync { get; set; }
  83. /// <summary>
  84. /// Enables or disables multi-core scheduling of threads
  85. /// </summary>
  86. public bool EnableMulticoreScheduling { get; set; }
  87. /// <summary>
  88. /// Enables integrity checks on Game content files
  89. /// </summary>
  90. public bool EnableFsIntegrityChecks { get; set; }
  91. /// <summary>
  92. /// Enables FS access log output to the console. Possible modes are 0-3
  93. /// </summary>
  94. public int FsGlobalAccessLogMode { get; set; }
  95. /// <summary>
  96. /// Use old ChocolArm64 ARM emulator
  97. /// </summary>
  98. public bool EnableLegacyJit { get; set; }
  99. /// <summary>
  100. /// Enable or disable ignoring missing services
  101. /// </summary>
  102. public bool IgnoreMissingServices { get; set; }
  103. /// <summary>
  104. /// The primary controller's type
  105. /// </summary>
  106. public ControllerStatus ControllerType { get; set; }
  107. /// <summary>
  108. /// Used to toggle columns in the GUI
  109. /// </summary>
  110. public List<bool> GuiColumns { get; set; }
  111. /// <summary>
  112. /// A list of directories containing games to be used to load games into the games list
  113. /// </summary>
  114. public List<string> GameDirs { get; set; }
  115. /// <summary>
  116. /// Enable or disable custom themes in the GUI
  117. /// </summary>
  118. public bool EnableCustomTheme { get; set; }
  119. /// <summary>
  120. /// Path to custom GUI theme
  121. /// </summary>
  122. public string CustomThemePath { get; set; }
  123. /// <summary>
  124. /// Enable or disable keyboard support (Independent from controllers binding)
  125. /// </summary>
  126. public bool EnableKeyboard { get; set; }
  127. /// <summary>
  128. /// Keyboard control bindings
  129. /// </summary>
  130. public NpadKeyboard KeyboardControls { get; set; }
  131. /// <summary>
  132. /// Controller control bindings
  133. /// </summary>
  134. public UI.Input.NpadController JoystickControls { get; private set; }
  135. /// <summary>
  136. /// Loads a configuration file from disk
  137. /// </summary>
  138. /// <param name="path">The path to the JSON configuration file</param>
  139. public static void Load(string path)
  140. {
  141. var resolver = CompositeResolver.Create(
  142. new[] { new ConfigurationEnumFormatter<Key>() },
  143. new[] { StandardResolver.AllowPrivateSnakeCase }
  144. );
  145. using (Stream stream = File.OpenRead(path))
  146. {
  147. Instance = JsonSerializer.Deserialize<Configuration>(stream, resolver);
  148. }
  149. }
  150. /// <summary>
  151. /// Loads a configuration file asynchronously from disk
  152. /// </summary>
  153. /// <param name="path">The path to the JSON configuration file</param>
  154. public static async Task LoadAsync(string path)
  155. {
  156. IJsonFormatterResolver resolver = CompositeResolver.Create(
  157. new[] { new ConfigurationEnumFormatter<Key>() },
  158. new[] { StandardResolver.AllowPrivateSnakeCase }
  159. );
  160. using (Stream stream = File.OpenRead(path))
  161. {
  162. Instance = await JsonSerializer.DeserializeAsync<Configuration>(stream, resolver);
  163. }
  164. }
  165. /// <summary>
  166. /// Save a configuration file to disk
  167. /// </summary>
  168. /// <param name="path">The path to the JSON configuration file</param>
  169. public static void SaveConfig(Configuration config, string path)
  170. {
  171. IJsonFormatterResolver resolver = CompositeResolver.Create(
  172. new[] { new ConfigurationEnumFormatter<Key>() },
  173. new[] { StandardResolver.AllowPrivateSnakeCase }
  174. );
  175. byte[] data = JsonSerializer.Serialize(config, resolver);
  176. File.WriteAllText(path, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
  177. }
  178. /// <summary>
  179. /// Configures a <see cref="Switch"/> instance
  180. /// </summary>
  181. /// <param name="device">The instance to configure</param>
  182. public static void InitialConfigure(Switch device)
  183. {
  184. if (Instance == null)
  185. {
  186. throw new InvalidOperationException("Configuration has not been loaded yet.");
  187. }
  188. SwitchSettings.ConfigureSettings(Instance);
  189. Logger.AddTarget(new AsyncLogTargetWrapper(
  190. new ConsoleLogTarget(),
  191. 1000,
  192. AsyncLogTargetOverflowAction.Block
  193. ));
  194. if (Instance.EnableFileLog)
  195. {
  196. Logger.AddTarget(new AsyncLogTargetWrapper(
  197. new FileLogTarget(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx.log")),
  198. 1000,
  199. AsyncLogTargetOverflowAction.Block
  200. ));
  201. }
  202. Configure(device, Instance);
  203. }
  204. public static void Configure(Switch device, Configuration SwitchConfig)
  205. {
  206. GraphicsConfig.ShadersDumpPath = SwitchConfig.GraphicsShadersDumpPath;
  207. Logger.SetEnable(LogLevel.Debug, SwitchConfig.LoggingEnableDebug );
  208. Logger.SetEnable(LogLevel.Stub, SwitchConfig.LoggingEnableStub );
  209. Logger.SetEnable(LogLevel.Info, SwitchConfig.LoggingEnableInfo );
  210. Logger.SetEnable(LogLevel.Warning, SwitchConfig.LoggingEnableWarn );
  211. Logger.SetEnable(LogLevel.Error, SwitchConfig.LoggingEnableError );
  212. Logger.SetEnable(LogLevel.Guest, SwitchConfig.LoggingEnableGuest );
  213. Logger.SetEnable(LogLevel.AccessLog, SwitchConfig.LoggingEnableFsAccessLog);
  214. if (SwitchConfig.LoggingFilteredClasses.Length > 0)
  215. {
  216. foreach (var logClass in EnumExtensions.GetValues<LogClass>())
  217. {
  218. Logger.SetEnable(logClass, false);
  219. }
  220. foreach (var logClass in SwitchConfig.LoggingFilteredClasses)
  221. {
  222. Logger.SetEnable(logClass, true);
  223. }
  224. }
  225. MainWindow.DiscordIntegrationEnabled = SwitchConfig.EnableDiscordIntegration;
  226. device.EnableDeviceVsync = SwitchConfig.EnableVsync;
  227. device.System.State.DockedMode = SwitchConfig.DockedMode;
  228. device.System.State.SetLanguage(SwitchConfig.SystemLanguage);
  229. if (SwitchConfig.EnableMulticoreScheduling)
  230. {
  231. device.System.EnableMultiCoreScheduling();
  232. }
  233. device.System.FsIntegrityCheckLevel = SwitchConfig.EnableFsIntegrityChecks
  234. ? IntegrityCheckLevel.ErrorOnInvalid
  235. : IntegrityCheckLevel.None;
  236. device.System.GlobalAccessLogMode = SwitchConfig.FsGlobalAccessLogMode;
  237. device.System.UseLegacyJit = SwitchConfig.EnableLegacyJit;
  238. ServiceConfiguration.IgnoreMissingServices = SwitchConfig.IgnoreMissingServices;
  239. }
  240. public static void ConfigureHid(Switch device, Configuration SwitchConfig)
  241. {
  242. if (SwitchConfig.JoystickControls.Enabled)
  243. {
  244. if (!Joystick.GetState(SwitchConfig.JoystickControls.Index).IsConnected)
  245. {
  246. SwitchConfig.JoystickControls.SetEnabled(false);
  247. }
  248. }
  249. device.Hid.InitializePrimaryController(SwitchConfig.ControllerType);
  250. device.Hid.InitializeKeyboard();
  251. }
  252. private class ConfigurationEnumFormatter<T> : IJsonFormatter<T>
  253. where T : struct
  254. {
  255. public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
  256. {
  257. formatterResolver.GetFormatterWithVerify<string>()
  258. .Serialize(ref writer, value.ToString(), formatterResolver);
  259. }
  260. public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
  261. {
  262. if (reader.ReadIsNull())
  263. {
  264. return default(T);
  265. }
  266. string enumName = formatterResolver.GetFormatterWithVerify<string>()
  267. .Deserialize(ref reader, formatterResolver);
  268. if (Enum.TryParse<T>(enumName, out T result))
  269. {
  270. return result;
  271. }
  272. return default(T);
  273. }
  274. }
  275. }
  276. }