Update nemirtingas overlay to latest to fix clipcursor related issue.

merge-requests/50/head
Mr_Goldberg 2022-08-16 12:29:58 -04:00
parent f01bb145b0
commit 276a9902df
No known key found for this signature in database
GPG Key ID: 8597D87419DEF278
3 changed files with 89 additions and 50 deletions

View File

@ -101,14 +101,25 @@ bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback, s
return false; return false;
} }
XEventsQueued = libX11.GetSymbol<decltype(::XEventsQueued)>("XEventsQueued"); struct {
XPending = libX11.GetSymbol<decltype(::XPending)>("XPending"); void** func_ptr;
void* hook_ptr;
const char* func_name;
} hook_array[] = {
{ (void**)&XEventsQueued, &X11_Hook::MyXEventsQueued, "XEventsQueued" },
{ (void**)&XPending , &X11_Hook::MyXPending , "XPending" },
};
if (XPending == nullptr || XEventsQueued == nullptr) for (auto& entry : hook_array)
{ {
SPDLOG_WARN("Failed to hook X11: Cannot load functions.({}, {})", DLL_NAME, (void*)XEventsQueued, (void*)XPending); *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; return false;
} }
}
SPDLOG_INFO("Hooked X11"); SPDLOG_INFO("Hooked X11");
_KeyCombinationCallback = std::move(_key_combination_callback); _KeyCombinationCallback = std::move(_key_combination_callback);
@ -124,12 +135,13 @@ bool X11_Hook::StartHook(std::function<bool(bool)>& _key_combination_callback, s
_Hooked = true; _Hooked = true;
UnhookAll();
BeginHook(); BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(void*&)XEventsQueued, (void*)&X11_Hook::MyXEventsQueued), for (auto& entry : hook_array)
std::make_pair<void**, void*>(&(void*&)XPending, (void*)&X11_Hook::MyXPending) {
); HookFunc(std::make_pair(entry.func_ptr, entry.hook_ptr));
}
EndHook(); EndHook();
} }
return true; return true;

View File

@ -93,25 +93,31 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
return false; return false;
} }
GetRawInputBuffer = libUser32.GetSymbol<decltype(::GetRawInputBuffer)>("GetRawInputBuffer"); struct {
GetRawInputData = libUser32.GetSymbol<decltype(::GetRawInputData)>("GetRawInputData"); void** func_ptr;
GetKeyState = libUser32.GetSymbol<decltype(::GetKeyState)>("GetKeyState"); void* hook_ptr;
GetAsyncKeyState = libUser32.GetSymbol<decltype(::GetAsyncKeyState)>("GetAsyncKeyState"); const char* func_name;
GetKeyboardState = libUser32.GetSymbol<decltype(::GetKeyboardState)>("GetKeyboardState"); } hook_array[] = {
GetCursorPos = libUser32.GetSymbol<decltype(::GetCursorPos)>("GetCursorPos"); { (void**)&GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer, "GetRawInputBuffer" },
SetCursorPos = libUser32.GetSymbol<decltype(::SetCursorPos)>("SetCursorPos"); { (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" },
};
if(GetRawInputBuffer == nullptr || for (auto& entry : hook_array)
GetRawInputData == nullptr ||
GetKeyState == nullptr ||
GetAsyncKeyState == nullptr ||
GetKeyboardState == nullptr ||
GetCursorPos == nullptr ||
SetCursorPos == nullptr)
{ {
SPDLOG_ERROR("Failed to hook Windows: Events functions missing."); *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; return false;
} }
}
SPDLOG_INFO("Hooked Windows"); SPDLOG_INFO("Hooked Windows");
_KeyCombinationCallback = std::move(_key_combination_callback); _KeyCombinationCallback = std::move(_key_combination_callback);
@ -126,15 +132,12 @@ bool Windows_Hook::StartHook(std::function<bool(bool)>& _key_combination_callbac
} }
BeginHook(); BeginHook();
HookFuncs(
std::make_pair<void**, void*>(&(PVOID&)GetRawInputBuffer, &Windows_Hook::MyGetRawInputBuffer), for (auto& entry : hook_array)
std::make_pair<void**, void*>(&(PVOID&)GetRawInputData , &Windows_Hook::MyGetRawInputData), {
std::make_pair<void**, void*>(&(PVOID&)GetKeyState , &Windows_Hook::MyGetKeyState), HookFunc(std::make_pair(entry.func_ptr, entry.hook_ptr));
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)
);
EndHook(); EndHook();
_Hooked = true; _Hooked = true;
@ -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 // Save the last known cursor pos when opening the overlay
// so we can spoof the GetCursorPos return value. // so we can spoof the GetCursorPos return value.
inst->GetCursorPos(&inst->_SavedCursorPos); inst->GetCursorPos(&inst->_SavedCursorPos);
inst->GetClipCursor(&inst->_SavedClipCursor);
}
else
{
inst->ClipCursor(&inst->_SavedClipCursor);
} }
inst->_KeyCombinationPushed = true; inst->_KeyCombinationPushed = true;
} }
@ -407,16 +415,33 @@ BOOL WINAPI Windows_Hook::MySetCursorPos(int X, int Y)
{ {
Windows_Hook* inst = Windows_Hook::Inst(); Windows_Hook* inst = Windows_Hook::Inst();
if (inst->_Initialized && inst->_KeyCombinationCallback(false)) 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;
}
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);
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
@ -427,12 +452,8 @@ Windows_Hook::Windows_Hook() :
_RecurseCallCount(0), _RecurseCallCount(0),
_GameHwnd(nullptr), _GameHwnd(nullptr),
_GameWndProc(nullptr), _GameWndProc(nullptr),
_KeyCombinationPushed(false), _DefaultClipCursor{ LONG(0xFFFF8000), LONG(0xFFFF8000), LONG(0x00007FFF), LONG(0x00007FFF) },
GetRawInputBuffer(nullptr), _KeyCombinationPushed(false)
GetRawInputData(nullptr),
GetKeyState(nullptr),
GetAsyncKeyState(nullptr),
GetKeyboardState(nullptr)
{ {
} }

View File

@ -37,6 +37,8 @@ private:
HWND _GameHwnd; HWND _GameHwnd;
WNDPROC _GameWndProc; WNDPROC _GameWndProc;
POINT _SavedCursorPos; POINT _SavedCursorPos;
RECT _SavedClipCursor;
CONST RECT _DefaultClipCursor;
// In (bool): Is toggle wanted // In (bool): Is toggle wanted
// Out(bool): Is the overlay visible, if true, inputs will be disabled // Out(bool): Is the overlay visible, if true, inputs will be disabled
@ -55,6 +57,8 @@ private:
decltype(::GetKeyboardState) *GetKeyboardState; decltype(::GetKeyboardState) *GetKeyboardState;
decltype(::GetCursorPos) *GetCursorPos; decltype(::GetCursorPos) *GetCursorPos;
decltype(::SetCursorPos) *SetCursorPos; decltype(::SetCursorPos) *SetCursorPos;
decltype(::GetClipCursor) *GetClipCursor;
decltype(::ClipCursor) *ClipCursor;
static LRESULT CALLBACK HookWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 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 MyGetRawInputBuffer(PRAWINPUT pData, PUINT pcbSize, UINT cbSizeHeader);
@ -64,6 +68,8 @@ private:
static BOOL WINAPI MyGetKeyboardState(PBYTE lpKeyState); static BOOL WINAPI MyGetKeyboardState(PBYTE lpKeyState);
static BOOL WINAPI MyGetCursorPos(LPPOINT lpPoint); static BOOL WINAPI MyGetCursorPos(LPPOINT lpPoint);
static BOOL WINAPI MySetCursorPos(int X, int Y); static BOOL WINAPI MySetCursorPos(int X, int Y);
static BOOL WINAPI MyGetClipCursor(RECT* lpRect);
static BOOL WINAPI MyClipCursor(CONST RECT* lpRect);
public: public:
std::string LibraryName; std::string LibraryName;