using JsonPrettyPrinterPlus; using LibHac.FsSystem; using OpenTK.Input; using Ryujinx.Common; using Ryujinx.Common.Logging; using Ryujinx.HLE; using Ryujinx.HLE.HOS.SystemState; using Ryujinx.HLE.HOS.Services; using Ryujinx.HLE.Input; using Ryujinx.UI; using Ryujinx.UI.Input; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using Utf8Json; using Utf8Json.Resolvers; namespace Ryujinx { public class Configuration { /// /// The default configuration instance /// public static Configuration Instance { get; private set; } /// /// Dumps shaders in this local directory /// public string GraphicsShadersDumpPath { get; set; } /// /// Enables printing debug log messages /// public bool LoggingEnableDebug { get; set; } /// /// Enables printing stub log messages /// public bool LoggingEnableStub { get; set; } /// /// Enables printing info log messages /// public bool LoggingEnableInfo { get; set; } /// /// Enables printing warning log messages /// public bool LoggingEnableWarn { get; set; } /// /// Enables printing error log messages /// public bool LoggingEnableError { get; set; } /// /// Enables printing guest log messages /// public bool LoggingEnableGuest { get; set; } /// /// Enables printing FS access log messages /// public bool LoggingEnableFsAccessLog { get; set; } /// /// Controls which log messages are written to the log targets /// public LogClass[] LoggingFilteredClasses { get; set; } /// /// Enables or disables logging to a file on disk /// public bool EnableFileLog { get; set; } /// /// Change System Language /// public SystemLanguage SystemLanguage { get; set; } /// /// Enables or disables Docked Mode /// public bool DockedMode { get; set; } /// /// Enables or disables Discord Rich Presence /// public bool EnableDiscordIntegration { get; set; } /// /// Enables or disables Vertical Sync /// public bool EnableVsync { get; set; } /// /// Enables or disables multi-core scheduling of threads /// public bool EnableMulticoreScheduling { get; set; } /// /// Enables integrity checks on Game content files /// public bool EnableFsIntegrityChecks { get; set; } /// /// Enables FS access log output to the console. Possible modes are 0-3 /// public int FsGlobalAccessLogMode { get; set; } /// /// Enable or disable ignoring missing services /// public bool IgnoreMissingServices { get; set; } /// /// The primary controller's type /// public ControllerStatus ControllerType { get; set; } /// /// Used to toggle columns in the GUI /// public List GuiColumns { get; set; } /// /// A list of directories containing games to be used to load games into the games list /// public List GameDirs { get; set; } /// /// Enable or disable custom themes in the GUI /// public bool EnableCustomTheme { get; set; } /// /// Path to custom GUI theme /// public string CustomThemePath { get; set; } /// /// Enable or disable keyboard support (Independent from controllers binding) /// public bool EnableKeyboard { get; set; } /// /// Keyboard control bindings /// public NpadKeyboard KeyboardControls { get; set; } /// /// Controller control bindings /// public UI.Input.NpadController JoystickControls { get; private set; } /// /// Loads a configuration file from disk /// /// The path to the JSON configuration file public static void Load(string path) { var resolver = CompositeResolver.Create( new[] { new ConfigurationEnumFormatter() }, new[] { StandardResolver.AllowPrivateSnakeCase } ); using (Stream stream = File.OpenRead(path)) { Instance = JsonSerializer.Deserialize(stream, resolver); } } /// /// Loads a configuration file asynchronously from disk /// /// The path to the JSON configuration file public static async Task LoadAsync(string path) { IJsonFormatterResolver resolver = CompositeResolver.Create( new[] { new ConfigurationEnumFormatter() }, new[] { StandardResolver.AllowPrivateSnakeCase } ); using (Stream stream = File.OpenRead(path)) { Instance = await JsonSerializer.DeserializeAsync(stream, resolver); } } /// /// Save a configuration file to disk /// /// The path to the JSON configuration file public static void SaveConfig(Configuration config, string path) { IJsonFormatterResolver resolver = CompositeResolver.Create( new[] { new ConfigurationEnumFormatter() }, new[] { StandardResolver.AllowPrivateSnakeCase } ); byte[] data = JsonSerializer.Serialize(config, resolver); File.WriteAllText(path, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson()); } /// /// Configures a instance /// /// The instance to configure public static void InitialConfigure(Switch device) { if (Instance == null) { throw new InvalidOperationException("Configuration has not been loaded yet."); } SwitchSettings.ConfigureSettings(Instance); Logger.AddTarget(new AsyncLogTargetWrapper( new ConsoleLogTarget(), 1000, AsyncLogTargetOverflowAction.Block )); if (Instance.EnableFileLog) { Logger.AddTarget(new AsyncLogTargetWrapper( new FileLogTarget(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx.log")), 1000, AsyncLogTargetOverflowAction.Block )); } Configure(device, Instance); } public static void Configure(Switch device, Configuration SwitchConfig) { GraphicsConfig.ShadersDumpPath = SwitchConfig.GraphicsShadersDumpPath; Logger.SetEnable(LogLevel.Debug, SwitchConfig.LoggingEnableDebug ); Logger.SetEnable(LogLevel.Stub, SwitchConfig.LoggingEnableStub ); Logger.SetEnable(LogLevel.Info, SwitchConfig.LoggingEnableInfo ); Logger.SetEnable(LogLevel.Warning, SwitchConfig.LoggingEnableWarn ); Logger.SetEnable(LogLevel.Error, SwitchConfig.LoggingEnableError ); Logger.SetEnable(LogLevel.Guest, SwitchConfig.LoggingEnableGuest ); Logger.SetEnable(LogLevel.AccessLog, SwitchConfig.LoggingEnableFsAccessLog); if (SwitchConfig.LoggingFilteredClasses.Length > 0) { foreach (var logClass in EnumExtensions.GetValues()) { Logger.SetEnable(logClass, false); } foreach (var logClass in SwitchConfig.LoggingFilteredClasses) { Logger.SetEnable(logClass, true); } } MainWindow.DiscordIntegrationEnabled = SwitchConfig.EnableDiscordIntegration; device.EnableDeviceVsync = SwitchConfig.EnableVsync; device.System.State.DockedMode = SwitchConfig.DockedMode; device.System.State.SetLanguage(SwitchConfig.SystemLanguage); if (SwitchConfig.EnableMulticoreScheduling) { device.System.EnableMultiCoreScheduling(); } device.System.FsIntegrityCheckLevel = SwitchConfig.EnableFsIntegrityChecks ? IntegrityCheckLevel.ErrorOnInvalid : IntegrityCheckLevel.None; device.System.GlobalAccessLogMode = SwitchConfig.FsGlobalAccessLogMode; ServiceConfiguration.IgnoreMissingServices = SwitchConfig.IgnoreMissingServices; } public static void ConfigureHid(Switch device, Configuration SwitchConfig) { if (SwitchConfig.JoystickControls.Enabled) { if (!Joystick.GetState(SwitchConfig.JoystickControls.Index).IsConnected) { SwitchConfig.JoystickControls.SetEnabled(false); } } device.Hid.InitializePrimaryController(SwitchConfig.ControllerType); device.Hid.InitializeKeyboard(); } private class ConfigurationEnumFormatter : IJsonFormatter where T : struct { public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) { formatterResolver.GetFormatterWithVerify() .Serialize(ref writer, value.ToString(), formatterResolver); } public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) { return default(T); } string enumName = formatterResolver.GetFormatterWithVerify() .Deserialize(ref reader, formatterResolver); if (Enum.TryParse(enumName, out T result)) { return result; } return default(T); } } } }