diff --git a/r5dev/engine/sys_engine.cpp b/r5dev/engine/sys_engine.cpp index 994ae4bc..d553a6c4 100644 --- a/r5dev/engine/sys_engine.cpp +++ b/r5dev/engine/sys_engine.cpp @@ -1,9 +1,35 @@ #include "core/stdafx.h" +#include "tier1/cvar.h" #include "sys_engine.h" +#ifdef DEDICATED +#include "game/shared/shareddefs.h" +#endif // DEDICATED /////////////////////////////////////////////////////////////////////////////// CEngine* g_pEngine = nullptr; +bool CEngine::_Frame(CEngine* thisp) +{ +#ifdef DEDICATED + + // The first engine frame is ran before the global variables are initialized. + // By default, the tick interval is set to '0.0f'; we can't divide by zero. + if (TICK_INTERVAL > 0.0f) + { + int nTickRate = TIME_TO_TICKS(1.0f); + if (fps_max->GetInt() != nTickRate) + { + // Clamp the framerate of the server to its simulation tick rate. + // This saves a significant amount of CPU time in CEngine::Frame, + // as the engine uses this to decided when to run a new frame. + fps_max->SetValue(nTickRate); + } + } + +#endif // DEDICATED + return v_CEngine_Frame(thisp); +} + /* //----------------------------------------------------------------------------- // Purpose: Start initializing the engine. @@ -82,4 +108,14 @@ void CEngine::SetQuitting(EngineDllQuitting_t quitDllState) const static int index = 9; CallVFunc(index, this, quitDllState); } -*/ \ No newline at end of file +*/ + +void VEngine::Attach() const +{ + DetourAttach((LPVOID*)&v_CEngine_Frame, &CEngine::_Frame); +} + +void VEngine::Detach() const +{ + DetourDetach((LPVOID*)&v_CEngine_Frame, &CEngine::_Frame); +} \ No newline at end of file diff --git a/r5dev/engine/sys_engine.h b/r5dev/engine/sys_engine.h index 71a661f4..b8a15523 100644 --- a/r5dev/engine/sys_engine.h +++ b/r5dev/engine/sys_engine.h @@ -4,6 +4,9 @@ class CEngine : public IEngine { +public: + static bool _Frame(CEngine* thisp); + private: EngineState_t m_nDLLState; EngineState_t m_nNextDLLState; @@ -19,6 +22,9 @@ private: }; /* ==== CENGINE ======================================================================================================================================================= */ +inline CMemory p_CEngine_Frame; +inline auto v_CEngine_Frame = p_CEngine_Frame.RCast(); + extern CEngine* g_pEngine; /////////////////////////////////////////////////////////////////////////////// @@ -30,9 +36,20 @@ class VEngine : public IDetour { virtual void GetAdr(void) const { - LogVarAdr("g_pEngine", reinterpret_cast(g_pEngine)); + LogFunAdr("CEngine::Frame", p_CEngine_Frame.GetPtr()); + LogVarAdr("g_Engine", reinterpret_cast(g_pEngine)); + } + virtual void GetFun(void) const + { +#if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) || defined (GAMEDLL_S2) + p_CEngine_Frame = g_GameDll.FindPatternSIMD("40 55 53 56 48 8D AC 24 ?? ?? ?? ?? 48 81 EC ?? ?? ?? ?? 48 8B 05 ?? ?? ?? ?? 48 8B F1"); +#elif defined (GAMEDLL_S2) + p_CEngine_Frame = g_GameDll.FindPatternSIMD("48 8B C4 56 48 81 EC ?? ?? ?? ?? 0F 29 70 B8"); +#else + p_CEngine_Frame = g_GameDll.FindPatternSIMD("48 8B C4 55 56 48 8D A8 ?? ?? ?? ?? 48 81 EC ?? ?? ?? ?? 0F 29 70 B8"); +#endif + v_CEngine_Frame = p_CEngine_Frame.RCast(); } - virtual void GetFun(void) const { } virtual void GetVar(void) const { #if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) @@ -42,7 +59,7 @@ class VEngine : public IDetour #endif } virtual void GetCon(void) const { } - virtual void Attach(void) const { } - virtual void Detach(void) const { } + virtual void Attach(void) const; + virtual void Detach(void) const; }; ///////////////////////////////////////////////////////////////////////////////