Moved window hooks to its own class

Every class has a job. Overlay is to show an overlay not to hook windows functions.
Hook_Manager's job is to manage hooks and should not contain a reference to the overlay. (It should not contains any reference to directX or OpenGL either but I'll see that later... Maybe)
This makes the overlay code much cleaner and "could" be used in Linux as well.
This commit is contained in:
Nemirtingas 2019-08-18 16:22:07 +02:00
parent f096a2d8a2
commit 5b0306dccc
11 changed files with 281 additions and 201 deletions

View file

@ -1,12 +1,11 @@
#include "../dll/base.h" #include "DX10_Hook.h"
#include "Windows_Hook.h"
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#include "DX10_Hook.h"
#include "Hook_Manager.h"
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_dx10.h> #include <impls/imgui_impl_dx10.h>
DX10_Hook* DX10_Hook::_inst = nullptr; DX10_Hook* DX10_Hook::_inst = nullptr;
@ -21,7 +20,7 @@ bool DX10_Hook::start_hook()
ID3D10Device* pDevice; ID3D10Device* pDevice;
DXGI_SWAP_CHAIN_DESC SwapChainDesc = {}; DXGI_SWAP_CHAIN_DESC SwapChainDesc = {};
decltype(D3D10CreateDeviceAndSwapChain)* D3D10CreateDeviceAndSwapChain = decltype(D3D10CreateDeviceAndSwapChain)* D3D10CreateDeviceAndSwapChain =
(decltype(D3D10CreateDeviceAndSwapChain))GetProcAddress(_dll, "D3D10CreateDeviceAndSwapChain"); (decltype(D3D10CreateDeviceAndSwapChain))GetProcAddress(reinterpret_cast<HMODULE>(_library), "D3D10CreateDeviceAndSwapChain");
SwapChainDesc.BufferCount = 1; SwapChainDesc.BufferCount = 1;
SwapChainDesc.BufferDesc.Width = 1; SwapChainDesc.BufferDesc.Width = 1;
SwapChainDesc.BufferDesc.Height = 1; SwapChainDesc.BufferDesc.Height = 1;
@ -50,6 +49,9 @@ bool DX10_Hook::start_hook()
std::make_pair<void**, void*>(&(PVOID&)DX10_Hook::ResizeBuffers, &DX10_Hook::MyResizeBuffers) std::make_pair<void**, void*>(&(PVOID&)DX10_Hook::ResizeBuffers, &DX10_Hook::MyResizeBuffers)
); );
EndHook(); EndHook();
if (Windows_Hook::Inst().start_hook())
get_steam_client()->steam_overlay->HookReady();
} }
else else
{ {
@ -69,7 +71,7 @@ void DX10_Hook::resetRenderState()
mainRenderTargetView->Release(); mainRenderTargetView->Release();
ImGui_ImplDX10_Shutdown(); ImGui_ImplDX10_Shutdown();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
initialized = false; initialized = false;
@ -97,19 +99,17 @@ void DX10_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView); pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
pBackBuffer->Release(); pBackBuffer->Release();
ImGui_ImplWin32_Init(desc.OutputWindow);
ImGui_ImplDX10_Init(pDevice); ImGui_ImplDX10_Init(pDevice);
Hook_Manager::Inst().ChangeGameWindow(desc.OutputWindow);
initialized = true; initialized = true;
} }
ImGui_ImplDX10_NewFrame(); ImGui_ImplDX10_NewFrame();
ImGui_ImplWin32_NewFrame(); Windows_Hook::Inst().prepareForOverlay(desc.OutputWindow);
ImGui::NewFrame(); ImGui::NewFrame();
Hook_Manager::Inst().CallOverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height); get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height);
ImGui::EndFrame(); ImGui::EndFrame();

View file

@ -1,12 +1,11 @@
#include "../dll/base.h" #include "DX11_Hook.h"
#include "Windows_Hook.h"
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#include "DX11_Hook.h"
#include "Hook_Manager.h"
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_dx11.h> #include <impls/imgui_impl_dx11.h>
DX11_Hook* DX11_Hook::_inst = nullptr; DX11_Hook* DX11_Hook::_inst = nullptr;
@ -31,7 +30,7 @@ bool DX11_Hook::start_hook()
ID3D11Device* pDevice; ID3D11Device* pDevice;
DXGI_SWAP_CHAIN_DESC SwapChainDesc = {}; DXGI_SWAP_CHAIN_DESC SwapChainDesc = {};
decltype(D3D11CreateDeviceAndSwapChain)* D3D11CreateDeviceAndSwapChain = decltype(D3D11CreateDeviceAndSwapChain)* D3D11CreateDeviceAndSwapChain =
(decltype(D3D11CreateDeviceAndSwapChain))GetProcAddress(_dll, "D3D11CreateDeviceAndSwapChain"); (decltype(D3D11CreateDeviceAndSwapChain))GetProcAddress(reinterpret_cast<HMODULE>(_library), "D3D11CreateDeviceAndSwapChain");
SwapChainDesc.BufferCount = 1; SwapChainDesc.BufferCount = 1;
SwapChainDesc.BufferDesc.Width = 1; SwapChainDesc.BufferDesc.Width = 1;
SwapChainDesc.BufferDesc.Height = 1; SwapChainDesc.BufferDesc.Height = 1;
@ -60,6 +59,9 @@ bool DX11_Hook::start_hook()
std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeBuffers, &DX11_Hook::MyResizeBuffers) std::make_pair<void**, void*>(&(PVOID&)DX11_Hook::ResizeBuffers, &DX11_Hook::MyResizeBuffers)
); );
EndHook(); EndHook();
if (Windows_Hook::Inst().start_hook())
get_steam_client()->steam_overlay->HookReady();
} }
else else
{ {
@ -81,7 +83,7 @@ void DX11_Hook::resetRenderState()
pContext->Release(); pContext->Release();
ImGui_ImplDX11_Shutdown(); ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
initialized = false; initialized = false;
@ -110,19 +112,17 @@ void DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView); pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
pBackBuffer->Release(); pBackBuffer->Release();
ImGui_ImplWin32_Init(desc.OutputWindow);
ImGui_ImplDX11_Init(pDevice, pContext); ImGui_ImplDX11_Init(pDevice, pContext);
Hook_Manager::Inst().ChangeGameWindow(desc.OutputWindow);
initialized = true; initialized = true;
} }
ImGui_ImplDX11_NewFrame(); ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame(); Windows_Hook::Inst().prepareForOverlay(desc.OutputWindow);
ImGui::NewFrame(); ImGui::NewFrame();
Hook_Manager::Inst().CallOverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height); get_steam_client()->steam_overlay->OverlayProc(desc.BufferDesc.Width, desc.BufferDesc.Height);
ImGui::EndFrame(); ImGui::EndFrame();

View file

@ -1,12 +1,11 @@
#include "../dll/base.h" #include "DX12_Hook.h"
#include "Windows_Hook.h"
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#include "DX12_Hook.h"
#include "Hook_Manager.h"
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_dx12.h> #include <impls/imgui_impl_dx12.h>
DX12_Hook* DX12_Hook::_inst = nullptr; DX12_Hook* DX12_Hook::_inst = nullptr;
@ -30,7 +29,7 @@ void DX12_Hook::resetRenderState()
pDescriptorHeap->Release(); pDescriptorHeap->Release();
ImGui_ImplDX12_Shutdown(); ImGui_ImplDX12_Shutdown();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
initialized = false; initialized = false;
@ -40,6 +39,7 @@ void DX12_Hook::resetRenderState()
// Try to make this function and overlay's proc as short as possible or it might affect game's fps. // Try to make this function and overlay's proc as short as possible or it might affect game's fps.
void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain) void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
{ {
/*
DXGI_SWAP_CHAIN_DESC desc; DXGI_SWAP_CHAIN_DESC desc;
pSwapChain->GetDesc(&desc); pSwapChain->GetDesc(&desc);
@ -93,6 +93,7 @@ void DX12_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain)
pCmdList->OMSetRenderTargets(1, &mainRenderTargetDescriptor, FALSE, NULL); pCmdList->OMSetRenderTargets(1, &mainRenderTargetDescriptor, FALSE, NULL);
pCmdList->SetDescriptorHeaps(1, &pDescriptorHeap); pCmdList->SetDescriptorHeaps(1, &pDescriptorHeap);
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), pCmdList); ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), pCmdList);
*/
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////

View file

@ -1,11 +1,11 @@
#include "DX9_Hook.h" #include "DX9_Hook.h"
#include "Windows_Hook.h"
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#include "Hook_Manager.h"
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_dx9.h> #include <impls/imgui_impl_dx9.h>
#include "steam_overlay.h" #include "steam_overlay.h"
@ -46,6 +46,9 @@ bool DX9_Hook::start_hook()
//std::make_pair<void**, void*>(&(PVOID&)EndScene, &DX9_Hook::MyEndScene) //std::make_pair<void**, void*>(&(PVOID&)EndScene, &DX9_Hook::MyEndScene)
); );
EndHook(); EndHook();
if (Windows_Hook::Inst().start_hook())
get_steam_client()->steam_overlay->HookReady();
} }
else else
{ {
@ -65,7 +68,7 @@ void DX9_Hook::resetRenderState()
{ {
initialized = false; initialized = false;
ImGui_ImplDX9_Shutdown(); ImGui_ImplDX9_Shutdown();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
} }
} }
@ -83,7 +86,7 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
pDevice->GetCreationParameters(&param); pDevice->GetCreationParameters(&param);
// Workaround to detect if we changed window. // Workaround to detect if we changed window.
if (param.hFocusWindow != Hook_Manager::Inst().GetOverlay()->GetGameHwnd()) if (param.hFocusWindow != Windows_Hook::Inst().GetGameHwnd())
resetRenderState(); resetRenderState();
if (!initialized) if (!initialized)
@ -92,19 +95,16 @@ void DX9_Hook::prepareForOverlay(IDirect3DDevice9 *pDevice)
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL; io.IniFilename = NULL;
ImGui_ImplWin32_Init(param.hFocusWindow);
ImGui_ImplDX9_Init(pDevice); ImGui_ImplDX9_Init(pDevice);
Hook_Manager::Inst().ChangeGameWindow(param.hFocusWindow);
initialized = true; initialized = true;
} }
ImGui_ImplDX9_NewFrame(); ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame(); Windows_Hook::Inst().prepareForOverlay(param.hFocusWindow);
ImGui::NewFrame(); ImGui::NewFrame();
Hook_Manager::Inst().CallOverlayProc(PresentParameters.BackBufferWidth, PresentParameters.BackBufferHeight); get_steam_client()->steam_overlay->OverlayProc(PresentParameters.BackBufferWidth, PresentParameters.BackBufferHeight);
ImGui::EndFrame(); ImGui::EndFrame();
@ -195,7 +195,7 @@ DX9_Hook::~DX9_Hook()
//ImGui_ImplDX9_Shutdown(); This makes some games hang when Releasing the D3D9 device (pDevice->Release()) //ImGui_ImplDX9_Shutdown(); This makes some games hang when Releasing the D3D9 device (pDevice->Release())
// maybe because D3D is already shut down when we try to free the device? // maybe because D3D is already shut down when we try to free the device?
ImGui_ImplDX9_InvalidateDeviceObjects(); ImGui_ImplDX9_InvalidateDeviceObjects();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
} }

View file

@ -39,7 +39,7 @@ HRESULT STDMETHODCALLTYPE Hook_Manager::MyIDXGISwapChain_Present(IDXGISwapChain*
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -57,7 +57,7 @@ HRESULT STDMETHODCALLTYPE Hook_Manager::MyIDXGISwapChain_Present(IDXGISwapChain*
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -72,7 +72,7 @@ HRESULT STDMETHODCALLTYPE Hook_Manager::MyIDXGISwapChain_Present(IDXGISwapChain*
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -93,7 +93,7 @@ HRESULT STDMETHODCALLTYPE Hook_Manager::MyPresent(IDirect3DDevice9* _this, CONST
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -110,7 +110,7 @@ HRESULT STDMETHODCALLTYPE Hook_Manager::MyPresentEx(IDirect3DDevice9Ex* _this, C
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -127,7 +127,7 @@ BOOL WINAPI Hook_Manager::MywglMakeCurrent(HDC hDC, HGLRC hGLRC)
{ {
// Hook failed, start over // Hook failed, start over
delete static_cast<Base_Hook*>(hook); delete static_cast<Base_Hook*>(hook);
Hook_Manager::Inst().HookRenderer(Hook_Manager::Inst().GetOverlay()); Hook_Manager::Inst().HookRenderer();
} }
else else
{ {
@ -391,16 +391,6 @@ void Hook_Manager::HookLoadLibrary()
} }
} }
void Hook_Manager::ChangeGameWindow(HWND hWnd) const
{
overlay->HookReady(hWnd);
}
void Hook_Manager::CallOverlayProc(int width, int height) const
{
overlay->OverlayProc(width, height);
}
#endif #endif
void Hook_Manager::UnHookAllRendererDetector() void Hook_Manager::UnHookAllRendererDetector()
@ -422,8 +412,6 @@ void Hook_Manager::UnHookAllRendererDetector()
Hook_Manager::Hook_Manager(): Hook_Manager::Hook_Manager():
#ifdef STEAM_WIN32 #ifdef STEAM_WIN32
_game_hwnd(nullptr),
_game_wndproc(nullptr),
_loadlibrary_hooked(false), _loadlibrary_hooked(false),
_dxgi_hooked(false), _dxgi_hooked(false),
_dx9_hooked(false), _dx9_hooked(false),
@ -444,9 +432,8 @@ Hook_Manager& Hook_Manager::Inst()
return hook; return hook;
} }
void Hook_Manager::HookRenderer(Steam_Overlay *ovlay) void Hook_Manager::HookRenderer()
{ {
overlay = ovlay;
#ifdef STEAM_WIN32 #ifdef STEAM_WIN32
HookLoadLibrary(); HookLoadLibrary();
std::vector<std::string> const libraries = { "opengl32.dll", "d3d12.dll", "d3d11.dll", "d3d10.dll", "d3d9.dll" }; std::vector<std::string> const libraries = { "opengl32.dll", "d3d12.dll", "d3d11.dll", "d3d10.dll", "d3d9.dll" };

View file

@ -46,8 +46,6 @@ protected:
void HookLoadLibrary(); void HookLoadLibrary();
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
WNDPROC _game_wndproc; // The game main window proc
HWND _game_hwnd; // The game main window
bool _loadlibrary_hooked; // Are the LoadLibrary functions hooked ? bool _loadlibrary_hooked; // Are the LoadLibrary functions hooked ?
bool _dx9_hooked; // DX9 Present and PresentEx Hooked ? bool _dx9_hooked; // DX9 Present and PresentEx Hooked ?
bool _dxgi_hooked; // DXGI Present is hooked ? (DX10, DX11, DX12) bool _dxgi_hooked; // DXGI Present is hooked ? (DX10, DX11, DX12)
@ -82,11 +80,6 @@ protected:
// If this is called, then OpenGL 3 will be used to render overlay // If this is called, then OpenGL 3 will be used to render overlay
static BOOL WINAPI MywglMakeCurrent(HDC hDC, HGLRC hGLRC); static BOOL WINAPI MywglMakeCurrent(HDC hDC, HGLRC hGLRC);
public:
void ChangeGameWindow(HWND hWnd) const;
protected:
#elif defined(__linux__) #elif defined(__linux__)
#endif #endif
@ -94,15 +87,12 @@ protected:
public: public:
static Hook_Manager& Inst(); static Hook_Manager& Inst();
void HookRenderer(Steam_Overlay *overlay); void HookRenderer();
// Set the found hook and free all other hooks // Set the found hook and free all other hooks
void FoundRenderer(Base_Hook *hook); void FoundRenderer(Base_Hook *hook);
inline void AddHook(Base_Hook* hook) { _hooks.push_back(hook); } inline void AddHook(Base_Hook* hook) { _hooks.push_back(hook); }
void CallOverlayProc(int width, int height) const;
Steam_Overlay* GetOverlay() const { return overlay; }
}; };
#endif//NO_OVERLAY #endif//NO_OVERLAY

View file

@ -1,12 +1,11 @@
#include "../dll/base.h" #include "OpenGL_Hook.h"
#include "Windows_Hook.h"
#include "Hook_Manager.h"
#include "../dll/dll.h"
#ifndef NO_OVERLAY #ifndef NO_OVERLAY
#include "OpenGL_Hook.h"
#include "Hook_Manager.h"
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include <impls/imgui_impl_opengl3.h> #include <impls/imgui_impl_opengl3.h>
#include <GL/glew.h> #include <GL/glew.h>
@ -33,6 +32,9 @@ bool OpenGL_Hook::start_hook()
std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers) std::make_pair<void**, void*>(&(PVOID&)wglSwapBuffers, &OpenGL_Hook::MywglSwapBuffers)
); );
EndHook(); EndHook();
if (Windows_Hook::Inst().start_hook())
get_steam_client()->steam_overlay->HookReady();
} }
else else
{ {
@ -50,7 +52,7 @@ void OpenGL_Hook::resetRenderState()
if (initialized) if (initialized)
{ {
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplWin32_Shutdown(); Windows_Hook::Inst().resetRenderState();
ImGui::DestroyContext(); ImGui::DestroyContext();
initialized = false; initialized = false;
@ -60,12 +62,13 @@ void OpenGL_Hook::resetRenderState()
// Try to make this function and overlay's proc as short as possible or it might affect game's fps. // Try to make this function and overlay's proc as short as possible or it might affect game's fps.
void OpenGL_Hook::prepareForOverlay(HDC hDC) void OpenGL_Hook::prepareForOverlay(HDC hDC)
{ {
HWND hWnd = WindowFromDC(hDC); HWND hWnd = WindowFromDC(hDC);
RECT rect; RECT rect;
GetClientRect(hWnd, &rect); GetClientRect(hWnd, &rect);
if (hWnd != Hook_Manager::Inst().GetOverlay()->GetGameHwnd()) if (hWnd != Windows_Hook::Inst().GetGameHwnd())
resetRenderState(); resetRenderState();
if (!initialized) if (!initialized)
@ -74,19 +77,16 @@ void OpenGL_Hook::prepareForOverlay(HDC hDC)
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.IniFilename = NULL; io.IniFilename = NULL;
ImGui_ImplWin32_Init(hWnd);
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
Hook_Manager::Inst().ChangeGameWindow(hWnd);
initialized = true; initialized = true;
} }
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplWin32_NewFrame(); Windows_Hook::Inst().prepareForOverlay(hWnd);
ImGui::NewFrame(); ImGui::NewFrame();
Hook_Manager::Inst().CallOverlayProc(rect.right, rect.bottom); get_steam_client()->steam_overlay->OverlayProc(rect.right, rect.bottom);
ImGui::EndFrame(); ImGui::EndFrame();

View file

@ -0,0 +1,155 @@
#include "Windows_Hook.h"
#include <imgui.h>
#include <impls/imgui_impl_win32.h>
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
#include "../dll/dll.h"
bool Windows_Hook::start_hook()
{
if (!_hooked)
{
GetRawInputBuffer = ::GetRawInputBuffer;
GetRawInputData = ::GetRawInputData;
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer),
std::make_pair<void**, void*>(&(PVOID&)GetRawInputData , &Windows_Hook::MyGetRawInputData)
);
EndHook();
_hooked = true;
}
return true;
}
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
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:
// Keyboard Events
case WM_KEYDOWN: case WM_KEYUP:
case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_SYSDEADCHAR:
case WM_CHAR: case WM_UNICHAR: case WM_DEADCHAR:
// Raw Input Events
case WM_INPUT:
return true;
}
return false;
}
LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Steam_Overlay* overlay = get_steam_client()->steam_overlay;
// 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))
overlay->ShowOverlay(!overlay->ShowOverlay());
}
}
if (overlay->ShowOverlay())
{
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
if (IgnoreMsg(uMsg))
return 0;
}
// Call the overlay window procedure
return CallWindowProc(Windows_Hook::Inst()._game_wndproc, hWnd, uMsg, wParam, lParam);
}
UINT WINAPI Windows_Hook::MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader)
{
if (!get_steam_client()->steam_overlay->ShowOverlay())
return Windows_Hook::Inst().GetRawInputBuffer(pData, pcbSize, cbSizeHeader);
return -1;
}
UINT WINAPI Windows_Hook::MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader)
{
if (!get_steam_client()->steam_overlay->ShowOverlay())
return Windows_Hook::Inst().GetRawInputData(hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
return -1;
}
/////////////////////////////////////////////////////////////////////////////////////
Windows_Hook::Windows_Hook() :
initialized(false),
_game_hwnd(nullptr),
_game_wndproc(nullptr),
GetRawInputBuffer(nullptr),
GetRawInputData(nullptr)
{
}
Windows_Hook::~Windows_Hook()
{
PRINT_DEBUG("Windows Hook removed\n");
resetRenderState();
}
Windows_Hook& Windows_Hook::Inst()
{
static Windows_Hook _inst;
return _inst;
}

View file

@ -0,0 +1,46 @@
#ifndef __INCLUDED_WINDOWS_HOOK_H__
#define __INCLUDED_WINDOWS_HOOK_H__
#include "Base_Hook.h"
#ifndef NO_OVERLAY
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
class Windows_Hook : public Base_Hook
{
public:
//static constexpr const char* DLL_NAME = "user32.dll";
private:
// Variables
bool initialized;
HWND _game_hwnd;
WNDPROC _game_wndproc;
// Functions
Windows_Hook();
virtual ~Windows_Hook();
// Hook to Windows window messages
decltype(GetRawInputBuffer)* GetRawInputBuffer;
decltype(GetRawInputData)* GetRawInputData;
static LRESULT CALLBACK HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static UINT WINAPI MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader);
static UINT WINAPI MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader);
public:
bool start_hook();
void resetRenderState();
void prepareForOverlay(HWND);
HWND GetGameHwnd() const;
WNDPROC GetGameWndProc() const;
static Windows_Hook& Inst();
};
#endif//NO_OVERLAY
#endif//__INCLUDED_WINDOWS_HOOK_H__

View file

@ -7,69 +7,17 @@
#include <sstream> #include <sstream>
#include <cctype> #include <cctype>
#include <imgui.h> #include <imgui.h>
#include <impls/imgui_impl_win32.h>
#include "../dll/dll.h" #include "../dll/dll.h"
#include "Hook_Manager.h"
#include "Windows_Hook.h"
// Look here for info on how to hook on linux // Look here for info on how to hook on linux
// https://github.com/AimTuxOfficial/AimTux/ // https://github.com/AimTuxOfficial/AimTux/
#include "notification.h" #include "notification.h"
static decltype(GetRawInputBuffer)* _GetRawInputBuffer = GetRawInputBuffer;
static decltype(GetRawInputData)* _GetRawInputData = GetRawInputData;
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool Steam_Overlay::IgnoreMsg(UINT uMsg)
{
switch (uMsg)
{
// Mouse Events
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:
// Keyboard Events
case WM_KEYDOWN: case WM_KEYUP:
case WM_SYSKEYDOWN: case WM_SYSKEYUP: case WM_SYSDEADCHAR:
case WM_CHAR: case WM_UNICHAR: case WM_DEADCHAR:
// Raw Input Events
case WM_INPUT:
return true;
}
return false;
}
LRESULT CALLBACK Steam_Overlay::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Steam_Overlay* _this = Hook_Manager::Inst().GetOverlay();
// 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))
_this->ShowOverlay(!_this->show_overlay);
}
}
if (_this->show_overlay)
{
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
if (_this->IgnoreMsg(uMsg))
return 0;
}
// Call the overlay window procedure
return CallWindowProc(_this->game_hwnd_proc, hWnd, uMsg, wParam, lParam);
}
void Steam_Overlay::steam_overlay_run_every_runcb(void* object) void Steam_Overlay::steam_overlay_run_every_runcb(void* object)
{ {
Steam_Overlay* _this = reinterpret_cast<Steam_Overlay*>(object); Steam_Overlay* _this = reinterpret_cast<Steam_Overlay*>(object);
@ -88,8 +36,6 @@ Steam_Overlay::Steam_Overlay(Settings* settings, SteamCallResults* callback_resu
callbacks(callbacks), callbacks(callbacks),
run_every_runcb(run_every_runcb), run_every_runcb(run_every_runcb),
network(network), network(network),
game_hwnd(NULL),
game_hwnd_proc(nullptr),
show_overlay(false), show_overlay(false),
is_ready(false), is_ready(false),
notif_position(ENotificationPosition::k_EPositionBottomLeft), notif_position(ENotificationPosition::k_EPositionBottomLeft),
@ -106,11 +52,6 @@ Steam_Overlay::~Steam_Overlay()
run_every_runcb->remove(&Steam_Overlay::steam_overlay_run_every_runcb, this); run_every_runcb->remove(&Steam_Overlay::steam_overlay_run_every_runcb, this);
} }
HWND Steam_Overlay::GetGameHwnd() const
{
return game_hwnd;
}
bool Steam_Overlay::Ready() const bool Steam_Overlay::Ready() const
{ {
return is_ready; return is_ready;
@ -134,55 +75,20 @@ void Steam_Overlay::SetNotificationInset(int nHorizontalInset, int nVerticalInse
void Steam_Overlay::SetupOverlay() void Steam_Overlay::SetupOverlay()
{ {
Hook_Manager::Inst().HookRenderer(this); Hook_Manager::Inst().HookRenderer();
} }
UINT WINAPI Steam_Overlay::MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader) void Steam_Overlay::HookReady()
{ {
Steam_Overlay* _this = Hook_Manager::Inst().GetOverlay(); if (!is_ready) // If this is the first time we are ready, hook directinput and xinput, so we can intercept em and disable mouse.
if( !_this->show_overlay )
return _GetRawInputBuffer(pData, pcbSize, cbSizeHeader);
return -1;
}
UINT WINAPI Steam_Overlay::MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader)
{
Steam_Overlay* _this = Hook_Manager::Inst().GetOverlay();
if (!_this->show_overlay)
return _GetRawInputData(hRawInput, uiCommand, pData, pcbSize, cbSizeHeader);
return -1;
}
void Steam_Overlay::HookReady(void* hWnd)
{
if (game_hwnd != hWnd)
{ {
if (!is_ready) // If this is the first time we are ready, hook directinput and xinput, so we can intercept em and disable mouse. // TODO: Uncomment this and draw our own cursor (cosmetics)
{ //ImGuiIO &io = ImGui::GetIO();
window_hooks.BeginHook(); //io.WantSetMousePos = false;
//io.MouseDrawCursor = false;
window_hooks.HookFuncs(std::make_pair<void**, void*>(&(PVOID&)_GetRawInputBuffer, &MyGetRawInputBuffer), //io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
std::make_pair<void**, void*>(&(PVOID&)_GetRawInputData, &MyGetRawInputData)
);
window_hooks.EndHook();
// TODO: Uncomment this and draw our own cursor (cosmetics) is_ready = true;
//ImGuiIO &io = ImGui::GetIO();
//io.WantSetMousePos = false;
//io.MouseDrawCursor = false;
//io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
is_ready = true;
}
if (game_hwnd)
SetWindowLongPtr(game_hwnd, GWLP_WNDPROC, (LONG_PTR)game_hwnd_proc);
game_hwnd = (HWND)hWnd;
game_hwnd_proc = (WNDPROC)SetWindowLongPtr(game_hwnd, GWLP_WNDPROC, (LONG_PTR)&Steam_Overlay::HookWndProc);
} }
} }
@ -201,6 +107,11 @@ void Steam_Overlay::OpenOverlay(const char* pchDialog)
ShowOverlay(true); ShowOverlay(true);
} }
bool Steam_Overlay::ShowOverlay() const
{
return show_overlay;
}
void Steam_Overlay::ShowOverlay(bool state) void Steam_Overlay::ShowOverlay(bool state)
{ {
static RECT old_clip; static RECT old_clip;
@ -211,6 +122,7 @@ void Steam_Overlay::ShowOverlay(bool state)
show_overlay = state; show_overlay = state;
if (show_overlay) if (show_overlay)
{ {
HWND game_hwnd = Windows_Hook::Inst().GetGameHwnd();
RECT cliRect, wndRect, clipRect; RECT cliRect, wndRect, clipRect;
GetClipCursor(&old_clip); GetClipCursor(&old_clip);

View file

@ -2,7 +2,6 @@
#define __INCLUDED_STEAM_OVERLAY_H__ #define __INCLUDED_STEAM_OVERLAY_H__
#include "../dll/base.h" #include "../dll/base.h"
#include "Hook_Manager.h"
#include <map> #include <map>
#include <queue> #include <queue>
@ -53,11 +52,8 @@ class Steam_Overlay
// friend id, show client window (to chat and accept invite maybe) // friend id, show client window (to chat and accept invite maybe)
std::map<Friend, friend_window_state, Friend_Less> friends; std::map<Friend, friend_window_state, Friend_Less> friends;
HWND game_hwnd;
WNDPROC game_hwnd_proc;
bool is_ready; bool is_ready;
bool show_overlay; bool show_overlay;
Base_Hook window_hooks;
ENotificationPosition notif_position; ENotificationPosition notif_position;
int h_inset, v_inset; int h_inset, v_inset;
@ -70,12 +66,6 @@ class Steam_Overlay
Steam_Overlay& operator=(Steam_Overlay const&) = delete; Steam_Overlay& operator=(Steam_Overlay const&) = delete;
Steam_Overlay& operator=(Steam_Overlay&&) = delete; Steam_Overlay& operator=(Steam_Overlay&&) = delete;
bool IgnoreMsg(UINT uMsg);
static LRESULT CALLBACK HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static UINT WINAPI MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader);
static UINT WINAPI MyGetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader);
static void steam_overlay_run_every_runcb(void* object); static void steam_overlay_run_every_runcb(void* object);
static void steam_overlay_callback(void* object, Common_Message* msg); static void steam_overlay_callback(void* object, Common_Message* msg);
@ -95,8 +85,6 @@ public:
~Steam_Overlay(); ~Steam_Overlay();
HWND GetGameHwnd() const;
bool Ready() const; bool Ready() const;
bool NeedPresent() const; bool NeedPresent() const;
@ -106,13 +94,14 @@ public:
void SetNotificationInset(int nHorizontalInset, int nVerticalInset); void SetNotificationInset(int nHorizontalInset, int nVerticalInset);
void SetupOverlay(); void SetupOverlay();
void HookReady(void* hWnd); void HookReady();
void OverlayProc(int width, int height); void OverlayProc(int width, int height);
void OpenOverlayInvite(CSteamID lobbyId); void OpenOverlayInvite(CSteamID lobbyId);
void OpenOverlay(const char* pchDialog); void OpenOverlay(const char* pchDialog);
bool ShowOverlay() const;
void ShowOverlay(bool state); void ShowOverlay(bool state);
void SetLobbyInvite(Friend friendId, uint64 lobbyId); void SetLobbyInvite(Friend friendId, uint64 lobbyId);