Configuration.cs 8.6 KB

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