Skip to content

Commit

Permalink
Update nemirtingas overlay to latest to fix clipcursor related issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr_Goldberg committed Aug 16, 2022
1 parent f01bb14 commit 276a990
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 52 deletions.
34 changes: 23 additions & 11 deletions overlay_experimental/linux/X11_Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,25 @@ bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback, s
return false;
}

XEventsQueued = libX11.GetSymbol<decltype(::XEventsQueued)>("XEventsQueued");
XPending = libX11.GetSymbol<decltype(::XPending)>("XPending");

if (XPending == nullptr || XEventsQueued == nullptr)
struct {
void** func_ptr;
void* hook_ptr;
const char* func_name;
} hook_array[] = {
{ (void**)&XEventsQueued, &X11_Hook::MyXEventsQueued, "XEventsQueued" },
{ (void**)&XPending , &X11_Hook::MyXPending , "XPending" },
};

for (auto& entry : hook_array)
{
SPDLOG_WARN("Failed to hook X11: Cannot load functions.({}, {})", DLL_NAME, (void*)XEventsQueued, (void*)XPending);
return false;
*entry.func_ptr = libX11.GetSymbol<void*>(entry.func_name);
if (entry.func_ptr == nullptr)
{
SPDLOG_ERROR("Failed to hook X11: Event function {} missing.", entry.func_name);
return false;
}
}

SPDLOG_INFO("Hooked X11");

_KeyCombinationCallback = std::move(_key_combination_callback);
Expand All @@ -124,12 +135,13 @@ bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback, s

_Hooked = true;

UnhookAll();
BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(void*&)XEventsQueued, (void*)&X11_Hook::MyXEventsQueued),
std::make_pair<void**, void*>(&(void*&)XPending, (void*)&X11_Hook::MyXPending)
);

for (auto& entry : hook_array)
{
HookFunc(std::make_pair(entry.func_ptr, entry.hook_ptr));
}

EndHook();
}
return true;
Expand Down
103 changes: 62 additions & 41 deletions overlay_experimental/windows/Windows_Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,30 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
return false;
}

GetRawInputBuffer = libUser32.GetSymbol<decltype(::GetRawInputBuffer)>("GetRawInputBuffer");
GetRawInputData = libUser32.GetSymbol<decltype(::GetRawInputData)>("GetRawInputData");
GetKeyState = libUser32.GetSymbol<decltype(::GetKeyState)>("GetKeyState");
GetAsyncKeyState = libUser32.GetSymbol<decltype(::GetAsyncKeyState)>("GetAsyncKeyState");
GetKeyboardState = libUser32.GetSymbol<decltype(::GetKeyboardState)>("GetKeyboardState");
GetCursorPos = libUser32.GetSymbol<decltype(::GetCursorPos)>("GetCursorPos");
SetCursorPos = libUser32.GetSymbol<decltype(::SetCursorPos)>("SetCursorPos");

if(GetRawInputBuffer == nullptr ||
GetRawInputData == nullptr ||
GetKeyState == nullptr ||
GetAsyncKeyState == nullptr ||
GetKeyboardState == nullptr ||
GetCursorPos == nullptr ||
SetCursorPos == nullptr)
struct {
void** func_ptr;
void* hook_ptr;
const char* func_name;
} hook_array[] = {
{ (void**)&GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer, "GetRawInputBuffer" },
{ (void**)&GetRawInputData , &Windows_Hook::MyGetRawInputData , "GetRawInputData" },
{ (void**)&GetKeyState , &Windows_Hook::MyGetKeyState , "GetKeyState" },
{ (void**)&GetAsyncKeyState , &Windows_Hook::MyGetAsyncKeyState , "GetAsyncKeyState" },
{ (void**)&GetKeyboardState , &Windows_Hook::MyGetKeyboardState , "GetKeyboardState" },
{ (void**)&GetCursorPos , &Windows_Hook::MyGetCursorPos , "GetCursorPos" },
{ (void**)&SetCursorPos , &Windows_Hook::MySetCursorPos , "SetCursorPos" },
{ (void**)&GetClipCursor , &Windows_Hook::MyGetClipCursor , "GetClipCursor" },
{ (void**)&ClipCursor , &Windows_Hook::MyClipCursor , "ClipCursor" },
};

for (auto& entry : hook_array)
{
SPDLOG_ERROR("Failed to hook Windows: Events functions missing.");
return false;
*entry.func_ptr = libUser32.GetSymbol<void*>(entry.func_name);
if (entry.func_ptr == nullptr)
{
SPDLOG_ERROR("Failed to hook Windows: Events function {} missing.", entry.func_name);
return false;
}
}

SPDLOG_INFO("Hooked Windows");
Expand All @@ -126,15 +132,12 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
}

BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer),
std::make_pair<void**, void*>(&(PVOID&)GetRawInputData , &Windows_Hook::MyGetRawInputData),
std::make_pair<void**, void*>(&(PVOID&)GetKeyState , &Windows_Hook::MyGetKeyState),
std::make_pair<void**, void*>(&(PVOID&)GetAsyncKeyState , &Windows_Hook::MyGetAsyncKeyState),
std::make_pair<void**, void*>(&(PVOID&)GetKeyboardState , &Windows_Hook::MyGetKeyboardState),
std::make_pair<void**, void*>(&(PVOID&)GetCursorPos , &Windows_Hook::MyGetCursorPos),
std::make_pair<void**, void*>(&(PVOID&)SetCursorPos , &Windows_Hook::MySetCursorPos)
);

for (auto& entry : hook_array)
{
HookFunc(std::make_pair(entry.func_ptr, entry.hook_ptr));
}

EndHook();

_Hooked = true;
Expand Down Expand Up @@ -289,6 +292,11 @@ LRESULT CALLBACK Windows_Hook::HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
// Save the last known cursor pos when opening the overlay
// so we can spoof the GetCursorPos return value.
inst->GetCursorPos(&inst->_SavedCursorPos);
inst->GetClipCursor(&inst->_SavedClipCursor);
}
else
{
inst->ClipCursor(&inst->_SavedClipCursor);
}
inst->_KeyCombinationPushed = true;
}
Expand Down Expand Up @@ -407,16 +415,33 @@ BOOL WINAPI Windows_Hook::MySetCursorPos(int X, int Y)
{
Windows_Hook* inst = Windows_Hook::Inst();

if (inst->_Initialized && inst->_KeyCombinationCallback(false))
{// That way, it will fail only if the real API fails.
// Hides error messages on some Unity debug builds.
POINT pos;
inst->GetCursorPos(&pos);
X = pos.x;
Y = pos.y;
}
if (!inst->_Initialized || !inst->_KeyCombinationCallback(false))
return inst->SetCursorPos(X, Y);

return inst->SetCursorPos(X, Y);
return TRUE;
}

BOOL WINAPI Windows_Hook::MyGetClipCursor(RECT* lpRect)
{
Windows_Hook* inst = Windows_Hook::Inst();
if (lpRect == nullptr || !inst->_Initialized || !inst->_KeyCombinationCallback(false))
return inst->GetClipCursor(lpRect);

*lpRect = inst->_SavedClipCursor;
return TRUE;
}

BOOL WINAPI Windows_Hook::MyClipCursor(CONST RECT* lpRect)
{
Windows_Hook* inst = Windows_Hook::Inst();
CONST RECT* v = lpRect == nullptr ? &inst->_DefaultClipCursor : lpRect;

inst->_SavedClipCursor = *v;

if (!inst->_Initialized || !inst->_KeyCombinationCallback(false))
return inst->ClipCursor(v);

return inst->ClipCursor(&inst->_DefaultClipCursor);
}

/////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -427,12 +452,8 @@ Windows_Hook::Windows_Hook() :
_RecurseCallCount(0),
_GameHwnd(nullptr),
_GameWndProc(nullptr),
_KeyCombinationPushed(false),
GetRawInputBuffer(nullptr),
GetRawInputData(nullptr),
GetKeyState(nullptr),
GetAsyncKeyState(nullptr),
GetKeyboardState(nullptr)
_DefaultClipCursor{ LONG(0xFFFF8000), LONG(0xFFFF8000), LONG(0x00007FFF), LONG(0x00007FFF) },
_KeyCombinationPushed(false)
{
}

Expand Down
6 changes: 6 additions & 0 deletions overlay_experimental/windows/Windows_Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Windows_Hook :
HWND _GameHwnd;
WNDPROC _GameWndProc;
POINT _SavedCursorPos;
RECT _SavedClipCursor;
CONST RECT _DefaultClipCursor;

// In (bool): Is toggle wanted
// Out(bool): Is the overlay visible, if true, inputs will be disabled
Expand All @@ -55,6 +57,8 @@ class Windows_Hook :
decltype(::GetKeyboardState) *GetKeyboardState;
decltype(::GetCursorPos) *GetCursorPos;
decltype(::SetCursorPos) *SetCursorPos;
decltype(::GetClipCursor) *GetClipCursor;
decltype(::ClipCursor) *ClipCursor;

static LRESULT CALLBACK HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static UINT WINAPI MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader);
Expand All @@ -64,6 +68,8 @@ class Windows_Hook :
static BOOL WINAPI MyGetKeyboardState(PBYTE lpKeyState);
static BOOL WINAPI MyGetCursorPos(LPPOINT lpPoint);
static BOOL WINAPI MySetCursorPos(int X, int Y);
static BOOL WINAPI MyGetClipCursor(RECT* lpRect);
static BOOL WINAPI MyClipCursor(CONST RECT* lpRect);

public:
std::string LibraryName;
Expand Down

0 comments on commit 276a990

Please sign in to comment.