Configuration.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using JsonPrettyPrinterPlus;
  2. using LibHac.FsSystem;
  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. /// Enable or disable ignoring missing services
  97. /// </summary>
  98. public bool IgnoreMissingServices { get; set; }
  99. /// <summary>
  100. /// The primary controller's type
  101. /// </summary>
  102. public ControllerStatus ControllerType { get; set; }
  103. /// <summary>
  104. /// Used to toggle columns in the GUI
  105. /// </summary>
  106. public List<bool> GuiColumns { get; set; }
  107. /// <summary>
  108. /// A list of directories containing games to be used to load games into the games list
  109. /// </summary>
  110. public List<string> GameDirs { get; set; }
  111. /// <summary>
  112. /// Enable or disable custom themes in the GUI
  113. /// </summary>
  114. public bool EnableCustomTheme { get; set; }
  115. /// <summary>
  116. /// Path to custom GUI theme
  117. /// </summary>
  118. public string CustomThemePath { get; set; }
  119. /// <summary>
  120. /// Enable or disable keyboard support (Independent from controllers binding)
  121. /// </summary>
  122. public bool EnableKeyboard { get; set; }
  123. /// <summary>
  124. /// Keyboard control bindings
  125. /// </summary>
  126. public NpadKeyboard KeyboardControls { get; set; }
  127. /// <summary>
  128. /// Controller control bindings
  129. /// </summary>
  130. public UI.Input.NpadController JoystickControls { get; private set; }
  131. /// <summary>
  132. /// Loads a configuration file from disk
  133. /// </summary>
  134. /// <param name="path">The path to the JSON configuration file</param>
  135. public static void Load(string path)
  136. {
  137. var resolver = CompositeResolver.Create(
  138. new[] { new ConfigurationEnumFormatter<Key>() },
  139. new[] { StandardResolver.AllowPrivateSnakeCase }
  140. );
  141. using (Stream stream = File.OpenRead(path))
  142. {
  143. Instance = JsonSerializer.Deserialize<Configuration>(stream, resolver);
  144. }
  145. }
  146. /// <summary>
  147. /// Loads a configuration file asynchronously from disk
  148. /// </summary>
  149. /// <param name="path">The path to the JSON configuration file</param>
  150. public static async Task LoadAsync(string path)
  151. {
  152. IJsonFormatterResolver resolver = CompositeResolver.Create(
  153. new[] { new ConfigurationEnumFormatter<Key>() },
  154. new[] { StandardResolver.AllowPrivateSnakeCase }
  155. );
  156. using (Stream stream = File.OpenRead(path))
  157. {
  158. Instance = await JsonSerializer.DeserializeAsync<Configuration>(stream, resolver);
  159. }
  160. }
  161. /// <summary>
  162. /// Save a configuration file to disk
  163. /// </summary>
  164. /// <param name="path">The path to the JSON configuration file</param>
  165. public static void SaveConfig(Configuration config, string path)
  166. {
  167. IJsonFormatterResolver resolver = CompositeResolver.Create(
  168. new[] { new ConfigurationEnumFormatter<Key>() },
  169. new[] { StandardResolver.AllowPrivateSnakeCase }
  170. );
  171. byte[] data = JsonSerializer.Serialize(config, resolver);
  172. File.WriteAllText(path, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
  173. }
  174. /// <summary>
  175. /// Configures a <see cref="Switch"/> instance
  176. /// </summary>
  177. /// <param name="device">The instance to configure</param>
  178. public static void InitialConfigure(Switch device)
  179. {
  180. if (Instance == null)
  181. {
  182. throw new InvalidOperationException("Configuration has not been loaded yet.");
  183. }
  184. SwitchSettings.ConfigureSettings(Instance);
  185. Logger.AddTarget(new AsyncLogTargetWrapper(
  186. new ConsoleLogTarget(),
  187. 1000,
  188. AsyncLogTargetOverflowAction.Block
  189. ));
  190. if (Instance.EnableFileLog)
  191. {
  192. Logger.AddTarget(new AsyncLogTargetWrapper(
  193. new FileLogTarget(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx.log")),
  194. 1000,
  195. AsyncLogTargetOverflowAction.Block
  196. ));
  197. }
  198. Configure(device, Instance);
  199. }
  200. public static void Configure(Switch device, Configuration SwitchConfig)
  201. {
  202. GraphicsConfig.ShadersDumpPath = SwitchConfig.GraphicsShadersDumpPath;
  203. Logger.SetEnable(LogLevel.Debug, SwitchConfig.LoggingEnableDebug );
  204. Logger.SetEnable(LogLevel.Stub, SwitchConfig.LoggingEnableStub );
  205. Logger.SetEnable(LogLevel.Info, SwitchConfig.LoggingEnableInfo );
  206. Logger.SetEnable(LogLevel.Warning, SwitchConfig.LoggingEnableWarn );
  207. Logger.SetEnable(LogLevel.Error, SwitchConfig.LoggingEnableError );
  208. Logger.SetEnable(LogLevel.Guest, SwitchConfig.LoggingEnableGuest );
  209. Logger.SetEnable(LogLevel.AccessLog, SwitchConfig.LoggingEnableFsAccessLog);
  210. if (SwitchConfig.LoggingFilteredClasses.Length > 0)
  211. {
  212. foreach (var logClass in EnumExtensions.GetValues<LogClass>())
  213. {
  214. Logger.SetEnable(logClass, false);
  215. }
  216. foreach (var logClass in SwitchConfig.LoggingFilteredClasses)
  217. {
  218. Logger.SetEnable(logClass, true);
  219. }
  220. }
  221. MainWindow.DiscordIntegrationEnabled = SwitchConfig.EnableDiscordIntegration;
  222. device.EnableDeviceVsync = SwitchConfig.EnableVsync;
  223. device.System.State.DockedMode = SwitchConfig.DockedMode;
  224. device.System.State.SetLanguage(SwitchConfig.SystemLanguage);
  225. if (SwitchConfig.EnableMulticoreScheduling)
  226. {
  227. device.System.EnableMultiCoreScheduling();
  228. }
  229. device.System.FsIntegrityCheckLevel = SwitchConfig.EnableFsIntegrityChecks
  230. ? IntegrityCheckLevel.ErrorOnInvalid
  231. : IntegrityCheckLevel.None;
  232. device.System.GlobalAccessLogMode = SwitchConfig.FsGlobalAccessLogMode;
  233. ServiceConfiguration.IgnoreMissingServices = SwitchConfig.IgnoreMissingServices;
  234. }
  235. public static void ConfigureHid(Switch device, Configuration SwitchConfig)
  236. {
  237. if (SwitchConfig.JoystickControls.Enabled)
  238. {
  239. if (!Joystick.GetState(SwitchConfig.JoystickControls.Index).IsConnected)
  240. {
  241. SwitchConfig.JoystickControls.SetEnabled(false);
  242. }
  243. }
  244. device.Hid.InitializePrimaryController(SwitchConfig.ControllerType);
  245. device.Hid.InitializeKeyboard();
  246. }
  247. private class ConfigurationEnumFormatter<T> : IJsonFormatter<T>
  248. where T : struct
  249. {
  250. public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
  251. {
  252. formatterResolver.GetFormatterWithVerify<string>()
  253. .Serialize(ref writer, value.ToString(), formatterResolver);
  254. }
  255. public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
  256. {
  257. if (reader.ReadIsNull())
  258. {
  259. return default(T);
  260. }
  261. string enumName = formatterResolver.GetFormatterWithVerify<string>()
  262. .Deserialize(ref reader, formatterResolver);
  263. if (Enum.TryParse<T>(enumName, out T result))
  264. {
  265. return result;
  266. }
  267. return default(T);
  268. }
  269. }
  270. }
  271. }