aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx/Config.cs
blob: dcead64839b04743c29c24cf8403b1f4895c843a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using LibHac;
using Ryujinx.Common.Logging;
using Ryujinx.HLE;
using Ryujinx.UI.Input;
using System;
using System.Globalization;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace Ryujinx
{
    public static class Config
    {
        public static JoyConKeyboard   JoyConKeyboard   { get; private set; }
        public static JoyConController JoyConController { get; private set; }

        public static void Read(Switch device)
        {
            string iniFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            string iniPath = Path.Combine(iniFolder, "Ryujinx.conf");

            IniParser parser = new IniParser(iniPath);

            GraphicsConfig.ShadersDumpPath = parser.Value("Graphics_Shaders_Dump_Path");

            Logger.SetEnable(LogLevel.Debug,   Convert.ToBoolean(parser.Value("Logging_Enable_Debug")));
            Logger.SetEnable(LogLevel.Stub,    Convert.ToBoolean(parser.Value("Logging_Enable_Stub")));
            Logger.SetEnable(LogLevel.Info,    Convert.ToBoolean(parser.Value("Logging_Enable_Info")));
            Logger.SetEnable(LogLevel.Warning, Convert.ToBoolean(parser.Value("Logging_Enable_Warn")));
            Logger.SetEnable(LogLevel.Error,   Convert.ToBoolean(parser.Value("Logging_Enable_Error")));

            string[] filteredLogClasses = parser.Value("Logging_Filtered_Classes").Split(',', StringSplitOptions.RemoveEmptyEntries);

            //When the classes are specified on the list, we only
            //enable the classes that are on the list.
            //So, first disable everything, then enable
            //the classes that the user added to the list.
            if (filteredLogClasses.Length > 0)
            {
                foreach (LogClass Class in Enum.GetValues(typeof(LogClass)))
                {
                    Logger.SetEnable(Class, false);
                }
            }

            foreach (string logClass in filteredLogClasses)
            {
                if (!string.IsNullOrEmpty(logClass.Trim()))
                {
                    foreach (LogClass Class in Enum.GetValues(typeof(LogClass)))
                    {
                        if (Class.ToString().ToLower().Contains(logClass.Trim().ToLower()))
                        {
                            Logger.SetEnable(Class, true);
                        }
                    }
                }
            }

            device.System.State.DockedMode = Convert.ToBoolean(parser.Value("Docked_Mode"));

            device.EnableDeviceVsync = Convert.ToBoolean(parser.Value("Enable_Vsync"));

            if (Convert.ToBoolean(parser.Value("Enable_MultiCore_Scheduling")))
            {
                device.System.EnableMultiCoreScheduling();
            }

            device.System.FsIntegrityCheckLevel = Convert.ToBoolean(parser.Value("Enable_FS_Integrity_Checks"))
                ? IntegrityCheckLevel.ErrorOnInvalid
                : IntegrityCheckLevel.None;

            JoyConKeyboard = new JoyConKeyboard(

                new JoyConKeyboardLeft
                {
                    StickUp     = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Up")),
                    StickDown   = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Down")),
                    StickLeft   = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Left")),
                    StickRight  = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Right")),
                    StickButton = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Stick_Button")),
                    DPadUp      = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_DPad_Up")),
                    DPadDown    = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_DPad_Down")),
                    DPadLeft    = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_DPad_Left")),
                    DPadRight   = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_DPad_Right")),
                    ButtonMinus = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Button_Minus")),
                    ButtonL     = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Button_L")),
                    ButtonZl    = Convert.ToInt16(parser.Value("Controls_Left_JoyConKeyboard_Button_ZL"))
                },

                new JoyConKeyboardRight
                {
                    StickUp     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Up")),
                    StickDown   = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Down")),
                    StickLeft   = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Left")),
                    StickRight  = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Right")),
                    StickButton = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Stick_Button")),
                    ButtonA     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_A")),
                    ButtonB     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_B")),
                    ButtonX     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_X")),
                    ButtonY     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_Y")),
                    ButtonPlus  = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_Plus")),
                    ButtonR     = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_R")),
                    ButtonZr    = Convert.ToInt16(parser.Value("Controls_Right_JoyConKeyboard_Button_ZR"))
                });

            JoyConController = new JoyConController(

                       Convert.ToBoolean(parser.Value("GamePad_Enable")),
                       Convert.ToInt32  (parser.Value("GamePad_Index")),
                (float)Convert.ToDouble (parser.Value("GamePad_Deadzone"),          CultureInfo.InvariantCulture),
                (float)Convert.ToDouble (parser.Value("GamePad_Trigger_Threshold"), CultureInfo.InvariantCulture),

                new JoyConControllerLeft
                {
                    Stick       = ToId(parser.Value("Controls_Left_JoyConController_Stick")),
                    StickButton = ToId(parser.Value("Controls_Left_JoyConController_Stick_Button")),
                    DPadUp      = ToId(parser.Value("Controls_Left_JoyConController_DPad_Up")),
                    DPadDown    = ToId(parser.Value("Controls_Left_JoyConController_DPad_Down")),
                    DPadLeft    = ToId(parser.Value("Controls_Left_JoyConController_DPad_Left")),
                    DPadRight   = ToId(parser.Value("Controls_Left_JoyConController_DPad_Right")),
                    ButtonMinus = ToId(parser.Value("Controls_Left_JoyConController_Button_Minus")),
                    ButtonL     = ToId(parser.Value("Controls_Left_JoyConController_Button_L")),
                    ButtonZl    = ToId(parser.Value("Controls_Left_JoyConController_Button_ZL"))
                },

                new JoyConControllerRight
                {
                    Stick       = ToId(parser.Value("Controls_Right_JoyConController_Stick")),
                    StickButton = ToId(parser.Value("Controls_Right_JoyConController_Stick_Button")),
                    ButtonA     = ToId(parser.Value("Controls_Right_JoyConController_Button_A")),
                    ButtonB     = ToId(parser.Value("Controls_Right_JoyConController_Button_B")),
                    ButtonX     = ToId(parser.Value("Controls_Right_JoyConController_Button_X")),
                    ButtonY     = ToId(parser.Value("Controls_Right_JoyConController_Button_Y")),
                    ButtonPlus  = ToId(parser.Value("Controls_Right_JoyConController_Button_Plus")),
                    ButtonR     = ToId(parser.Value("Controls_Right_JoyConController_Button_R")),
                    ButtonZr    = ToId(parser.Value("Controls_Right_JoyConController_Button_ZR"))
                });
        }

        private static ControllerInputId ToId(string key)
        {
            switch (key.ToUpper())
            {
                case "LSTICK":    return ControllerInputId.LStick;
                case "DPADUP":    return ControllerInputId.DPadUp;
                case "DPADDOWN":  return ControllerInputId.DPadDown;
                case "DPADLEFT":  return ControllerInputId.DPadLeft;
                case "DPADRIGHT": return ControllerInputId.DPadRight;
                case "BACK":      return ControllerInputId.Back;
                case "LSHOULDER": return ControllerInputId.LShoulder;
                case "LTRIGGER":  return ControllerInputId.LTrigger;

                case "RSTICK":    return ControllerInputId.RStick;
                case "A":         return ControllerInputId.A;
                case "B":         return ControllerInputId.B;
                case "X":         return ControllerInputId.X;
                case "Y":         return ControllerInputId.Y;
                case "START":     return ControllerInputId.Start;
                case "RSHOULDER": return ControllerInputId.RShoulder;
                case "RTRIGGER":  return ControllerInputId.RTrigger;

                case "LJOYSTICK": return ControllerInputId.LJoystick;
                case "RJOYSTICK": return ControllerInputId.RJoystick;

                default: return ControllerInputId.Invalid;
            }
        }
    }

    //https://stackoverflow.com/a/37772571
    public class IniParser
    {
        private readonly Dictionary<string, string> _values;

        public IniParser(string path)
        {
            _values = File.ReadLines(path)
                .Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith('#'))
                .Select(line => line.Split('=', 2))
                .ToDictionary(parts => parts[0].Trim(), parts => parts.Length > 1 ? parts[1].Trim() : null);
        }

        public string Value(string name)
        {
            return _values.TryGetValue(name, out string value) ? value : null;
        }
    }
}