Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
131
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. BYTE* rawinputBuffer = new BYTE[2048]; // allocate byte array for raw input struct and assume 2048 btyes is enough
  32. UINT rawinputBufferSize = 2048;
  33.  
  34. LRESULT CALLBACK myWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
  35.     switch (message) {
  36.     case WM_INPUT: {
  37.         // copy data into raw input structure
  38.         if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, rawinputBuffer, &rawinputBufferSize, sizeof(RAWINPUTHEADER)) <= 0) {
  39.             return CallWindowProc(wndProc, hWnd, message, wParam, lParam);
  40.         }
  41.  
  42.         RAWINPUT* raw = (RAWINPUT*)rawinputBuffer;
  43.  
  44.         if (raw->header.dwType == RIM_TYPEMOUSE) {
  45.             deltaX -= raw->data.mouse.lLastX * xDegreesFor1Dot;
  46.             deltaY += raw->data.mouse.lLastY * yDegreesFor1Dot;
  47.  
  48.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN) {
  49.                 INPUT input[1];
  50.                 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;
  51.                 SendInput(1, input, sizeof(input[0]));
  52.             }
  53.  
  54.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP) {
  55.                 INPUT input[1];
  56.                 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;
  57.                 SendInput(1, input, sizeof(input[0]));
  58.             }
  59.  
  60.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN) {
  61.                 INPUT input[1];
  62.                 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;
  63.                 SendInput(1, input, sizeof(input[0]));
  64.             }
  65.  
  66.             if (raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP) {
  67.                 INPUT input[1];
  68.                 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;
  69.                 SendInput(1, input, sizeof(input[0]));
  70.             }
  71.         }
  72.  
  73.         break;
  74.     }
  75.     }
  76.  
  77.     return CallWindowProc(wndProc, hWnd, message, wParam, lParam);
  78. }
  79.  
  80. void myUpdateCrosshair(__int64 a1, __int64 a5, float* deltaYDegrees, float* deltaXDegrees, float a4, float a6) {
  81.     *deltaXDegrees = deltaX;
  82.     deltaX = 0;
  83.     *deltaYDegrees = deltaY;
  84.     deltaY = 0;
  85. }
  86.  
  87. void doHooks() {
  88.     if (MH_Initialize() != MH_OK) {
  89.         OutputDebugString("Failed to start MinHook");
  90.         return;
  91.     }
  92.  
  93.     BYTE* updateCrosshairAddress = ((BYTE*)GetModuleHandle(NULL)) + 0x1C9760;
  94.  
  95.     if (MH_CreateHook(updateCrosshairAddress, &myUpdateCrosshair, reinterpret_cast<LPVOID*>(&updateCrosshair)) != MH_OK) {
  96.         OutputDebugString("Failed to create hook");
  97.         return;
  98.     }
  99.  
  100.     if (MH_EnableHook(updateCrosshairAddress) != MH_OK) {
  101.         OutputDebugString("Failed to enable hook");
  102.         return;
  103.     }
  104.  
  105.     HWND hWnd = FindWindow("Lovecraft", NULL); // alternatively, enumerate windows by process or get it from WinMain's call to CreateWindowEx
  106.     // Message-only window with title "Client_MessageWindow" is not used in game
  107.     if (!hWnd) {
  108.         OutputDebugString("Couldn't find window handle");
  109.         return;
  110.     }
  111.     wndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)myWndProc); // hook can be replaced with GetMessage loop for raw input messages
  112. }
  113.  
  114. void registerMouseForRawInput(bool unregister) {
  115.     RAWINPUTDEVICE Rid[1];
  116.  
  117.     Rid[0].usUsagePage = 0x01;
  118.     Rid[0].usUsage = 0x02;
  119.     Rid[0].dwFlags = unregister ? RIDEV_REMOVE : 0; // RIDEV_NOLEGACY breaks raw input
  120.     Rid[0].hwndTarget = NULL;
  121.  
  122.     if (!RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]))) {
  123.         OutputDebugString("RegisterRawInputDevices failed");
  124.     }
  125. }
  126.  
  127. void start() {
  128.     std::string line;
  129.     std::ifstream file("sens.txt");
  130.     if (file.is_open()) {
  131.         getline(file, line,',');
  132.         sens = std::stof(line, NULL);
  133.         getline(file, line, ',');
  134.         yaw = std::stof(line, NULL);
  135.         getline(file, line, ',');
  136.         pitch = std::stof(line, NULL);
  137.         file.close();
  138.     } else {
  139.         OutputDebugString("Unable to open sens.txt");
  140.     }
  141.  
  142.     xDegreesFor1Dot = sens * yaw;
  143.     yDegreesFor1Dot = sens * pitch;
  144.  
  145.     doHooks();
  146.     registerMouseForRawInput(false);
  147.  
  148.     /*** This is a bad hack designed to fix the fact that dinput steals mouse control on alt-tab ***/
  149.     if (!RegisterHotKey(NULL, 1, 0, VK_F2)) {
  150.         OutputDebugString("Register hotkey failed");
  151.     }
  152.     MSG msg = { 0 };
  153.     while (GetMessage(&msg, NULL, 0, 0) != 0) {
  154.         if (msg.message == WM_HOTKEY && msg.wParam == 1) {
  155.             registerMouseForRawInput(true);
  156.             registerMouseForRawInput(false);
  157.         }
  158.     }
  159.  
  160.     delete[] rawinputBuffer;
  161. }
  162.  
  163. BOOL WINAPI DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) {
  164.     switch (ul_reason_for_call) {
  165.     case DLL_PROCESS_ATTACH: {
  166.         DisableThreadLibraryCalls(hModule);
  167.         CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&start, 0, 0, 0);
  168.         break;
  169.     }
  170.  
  171.     case DLL_THREAD_ATTACH: {
  172.         break;
  173.     }
  174.  
  175.     case DLL_THREAD_DETACH: {
  176.         break;
  177.     }
  178.  
  179.     case DLL_PROCESS_DETACH: {
  180.         break;
  181.     }
  182.     }
  183.     return TRUE;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement