Configuration.cs 8.4 KB

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