From a4d1c16c8c8465dcb57042b920a913bc877fb964 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 13 Oct 2024 10:37:09 +0200 Subject: [PATCH] aroma-wip, untested --- .../controller_patcher/ControllerPatcher.hpp | 2 + source/ConfigReader.cpp | 6 +- source/ControllerPatcher.cpp | 54 ++++++++----- source/config/ConfigParser.cpp | 2 +- source/config/ConfigValues.cpp | 4 +- source/config/ConfigValues.hpp | 2 +- source/fs/FSUtils.cpp | 2 +- source/network/CPTCPServer.cpp | 1 + source/network/UDPClient.cpp | 6 +- source/network/UDPServer.cpp | 8 +- source/patcher/ControllerPatcherUtils.cpp | 2 +- source/utils/PadConst.cpp | 12 +-- source/utils/TCPServer.cpp | 6 +- source/utils/logger.h | 75 ++++++++++++++----- 14 files changed, 119 insertions(+), 63 deletions(-) diff --git a/include/controller_patcher/ControllerPatcher.hpp b/include/controller_patcher/ControllerPatcher.hpp index 0bb065c..20c2b38 100644 --- a/include/controller_patcher/ControllerPatcher.hpp +++ b/include/controller_patcher/ControllerPatcher.hpp @@ -108,6 +108,8 @@ public: **/ static CONTROLLER_PATCHER_RESULT_OR_ERROR enableControllerMapping(); + static CONTROLLER_PATCHER_RESULT_OR_ERROR UpdateSamplingFunctionAddress(); + /** Disbale the Controller mapping. Afterwards all connected controllers will be used for the gamepad. @return When the functions failed result < 0 is returned. If the result is == 0 the function was successful. diff --git a/source/ConfigReader.cpp b/source/ConfigReader.cpp index 31188da..a261aeb 100644 --- a/source/ConfigReader.cpp +++ b/source/ConfigReader.cpp @@ -102,8 +102,10 @@ void ConfigReader::processFileList(std::vector path) { std::string ConfigReader::loadFileToString(std::string path) { std::string strBuffer = ""; uint8_t *buffer = NULL; - if (FSUtils::LoadFileToMem(path.c_str(), &buffer, NULL) > 0) { - strBuffer = std::string((char *) buffer); + uint32_t outLen = 0; + if (FSUtils::LoadFileToMem(path.c_str(), &buffer, &outLen) > 0) { + strBuffer = std::string((char *) buffer, outLen); + free(buffer); strBuffer = StringTools::removeCharFromString(strBuffer, '\r'); strBuffer = StringTools::removeCharFromString(strBuffer, ' '); strBuffer = StringTools::removeCharFromString(strBuffer, '\t'); diff --git a/source/ControllerPatcher.cpp b/source/ControllerPatcher.cpp index 6c666ce..9c10a50 100644 --- a/source/ControllerPatcher.cpp +++ b/source/ControllerPatcher.cpp @@ -485,22 +485,35 @@ void ControllerPatcher::ResetConfig() { ControllerPatcherUtils::setConfigValue((uint8_t *) &config_controller[xinput_slot][CONTRPS_VPAD_BUTTON_R_STICK_Y_INVERT], CONTROLLER_PATCHER_VALUE_SET, HID_XINPUT_STICK_R_Y[STICK_CONF_INVERT]); } -BOOL ControllerPatcher::Init(const char *pathToConfig) { - OSDynLoad_Module handle; - void *kpad_ptr; - OSDynLoad_Acquire("padscore", &handle); - OSDynLoad_FindExport(handle, OS_DYNLOAD_EXPORT_FUNC, "KPADRead", &kpad_ptr); - - gSamplingCallback = (WPADSamplingCallback) ((uint32_t) kpad_ptr + 0x1F0); - if (*(uint32_t *) gSamplingCallback != FIRST_INSTRUCTION_IN_SAMPLING_CALLBACK) { - //In Firmware <= 5.1.2 the offset changes - gSamplingCallback = (WPADSamplingCallback) ((uint32_t) kpad_ptr + 0x1F8); - if (*(uint32_t *) gSamplingCallback != FIRST_INSTRUCTION_IN_SAMPLING_CALLBACK) { - //Should never happen. I looked into the padscore.rpl of ALL firmwares. - gSamplingCallback = NULL; +CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::UpdateSamplingFunctionAddress() { + OSDynLoad_Module handle = nullptr; + WPADSamplingCallback newAddr = nullptr; + auto err = OSDynLoad_IsModuleLoaded("padscore", &handle); + if (err == OS_DYNLOAD_OK && handle != nullptr) { + void *kpad_ptr = nullptr; + if (OSDynLoad_FindExport(handle, OS_DYNLOAD_EXPORT_FUNC, "KPADRead", &kpad_ptr) == OS_DYNLOAD_OK) { + newAddr = (WPADSamplingCallback) ((uint32_t) kpad_ptr + 0x1F0); + if (*(uint32_t *) newAddr != FIRST_INSTRUCTION_IN_SAMPLING_CALLBACK) { + //In Firmware <= 5.1.2 the offset changes + newAddr = (WPADSamplingCallback) ((uint32_t) kpad_ptr + 0x1F8); + if (*(uint32_t *) newAddr != FIRST_INSTRUCTION_IN_SAMPLING_CALLBACK) { + //Should never happen. I looked into the padscore.rpl of ALL firmwares. + newAddr = nullptr; + } + } } + } else { + OSReport("Failed to update sampling addr\n"); + } + gSamplingCallback = newAddr; + return CONTROLLER_PATCHER_ERROR_NONE; +} + +BOOL ControllerPatcher::Init(const char *pathToConfig) { + gSamplingCallback = nullptr; + if (gSamplingCallback != nullptr) { + DEBUG_FUNCTION_LINE("Found the gSamplingCallback at %08X ", gSamplingCallback); } - DEBUG_FUNCTION_LINE("Found the gSamplingCallback at %08X ", gSamplingCallback); if (HID_DEBUG) { DEBUG_FUNCTION_LINE("Init called! "); @@ -519,7 +532,6 @@ BOOL ControllerPatcher::Init(const char *pathToConfig) { } if (pathToConfig != NULL && gConfig_done != HID_SDCARD_READ) { - DEBUG_FUNCTION_LINE("Reading config files from SD Card"); DEBUG_FUNCTION_LINE("Reading config files from SD Card"); ConfigReader *reader = ConfigReader::getInstance(); @@ -547,9 +559,11 @@ void ControllerPatcher::startNetworkServer() { } void ControllerPatcher::stopNetworkServer() { - DEBUG_FUNCTION_LINE("called! "); + DEBUG_FUNCTION_LINE("called!!"); UDPServer::destroyInstance(); + DEBUG_FUNCTION_LINE("called! "); UDPClient::destroyInstance(); + DEBUG_FUNCTION_LINE("called! "); CPTCPServer::destroyInstance(); } @@ -1138,7 +1152,7 @@ CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::resetCallbackData() { memset(gWPADConnectCallback, 0, sizeof(gWPADConnectCallback)); memset(gKPADConnectCallback, 0, sizeof(gKPADConnectCallback)); memset(gExtensionCallback, 0, sizeof(gExtensionCallback)); - gSamplingCallback = 0; + gSamplingCallback = nullptr; gCallbackCooldown = 0; return CONTROLLER_PATCHER_ERROR_NONE; } @@ -1199,15 +1213,15 @@ CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::handleCallbackData(BOOL bu CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcher::handleCallbackDataInternal(WPADChan chan) { if (gWPADConnectCallback[chan] != NULL) { - log_printf("Called WPAD connect callback for pro controller in slot %d!", chan + 1); + DEBUG_FUNCTION_LINE("Called WPAD connect callback for pro controller in slot %d!", chan + 1); gWPADConnectCallback[chan](chan, 0); } if (gKPADConnectCallback[chan] != NULL) { - log_printf("Called KPAD connect callback for pro controller in slot %d!", chan + 1); + DEBUG_FUNCTION_LINE("Called KPAD connect callback for pro controller in slot %d!", chan + 1); gKPADConnectCallback[chan](chan, 0); } if (gExtensionCallback[chan] != NULL) { - log_printf("Called extension callback for pro controller in slot %d!", chan + 1); + DEBUG_FUNCTION_LINE("Called extension callback for pro controller in slot %d!", chan + 1); gExtensionCallback[chan](chan, WPAD_EXT_PRO_CONTROLLER); } return CONTROLLER_PATCHER_ERROR_NONE; diff --git a/source/config/ConfigParser.cpp b/source/config/ConfigParser.cpp index 8e63f04..5b24149 100644 --- a/source/config/ConfigParser.cpp +++ b/source/config/ConfigParser.cpp @@ -23,7 +23,7 @@ ConfigParser::ConfigParser(std::string configData) { this->content = configData; - this->contentLines = StringTools::stringSplit(content, ""); + this->contentLines = StringTools::stringSplit(content, "\n"); if (contentLines.empty()) { return; diff --git a/source/config/ConfigValues.cpp b/source/config/ConfigValues.cpp index 8479bda..5c0b238 100644 --- a/source/config/ConfigValues.cpp +++ b/source/config/ConfigValues.cpp @@ -53,7 +53,7 @@ BOOL ConfigValues::setIfValueIsAControllerPresetEx(std::string value, int32_t sl //We need this function here so we can use preset sticks. BOOL ConfigValues::setIfValueIsPreset(std::map values, std::string possibleValue, int32_t slot, int32_t keyslot) { - if (slot > gHIDMaxDevices || slot < 0 || keyslot < 0 || keyslot >= CONTRPS_MAX_VALUE) { + if (slot >= gHIDMaxDevices || slot < 0 || keyslot < 0 || keyslot >= CONTRPS_MAX_VALUE) { return false; } const uint8_t *values_ = NULL; @@ -127,7 +127,7 @@ std::string ConfigValues::getStringByVIDPIDEx(uint16_t vid, uint16_t pid) { if (it != deviceNames.end()) { result = it->second; } else { - result = StringTools::strfmt("VID: 0x%04X\nPID: 0x%04X", vid, pid); + result = StringTools::strfmt("0x%04X / 0x%04X", vid, pid); } return result; } diff --git a/source/config/ConfigValues.hpp b/source/config/ConfigValues.hpp index 97787f0..462f14b 100644 --- a/source/config/ConfigValues.hpp +++ b/source/config/ConfigValues.hpp @@ -128,7 +128,7 @@ private: **/ static std::string getStringByVIDPID(uint16_t vid, uint16_t pid) { ConfigValues *cur_instance = getInstance(); - if (cur_instance == NULL) return ""; + if (cur_instance == nullptr) return StringTools::strfmt("0x%04X / 0x%04X", vid, pid); return cur_instance->getStringByVIDPIDEx(vid, pid); } diff --git a/source/fs/FSUtils.cpp b/source/fs/FSUtils.cpp index a02b234..a94c245 100644 --- a/source/fs/FSUtils.cpp +++ b/source/fs/FSUtils.cpp @@ -26,7 +26,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ return -2; } - uint32_t blocksize = 0x4000; + uint32_t blocksize = 0x20000; uint32_t done = 0; int32_t readBytes = 0; diff --git a/source/network/CPTCPServer.cpp b/source/network/CPTCPServer.cpp index 01b002d..96aa6d8 100644 --- a/source/network/CPTCPServer.cpp +++ b/source/network/CPTCPServer.cpp @@ -29,6 +29,7 @@ CPTCPServer::CPTCPServer(int32_t port) : TCPServer(port, CPTCPServer::getPriorit } CPTCPServer::~CPTCPServer() { + DEBUG_FUNCTION_LINE("~CPTCPServer"); CPTCPServer::AttachDetach(HID_DEVICE_DETACH); } diff --git a/source/network/UDPClient.cpp b/source/network/UDPClient.cpp index 3a04c84..9e4d022 100644 --- a/source/network/UDPClient.cpp +++ b/source/network/UDPClient.cpp @@ -43,12 +43,12 @@ UDPClient::UDPClient(uint32_t ip, int32_t port) { } UDPClient::~UDPClient() { + DEBUG_FUNCTION_LINE("~UDPClient"); if (this->sockfd != -1) { close(sockfd); } - if (HID_DEBUG) { - log_printf("UDPClient::~UDPClient(line %d): Thread has been closed", __LINE__); - } + + DEBUG_FUNCTION_LINE("UDPClient::~UDPClient(line %d): Thread has been closed", __LINE__); } BOOL UDPClient::sendData(char *data, int32_t length) { diff --git a/source/network/UDPServer.cpp b/source/network/UDPServer.cpp index 082b4d0..f4a1fa5 100644 --- a/source/network/UDPServer.cpp +++ b/source/network/UDPServer.cpp @@ -18,7 +18,7 @@ #include #include #include - +#include #include #define MAX_UDP_SIZE 0x578 @@ -45,9 +45,11 @@ UDPServer::UDPServer(int32_t port) { } UDPServer::~UDPServer() { + CThread *pThreadPointer = UDPServer::pThread; if (pThreadPointer != NULL) { exitThread = 1; + OSMemoryBarrier(); if (pThreadPointer != NULL) { delete pThreadPointer; UDPServer::pThread = NULL; @@ -57,9 +59,9 @@ UDPServer::~UDPServer() { this->sockfd = -1; } } - if (HID_DEBUG) { + DEBUG_FUNCTION_LINE("Thread has been closed"); - } + } void UDPServer::StartUDPThread(UDPServer *server) { diff --git a/source/patcher/ControllerPatcherUtils.cpp b/source/patcher/ControllerPatcherUtils.cpp index b6746fc..bf081f8 100644 --- a/source/patcher/ControllerPatcherUtils.cpp +++ b/source/patcher/ControllerPatcherUtils.cpp @@ -1021,7 +1021,7 @@ ControllerMappingPAD *ControllerPatcherUtils::getControllerMappingByType(UContro } CONTROLLER_PATCHER_RESULT_OR_ERROR ControllerPatcherUtils::doSampling(uint16_t deviceslot, uint8_t padslot = 0, BOOL ignorePadSlot = false) { - if (gSamplingCallback != NULL) { + if (gSamplingCallback != nullptr) { doSamplingSingle(WPAD_CHAN_0, deviceslot, padslot, ignorePadSlot); doSamplingSingle(WPAD_CHAN_1, deviceslot, padslot, ignorePadSlot); doSamplingSingle(WPAD_CHAN_2, deviceslot, padslot, ignorePadSlot); diff --git a/source/utils/PadConst.cpp b/source/utils/PadConst.cpp index 51e7f6f..0aed2cb 100644 --- a/source/utils/PadConst.cpp +++ b/source/utils/PadConst.cpp @@ -27,14 +27,14 @@ const uint8_t DEF_STICK_OFFSET_MINMAX = CONTRPS_VPAD_BUTTON_L_STICK_X_MINMAX - //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! Device names //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -const char *HID_GC_STRING = "GameCube\nUSB-Adapter"; +const char *HID_GC_STRING = "GameCube USB-Adapter"; const char *HID_KEYBOARD_STRING = "Keyboard"; const char *HID_MOUSE_STRING = "Mouse"; -const char *HID_DS3_STRING = "DualShock 3\nController"; -const char *HID_DS4_STRING = "DualShock 4\nController"; -const char *HID_NEW_DS4_STRING = "DualShock 4\nController"; -const char *HID_XINPUT_STRING = "XInput\nController"; -const char *HID_SWITCH_PRO_STRING = "Switch\nPro Controller"; +const char *HID_DS3_STRING = "DualShock 3 Controller"; +const char *HID_DS4_STRING = "DualShock 4 Controller"; +const char *HID_NEW_DS4_STRING = "DualShock 4 Controller"; +const char *HID_XINPUT_STRING = "XInput Controller"; +const char *HID_SWITCH_PRO_STRING = "Switch Pro Controller"; //!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //! GC-Adapter diff --git a/source/utils/TCPServer.cpp b/source/utils/TCPServer.cpp index 3a81b55..7013be0 100644 --- a/source/utils/TCPServer.cpp +++ b/source/utils/TCPServer.cpp @@ -20,17 +20,17 @@ TCPServer::TCPServer(int32_t port, int32_t priority) { TCPServer::~TCPServer() { CloseSockets(); - //DEBUG_FUNCTION_LINE("Thread will be closed"); + DEBUG_FUNCTION_LINE("Thread will be closed"); exitThread = 1; ICInvalidateRange((void *) &exitThread, 4); DCFlushRange((void *) &exitThread, 4); if (pThread != NULL) { - //DEBUG_FUNCTION_LINE("Deleting it!"); + DEBUG_FUNCTION_LINE("Deleting it!"); delete pThread; } - //DEBUG_FUNCTION_LINE("Thread done"); + DEBUG_FUNCTION_LINE("Thread done"); pThread = NULL; } diff --git a/source/utils/logger.h b/source/utils/logger.h index 8f6a5a0..ba75db7 100644 --- a/source/utils/logger.h +++ b/source/utils/logger.h @@ -1,39 +1,74 @@ #pragma once +#include +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include -#include +#define LOG_APP_TYPE "L" +#define LOG_APP_NAME "controllerpatcher" -#define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) -#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILENAME_X__) +#define __FILENAME__ ({ \ + const char *__filename = __FILE__; \ + const char *__pos = strrchr(__filename, '/'); \ + if (!__pos) __pos = strrchr(__filename, '\\'); \ + __pos ? __pos + 1 : __filename; \ +}) -#ifdef __LOGGING__ +#define LOG(LOG_FUNC, FMT, ARGS...) LOG_EX_DEFAULT(LOG_FUNC, "", "", FMT, ##ARGS) -#define log_print(str) WHBLogPrint(str) -#define log_printf(FMT, ARGS...) WHBLogPrintf(FMT, ##ARGS); +#define LOG_EX_DEFAULT(LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ARGS...) LOG_EX(__FILENAME__, __FUNCTION__, __LINE__, LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ##ARGS) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...) \ - do { \ - WHBLogPrintf("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ - } while (0); +#define LOG_EX(FILENAME, FUNCTION, LINE, LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ARGS...) \ + do { \ + LOG_FUNC("[(%s)%18s][%23s]%30s@L%04d: " LOG_LEVEL "" FMT "" LINE_END, LOG_APP_TYPE, LOG_APP_NAME, FILENAME, FUNCTION, LINE, ##ARGS); \ + } while (0) -#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) \ - do { \ - WHBLogWritef("[%23s]%30s@L%04d: " FMT "", __FILENAME__, __FUNCTION__, __LINE__, ##ARGS); \ - } while (0); +#ifdef DEBUG + +#ifdef VERBOSE_DEBUG +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) LOG(WHBLogPrintf, FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, WHBLogPrintf, "", "", FMT, ##ARGS); +#else +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(FMT, ARGS...) while (0) +#endif + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) LOG(WHBLogPrintf, FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) LOG(WHBLogWritef, FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX_DEFAULT(WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX_DEFAULT(WHBLogPrintf, "##WARN ## ", "", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX_DEFAULT(WHBLogPrintf, "##INFO ## ", "", FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR_LAMBDA(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS); #else -#define log_print(str) -#define log_printf(FMT, ARGS...) -#define DEBUG_FUNCTION_LINE(FMT, ARGS...) -#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) +#define DEBUG_FUNCTION_LINE_VERBOSE_EX(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "", "\n", FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0) + +#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "##ERROR## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "##WARN ## ", "\n", FMT, ##ARGS) +#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX_DEFAULT(OSReport, "##INFO ## ", "\n", FMT, ##ARGS) + +#define DEBUG_FUNCTION_LINE_ERR_LAMBDA(FILENAME, FUNCTION, LINE, FMT, ARGS...) LOG_EX(FILENAME, FUNCTION, LINE, OSReport, "##ERROR## ", "\n", FMT, ##ARGS); #endif +void initLogging(); + +void deinitLogging(); + #ifdef __cplusplus } -#endif \ No newline at end of file +#endif