Advertisement
Guest User

QC Raw Input

a guest
Dec 16th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Visual Studio solution: x64, no precompiled headers, -MT, MinHook https://github.com/TsudaKageyu/minhook/issues/62
  2. #undef UNICODE
  3. #include "MinHook.h"
  4. #include <Windows.h>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. #if defined _M_X64
  10. #pragma comment(lib, "libMinHook.x64.lib")
  11. #elif defined _M_IX86
  12. #pragma comment(lib, "libMinHook.x86.lib")
  13. #endif
  14.  
  15. WNDPROC wndProc = NULL;
  16. void (*updateCrosshair)(__int64, __int64, float*, float*, float, float) = NULL;
  17.  
  18. float sens = 1.0f;
  19. float yaw = 0.022f;
  20. float pitch = 0.022f;
  21. float xDegreesFor1Dot = 0;
  22. float yDegreesFor1Dot = 0;
  23. float deltaX = 0;
  24. float deltaY = 0;
  25.  
  26. const WORD num1 = 0x4f;
  27. const WORD num2 = 0x50;
  28. const WORD num3 = 0x51;
  29. const WORD num4 = 0x4b;
  30.  
  31. LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  32.     switch (message) {
  33.     case WM_INPUT: {
  34.         // get size of raw input structure
  35.         UINT rawinputBufferSize;
  36.         GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &rawinputBufferSize, sizeof(RAWINPUTHEADER));
  37.  
  38.         // allocate byte array for raw input struct
  39.         BYTE* rawinputBuffer = new BYTE[rawinputBufferSize];
  40.         if (!rawinputBuffer) return CallWindowProc(wndProc, hWnd, message, wParam, lParam);
  41.  
  42.         // copy data into raw input structure
  43.         if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawinputBuffer, &rawinputBufferSize, sizeof(RAWINPUTHEADER)) != rawinputBufferSize) {
  44.             OutputDebugString("GetRawInputData does not return correct size!");
  45.         }
  46.  
  47.         RAWINPUT* raw = (RAWINPUT*)rawinputBuffer;
  48.  
  49.         if (raw->header.dwType == RIM_TYPEMOUSE) {
  50.             deltaX -= raw->data.mouse.lLastX * xDegreesFor1Dot;
  51.             deltaY += raw->data.mouse.lLastY * yDegreesFor1Dot;
  52.  
  53.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN) {
  54.                 INPUT input[1];
  55.                 input[0].type = INPUT_KEYBOARD; input[0].ki.wScan = num1; input[0].ki.dwFlags = KEYEVENTF_SCANCODE; input[0].ki.time = 0; input[0].ki.dwExtraInfo = 0;
  56.                 SendInput(1, input, sizeof(input[0]));
  57.             }
  58.  
  59.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP) {
  60.                 INPUT input[1];
  61.                 input[0].type = INPUT_KEYBOARD; input[0].ki.wScan = num1; input[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; input[0].ki.time = 0; input[0].ki.dwExtraInfo = 0;
  62.                 SendInput(1, input, sizeof(input[0]));
  63.             }
  64.  
  65.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN) {
  66.                 INPUT input[1];
  67.                 input[0].type = INPUT_KEYBOARD; input[0].ki.wScan = num2; input[0].ki.dwFlags = KEYEVENTF_SCANCODE; input[0].ki.time = 0; input[0].ki.dwExtraInfo = 0;
  68.                 SendInput(1, input, sizeof(input[0]));
  69.             }
  70.  
  71.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP) {
  72.                 INPUT input[1];
  73.                 input[0].type = INPUT_KEYBOARD; input[0].ki.wScan = num2; input[0].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; input[0].ki.time = 0; input[0].ki.dwExtraInfo = 0;
  74.                 SendInput(1, input, sizeof(input[0]));
  75.             }
  76.         }
  77.  
  78.         delete[] rawinputBuffer;
  79.         break;
  80.     }
  81.     }
  82.  
  83.     return CallWindowProc(wndProc, hWnd, message, wParam, lParam);
  84. }
  85.  
  86. void myUpdateCrosshair(__int64 a1, __int64 a5, float* deltaYDegrees, float* deltaXDegrees, float a4, float a6) {
  87.     *deltaXDegrees = deltaX;
  88.     deltaX = 0;
  89.     *deltaYDegrees = deltaY;
  90.     deltaY = 0;
  91. }
  92.  
  93. void doHooks() {
  94.     if (MH_Initialize() != MH_OK) {
  95.         OutputDebugString("Failed to start MinHook");
  96.         return;
  97.     }
  98.  
  99.     BYTE* updateCrosshairAddress = ((BYTE*)GetModuleHandle(NULL)) + 0x1C9760;
  100.  
  101.     if (MH_CreateHook(updateCrosshairAddress, &myUpdateCrosshair, reinterpret_cast<LPVOID*>(&updateCrosshair)) != MH_OK) {
  102.         OutputDebugString("Failed to create hook");
  103.         return;
  104.     }
  105.  
  106.     if (MH_EnableHook(updateCrosshairAddress) != MH_OK) {
  107.         OutputDebugString("Failed to enable hook");
  108.         return;
  109.     }
  110.  
  111.     HWND hWnd = FindWindow("Lovecraft", NULL); // alternatively, enumerate windows by process or get it from WinMain's call to CreateWindowEx
  112.     // Message-only window with title "Client_MessageWindow" is not used in game
  113.     if (!hWnd) {
  114.         OutputDebugString("Couldn't find window handle");
  115.         return;
  116.     }
  117.     wndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)myWndProc); // hook can be replaced with GetMessage loop for raw input messages
  118. }
  119.  
  120. void registerMouseForRawInput(bool unregister) {
  121.     RAWINPUTDEVICE Rid[1];
  122.  
  123.     Rid[0].usUsagePage = 0x01;
  124.     Rid[0].usUsage = 0x02;
  125.     Rid[0].dwFlags = unregister ? RIDEV_REMOVE : 0; // RIDEV_NOLEGACY breaks raw input
  126.     Rid[0].hwndTarget = NULL;
  127.  
  128.     if (!RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))) {
  129.         OutputDebugString("RegisterRawInputDevices failed");
  130.     }
  131. }
  132.  
  133. void start() {
  134.     std::string line;
  135.     std::ifstream file("sens.txt");
  136.     if (file.is_open()) {
  137.         getline(file, line,',');
  138.         sens = std::stof(line, NULL);
  139.         getline(file, line, ',');
  140.         yaw = std::stof(line, NULL);
  141.         getline(file, line, ',');
  142.         pitch = std::stof(line, NULL);
  143.         file.close();
  144.     } else {
  145.         OutputDebugString("Unable to open sens.txt");
  146.     }
  147.  
  148.     xDegreesFor1Dot = sens * yaw;
  149.     yDegreesFor1Dot = sens * pitch;
  150.  
  151.     doHooks();
  152.     registerMouseForRawInput(false);
  153.  
  154.     /*** This is a bad hack designed to fix the fact that dinput steals mouse control on alt-tab ***/
  155.     if (!RegisterHotKey(NULL, 1, 0, VK_F2)) {
  156.         OutputDebugString("Register hotkey failed");
  157.     }
  158.     MSG msg = { 0 };
  159.     while (GetMessage(&msg, NULL, 0, 0) != 0) {
  160.         if (msg.message == WM_HOTKEY && msg.wParam == 1) {
  161.             registerMouseForRawInput(true);
  162.             registerMouseForRawInput(false);
  163.         }
  164.     }
  165. }
  166.  
  167. BOOL WINAPI DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) {
  168.     switch (ul_reason_for_call) {
  169.     case DLL_PROCESS_ATTACH: {
  170.         DisableThreadLibraryCalls(hModule);
  171.         CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&start, 0, 0, 0);
  172.         break;
  173.     }
  174.  
  175.     case DLL_THREAD_ATTACH: {
  176.         break;
  177.     }
  178.  
  179.     case DLL_THREAD_DETACH: {
  180.         break;
  181.     }
  182.  
  183.     case DLL_PROCESS_DETACH: {
  184.         break;
  185.     }
  186.     }
  187.     return TRUE;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement