2019-08-18 16:22:07 +02:00
|
|
|
#include "Windows_Hook.h"
|
2019-09-01 20:53:16 +02:00
|
|
|
#include "../Renderer_Detector.h"
|
|
|
|
#include "../../dll/dll.h"
|
2019-08-27 15:38:07 +02:00
|
|
|
|
2020-01-19 18:55:14 +01:00
|
|
|
#ifdef EMU_OVERLAY
|
2019-08-18 16:22:07 +02:00
|
|
|
|
|
|
|
#include <imgui.h>
|
2019-09-01 20:53:16 +02:00
|
|
|
#include <impls/windows/imgui_impl_win32.h>
|
2019-08-18 16:22:07 +02:00
|
|
|
|
|
|
|
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
Windows_Hook* Windows_Hook::_inst = nullptr;
|
2019-08-20 23:27:17 +02:00
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
bool Windows_Hook::start_hook()
|
|
|
|
{
|
2019-08-25 21:22:25 +02:00
|
|
|
bool res = true;
|
2019-08-27 15:38:07 +02:00
|
|
|
if (!hooked)
|
2019-08-18 16:22:07 +02:00
|
|
|
{
|
|
|
|
GetRawInputBuffer = ::GetRawInputBuffer;
|
|
|
|
GetRawInputData = ::GetRawInputData;
|
2021-02-07 04:38:51 +01:00
|
|
|
SetCursorPos = ::SetCursorPos;
|
2019-08-18 16:22:07 +02:00
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
PRINT_DEBUG("Hooked Windows\n");
|
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
BeginHook();
|
|
|
|
HookFuncs(
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer),
|
2021-02-07 04:38:51 +01:00
|
|
|
std::make_pair<void**, void*>(&(PVOID&)GetRawInputData , &Windows_Hook::MyGetRawInputData),
|
|
|
|
std::make_pair<void**, void*>(&(PVOID&)SetCursorPos , &Windows_Hook::MySetCursorPos)
|
2019-08-18 16:22:07 +02:00
|
|
|
);
|
|
|
|
EndHook();
|
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
hooked = true;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
2019-08-25 21:22:25 +02:00
|
|
|
return res;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Windows_Hook::resetRenderState()
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
{
|
|
|
|
initialized = false;
|
|
|
|
SetWindowLongPtr(_game_hwnd, GWLP_WNDPROC, (LONG_PTR)_game_wndproc);
|
|
|
|
_game_hwnd = nullptr;
|
|
|
|
_game_wndproc = nullptr;
|
|
|
|
ImGui_ImplWin32_Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Windows_Hook::prepareForOverlay(HWND hWnd)
|
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
ImGui_ImplWin32_Init(hWnd);
|
|
|
|
|
|
|
|
_game_hwnd = hWnd;
|
|
|
|
_game_wndproc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)&Windows_Hook::HookWndProc);
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui_ImplWin32_NewFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
HWND Windows_Hook::GetGameHwnd() const
|
|
|
|
{
|
|
|
|
return _game_hwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
WNDPROC Windows_Hook::GetGameWndProc() const
|
|
|
|
{
|
|
|
|
return _game_wndproc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Windows window hooks
|
|
|
|
bool IgnoreMsg(UINT uMsg)
|
|
|
|
{
|
|
|
|
switch (uMsg)
|
|
|
|
{
|
|
|
|
// Mouse Events
|
2019-10-15 11:11:30 +02:00
|
|
|
case WM_MOUSEMOVE:
|
|
|
|
case WM_MOUSEWHEEL: case WM_MOUSEHWHEEL:
|
|
|
|
case WM_LBUTTONUP: case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
|
|
|
|
case WM_RBUTTONUP: case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
|
|
|
|
case WM_MBUTTONUP: case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
|
|
|
|
case WM_XBUTTONUP: case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
|
|
|
|
case WM_MOUSEACTIVATE: case WM_MOUSEHOVER: case WM_MOUSELEAVE:
|
2019-08-18 16:22:07 +02:00
|
|
|
// Keyboard Events
|
2019-10-15 11:11:30 +02:00
|
|
|
case WM_KEYDOWN: case WM_KEYUP:
|
|
|
|
case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_SYSDEADCHAR:
|
|
|
|
case WM_CHAR: case WM_UNICHAR: case WM_DEADCHAR:
|
2019-08-18 16:22:07 +02:00
|
|
|
// Raw Input Events
|
2019-10-15 11:11:30 +02:00
|
|
|
case WM_INPUT:
|
|
|
|
return true;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-11 00:32:36 +02:00
|
|
|
void RawMouseEvent(RAWINPUT& raw)
|
|
|
|
{
|
|
|
|
if (raw.header.dwType == RIM_TYPEMOUSE)
|
|
|
|
{
|
|
|
|
if (raw.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_LBUTTONDOWN, 0, 0);
|
|
|
|
else if (raw.data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_LBUTTONUP, 0, 0);
|
|
|
|
else if (raw.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_DOWN)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_MBUTTONDOWN, 0, 0);
|
|
|
|
else if (raw.data.mouse.usButtonFlags & RI_MOUSE_MIDDLE_BUTTON_UP)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_MBUTTONUP, 0, 0);
|
|
|
|
else if (raw.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_RBUTTONDOWN, 0, 0);
|
|
|
|
else if (raw.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP)
|
|
|
|
ImGui_ImplWin32_WndProcHandler(Windows_Hook::Inst()->GetGameHwnd(), WM_RBUTTONUP, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
Steam_Overlay* overlay = get_steam_client()->steam_overlay;
|
2019-08-18 17:49:47 +02:00
|
|
|
bool show = overlay->ShowOverlay();
|
2019-08-18 16:22:07 +02:00
|
|
|
// Is the event is a key press
|
|
|
|
if (uMsg == WM_KEYDOWN)
|
|
|
|
{
|
|
|
|
// Tab is pressed and was not pressed before
|
|
|
|
if (wParam == VK_TAB && !(lParam & (1 << 30)))
|
|
|
|
{
|
|
|
|
// If Left Shift is pressed
|
|
|
|
if (GetAsyncKeyState(VK_LSHIFT) & (1 << 15))
|
2019-08-18 17:49:47 +02:00
|
|
|
{
|
2019-10-11 00:32:36 +02:00
|
|
|
show = !overlay->ShowOverlay();
|
|
|
|
overlay->ShowOverlay(show);
|
2019-08-18 17:49:47 +02:00
|
|
|
}
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 11:11:30 +02:00
|
|
|
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
|
2019-08-18 17:49:47 +02:00
|
|
|
if (show)
|
2019-08-18 16:22:07 +02:00
|
|
|
{
|
|
|
|
if (IgnoreMsg(uMsg))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the overlay window procedure
|
2019-08-27 15:38:07 +02:00
|
|
|
return CallWindowProc(Windows_Hook::Inst()->_game_wndproc, hWnd, uMsg, wParam, lParam);
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT WINAPI Windows_Hook::MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
|
|
|
|
{
|
2019-10-11 00:32:36 +02:00
|
|
|
if (pData == nullptr || !get_steam_client()->steam_overlay->ShowOverlay())
|
2019-08-27 15:38:07 +02:00
|
|
|
return Windows_Hook::Inst()->GetRawInputBuffer(pData, pcbSize, cbSizeHeader);
|
2019-08-18 16:22:07 +02:00
|
|
|
|
2019-10-11 00:32:36 +02:00
|
|
|
int num = Windows_Hook::Inst()->GetRawInputBuffer(pData, pcbSize, cbSizeHeader);
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
RawMouseEvent(pData[i]);
|
|
|
|
|
|
|
|
return 0;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
UINT WINAPI Windows_Hook::MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader)
|
|
|
|
{
|
2019-10-11 00:32:36 +02:00
|
|
|
if (pData == nullptr || !get_steam_client()->steam_overlay->ShowOverlay())
|
2019-08-27 15:38:07 +02:00
|
|
|
return Windows_Hook::Inst()->GetRawInputData(hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
|
2019-08-18 16:22:07 +02:00
|
|
|
|
2019-10-11 00:32:36 +02:00
|
|
|
Windows_Hook::Inst()->GetRawInputData(hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
|
|
|
|
|
|
|
|
RawMouseEvent(*reinterpret_cast<RAWINPUT*>(pData));
|
|
|
|
|
|
|
|
*pcbSize = 0;
|
|
|
|
|
|
|
|
return 0;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
2021-02-07 04:38:51 +01:00
|
|
|
|
|
|
|
BOOL WINAPI Windows_Hook::MySetCursorPos(int x, int y)
|
|
|
|
{
|
|
|
|
if (get_steam_client()->steam_overlay->ShowOverlay()) {
|
|
|
|
POINT p;
|
|
|
|
GetCursorPos(&p);
|
|
|
|
x = p.x;
|
|
|
|
y = p.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Windows_Hook::Inst()->SetCursorPos(x, y);
|
|
|
|
}
|
2019-08-18 16:22:07 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
Windows_Hook::Windows_Hook() :
|
|
|
|
initialized(false),
|
2019-08-27 15:38:07 +02:00
|
|
|
hooked(false),
|
2019-08-18 16:22:07 +02:00
|
|
|
_game_hwnd(nullptr),
|
|
|
|
_game_wndproc(nullptr),
|
|
|
|
GetRawInputBuffer(nullptr),
|
|
|
|
GetRawInputData(nullptr)
|
|
|
|
{
|
2019-08-25 21:22:25 +02:00
|
|
|
//_library = LoadLibrary(DLL_NAME);
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Windows_Hook::~Windows_Hook()
|
|
|
|
{
|
|
|
|
PRINT_DEBUG("Windows Hook removed\n");
|
|
|
|
|
|
|
|
resetRenderState();
|
2019-08-25 21:22:25 +02:00
|
|
|
|
|
|
|
//FreeLibrary(reinterpret_cast<HMODULE>(_library));
|
2019-08-27 15:38:07 +02:00
|
|
|
|
|
|
|
_inst = nullptr;
|
2019-08-18 16:22:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 15:38:07 +02:00
|
|
|
Windows_Hook* Windows_Hook::Inst()
|
2019-08-18 16:22:07 +02:00
|
|
|
{
|
2019-08-27 15:38:07 +02:00
|
|
|
if (_inst == nullptr)
|
|
|
|
_inst = new Windows_Hook;
|
|
|
|
|
2019-08-18 16:22:07 +02:00
|
|
|
return _inst;
|
2019-08-26 16:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* Windows_Hook::get_lib_name() const
|
|
|
|
{
|
2020-07-16 11:56:24 +02:00
|
|
|
return WINDOWS_DLL;
|
2019-08-27 15:38:07 +02:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:55:14 +01:00
|
|
|
#endif//EMU_OVERLAY
|