Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. // dllmain.cpp : Defines the entry point for the DLL application.
  2. #include "pch.h"
  3. #include <Windows.h>
  4. #include <iostream>
  5. #include "memory.h"
  6. #include <iostream>
  7. #include <Windows.h>
  8. #include <Xinput.h>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. #define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
  14.  
  15. class Gamepad
  16. {
  17. private:
  18. int cId;
  19. XINPUT_STATE state;
  20.  
  21. float deadzoneX;
  22. float deadzoneY;
  23.  
  24. public:
  25. Gamepad() : deadzoneX(0.05f), deadzoneY(0.02f) {}
  26. Gamepad(float dzX, float dzY) : deadzoneX(dzX), deadzoneY(dzY) {}
  27.  
  28. float leftStickX;
  29. float leftStickY;
  30. float rightStickX;
  31. float rightStickY;
  32. float leftTrigger;
  33. float rightTrigger;
  34.  
  35. int GetPort();
  36. XINPUT_GAMEPAD* GetState();
  37. bool CheckConnection();
  38. bool Refresh();
  39. bool IsPressed(WORD);
  40. };
  41.  
  42. int Gamepad::GetPort()
  43. {
  44. return cId + 1;
  45. }
  46.  
  47. XINPUT_GAMEPAD* Gamepad::GetState()
  48. {
  49. return &state.Gamepad;
  50. }
  51.  
  52. bool Gamepad::CheckConnection()
  53. {
  54. int controllerId = -1;
  55.  
  56. for (DWORD i = 0; i < XUSER_MAX_COUNT && controllerId == -1; i++)
  57. {
  58. XINPUT_STATE state;
  59. ZeroMemory(&state, sizeof(XINPUT_STATE));
  60.  
  61. if (XInputGetState(i, &state) == ERROR_SUCCESS)
  62. controllerId = i;
  63. }
  64.  
  65. cId = controllerId;
  66.  
  67. return controllerId != -1;
  68. }
  69.  
  70. // Returns false if the controller has been disconnected
  71. bool Gamepad::Refresh()
  72. {
  73. if (cId == -1)
  74. CheckConnection();
  75.  
  76. if (cId != -1)
  77. {
  78. ZeroMemory(&state, sizeof(XINPUT_STATE));
  79. if (XInputGetState(cId, &state) != ERROR_SUCCESS)
  80. {
  81. cId = -1;
  82. return false;
  83. }
  84.  
  85. float normLX = fmaxf(-1, (float)state.Gamepad.sThumbLX / 32767);
  86. float normLY = fmaxf(-1, (float)state.Gamepad.sThumbLY / 32767);
  87.  
  88. leftStickX = (abs(normLX) < deadzoneX ? 0 : (abs(normLX) - deadzoneX) * (normLX / abs(normLX)));
  89. leftStickY = (abs(normLY) < deadzoneY ? 0 : (abs(normLY) - deadzoneY) * (normLY / abs(normLY)));
  90.  
  91. if (deadzoneX > 0) leftStickX *= 1 / (1 - deadzoneX);
  92. if (deadzoneY > 0) leftStickY *= 1 / (1 - deadzoneY);
  93.  
  94. float normRX = fmaxf(-1, (float)state.Gamepad.sThumbRX / 32767);
  95. float normRY = fmaxf(-1, (float)state.Gamepad.sThumbRY / 32767);
  96.  
  97. rightStickX = (abs(normRX) < deadzoneX ? 0 : (abs(normRX) - deadzoneX) * (normRX / abs(normRX)));
  98. rightStickY = (abs(normRY) < deadzoneY ? 0 : (abs(normRY) - deadzoneY) * (normRY / abs(normRY)));
  99.  
  100. if (deadzoneX > 0) rightStickX *= 1 / (1 - deadzoneX);
  101. if (deadzoneY > 0) rightStickY *= 1 / (1 - deadzoneY);
  102.  
  103. leftTrigger = (float)state.Gamepad.bLeftTrigger / 255;
  104. rightTrigger = (float)state.Gamepad.bRightTrigger / 255;
  105.  
  106. return true;
  107. }
  108. return false;
  109. }
  110.  
  111. bool Gamepad::IsPressed(WORD button)
  112. {
  113. return (state.Gamepad.wButtons & button) != 0;
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. void ChangeMemory(DWORD baseadress, int value, DWORD offset1, DWORD offset2, bool msg)
  127. {
  128. DWORD d, ds;
  129. DWORD* adress = (DWORD*)((*(DWORD*)(baseadress + offset1)) + offset2);
  130.  
  131. if (msg)
  132. {
  133. char szTest[10];
  134. sprintf_s(szTest, "The final adress is : %X", adress);
  135. MessageBoxA(NULL, szTest, NULL, NULL);
  136. }
  137.  
  138. *(int*)adress = value;
  139. }
  140.  
  141.  
  142. __declspec(dllexport) int Patch(void)
  143. {
  144. Gamepad gamepad;
  145.  
  146. bool wasConnected = true;
  147.  
  148.  
  149.  
  150. while (true)
  151. {
  152. Sleep(10);
  153.  
  154. if (!gamepad.Refresh())
  155. {
  156. if (wasConnected)
  157. {
  158. wasConnected = false;
  159.  
  160. //cout << "Please connect an Xbox 360 controller." << endl;
  161. }
  162. }
  163. else
  164. {
  165. if (!wasConnected)
  166. {
  167. wasConnected = true;
  168.  
  169. //cout << "Controller connected on port " << gamepad.GetPort() << endl;
  170. }
  171.  
  172. //cout << "Left thumb stick: (" << gamepad.leftStickX << ", " << gamepad.leftStickY << ") Right thumb stick : (" << gamepad.rightStickX << ", " << gamepad.rightStickY << ")" << endl;
  173.  
  174. //cout << "Left analog trigger: " << gamepad.leftTrigger << " Right analog trigger: " << gamepad.rightTrigger << endl;
  175.  
  176. if (gamepad.rightTrigger > 0) {
  177. ChangeMemory(0x00400000, 1, 0x000CCBB4, 0x148, false);
  178. }
  179.  
  180. if (gamepad.IsPressed(XINPUT_GAMEPAD_A)) cout << "(A) button pressed" << endl;
  181. }
  182. }
  183.  
  184. return 0;
  185. }
  186.  
  187. BOOL APIENTRY DllMain(HMODULE hModule,
  188. DWORD ul_reason_for_call,
  189. LPVOID lpReserved
  190. )
  191. {
  192. switch (ul_reason_for_call)
  193. {
  194. case DLL_PROCESS_ATTACH:
  195. CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Patch, 0, 0, 0);
  196. break;
  197. case DLL_THREAD_ATTACH:
  198. case DLL_THREAD_DETACH:
  199. case DLL_PROCESS_DETACH:
  200. break;
  201. }
  202. return TRUE;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement