From 09f9faec7cab224e59fb1743f2bfc31acdf03746 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 04:07:27 +0100 Subject: [PATCH 1/6] Fix Multiple controllers / Cleanup By Unhooking Steams hook to XInputGetState, we can detect how many Controllers Steam "emulates", before pluggin in any virtual controllers. This *should* enable the use of multiple Valve SteamControllers. The unhooking procedure currently only works on AMD64 and prob. only with XInput1_4 --- SteamTarget/VirtualControllerThread.cpp | 171 +++++++++++++++--------- SteamTarget/VirtualControllerThread.h | 39 ++++-- 2 files changed, 139 insertions(+), 71 deletions(-) diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 5d5e7bd..85f08f8 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -14,8 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "VirtualControllerThread.h" - -ULONG VirtualControllerThread::ulTargetSerials[XUSER_MAX_COUNT]; +// VirtualControllerThread::VirtualControllerThread() { @@ -33,7 +32,6 @@ VirtualControllerThread::VirtualControllerThread() for (int i = 0; i < XUSER_MAX_COUNT; i++) { VIGEM_TARGET_INIT(&vtX360[i]); - VirtualControllerThread::ulTargetSerials[i] = NULL; } } @@ -62,7 +60,7 @@ void VirtualControllerThread::stop() void VirtualControllerThread::resetControllers() { - iRealControllers = getRealControllers(); + //iRealControllers = getRealControllers(); } bool VirtualControllerThread::isRunning() @@ -73,91 +71,108 @@ bool VirtualControllerThread::isRunning() void VirtualControllerThread::controllerLoop() { DWORD result; - sf::Clock reCheckControllerTimer; - int i, j; + DWORD result2; + sf::Clock testTimer; + //int i, j; while (bShouldRun) { sfClock.restart(); - if (reCheckControllerTimer.getElapsedTime().asSeconds() >= 1.f) + if (realXGetState == nullptr && testTimer.getElapsedTime().asSeconds() > 1) { - iTotalControllers = 0; - for (i = 0; i < XUSER_MAX_COUNT; i++) - { - ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); + HMODULE xinputmod = nullptr; - result = XInputGetState(i, &xsState[i]); + HANDLE hProcess = GetCurrentProcess(); + HMODULE hMods[1024]; + DWORD cbNeeded; + EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded); + for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) + { + TCHAR szModName[MAX_PATH]; - if (result == ERROR_SUCCESS) + if (GetModuleBaseName(hProcess, hMods[i], szModName, + sizeof(szModName) / sizeof(TCHAR))) { - iTotalControllers++; - } - else { - break; + std::wstring name(&szModName[0]); + auto& f = std::use_facet>(std::locale()); + f.tolower(&name[0], &name[0] + name.size()); + if (name.find(std::wstring(L"xinput")) != std::wstring::npos) + { + xinputmod = hMods[i]; + break; + } } } - iTotalControllers -= iVirtualControllers; - reCheckControllerTimer.restart(); - } - for (i = iRealControllers; i < iTotalControllers && i < XUSER_MAX_COUNT; i++) - { - //////// - ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); + uint64_t testaddr = reinterpret_cast(GetProcAddress(xinputmod, "XInputGetState")); + std::cout << "testaddr: " << std::hex << testaddr << "\n"; - result = XInputGetState(i, &xsState[i]); + XInputGetState_t realXgstate = reinterpret_cast(testaddr); - if (result == ERROR_SUCCESS) + std::cout << "realXgstate: " << std::hex << realXgstate << "\n"; + for (int i = 0; i < 5; i++) { + valveHookBytes[i] = *reinterpret_cast(reinterpret_cast(*realXgstate) + i); + } - if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i]))) - { - iVirtualControllers++; - - std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; - - VirtualControllerThread::ulTargetSerials[i] = vtX360[i].SerialNo; + realXGetState = realXgstate; + } - vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + if (realXGetState != nullptr) + { + if (!checkedControllers) + { + for (int i = 0; i < XUSER_MAX_COUNT; i++) + { + XINPUT_STATE state = { 0 }; + result = XInputGetStateWrapper(i, &state); + result2 = callRealXinputGetState(i, &state); + if (result != result2) + controllerCount++; } - - RtlCopyMemory(&xrReport[i], &xsState[i].Gamepad, sizeof(XUSB_REPORT)); - - vigem_xusb_submit_report(vtX360[i], xrReport[i]); + std::cout << "ControllerCount: " << std::to_string(controllerCount) << "\n"; + checkedControllers = true; } - else + + for (int i = 0; i < controllerCount; i++) { - if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + //////// + ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); + result = XInputGetStateWrapper(i, &xsState[i]); + + if (result == ERROR_SUCCESS) { - iVirtualControllers--; - iTotalControllers = 0; - for (j = 0; j < XUSER_MAX_COUNT; j++) - { - ZeroMemory(&xsState[j], sizeof(XINPUT_STATE)); + vigem_target_set_vid(&vtX360[i], 0x1234); + vigem_target_set_pid(&vtX360[i], 0x0001); - result = XInputGetState(j, &xsState[j]); + if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i]))) + { + std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; + vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + } - if (result == ERROR_SUCCESS) - { - iTotalControllers++; - } - else { - break; - } + vigem_xusb_submit_report(vtX360[i], *reinterpret_cast(&xsState[i].Gamepad)); + } + else + { + if (VIGEM_SUCCESS(vigem_target_unplug(&vtX360[i]))) + { + std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; } - iTotalControllers -= iVirtualControllers; - std::cout << "Unplugged controller " << vtX360[i].SerialNo << std::endl; - VirtualControllerThread::ulTargetSerials[i] = NULL; } } } + + tickTime = sfClock.getElapsedTime().asMicroseconds(); if (tickTime < delay) { std::this_thread::sleep_for(std::chrono::microseconds(delay - tickTime)); } + + //std::this_thread::sleep_for(std::chrono::seconds(1)); } } @@ -194,14 +209,46 @@ void VirtualControllerThread::controllerCallback(VIGEM_TARGET Target, UCHAR Larg XINPUT_VIBRATION vibration; ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION)); vibration.wLeftMotorSpeed = LargeMotor * 0xff; //Controllers only use 1 byte, XInput-API uses two, ViGEm also only uses one, like the hardware does, so we have to multiply - vibration.wRightMotorSpeed = SmallMotor * 0xff; + vibration.wRightMotorSpeed = SmallMotor * 0xff; //Yeah yeah I do know about bitshifting and the multiplication not being 100% correct... - for (int i = 0; i < XUSER_MAX_COUNT; i++) + //for (int i = 0; i < XUSER_MAX_COUNT; i++) + //{ + // if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) + // { + XInputSetState(Target.SerialNo-1, &vibration); + // } + //} +} + +DWORD VirtualControllerThread::XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState) +{ + return XInputGetState(dwUserIndex, pState); +} + +DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState) +{ + DWORD ret; + DWORD dwOldProtect, dwBkup; + + BYTE* Address = reinterpret_cast(realXGetState); + VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); + for (DWORD i = 0; i < 5; i++) { - if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) - { - XInputSetState(i, &vibration); - } + *(Address + i) = realBytes[i]; + } + VirtualProtect(Address, 4, dwOldProtect, &dwBkup); + + ret = realXGetState(dwUserIndex, pState); + + VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); + for (int i = 0; i < 5; i++) + { + *(Address + i) = valveHookBytes[i]; } + VirtualProtect(Address, 5, dwOldProtect, &dwBkup); + + return ret; } + + diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 5a07a50..3c011f6 100644 --- a/SteamTarget/VirtualControllerThread.h +++ b/SteamTarget/VirtualControllerThread.h @@ -17,14 +17,17 @@ limitations under the License. #include #include -#include -#include +#include +#include +#include #include +#include -#include +#include -#include +#include +#include class VirtualControllerThread { @@ -41,16 +44,30 @@ public: private: - bool bShouldRun = false; + std::atomic bShouldRun = false; + + + typedef DWORD(WINAPI* XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE* pState); - int iRealControllers = 0; - int iTotalControllers = 0; + uint8_t valveHookBytes[5]; + uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; + //uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 }; + + XInputGetState_t x_get_state = &XInputGetState; + + XInputGetState_t realXGetState = nullptr; + + //int iRealControllers = 0; + //int iTotalControllers = 0; int iVirtualControllers = 0; + int controllerCount = 0; + + bool checkedControllers = false; + + //static std::vector ulTargetSerials; - static ULONG ulTargetSerials[XUSER_MAX_COUNT]; VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; XINPUT_STATE xsState[XUSER_MAX_COUNT]; - XUSB_REPORT xrReport[XUSER_MAX_COUNT]; std::thread controllerThread; @@ -64,5 +81,9 @@ private: static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); + static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); + + DWORD callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState); + }; From a9d947812ed3aca41e695ce192912d724419fe00 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 14:34:01 +0100 Subject: [PATCH 2/6] SteamTarget: Use VID / PID of Valve's SteamController / Cleanup By using VID and PID of Valve's SteamController, Steam doesn't give us ANOTHER "fake" XInput device Therefore it's not necessary anymore to count controllers before we plug in any virtual controllers --- SteamTarget/VirtualControllerThread.cpp | 132 ++++++++---------------- SteamTarget/VirtualControllerThread.h | 20 +--- 2 files changed, 47 insertions(+), 105 deletions(-) diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 85f08f8..279f721 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -18,8 +18,6 @@ limitations under the License. VirtualControllerThread::VirtualControllerThread() { - resetControllers(); - if (!VIGEM_SUCCESS(vigem_init())) { std::cout << "Error initializing ViGem!" << std::endl; @@ -27,13 +25,11 @@ VirtualControllerThread::VirtualControllerThread() bShouldRun = false; } - VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; for (int i = 0; i < XUSER_MAX_COUNT; i++) { VIGEM_TARGET_INIT(&vtX360[i]); } - } @@ -58,10 +54,6 @@ void VirtualControllerThread::stop() } } -void VirtualControllerThread::resetControllers() -{ - //iRealControllers = getRealControllers(); -} bool VirtualControllerThread::isRunning() { @@ -73,12 +65,15 @@ void VirtualControllerThread::controllerLoop() DWORD result; DWORD result2; sf::Clock testTimer; - //int i, j; while (bShouldRun) { sfClock.restart(); - if (realXGetState == nullptr && testTimer.getElapsedTime().asSeconds() > 1) + // We have to retrieve the XInputGetState function by loading it via GetProcAdress + // otherwise the M$ compiler calls to a jumptable, jumping to the real function + // We can't have this if we wan't to dynamically unpatch and repatch Valve's XInput hook + // Also wait a second, jut to be sure Steam has done it's hooking thing... + if (XGetState == nullptr && testTimer.getElapsedTime().asSeconds() > 1) { HMODULE xinputmod = nullptr; @@ -104,11 +99,7 @@ void VirtualControllerThread::controllerLoop() } } - uint64_t testaddr = reinterpret_cast(GetProcAddress(xinputmod, "XInputGetState")); - - std::cout << "testaddr: " << std::hex << testaddr << "\n"; - - XInputGetState_t realXgstate = reinterpret_cast(testaddr); + XInputGetState_t realXgstate = reinterpret_cast(GetProcAddress(xinputmod, "XInputGetState")); std::cout << "realXgstate: " << std::hex << realXgstate << "\n"; for (int i = 0; i < 5; i++) @@ -116,43 +107,46 @@ void VirtualControllerThread::controllerLoop() valveHookBytes[i] = *reinterpret_cast(reinterpret_cast(*realXgstate) + i); } - realXGetState = realXgstate; + XGetState = realXgstate; + controllerCount = 1; } - if (realXGetState != nullptr) + if (XGetState != nullptr) { - if (!checkedControllers) - { - for (int i = 0; i < XUSER_MAX_COUNT; i++) - { - XINPUT_STATE state = { 0 }; - result = XInputGetStateWrapper(i, &state); - result2 = callRealXinputGetState(i, &state); - if (result != result2) - controllerCount++; - } - std::cout << "ControllerCount: " << std::to_string(controllerCount) << "\n"; - checkedControllers = true; - } - - for (int i = 0; i < controllerCount; i++) + for (int i = 0; i < XUSER_MAX_COUNT; i++) { //////// - ZeroMemory(&xsState[i], sizeof(XINPUT_STATE)); - result = XInputGetStateWrapper(i, &xsState[i]); + XINPUT_STATE state = { 0 }; + result = XInputGetStateWrapper(i, &state); + XINPUT_STATE state2 = { 0 }; + result2 = callRealXinputGetState(i, &state2); if (result == ERROR_SUCCESS) { - vigem_target_set_vid(&vtX360[i], 0x1234); - vigem_target_set_pid(&vtX360[i], 0x0001); - - if (VIGEM_SUCCESS(vigem_target_plugin(Xbox360Wired, &vtX360[i]))) + if (result2 != ERROR_SUCCESS) { - std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; - vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + // By using VID and PID of Valve's SteamController, Steam doesn't give us ANOTHER "fake" XInput device + // Leading to endless pain and suffering. + // Or really, leading to pluggin in one virtual controller after another and mirroring inputs + // Also annoying the shit out of the user when they open the overlay as steam prompts to setup new XInput devices + // Also avoiding any fake inputs from Valve's default controllerprofile + // -> Leading to endless pain and suffering + vigem_target_set_vid(&vtX360[i], 0x28de); //Valve SteamController VID + vigem_target_set_pid(&vtX360[i], 0x1102); //Valve SteamController PID + + int vigem_res = vigem_target_plugin(Xbox360Wired, &vtX360[i]); + if (vigem_res == VIGEM_ERROR_TARGET_UNINITIALIZED) + { + VIGEM_TARGET_INIT(&vtX360[i]); + } + if (vigem_res == VIGEM_ERROR_NONE) + { + std::cout << "Plugged in controller " << vtX360[i].SerialNo << std::endl; + vigem_register_xusb_notification(reinterpret_cast(&VirtualControllerThread::controllerCallback), vtX360[i]); + } } - vigem_xusb_submit_report(vtX360[i], *reinterpret_cast(&xsState[i].Gamepad)); + vigem_xusb_submit_report(vtX360[i], *reinterpret_cast(&state.Gamepad)); } else { @@ -164,44 +158,13 @@ void VirtualControllerThread::controllerLoop() } } - - tickTime = sfClock.getElapsedTime().asMicroseconds(); if (tickTime < delay) { std::this_thread::sleep_for(std::chrono::microseconds(delay - tickTime)); } - //std::this_thread::sleep_for(std::chrono::seconds(1)); - } -} - -int VirtualControllerThread::getRealControllers() -{ - int realControllers = 0; - UINT numDevices = NULL; - - GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST)); - - PRAWINPUTDEVICELIST rawInputDeviceList; - rawInputDeviceList = (PRAWINPUTDEVICELIST)malloc(sizeof(RAWINPUTDEVICELIST) * numDevices); - GetRawInputDeviceList(rawInputDeviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)); - - for (unsigned int i = 0; i < numDevices; i++) - { - RID_DEVICE_INFO devInfo; - devInfo.cbSize = sizeof(RID_DEVICE_INFO); - GetRawInputDeviceInfo(rawInputDeviceList[i].hDevice, RIDI_DEVICEINFO, &devInfo, (PUINT)&devInfo.cbSize); - if (devInfo.hid.dwVendorId == 0x45e && devInfo.hid.dwProductId == 0x28e) - { - realControllers++; - } - } - - free(rawInputDeviceList); - std::cout << "Detected " << realControllers << " real connected X360 Controllers" << std::endl; - return realControllers; } void VirtualControllerThread::controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber) @@ -211,14 +174,7 @@ void VirtualControllerThread::controllerCallback(VIGEM_TARGET Target, UCHAR Larg vibration.wLeftMotorSpeed = LargeMotor * 0xff; //Controllers only use 1 byte, XInput-API uses two, ViGEm also only uses one, like the hardware does, so we have to multiply vibration.wRightMotorSpeed = SmallMotor * 0xff; //Yeah yeah I do know about bitshifting and the multiplication not being 100% correct... - - //for (int i = 0; i < XUSER_MAX_COUNT; i++) - //{ - // if (VirtualControllerThread::ulTargetSerials[i] == Target.SerialNo) - // { - XInputSetState(Target.SerialNo-1, &vibration); - // } - //} + XInputSetState(Target.SerialNo-1, &vibration); } DWORD VirtualControllerThread::XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState) @@ -231,22 +187,22 @@ DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_ DWORD ret; DWORD dwOldProtect, dwBkup; - BYTE* Address = reinterpret_cast(realXGetState); - VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); - for (DWORD i = 0; i < 5; i++) + BYTE* Address = reinterpret_cast(XGetState); + VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. + for (DWORD i = 0; i < opPatchLenght; i++) //unpatch Valve's hook { *(Address + i) = realBytes[i]; } - VirtualProtect(Address, 4, dwOldProtect, &dwBkup); + VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... - ret = realXGetState(dwUserIndex, pState); + ret = XGetState(dwUserIndex, pState); //Cal REAL XInputGetState... - VirtualProtect(Address, 5, PAGE_EXECUTE_READWRITE, &dwOldProtect); - for (int i = 0; i < 5; i++) + VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. + for (int i = 0; i < opPatchLenght; i++) //repatch Valve's hook { *(Address + i) = valveHookBytes[i]; } - VirtualProtect(Address, 5, dwOldProtect, &dwBkup); + VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... return ret; } diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 3c011f6..40a6b78 100644 --- a/SteamTarget/VirtualControllerThread.h +++ b/SteamTarget/VirtualControllerThread.h @@ -38,8 +38,6 @@ public: void run(); void stop(); - void resetControllers(); - bool isRunning(); private: @@ -49,25 +47,15 @@ private: typedef DWORD(WINAPI* XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE* pState); + static const uint8_t opPatchLenght = 5; uint8_t valveHookBytes[5]; - uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; + const uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; //uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 }; - XInputGetState_t x_get_state = &XInputGetState; - - XInputGetState_t realXGetState = nullptr; - - //int iRealControllers = 0; - //int iTotalControllers = 0; - int iVirtualControllers = 0; int controllerCount = 0; - - bool checkedControllers = false; - - //static std::vector ulTargetSerials; + XInputGetState_t XGetState = nullptr; VIGEM_TARGET vtX360[XUSER_MAX_COUNT]; - XINPUT_STATE xsState[XUSER_MAX_COUNT]; std::thread controllerThread; @@ -77,8 +65,6 @@ private: void controllerLoop(); - int getRealControllers(); - static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); From 46935423210907ee45f9119f1b88f13ffa0ac3fc Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 20:10:16 +0100 Subject: [PATCH 3/6] SteamTarget: Remove unnecessary VirtualProtect calls --- SteamTarget/VirtualControllerThread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 279f721..225cb7b 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -193,11 +193,11 @@ DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_ { *(Address + i) = realBytes[i]; } - VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... + //VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... ret = XGetState(dwUserIndex, pState); //Cal REAL XInputGetState... - VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. + //VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. for (int i = 0; i < opPatchLenght; i++) //repatch Valve's hook { *(Address + i) = valveHookBytes[i]; From 9014a8c7afe1dfe5f2dc64d3e25df6cd865b0dc7 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 21:43:14 +0100 Subject: [PATCH 4/6] SteamTarget: Add x86 opcodes for unpatching Valve's XInputGetState hook --- SteamTarget/VirtualControllerThread.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 40a6b78..6ad3755 100644 --- a/SteamTarget/VirtualControllerThread.h +++ b/SteamTarget/VirtualControllerThread.h @@ -49,7 +49,14 @@ private: static const uint8_t opPatchLenght = 5; uint8_t valveHookBytes[5]; + + // First 5 bytes are the same for XInput1_4.dll and XInput9_1_0.dll (on AMD64 at least, didn't check yet for x86, there is no ViGEm build for Win7 anway...) + // So no change has to be made for Win7 Targets +#ifdef _AMD64_ const uint8_t realBytes[5] = {0x48, 0x89, 0x5C, 0x24, 0x08}; +#else + const uint8_t realBytes[5] = { 0x8B, 0xFF, 0x55, 0x8B, 0xEC }; +#endif //uint8_t realBytes[5] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x90 }; int controllerCount = 0; From 95eb87e183c227a3e1617ef647929bd3274702d7 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 21:43:29 +0100 Subject: [PATCH 5/6] Cleanup --- SteamTarget/VirtualControllerThread.cpp | 2 -- SteamTarget/VirtualControllerThread.h | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index 225cb7b..f73e266 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -193,11 +193,9 @@ DWORD VirtualControllerThread::callRealXinputGetState(DWORD dwUserIndex, XINPUT_ { *(Address + i) = realBytes[i]; } - //VirtualProtect(Address, opPatchLenght, dwOldProtect, &dwBkup); //Revert permission change... ret = XGetState(dwUserIndex, pState); //Cal REAL XInputGetState... - //VirtualProtect(Address, opPatchLenght, PAGE_EXECUTE_READWRITE, &dwOldProtect); //Change permissions of memory.. for (int i = 0; i < opPatchLenght; i++) //repatch Valve's hook { *(Address + i) = valveHookBytes[i]; diff --git a/SteamTarget/VirtualControllerThread.h b/SteamTarget/VirtualControllerThread.h index 6ad3755..3565d02 100644 --- a/SteamTarget/VirtualControllerThread.h +++ b/SteamTarget/VirtualControllerThread.h @@ -74,7 +74,7 @@ private: static void controllerCallback(VIGEM_TARGET Target, UCHAR LargeMotor, UCHAR SmallMotor, UCHAR LedNumber); - static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); + static DWORD XInputGetStateWrapper(DWORD dwUserIndex, XINPUT_STATE* pState); //Easier to find in x64dbg... DWORD callRealXinputGetState(DWORD dwUserIndex, XINPUT_STATE* pState); From 0be26dce37529303b62874548b9288b8ab5f5f16 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Wed, 15 Feb 2017 21:49:45 +0100 Subject: [PATCH 6/6] Cleanup --- SteamTarget/VirtualControllerThread.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SteamTarget/VirtualControllerThread.cpp b/SteamTarget/VirtualControllerThread.cpp index f73e266..2e68278 100644 --- a/SteamTarget/VirtualControllerThread.cpp +++ b/SteamTarget/VirtualControllerThread.cpp @@ -64,7 +64,7 @@ void VirtualControllerThread::controllerLoop() { DWORD result; DWORD result2; - sf::Clock testTimer; + sf::Clock waitForHookTimer; while (bShouldRun) { sfClock.restart(); @@ -73,7 +73,7 @@ void VirtualControllerThread::controllerLoop() // otherwise the M$ compiler calls to a jumptable, jumping to the real function // We can't have this if we wan't to dynamically unpatch and repatch Valve's XInput hook // Also wait a second, jut to be sure Steam has done it's hooking thing... - if (XGetState == nullptr && testTimer.getElapsedTime().asSeconds() > 1) + if (XGetState == nullptr && waitForHookTimer.getElapsedTime().asSeconds() > 1) { HMODULE xinputmod = nullptr;