forked from ndeadly/MissionControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcmitm_config.cpp
More file actions
142 lines (120 loc) · 5.27 KB
/
mcmitm_config.cpp
File metadata and controls
142 lines (120 loc) · 5.27 KB
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
/*
* Copyright (c) 2020-2022 ndeadly
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "mcmitm_config.hpp"
namespace ams::mitm {
namespace {
constexpr const char *config_file_location = "sdmc:/config/MissionControl/missioncontrol.ini";
SetLanguage g_system_language;
MissionControlConfig g_global_config = {
.general = {
.enable_rumble = true,
.enable_motion = true
},
.misc = {
.enable_dualshock4_lightbar = true,
.enable_dualsense_lightbar = true,
.enable_dualsense_player_leds = true,
.dualsense_vibration_intensity = 4,
.spoof_nso_as_pro_controller = false
}
};
void ParseBoolean(const char *value, bool *out) {
if (strcasecmp(value, "true") == 0)
*out = true;
else if (strcasecmp(value, "false") == 0)
*out = false;
}
void ParseInt(const char *value, int *out, int min=INT_MIN, int max=INT_MAX) {
int tmp = std::strtol(value, nullptr, 10);
if ((tmp >= min) && (tmp <= max))
*out = tmp;
}
void ParseBluetoothAddress(const char *value, bluetooth::Address *out) {
// Check length of address string is correct
if (std::strlen(value) != 3*sizeof(bluetooth::Address) - 1) return;
// Parse bluetooth mac address
char buf[2 + 1];
bluetooth::Address address = {};
for (uint32_t i = 0; i < sizeof(bluetooth::Address); ++i) {
// Convert hex pair to number
std::memcpy(buf, &value[i*3], 2);
address.address[i] = static_cast<uint8_t>(std::strtoul(buf, nullptr, 16));
// Check for colon separator
if ((i < sizeof(bluetooth::Address) - 1) && (value[i*3 + 2] != ':'))
return;
}
*out = address;
}
int ConfigIniHandler(void *user, const char *section, const char *name, const char *value) {
auto config = reinterpret_cast<MissionControlConfig *>(user);
if (strcasecmp(section, "general") == 0) {
if (strcasecmp(name, "enable_rumble") == 0)
ParseBoolean(value, &config->general.enable_rumble);
else if (strcasecmp(name, "enable_motion") == 0)
ParseBoolean(value, &config->general.enable_motion);
}
else if (strcasecmp(section, "bluetooth") == 0) {
if (strcasecmp(name, "host_name") == 0)
std::strncpy(config->bluetooth.host_name, value, sizeof(config->bluetooth.host_name));
else if (strcasecmp(name, "host_address") == 0)
ParseBluetoothAddress(value, &config->bluetooth.host_address);
}
else if (strcasecmp(section, "misc") == 0) {
if (strcasecmp(name, "enable_dualshock4_lightbar") == 0)
ParseBoolean(value, &config->misc.enable_dualshock4_lightbar);
else if (strcasecmp(name, "enable_dualsense_lightbar") == 0)
ParseBoolean(value, &config->misc.enable_dualsense_lightbar);
else if (strcasecmp(name, "enable_dualsense_player_leds") == 0)
ParseBoolean(value, &config->misc.enable_dualsense_player_leds);
else if (strcasecmp(name, "dualsense_vibration_intensity") == 0)
ParseInt(value, &config->misc.dualsense_vibration_intensity, 1, 8);
else if (strcasecmp(name, "spoof_nso_as_pro_controller") == 0)
ParseBoolean(value, &config->misc.spoof_nso_as_pro_controller);
}
else {
return 0;
}
return 1;
}
}
void ParseIniConfig() {
/* Open the file. */
fs::FileHandle file;
{
if (R_FAILED(fs::OpenFile(std::addressof(file), config_file_location, fs::OpenMode_Read))) {
return;
}
}
ON_SCOPE_EXIT { fs::CloseFile(file); };
/* Parse the config. */
util::ini::ParseFile(file, &g_global_config, ConfigIniHandler);
}
void InitializeConfig() {
ParseIniConfig();
R_ABORT_UNLESS(setInitialize());
ON_SCOPE_EXIT { setExit(); };
u64 language_code = 0;
R_ABORT_UNLESS(setGetSystemLanguage(&language_code));
R_ABORT_UNLESS(setMakeLanguage(language_code, &g_system_language));
}
MissionControlConfig *GetGlobalConfig() {
return &g_global_config;
}
SetLanguage GetSystemLanguage() {
return g_system_language;
}
}