Configuration.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using LibHac.IO;
  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. /// Controls which log messages are written to the log targets
  49. /// </summary>
  50. public LogClass[] LoggingFilteredClasses { get; private set; }
  51. /// <summary>
  52. /// Enables or disables logging to a file on disk
  53. /// </summary>
  54. public bool EnableFileLog { get; private set; }
  55. /// <summary>
  56. /// Change System Language
  57. /// </summary>
  58. public SystemLanguage SystemLanguage { get; private set; }
  59. /// <summary>
  60. /// Enables or disables Docked Mode
  61. /// </summary>
  62. public bool DockedMode { get; private set; }
  63. /// <summary>
  64. /// Enables or disables Discord Rich Presense
  65. /// </summary>
  66. public bool EnableDiscordIntergration { get; private set; }
  67. /// <summary>
  68. /// Enables or disables Vertical Sync
  69. /// </summary>
  70. public bool EnableVsync { get; private set; }
  71. /// <summary>
  72. /// Enables or disables multi-core scheduling of threads
  73. /// </summary>
  74. public bool EnableMulticoreScheduling { get; private set; }
  75. /// <summary>
  76. /// Enables integrity checks on Game content files
  77. /// </summary>
  78. public bool EnableFsIntegrityChecks { get; private set; }
  79. /// <summary>
  80. /// Enable or Disable aggressive CPU optimizations
  81. /// </summary>
  82. public bool EnableAggressiveCpuOpts { get; private set; }
  83. /// <summary>
  84. /// Enable or disable ignoring missing services
  85. /// </summary>
  86. public bool IgnoreMissingServices { get; private set; }
  87. /// <summary>
  88. /// The primary controller's type
  89. /// </summary>
  90. public HidControllerType ControllerType { get; private set; }
  91. /// <summary>
  92. /// Enable or disable keyboard support (Independent from controllers binding)
  93. /// </summary>
  94. public bool EnableKeyboard { get; private set; }
  95. /// <summary>
  96. /// Keyboard control bindings
  97. /// </summary>
  98. public NpadKeyboard KeyboardControls { get; private set; }
  99. /// <summary>
  100. /// Controller control bindings
  101. /// </summary>
  102. public NpadController GamepadControls { get; private set; }
  103. /// <summary>
  104. /// Loads a configuration file from disk
  105. /// </summary>
  106. /// <param name="path">The path to the JSON configuration file</param>
  107. public static void Load(string path)
  108. {
  109. var resolver = CompositeResolver.Create(
  110. new[] { new ConfigurationEnumFormatter<Key>() },
  111. new[] { StandardResolver.AllowPrivateSnakeCase }
  112. );
  113. using (Stream stream = File.OpenRead(path))
  114. {
  115. Instance = JsonSerializer.Deserialize<Configuration>(stream, resolver);
  116. }
  117. }
  118. /// <summary>
  119. /// Loads a configuration file asynchronously from disk
  120. /// </summary>
  121. /// <param name="path">The path to the JSON configuration file</param>
  122. public static async Task LoadAsync(string path)
  123. {
  124. var resolver = CompositeResolver.Create(
  125. new[] { new ConfigurationEnumFormatter<Key>() },
  126. new[] { StandardResolver.AllowPrivateSnakeCase }
  127. );
  128. using (Stream stream = File.OpenRead(path))
  129. {
  130. Instance = await JsonSerializer.DeserializeAsync<Configuration>(stream, resolver);
  131. }
  132. }
  133. /// <summary>
  134. /// Configures a <see cref="Switch"/> instance
  135. /// </summary>
  136. /// <param name="device">The instance to configure</param>
  137. public static void Configure(Switch device)
  138. {
  139. if (Instance == null)
  140. {
  141. throw new InvalidOperationException("Configuration has not been loaded yet.");
  142. }
  143. GraphicsConfig.ShadersDumpPath = Instance.GraphicsShadersDumpPath;
  144. Logger.AddTarget(new AsyncLogTargetWrapper(
  145. new ConsoleLogTarget(),
  146. 1000,
  147. AsyncLogTargetOverflowAction.Block
  148. ));
  149. if (Instance.EnableFileLog)
  150. {
  151. Logger.AddTarget(new AsyncLogTargetWrapper(
  152. new FileLogTarget(Path.Combine(Program.ApplicationDirectory, "Ryujinx.log")),
  153. 1000,
  154. AsyncLogTargetOverflowAction.Block
  155. ));
  156. }
  157. Logger.SetEnable(LogLevel.Debug, Instance.LoggingEnableDebug);
  158. Logger.SetEnable(LogLevel.Stub, Instance.LoggingEnableStub);
  159. Logger.SetEnable(LogLevel.Info, Instance.LoggingEnableInfo);
  160. Logger.SetEnable(LogLevel.Warning, Instance.LoggingEnableWarn);
  161. Logger.SetEnable(LogLevel.Error, Instance.LoggingEnableError);
  162. if (Instance.LoggingFilteredClasses.Length > 0)
  163. {
  164. foreach (var logClass in EnumExtensions.GetValues<LogClass>())
  165. {
  166. Logger.SetEnable(logClass, false);
  167. }
  168. foreach (var logClass in Instance.LoggingFilteredClasses)
  169. {
  170. Logger.SetEnable(logClass, true);
  171. }
  172. }
  173. device.System.State.DiscordIntergrationEnabled = Instance.EnableDiscordIntergration;
  174. device.EnableDeviceVsync = Instance.EnableVsync;
  175. device.System.State.DockedMode = Instance.DockedMode;
  176. device.System.State.SetLanguage(Instance.SystemLanguage);
  177. if (Instance.EnableMulticoreScheduling)
  178. {
  179. device.System.EnableMultiCoreScheduling();
  180. }
  181. device.System.FsIntegrityCheckLevel = Instance.EnableFsIntegrityChecks
  182. ? IntegrityCheckLevel.ErrorOnInvalid
  183. : IntegrityCheckLevel.None;
  184. if (Instance.EnableAggressiveCpuOpts)
  185. {
  186. Optimizations.AssumeStrictAbiCompliance = true;
  187. }
  188. ServiceConfiguration.IgnoreMissingServices = Instance.IgnoreMissingServices;
  189. if(Instance.GamepadControls.Enabled)
  190. {
  191. if (GamePad.GetName(Instance.GamepadControls.Index) == "Unmapped Controller")
  192. {
  193. Instance.GamepadControls.SetEnabled(false);
  194. }
  195. }
  196. device.Hid.InitilizePrimaryController(Instance.ControllerType);
  197. device.Hid.InitilizeKeyboard();
  198. }
  199. private class ConfigurationEnumFormatter<T> : IJsonFormatter<T>
  200. where T : struct
  201. {
  202. public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
  203. {
  204. formatterResolver.GetFormatterWithVerify<string>()
  205. .Serialize(ref writer, value.ToString(), formatterResolver);
  206. }
  207. public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
  208. {
  209. if (reader.ReadIsNull())
  210. {
  211. return default(T);
  212. }
  213. var enumName = formatterResolver.GetFormatterWithVerify<string>()
  214. .Deserialize(ref reader, formatterResolver);
  215. if(Enum.TryParse<T>(enumName, out T result))
  216. {
  217. return result;
  218. }
  219. return default(T);
  220. }
  221. }
  222. }
  223. }