Configuration.cs 9.6 KB

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