Configuration.cs 7.8 KB

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