controller_patcher/source/config/ConfigParser.cpp

404 lines
15 KiB
C++
Raw Permalink Normal View History

/****************************************************************************
* Copyright (C) 2016,2017 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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/>.
****************************************************************************/
2017-05-07 14:44:09 +02:00
#include "./ConfigParser.hpp"
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2023-04-10 11:45:58 +02:00
#include <utils/StringTools.h>
2018-06-19 17:46:37 +02:00
ConfigParser::ConfigParser(std::string configData) {
2023-04-10 11:45:58 +02:00
this->content = configData;
2024-10-13 10:37:09 +02:00
this->contentLines = StringTools::stringSplit(content, "\n");
2023-04-10 11:45:58 +02:00
if (contentLines.empty()) {
2018-06-19 17:46:37 +02:00
return;
}
//remove the comments and make everything uppercase
2023-04-10 11:45:58 +02:00
for (uint32_t i = 0; i < contentLines.size(); i++) {
2017-10-29 09:34:47 +01:00
std::vector<std::string> comments = StringTools::stringSplit(contentLines[i], "//");
2023-04-10 11:45:58 +02:00
if (!comments.empty()) {
contentLines[i] = comments[0];
}
//we want everything uppercase
2023-04-10 11:45:58 +02:00
std::transform(contentLines[i].begin(), contentLines[i].end(), contentLines[i].begin(), ::toupper);
}
//remove empty lines
std::vector<std::string> contentline2;
2023-04-10 11:45:58 +02:00
for (uint32_t i = 0; i < contentLines.size(); i++) {
if (strlen(contentLines[i].c_str()) > 0) {
2018-06-19 17:46:37 +02:00
contentline2.push_back(contentLines[i]);
}
}
contentLines = contentline2;
Init();
}
2018-06-19 17:46:37 +02:00
ConfigParser::~ConfigParser() {
}
2018-06-19 17:46:37 +02:00
PARSE_TYPE ConfigParser::getType() {
return type_b;
}
2018-06-19 17:46:37 +02:00
void ConfigParser::setType(PARSE_TYPE newType) {
this->type_b = newType;
}
2018-06-20 15:07:15 +02:00
uint16_t ConfigParser::getSlot() {
return this->slot_b;
}
2018-06-20 15:07:15 +02:00
void ConfigParser::setSlot(uint16_t newSlot) {
this->slot_b = newSlot;
}
2018-06-20 15:07:15 +02:00
BOOL ConfigParser::Init() {
2023-04-10 11:45:58 +02:00
if (contentLines.size() == 0) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("File seems to be empty. Make sure to have a proper header");
return false;
}
2023-04-10 11:45:58 +02:00
const char *line = contentLines[0].c_str();
int32_t len = strlen(line);
if (len <= 4) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Header is too short.");
return false;
}
std::string identify;
2023-04-10 11:45:58 +02:00
if (line[0] == '[' && line[len - 1] == ']') {
identify = contentLines[0].substr(1, len - 2);
2018-06-19 17:46:37 +02:00
} else {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Not a proper config file!");
return false;
}
2023-04-10 11:45:58 +02:00
if (identify.compare("GAMEPAD") == 0) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a gamepad config file!");
setSlot(gGamePadSlot);
setType(PARSE_GAMEPAD);
2023-04-10 11:45:58 +02:00
} else if (identify.compare("MOUSE") == 0) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a mouse config file!");
setSlot(gMouseSlot);
setType(PARSE_MOUSE);
this->vid = HID_MOUSE_VID;
this->pid = HID_MOUSE_PID;
2023-04-10 11:45:58 +02:00
} else if (identify.compare("KEYBOARD") == 0) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a keyboard config file!");
setSlot(gHID_SLOT_KEYBOARD);
setType(PARSE_KEYBOARD);
this->vid = HID_KEYBOARD_VID;
this->pid = HID_KEYBOARD_PID;
2018-06-19 17:46:37 +02:00
} else {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a controller config file!");
setSlot(getSlotController(identify));
setType(PARSE_CONTROLLER);
}
2023-04-10 11:45:58 +02:00
if (getSlot() == HID_INVALID_SLOT) {
return false;
}
ConfigReader::increaseNumberOfLoadedFiles();
return true;
}
2018-06-19 17:46:37 +02:00
void ConfigParser::parseSingleLine(std::string line) {
2023-04-10 11:45:58 +02:00
if (line.empty()) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Can't parse line. it's empty");
2018-06-19 17:46:37 +02:00
return;
}
2023-04-10 11:45:58 +02:00
std::vector<std::string> cur_values = StringTools::stringSplit(line, "=");
if (cur_values.size() != 2) {
if (HID_DEBUG || cur_values.size() > 2) {
DEBUG_FUNCTION_LINE("Not a valid key=pair line %s", line.c_str());
2018-06-19 17:46:37 +02:00
}
return;
2018-06-19 17:46:37 +02:00
} else {
2018-06-20 15:07:15 +02:00
uint16_t hid_slot = getSlot();
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("leftpart = \"%s\" ", cur_values[0].c_str());
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("rightpart = \"%s\" ", cur_values[1].c_str());
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
int32_t keyslot = -1;
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Checking single value");
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
if (getType() == PARSE_GAMEPAD || getType() == PARSE_KEYBOARD) {
keyslot = ConfigValues::getKeySlotGamePad(cur_values[0]);
2023-04-10 11:45:58 +02:00
} else if (getType() == PARSE_MOUSE) {
keyslot = ConfigValues::getKeySlotMouse(cur_values[0]);
2018-06-19 17:46:37 +02:00
} else {
keyslot = ConfigValues::getKeySlotDefaultSingleValue(cur_values[0]);
}
2023-04-10 11:45:58 +02:00
if (keyslot != -1) {
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a single value");
2018-06-19 17:46:37 +02:00
}
long rightValue = -1;
2023-04-10 11:45:58 +02:00
BOOL valueSet = false;
if (cur_values[0].compare("DPAD_MODE") == 0) {
const uint8_t *values_ = NULL;
if ((values_ = ConfigValues::getValuesStickPreset(cur_values[1])) != NULL) {
if (values_[STICK_CONF_MAGIC_VERSION] != STICK_CONF_MAGIC_VALUE)
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Settings preset DPAD MODE and Mask");
2018-06-19 17:46:37 +02:00
}
config_controller[hid_slot][CONTRPS_DPAD_MODE][0] = CONTROLLER_PATCHER_VALUE_SET;
config_controller[hid_slot][CONTRPS_DPAD_MODE][1] = values_[CONTRDPAD_MODE];
2023-04-10 11:45:58 +02:00
if (values_[CONTRDPAD_MASK] != 0x00) {
config_controller[hid_slot][CONTRPS_DPAD_MASK][0] = CONTROLLER_PATCHER_VALUE_SET;
config_controller[hid_slot][CONTRPS_DPAD_MASK][1] = values_[CONTRDPAD_MASK];
}
valueSet = true;
}
}
2023-04-10 11:45:58 +02:00
if (!valueSet) {
if (getType() == PARSE_KEYBOARD) {
if ((rightValue = ConfigValues::getPresetValuesKeyboard(cur_values[1])) != -1) {
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Used pre-defined Keyboard! \"%s\" is %d", cur_values[1].c_str(), rightValue);
2018-06-19 17:46:37 +02:00
}
} else {
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("I need to parse %s", cur_values[1].c_str());
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
char *ptr;
rightValue = strtol(cur_values[1].c_str(), &ptr, 16);
}
2018-06-19 17:46:37 +02:00
} else {
rightValue = ConfigValues::getPresetValue(cur_values[1]);
2023-04-10 11:45:58 +02:00
if (getType() == PARSE_MOUSE) { //No parsing for the mouse
if (rightValue == -1) {
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Invalid mouse value, lets skip it %s", cur_values[1].c_str());
2018-06-19 17:46:37 +02:00
}
return;
}
} else {
2023-04-10 11:45:58 +02:00
if (rightValue == -1) {
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("I need to parse %s", cur_values[1].c_str());
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
char *ptr;
rightValue = strtol(cur_values[1].c_str(), &ptr, 16);
}
}
}
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Setting value to %d", rightValue);
2018-06-19 17:46:37 +02:00
}
config_controller[hid_slot][keyslot][0] = CONTROLLER_PATCHER_VALUE_SET;
config_controller[hid_slot][keyslot][1] = rightValue;
}
2018-06-19 17:46:37 +02:00
} else {
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Check pair value");
2018-06-19 17:46:37 +02:00
}
keyslot = ConfigValues::getKeySlotDefaultPairedValue(cur_values[0]);
2023-04-10 11:45:58 +02:00
if (keyslot != -1) {
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a pair value");
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
if (!ConfigValues::getInstance()->setIfValueIsAControllerPreset(cur_values[1], getSlot(), keyslot)) {
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("And its no preset");
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
std::vector<std::string> rightvalues = StringTools::stringSplit(cur_values[1], ",");
2023-04-10 11:45:58 +02:00
if (rightvalues.size() != 2) {
DEBUG_FUNCTION_LINE("%d instead of 2 key=values pairs in line", rightvalues.size());
return;
}
2023-04-10 11:45:58 +02:00
char *ptr;
long firstValue = strtol(rightvalues[0].c_str(), &ptr, 16);
long secondValue = strtol(rightvalues[1].c_str(), &ptr, 16);
config_controller[hid_slot][keyslot][0] = firstValue;
config_controller[hid_slot][keyslot][1] = secondValue;
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Set %02X,%02X", firstValue, secondValue);
2018-06-19 17:46:37 +02:00
}
} else {
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Found preset value!!");
2018-06-19 17:46:37 +02:00
}
}
2018-06-19 17:46:37 +02:00
} else {
2023-04-10 11:45:58 +02:00
DEBUG_FUNCTION_LINE("The setting \"%s\" is unknown!", cur_values[0].c_str());
}
}
}
}
2018-06-20 15:07:15 +02:00
BOOL ConfigParser::resetConfig() {
int32_t slot = getSlot();
2023-04-10 11:45:58 +02:00
if ((slot == HID_INVALID_SLOT) || (slot >= gHIDMaxDevices)) return false;
for (int32_t j = (CONTRPS_PID + 1); j < CONTRPS_MAX_VALUE; j++) {
config_controller[slot][j][0] = CONTROLLER_PATCHER_INVALIDVALUE;
config_controller[slot][j][1] = CONTROLLER_PATCHER_INVALIDVALUE;
}
return true;
}
2023-04-10 11:45:58 +02:00
int32_t ConfigParser::getSlotController(std::string identify) {
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Getting Controller Slot");
2018-06-19 17:46:37 +02:00
}
2023-04-10 11:45:58 +02:00
std::vector<std::string> values = StringTools::stringSplit(identify, ",");
2023-04-10 11:45:58 +02:00
if (values.size() != 2) {
DEBUG_FUNCTION_LINE("You need to provide a VID and PID. e.g. \"[vid=0x451,pid=0x152]\". (%s)", identify.c_str());
return HID_INVALID_SLOT;
}
2023-04-10 11:45:58 +02:00
int32_t vid = getValueFromKeyValue(values[0], "VID", "=");
if (vid < 0) {
return HID_INVALID_SLOT;
}
2023-04-10 11:45:58 +02:00
int32_t pid = getValueFromKeyValue(values[1], "PID", "=");
if (pid < 0) {
return HID_INVALID_SLOT;
}
2023-04-10 11:45:58 +02:00
DEBUG_FUNCTION_LINE("VID: %04x PID: %04x", vid, pid);
this->vid = vid;
this->pid = pid;
DeviceInfo deviceinfo;
2023-04-10 11:45:58 +02:00
memset(&deviceinfo, 0, sizeof(deviceinfo));
deviceinfo.vidpid.vid = vid;
deviceinfo.vidpid.pid = pid;
2023-04-10 11:45:58 +02:00
int32_t result = ControllerPatcherUtils::getDeviceInfoFromVidPid(&deviceinfo);
int32_t slot = deviceinfo.slotdata.deviceslot;
int32_t hid = 0;
if (result < 0) {
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Its a new controller, lets save it");
2018-06-19 17:46:37 +02:00
}
HIDSlotData slotdata;
ControllerPatcherUtils::getNextSlotData(&slotdata);
slot = slotdata.deviceslot;
2023-04-10 11:45:58 +02:00
hid = slotdata.hidmask;
2023-04-10 11:45:58 +02:00
if (slot >= gHIDMaxDevices) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("We don't a space for a new controller, please delete .inis");
return HID_INVALID_SLOT;
}
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Got new slot! slot: %d hid %s .. Lets registrate it!", slot, StringTools::byte_to_binary(hid));
2018-06-19 17:46:37 +02:00
}
config_controller[slot][CONTRPS_VID][0] = (vid & 0xFF00) >> 8;
config_controller[slot][CONTRPS_VID][1] = (vid & 0x00FF);
config_controller[slot][CONTRPS_PID][0] = (pid & 0xFF00) >> 8;
config_controller[slot][CONTRPS_PID][1] = (pid & 0x00FF);
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Saved vid: %04X pid: %04X",
2018-06-19 17:46:37 +02:00
config_controller[slot][CONTRPS_VID][0] * 0x100 + config_controller[slot][CONTRPS_VID][1],
config_controller[slot][CONTRPS_PID][0] * 0x100 + config_controller[slot][CONTRPS_PID][1]);
}
2023-04-10 11:45:58 +02:00
config_controller_hidmask[slot] = hid;
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Saved the hid");
2018-06-19 17:46:37 +02:00
}
2018-06-19 17:46:37 +02:00
} else {
2023-04-10 11:45:58 +02:00
if (slot < gHIDMaxDevices) {
hid = config_controller_hidmask[slot];
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE(">>>>>> found slot %d (hid:%s). Modifing existing data <<<<<<<<", slot, StringTools::byte_to_binary(hid));
2018-06-19 17:46:37 +02:00
}
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("We already have data of this controller, lets modify it");
2018-06-19 17:46:37 +02:00
} else {
2023-04-10 11:45:58 +02:00
DEBUG_FUNCTION_LINE("Something really odd happend to the slots. %d is bigger then max (%d)", slot, gHIDMaxDevices);
return HID_INVALID_SLOT;
}
}
2023-04-10 11:45:58 +02:00
DEBUG_FUNCTION_LINE("using slot: %d hid %08X", slot, hid);
return slot;
}
2018-06-20 15:07:15 +02:00
BOOL ConfigParser::parseIni() {
2023-04-10 11:45:58 +02:00
if (getSlot() == HID_INVALID_SLOT) {
DEBUG_FUNCTION_LINE("Couldn't parse file. Not a valid slot. Probably broken config. Or you tried to have more than %d devices", getType(), gHIDMaxDevices);
return false;
}
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("Parsing content, type %d", getType());
2018-06-19 17:46:37 +02:00
}
2018-06-20 15:07:15 +02:00
int32_t start = 1;
2023-04-10 11:45:58 +02:00
if (contentLines.size() <= 1) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("File only contains a header.");
return false;
}
2023-04-10 11:45:58 +02:00
if (contentLines[1].compare("[IGNOREDEFAULT]") == 0) {
resetConfig();
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Ignoring existing settings of this device");
start++;
}
2023-04-10 11:45:58 +02:00
for (uint32_t i = start; i < contentLines.size(); i++) {
if (HID_DEBUG) {
DEBUG_FUNCTION_LINE("line %d: \"%s\" ", (i + 1), contentLines[i].c_str());
2018-06-19 17:46:37 +02:00
}
parseSingleLine(contentLines[i]);
}
2023-04-10 11:45:58 +02:00
if (HID_DEBUG) {
2020-12-16 02:04:31 +01:00
DEBUG_FUNCTION_LINE("Parsing of the file is done.");
2018-06-19 17:46:37 +02:00
}
return true;
}
2023-04-10 11:45:58 +02:00
int32_t ConfigParser::getValueFromKeyValue(std::string value_pair, std::string expectedKey, std::string delimiter) {
std::vector<std::string> string_value = StringTools::stringSplit(value_pair, delimiter);
if (string_value.size() != 2) {
if (HID_DEBUG || string_value.size() > 2) {
DEBUG_FUNCTION_LINE("Not a valid key=pair line %s", value_pair.c_str());
2018-06-19 17:46:37 +02:00
}
return -1;
}
2023-04-10 11:45:58 +02:00
if (string_value[0].compare(expectedKey) != 0) {
DEBUG_FUNCTION_LINE("Key part not %s, its %s", expectedKey.c_str(), string_value[0].c_str());
return -1;
}
2023-04-10 11:45:58 +02:00
char *ptr;
int32_t value = strtol(string_value[1].c_str(), &ptr, 16);
return value;
}