Advertisement
Guest User

Untitled

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