Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dllmain.cpp : Defines the entry point for the DLL application.
- #include "pch.h"
- #include <Windows.h>
- #include <iostream>
- #include "memory.h"
- #include <iostream>
- #include <Windows.h>
- #include <Xinput.h>
- using namespace std;
- #define EXTERN_DLL_EXPORT extern "C" __declspec(dllexport)
- class Gamepad
- {
- private:
- int cId;
- XINPUT_STATE state;
- float deadzoneX;
- float deadzoneY;
- public:
- Gamepad() : deadzoneX(0.05f), deadzoneY(0.02f) {}
- Gamepad(float dzX, float dzY) : deadzoneX(dzX), deadzoneY(dzY) {}
- float leftStickX;
- float leftStickY;
- float rightStickX;
- float rightStickY;
- float leftTrigger;
- float rightTrigger;
- int GetPort();
- XINPUT_GAMEPAD* GetState();
- bool CheckConnection();
- bool Refresh();
- bool IsPressed(WORD);
- };
- int Gamepad::GetPort()
- {
- return cId + 1;
- }
- XINPUT_GAMEPAD* Gamepad::GetState()
- {
- return &state.Gamepad;
- }
- bool Gamepad::CheckConnection()
- {
- int controllerId = -1;
- for (DWORD i = 0; i < XUSER_MAX_COUNT && controllerId == -1; i++)
- {
- XINPUT_STATE state;
- ZeroMemory(&state, sizeof(XINPUT_STATE));
- if (XInputGetState(i, &state) == ERROR_SUCCESS)
- controllerId = i;
- }
- cId = controllerId;
- return controllerId != -1;
- }
- // Returns false if the controller has been disconnected
- bool Gamepad::Refresh()
- {
- if (cId == -1)
- CheckConnection();
- if (cId != -1)
- {
- ZeroMemory(&state, sizeof(XINPUT_STATE));
- if (XInputGetState(cId, &state) != ERROR_SUCCESS)
- {
- cId = -1;
- return false;
- }
- float normLX = fmaxf(-1, (float)state.Gamepad.sThumbLX / 32767);
- float normLY = fmaxf(-1, (float)state.Gamepad.sThumbLY / 32767);
- leftStickX = (abs(normLX) < deadzoneX ? 0 : (abs(normLX) - deadzoneX) * (normLX / abs(normLX)));
- leftStickY = (abs(normLY) < deadzoneY ? 0 : (abs(normLY) - deadzoneY) * (normLY / abs(normLY)));
- if (deadzoneX > 0) leftStickX *= 1 / (1 - deadzoneX);
- if (deadzoneY > 0) leftStickY *= 1 / (1 - deadzoneY);
- float normRX = fmaxf(-1, (float)state.Gamepad.sThumbRX / 32767);
- float normRY = fmaxf(-1, (float)state.Gamepad.sThumbRY / 32767);
- rightStickX = (abs(normRX) < deadzoneX ? 0 : (abs(normRX) - deadzoneX) * (normRX / abs(normRX)));
- rightStickY = (abs(normRY) < deadzoneY ? 0 : (abs(normRY) - deadzoneY) * (normRY / abs(normRY)));
- if (deadzoneX > 0) rightStickX *= 1 / (1 - deadzoneX);
- if (deadzoneY > 0) rightStickY *= 1 / (1 - deadzoneY);
- leftTrigger = (float)state.Gamepad.bLeftTrigger / 255;
- rightTrigger = (float)state.Gamepad.bRightTrigger / 255;
- return true;
- }
- return false;
- }
- bool Gamepad::IsPressed(WORD button)
- {
- return (state.Gamepad.wButtons & button) != 0;
- }
- void ChangeMemory(DWORD baseadress, int value, DWORD offset1, DWORD offset2, bool msg)
- {
- DWORD d, ds;
- DWORD* adress = (DWORD*)((*(DWORD*)(baseadress + offset1)) + offset2);
- if (msg)
- {
- char szTest[10];
- sprintf_s(szTest, "The final adress is : %X", adress);
- MessageBoxA(NULL, szTest, NULL, NULL);
- }
- *(int*)adress = value;
- }
- __declspec(dllexport) int Patch(void)
- {
- Gamepad gamepad;
- bool wasConnected = true;
- while (true)
- {
- Sleep(10);
- if (!gamepad.Refresh())
- {
- if (wasConnected)
- {
- wasConnected = false;
- //cout << "Please connect an Xbox 360 controller." << endl;
- }
- }
- else
- {
- if (!wasConnected)
- {
- wasConnected = true;
- //cout << "Controller connected on port " << gamepad.GetPort() << endl;
- }
- //cout << "Left thumb stick: (" << gamepad.leftStickX << ", " << gamepad.leftStickY << ") Right thumb stick : (" << gamepad.rightStickX << ", " << gamepad.rightStickY << ")" << endl;
- //cout << "Left analog trigger: " << gamepad.leftTrigger << " Right analog trigger: " << gamepad.rightTrigger << endl;
- if (gamepad.rightTrigger > 0) {
- ChangeMemory(0x00400000, 1, 0x000CCBB4, 0x148, false);
- }
- if (gamepad.IsPressed(XINPUT_GAMEPAD_A)) cout << "(A) button pressed" << endl;
- }
- }
- return 0;
- }
- BOOL APIENTRY DllMain(HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
- {
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Patch, 0, 0, 0);
- break;
- case DLL_THREAD_ATTACH:
- case DLL_THREAD_DETACH:
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement