From 13fa6d902b62d496b5ef82beeb81027d91c89863 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 24 Dec 2023 16:29:42 +0100 Subject: [PATCH] EngineAPI: key event dispatcher rebuild --- r5dev/common/global.cpp | 3 +++ r5dev/common/global.h | 2 ++ r5dev/engine/keys.h | 8 ++++++- r5dev/engine/sys_dll2.cpp | 15 +++++++++++- r5dev/engine/sys_mainwind.cpp | 38 +++++++++++++++++++++++++++++- r5dev/engine/sys_mainwind.h | 6 ++++- r5dev/inputsystem/inputsystem.cpp | 3 ++- r5dev/inputsystem/inputsystem.h | 5 ++++ r5dev/vgui/vgui_baseui_interface.h | 5 ++++ 9 files changed, 80 insertions(+), 5 deletions(-) diff --git a/r5dev/common/global.cpp b/r5dev/common/global.cpp index 850ba453..c538eaef 100644 --- a/r5dev/common/global.cpp +++ b/r5dev/common/global.cpp @@ -28,6 +28,8 @@ ConVar* fps_max_rt = nullptr; ConVar* fps_max_rt_tolerance = nullptr; ConVar* fps_max_rt_sleep_threshold = nullptr; ConVar* fps_max_gfx = nullptr; + +ConVar* in_syncRT = nullptr; #endif // !DEDICATED ConVar* base_tickinterval_sp = nullptr; @@ -565,6 +567,7 @@ void ConVar_InitShipped(void) #ifndef DEDICATED miles_language = g_pCVar->FindVar("miles_language"); rui_defaultDebugFontFace = g_pCVar->FindVar("rui_defaultDebugFontFace"); + in_syncRT = g_pCVar->FindVar("in_syncRT"); r_visualizetraces = g_pCVar->FindVar("r_visualizetraces"); r_visualizetraces_duration = g_pCVar->FindVar("r_visualizetraces_duration"); #endif // !DEDICATED diff --git a/r5dev/common/global.h b/r5dev/common/global.h index 3d546b56..3ae5c83e 100644 --- a/r5dev/common/global.h +++ b/r5dev/common/global.h @@ -19,6 +19,8 @@ extern ConVar* fps_max_rt; extern ConVar* fps_max_rt_tolerance; extern ConVar* fps_max_rt_sleep_threshold; extern ConVar* fps_max_gfx; + +extern ConVar* in_syncRT; #endif // !DEDICATED extern ConVar* base_tickinterval_sp; diff --git a/r5dev/engine/keys.h b/r5dev/engine/keys.h index 317eadef..c4b7e829 100644 --- a/r5dev/engine/keys.h +++ b/r5dev/engine/keys.h @@ -41,6 +41,8 @@ struct KeyInfo_t short paddingMaybe; }; +inline bool(*Key_Event)(const KeyEvent_t& keyEvent); + extern KeyInfo_t* g_pKeyInfo; // ARRAYSIZE = ButtonCode_t::BUTTON_CODE_LAST extern ButtonCode_t* g_pKeyEventTicks; // ARRAYSIZE = ButtonCode_t::BUTTON_CODE_LAST extern short* g_nKeyEventCount; @@ -49,11 +51,15 @@ class VKeys : public IDetour { virtual void GetAdr(void) const { + LogFunAdr("Key_Event", reinterpret_cast(Key_Event)); LogVarAdr("g_pKeyInfo", reinterpret_cast(g_pKeyInfo)); LogVarAdr("g_pKeyEventTicks", reinterpret_cast(g_pKeyEventTicks)); LogVarAdr("g_nKeyEventCount", reinterpret_cast(g_nKeyEventCount)); } - virtual void GetFun(void) const { } + virtual void GetFun(void) const + { + Key_Event = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 48 89 74 24 ?? 57 48 83 EC 20 4C 63 41 08").RCast(); + } virtual void GetVar(void) const { g_pKeyInfo = g_GameDll.FindPatternSIMD("48 83 EC 28 33 D2 48 8D 0D ?? ?? ?? ?? 41 B8 ?? ?? ?? ?? E8 ?? ?? ?? ?? 33 C0 C6 05 ?? ?? ?? ?? ??") diff --git a/r5dev/engine/sys_dll2.cpp b/r5dev/engine/sys_dll2.cpp index d9dc35c2..9abae8d9 100644 --- a/r5dev/engine/sys_dll2.cpp +++ b/r5dev/engine/sys_dll2.cpp @@ -17,6 +17,8 @@ #include "engine/traceinit.h" #ifndef DEDICATED #include "engine/sys_mainwind.h" +#include "inputsystem/inputsystem.h" +#include "vgui/vgui_baseui_interface.h" #include "materialsystem/cmaterialsystem.h" #include "windows/id3dx.h" #include "client/vengineclient_impl.h" @@ -162,7 +164,18 @@ void CEngineAPI::VSetStartupInfo(CEngineAPI* pEngineAPI, StartupInfo_t* pStartup void CEngineAPI::PumpMessages() { #ifndef DEDICATED - CEngineAPI_PumpMessages(); + MSG msg; + while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + + if (in_syncRT->GetBool()) + (*g_fnSyncRTWithIn)(); + + g_pInputSystem->PollInputState(UIInputEventHandler); + g_pGame->DispatchAllStoredGameMessages(); #endif // !DEDICATED } diff --git a/r5dev/engine/sys_mainwind.cpp b/r5dev/engine/sys_mainwind.cpp index 43407dc3..e963a551 100644 --- a/r5dev/engine/sys_mainwind.cpp +++ b/r5dev/engine/sys_mainwind.cpp @@ -9,6 +9,7 @@ #include "windows/input.h" #include "engine/sys_mainwind.h" #include "engine/sys_engine.h" +#include "engine/keys.h" #include "gameui/IConsole.h" #include "gameui/IBrowser.h" @@ -97,7 +98,7 @@ int CGame::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) //----------------------------------------------------------------------------- // Purpose: gets the window rect //----------------------------------------------------------------------------- -void CGame::GetWindowRect(int* x, int* y, int* w, int* h) +void CGame::GetWindowRect(int* const x, int* const y, int* const w, int* const h) const { if (x) { @@ -117,6 +118,41 @@ void CGame::GetWindowRect(int* x, int* y, int* w, int* h) } } +//----------------------------------------------------------------------------- +// Purpose: dispatch key event +//----------------------------------------------------------------------------- +void CGame::DispatchKeyEvent(const uint64_t msTime, const ButtonCode_t buttonCode) const +{ + const float duration = buttonCode == KEY_XBUTTON_BACK ? 1.0f : 0.2f; + KeyInfo_t& keyInfo = g_pKeyInfo[buttonCode]; + + if (keyInfo.m_bKeyDown && ((msTime - keyInfo.m_nEventTick) * 0.001f) >= duration) + { + KeyEvent_t keyEvent; + + keyEvent.m_pCommand = keyInfo.m_pKeyBinding[KeyInfo_t::KEY_HELD_BIND]; + keyEvent.m_nTick = buttonCode; + keyEvent.m_bDown = true; + + Key_Event(keyEvent); + keyInfo.m_bKeyDown = true; + } +} + +//----------------------------------------------------------------------------- +// Purpose: dispatch all the queued up messages +//----------------------------------------------------------------------------- +void CGame::DispatchAllStoredGameMessages() const +{ + const uint64_t ticks = Plat_MSTime(); + const short eventCount = *g_nKeyEventCount; + + for (short i = 0; i < eventCount; i++) + { + DispatchKeyEvent(ticks, g_pKeyEventTicks[i]); + } +} + /////////////////////////////////////////////////////////////////////////////// void VGame::Detour(const bool bAttach) const { diff --git a/r5dev/engine/sys_mainwind.h b/r5dev/engine/sys_mainwind.h index 4c44b56e..9ed5a2ac 100644 --- a/r5dev/engine/sys_mainwind.h +++ b/r5dev/engine/sys_mainwind.h @@ -5,6 +5,7 @@ //===========================================================================// #ifndef SYS_MAINWIND_H #define SYS_MAINWIND_H +#include "inputsystem/iinputsystem.h" inline CMemory p_CGame__AttachToWindow; inline void (*v_CGame__AttachToWindow)(void); @@ -25,7 +26,7 @@ public: inline HWND GetWindow() const { return m_hWindow; } - void GetWindowRect(int* x, int* y, int* w, int* h); + void GetWindowRect(int* const x, int* const y, int* const w, int* const h) const; inline int GetDesktopWidth() const { return m_iDesktopWidth; } inline int GetDesktopHeight() const { return m_iDesktopHeight; } @@ -33,6 +34,9 @@ public: inline float GetTVRefreshRate() const // Avoid stutter on TV's running on broadcast frame rates. { return ((float)m_iDesktopRefreshRate == 59.0f || (float)m_iDesktopRefreshRate == 60.0f) ? 59.939999f : (float)m_iDesktopRefreshRate; } + void DispatchKeyEvent(const uint64_t msTime, const ButtonCode_t buttonCode) const; + void DispatchAllStoredGameMessages() const; + private: HWND m_hWindow; HINSTANCE m_hInstance; diff --git a/r5dev/inputsystem/inputsystem.cpp b/r5dev/inputsystem/inputsystem.cpp index 256fc8b0..f181d0ca 100644 --- a/r5dev/inputsystem/inputsystem.cpp +++ b/r5dev/inputsystem/inputsystem.cpp @@ -50,4 +50,5 @@ //} /////////////////////////////////////////////////////////////////////////////// -CInputSystem* g_pInputSystem = nullptr; \ No newline at end of file +CInputSystem* g_pInputSystem = nullptr; +bool(**g_fnSyncRTWithIn)(void) = nullptr; diff --git a/r5dev/inputsystem/inputsystem.h b/r5dev/inputsystem/inputsystem.h index 16bbaf30..52b666b5 100644 --- a/r5dev/inputsystem/inputsystem.h +++ b/r5dev/inputsystem/inputsystem.h @@ -163,6 +163,7 @@ static_assert(sizeof(CInputSystem) == 0x18E8); /////////////////////////////////////////////////////////////////////////////// extern CInputSystem* g_pInputSystem; +extern bool(**g_fnSyncRTWithIn)(void); // Belongs to an array of callbacks, see CMaterialSystem::MatsysMode_Init(). /////////////////////////////////////////////////////////////////////////////// class VInputSystem : public IDetour @@ -170,12 +171,16 @@ class VInputSystem : public IDetour virtual void GetAdr(void) const { LogVarAdr("g_pInputSystem", reinterpret_cast(g_pInputSystem)); + LogVarAdr("g_fnSyncRTWithIn", reinterpret_cast(g_fnSyncRTWithIn)); } virtual void GetFun(void) const { } virtual void GetVar(void) const { g_pInputSystem = g_GameDll.FindPatternSIMD("48 83 EC 28 48 8B 0D ?? ?? ?? ?? 48 8D 05 ?? ?? ?? ?? 48 89 05 ?? ?? ?? ?? 48 85 C9 74 11") .FindPatternSelf("48 89 05", CMemory::Direction::DOWN, 40).ResolveRelativeAddressSelf(0x3, 0x7).RCast(); + + const CMemory l_EngineApi_PumpMessages = g_GameDll.FindPatternSIMD("48 89 5C 24 ?? 55 48 81 EC ?? ?? ?? ?? 45 33 C9"); + g_fnSyncRTWithIn = l_EngineApi_PumpMessages.FindPattern("74 06").FindPatternSelf("FF 15").ResolveRelativeAddressSelf(2, 6).RCast(); } virtual void GetCon(void) const { } virtual void Detour(const bool bAttach) const { } diff --git a/r5dev/vgui/vgui_baseui_interface.h b/r5dev/vgui/vgui_baseui_interface.h index 3801cf2c..d4a5b4ea 100644 --- a/r5dev/vgui/vgui_baseui_interface.h +++ b/r5dev/vgui/vgui_baseui_interface.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include "inputsystem/iinputsystem.h" enum class PaintMode_t { @@ -85,6 +86,7 @@ inline void*(*CEngineVGui_RenderStart)(CMatSystemSurface* pMatSystemSurface); inline CMemory p_CEngineVGui_RenderEnd; inline void*(*CEngineVGui_RenderEnd)(void); +inline InputEventCallback_t UIInputEventHandler = nullptr; inline CEngineVGui* g_pEngineVGui = nullptr; /////////////////////////////////////////////////////////////////////////////// @@ -95,6 +97,7 @@ class VEngineVGui : public IDetour LogFunAdr("CEngineVGui::Paint", p_CEngineVGui_Paint.GetPtr()); LogFunAdr("CEngineVGui::RenderStart", p_CEngineVGui_RenderStart.GetPtr()); LogFunAdr("CEngineVGui::RenderEnd", p_CEngineVGui_RenderEnd.GetPtr()); + LogFunAdr("UIInputEventHandler", reinterpret_cast(UIInputEventHandler)); LogVarAdr("g_pEngineVGui", reinterpret_cast(g_pEngineVGui)); } virtual void GetFun(void) const @@ -114,6 +117,8 @@ class VEngineVGui : public IDetour #endif p_CEngineVGui_RenderEnd = g_GameDll.FindPatternSIMD("40 53 48 83 EC 20 48 8B 0D ?? ?? ?? ?? C6 05 ?? ?? ?? ?? ?? 48 8B 01"); CEngineVGui_RenderEnd = p_CEngineVGui_RenderEnd.RCast(); /*40 53 48 83 EC 20 48 8B 0D ?? ?? ?? ?? C6 05 ?? ?? ?? ?? ?? 48 8B 01*/ + + UIInputEventHandler = g_GameDll.FindPatternSIMD("40 53 48 83 EC 40 48 63 01").RCast(); } virtual void GetVar(void) const {