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
/// <summary>
/// The default configuration instance
/// </summary>
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<bool> GuiColumns { get; set; }
/// A list of directories containing games to be used to load games into the games list
public List<string> 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
/// <param name="path">The path to the JSON configuration file</param>
public static void Load(string path)
var resolver = CompositeResolver.Create(
new[] { new ConfigurationEnumFormatter<Key>() },
new[] { StandardResolver.AllowPrivateSnakeCase }
);
using (Stream stream = File.OpenRead(path))
Instance = JsonSerializer.Deserialize<Configuration>(stream, resolver);
}
/// Loads a configuration file asynchronously from disk
public static async Task LoadAsync(string path)
IJsonFormatterResolver resolver = CompositeResolver.Create(
Instance = await JsonSerializer.DeserializeAsync<Configuration>(stream, resolver);
/// Save a configuration file to disk
public static void SaveConfig(Configuration config, string path)
byte[] data = JsonSerializer.Serialize(config, resolver);
File.WriteAllText(path, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
/// Configures a <see cref="Switch"/> instance
/// <param name="device">The instance to configure</param>
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)
new FileLogTarget(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Ryujinx.log")),
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<LogClass>())
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<T> : IJsonFormatter<T>
where T : struct
public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
formatterResolver.GetFormatterWithVerify<string>()
.Serialize(ref writer, value.ToString(), formatterResolver);
public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
if (reader.ReadIsNull())
return default(T);
string enumName = formatterResolver.GetFormatterWithVerify<string>()
.Deserialize(ref reader, formatterResolver);
if (Enum.TryParse<T>(enumName, out T result))
return result;