From 96bf41655a064b230dda8f5e5cd57d39d2d83696 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Fri, 25 Mar 2022 13:17:57 +0100 Subject: [PATCH] Use CBuf_AddText() and Cbuf_Execute() instead Small improvement --- r5dev/client/cdll_engine_int.cpp | 23 +++++++++++------- r5dev/engine/host_state.cpp | 40 ++++++++++++++++---------------- r5dev/engine/sv_rcon.cpp | 5 ++-- r5dev/gameui/IConsole.cpp | 2 +- r5dev/tier0/cmd.cpp | 10 ++++++++ r5dev/tier0/cmd.h | 6 +++-- r5dev/windows/console.cpp | 6 +++-- 7 files changed, 57 insertions(+), 35 deletions(-) diff --git a/r5dev/client/cdll_engine_int.cpp b/r5dev/client/cdll_engine_int.cpp index bbc772cd..cf025ad0 100644 --- a/r5dev/client/cdll_engine_int.cpp +++ b/r5dev/client/cdll_engine_int.cpp @@ -32,18 +32,25 @@ void __fastcall HFrameStageNotify(CHLClient* rcx, ClientFrameStage_t frameStage) #if defined (GAMEDLL_S0) || defined (GAMEDLL_S1) || defined (GAMEDLL_S2) // !TEMP UNTIL CHOSTSTATE IS BUILD AGNOSTIC! // if (!g_pCmdLine->CheckParm("-devsdk")) { - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_server.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_client.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"autoexec.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_client.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_server.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_server.cfg\"", cmd_source_t::kCommandSrcCode); +#ifndef DEDICATED + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_client.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_client.cfg\"", cmd_source_t::kCommandSrcCode); +#endif // !DEDICATED + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec.cfg\"", cmd_source_t::kCommandSrcCode); } else // Development configs. { - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_server_dev.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_client_dev.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_dev.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_client_dev.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_server_dev.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_server_dev.cfg\"", cmd_source_t::kCommandSrcCode); +#ifndef DEDICATED + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_client_dev.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_client_dev.cfg\"", cmd_source_t::kCommandSrcCode); +#endif // !DEDICATED + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_dev.cfg\"", cmd_source_t::kCommandSrcCode); } + Cbuf_Execute(); *(bool*)m_bRestrictServerCommands = true; // Restrict commands. ConCommandBase* disconnect = (ConCommandBase*)g_pCVar->FindCommand("disconnect"); diff --git a/r5dev/engine/host_state.cpp b/r5dev/engine/host_state.cpp index c727717b..7a20bd3a 100644 --- a/r5dev/engine/host_state.cpp +++ b/r5dev/engine/host_state.cpp @@ -34,7 +34,6 @@ #endif // !GAMECLIENTONLY bool g_bLevelResourceInitialized = false; - //----------------------------------------------------------------------------- // Purpose: state machine's main processing loop //----------------------------------------------------------------------------- @@ -73,7 +72,7 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) { case HostStates_t::HS_NEW_GAME: { - DevMsg(eDLL_T::ENGINE, "Loading level: '%s'\n", g_pHostState->m_levelName); + DevMsg(eDLL_T::ENGINE, "%s - Loading level: '%s'\n", "CHostState::FrameUpdate", g_pHostState->m_levelName); g_pHostState->State_NewGame(); break; } @@ -95,7 +94,7 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) } case HostStates_t::HS_GAME_SHUTDOWN: { - DevMsg(eDLL_T::ENGINE, "Shutdown host game\n"); + DevMsg(eDLL_T::ENGINE, "%s - Shutdown host game\n", "CHostState::FrameUpdate"); g_bLevelResourceInitialized = false; Host_Game_ShutdownFn(g_pHostState); @@ -103,7 +102,7 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) } case HostStates_t::HS_RESTART: { - DevMsg(eDLL_T::ENGINE, "Restarting state machine\n"); + DevMsg(eDLL_T::ENGINE, "%s - Restarting state machine\n", "CHostState::FrameUpdate"); g_bLevelResourceInitialized = false; #ifndef DEDICATED CL_EndMovieFn(); @@ -114,7 +113,7 @@ FORCEINLINE void CHostState::FrameUpdate(void* rcx, void* rdx, float time) } case HostStates_t::HS_SHUTDOWN: { - DevMsg(eDLL_T::ENGINE, "Shutdown state machine\n"); + DevMsg(eDLL_T::ENGINE, "%s - Shutdown state machine\n", "CHostState::FrameUpdate"); g_bLevelResourceInitialized = false; #ifndef DEDICATED CL_EndMovieFn(); @@ -196,24 +195,25 @@ FORCEINLINE void CHostState::LoadConfig(void) const { if (!g_pCmdLine->CheckParm("-devsdk")) { - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_server.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_server.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_server.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_server.cfg\"", cmd_source_t::kCommandSrcCode); #ifndef DEDICATED - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_client.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_client.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_client.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_client.cfg\"", cmd_source_t::kCommandSrcCode); #endif // !DEDICATED - IVEngineClient_CommandExecute(NULL, "exec \"autoexec.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec.cfg\"", cmd_source_t::kCommandSrcCode); } else // Development configs. { - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_server_dev.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_server_dev.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_server_dev.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_server_dev.cfg\"", cmd_source_t::kCommandSrcCode); #ifndef DEDICATED - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_client_dev.cfg\""); - IVEngineClient_CommandExecute(NULL, "exec \"rcon_client_dev.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_client_dev.cfg\"", cmd_source_t::kCommandSrcCode); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"rcon_client_dev.cfg\"", cmd_source_t::kCommandSrcCode); #endif // !DEDICATED - IVEngineClient_CommandExecute(NULL, "exec \"autoexec_dev.cfg\""); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), "exec \"autoexec_dev.cfg\"", cmd_source_t::kCommandSrcCode); } + Cbuf_Execute(); } //----------------------------------------------------------------------------- @@ -265,7 +265,7 @@ FORCEINLINE void CHostState::State_NewGame(void) if (!CModelLoader_Map_IsValidFn(g_CModelLoader, m_levelName) // Check if map is valid and if we can start a new game. || !Host_NewGameFn(m_levelName, nullptr, m_bBackgroundLevel, m_bSplitScreenConnect, nullptr) || !g_ServerGameClients) { - Error(eDLL_T::ENGINE, "Error: Map not valid\n"); + Error(eDLL_T::ENGINE, "%s - Error: Map not valid\n", "CHostState::State_NewGame"); #ifndef DEDICATED SCR_EndLoadingPlaque(); #endif // !DEDICATED @@ -286,7 +286,7 @@ FORCEINLINE void CHostState::State_NewGame(void) //----------------------------------------------------------------------------- FORCEINLINE void CHostState::State_ChangeLevelSP(void) { - DevMsg(eDLL_T::ENGINE, "Changing singleplayer level to: '%s'\n", m_levelName); + DevMsg(eDLL_T::ENGINE, "%s - Changing singleplayer level to: '%s'\n", "CHostState::State_ChangeLevelSP", m_levelName); m_flShortFrameTime = 1.5; // Set frame time. g_bLevelResourceInitialized = false; @@ -296,7 +296,7 @@ FORCEINLINE void CHostState::State_ChangeLevelSP(void) } else { - Error(eDLL_T::ENGINE, "Error: Unable to find map: '%s'\n", m_levelName); + Error(eDLL_T::ENGINE, "%s - Error: Unable to find map: '%s'\n", "CHostState::State_ChangeLevelSP", m_levelName); } m_iCurrentState = HostStates_t::HS_RUN; // Set current state to run. @@ -313,7 +313,7 @@ FORCEINLINE void CHostState::State_ChangeLevelSP(void) //----------------------------------------------------------------------------- FORCEINLINE void CHostState::State_ChangeLevelMP(void) { - DevMsg(eDLL_T::ENGINE, "Changing multiplayer level to: '%s'\n", m_levelName); + DevMsg(eDLL_T::ENGINE, "%s - Changing multiplayer level to: '%s'\n", "CHostState::State_ChangeLevelMP", m_levelName); m_flShortFrameTime = 0.5; // Set frame time. g_bLevelResourceInitialized = false; @@ -330,7 +330,7 @@ FORCEINLINE void CHostState::State_ChangeLevelMP(void) } else { - Error(eDLL_T::ENGINE, "Error: Unable to find map: '%s'\n", m_levelName); + Error(eDLL_T::ENGINE, "%s - Error: Unable to find map: '%s'\n", "CHostState::State_ChangeLevelMP", m_levelName); } m_iCurrentState = HostStates_t::HS_RUN; // Set current state to run. diff --git a/r5dev/engine/sv_rcon.cpp b/r5dev/engine/sv_rcon.cpp index a2af5cba..23061c84 100644 --- a/r5dev/engine/sv_rcon.cpp +++ b/r5dev/engine/sv_rcon.cpp @@ -15,7 +15,6 @@ #include "protoc/sv_rcon.pb.h" #include "protoc/cl_rcon.pb.h" #include "mathlib/sha256.h" -#include "client/IVEngineClient.h" #include "common/igameserverdata.h" //----------------------------------------------------------------------------- @@ -390,7 +389,9 @@ void CRConServer::Execute(const cl_rcon::request& cl_request) const else // Execute command with "". { std::string svExec = cl_request.requestbuf() + " \"" + cl_request.requestval() + "\""; - IVEngineClient_CommandExecute(NULL, svExec.c_str()); + + Cbuf_AddText(Cbuf_GetCurrentPlayer(), svExec.c_str(), cmd_source_t::kCommandSrcCode); + Cbuf_Execute(); } } diff --git a/r5dev/gameui/IConsole.cpp b/r5dev/gameui/IConsole.cpp index 4c4e9760..f9e41381 100644 --- a/r5dev/gameui/IConsole.cpp +++ b/r5dev/gameui/IConsole.cpp @@ -663,7 +663,7 @@ int CConsole::TextEditCallbackStub(ImGuiInputTextCallbackData* iData) void CConsole::AddLog(const char* fmt, ...) IM_FMTARGS(2) { char buf[1024]; - va_list args; + va_list args{}; va_start(args, fmt); vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); buf[IM_ARRAYSIZE(buf) - 1] = 0; diff --git a/r5dev/tier0/cmd.cpp b/r5dev/tier0/cmd.cpp index 35af38d1..c98af4ca 100644 --- a/r5dev/tier0/cmd.cpp +++ b/r5dev/tier0/cmd.cpp @@ -295,6 +295,16 @@ char* ConCommandBase::CopyString(const char* szFrom) const return szTo; } +//----------------------------------------------------------------------------- +// Purpose: Returns current player calling this function +// Output : ECommandTarget_t - +//----------------------------------------------------------------------------- +ECommandTarget_t Cbuf_GetCurrentPlayer(void) +{ + // Always returns 'CBUF_FIRST_PLAYER' in Respawn's code. + return ECommandTarget_t::CBUF_FIRST_PLAYER; +} + /////////////////////////////////////////////////////////////////////////////// void ConCommand_Attach() { diff --git a/r5dev/tier0/cmd.h b/r5dev/tier0/cmd.h index 01bd2e82..229cab81 100644 --- a/r5dev/tier0/cmd.h +++ b/r5dev/tier0/cmd.h @@ -131,8 +131,8 @@ public: namespace { /* ==== COMMAND_BUFFER ================================================================================================================================================== */ - ADDRESS p_Cbuf_AddText = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x44\x89\x4C\x24\x00\x48\x89\x4C\x24\x00\x55\x56\x57\x41\x54\x41\x55\x41\x56\x41\x57\x48\x81\xEC\x00\x00\x00\x00\x48\x8D\x6C\x24\x00\x48\x89\x9D\x00\x00\x00\x00\x4C\x8B", "xxxx?xxxx?xxxxxxxxxxxxxx????xxxx?xxx????xx"); - bool (*Cbuf_AddText)(ECommandTarget_t eTarget, const char* pText, cmd_source_t cmdSource, int nTickDelay) = (bool (*)(ECommandTarget_t, const char*, cmd_source_t, int))p_Cbuf_AddText.GetPtr(); /*44 89 4C 24 ? 48 89 4C 24 ? 55 56 57 41 54 41 55 41 56 41 57 48 81 EC ? ? ? ? 48 8D 6C 24 ? 48 89 9D ? ? ? ? 4C 8B*/ + ADDRESS p_Cbuf_AddText = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x20\x48\x63\xD9\x41\x8B\xF8\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xF2\xFF\x15\x00\x00\x00\x00\x48\x8D\x05\x00\x00\x00\x00\x41\xB9\x00\x00\x00\x00", "xxxx?xxxx?xxxxxxxxxxxxxx????xxxxx????xxx????xx????"); + void (*Cbuf_AddText)(ECommandTarget_t eTarget, const char* pText, cmd_source_t cmdSource) = (void (*)(ECommandTarget_t, const char*, cmd_source_t))p_Cbuf_AddText.GetPtr(); /*48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC 20 48 63 D9 41 8B F8 48 8D 0D ? ? ? ? 48 8B F2 FF 15 ? ? ? ? 48 8D 05 ? ? ? ? 41 B9 ? ? ? ?*/ ADDRESS p_Cbuf_Execute = g_mGameDll.FindPatternSIMD((std::uint8_t*)"\x48\x89\x5C\x24\x00\x48\x89\x6C\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xEC\x20\xFF\x15\x00\x00\x00\x00", "xxxx?xxxx?xxxx?xxxxxxx????"); void (*Cbuf_Execute)(void) = (void (*)(void))p_Cbuf_Execute.GetPtr(); /*48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 48 83 EC 20 FF 15 ? ? ? ?*/ @@ -157,6 +157,8 @@ namespace } /////////////////////////////////////////////////////////////////////////////// +ECommandTarget_t Cbuf_GetCurrentPlayer(void); + void ConCommand_Attach(); void ConCommand_Detach(); diff --git a/r5dev/windows/console.cpp b/r5dev/windows/console.cpp index a43e46f6..4b85e69c 100644 --- a/r5dev/windows/console.cpp +++ b/r5dev/windows/console.cpp @@ -7,11 +7,11 @@ #include "core/stdafx.h" #include "core/init.h" #include "core/logdef.h" +#include "tier0/cmd.h" #ifndef DEDICATED #include "windows/id3dx.h" #endif // !DEDICATED #include "windows/console.h" -#include "client/IVEngineClient.h" #include "common/opcodes.h" //----------------------------------------------------------------------------- @@ -145,7 +145,9 @@ DWORD __stdcall ProcessConsoleWorker(LPVOID) if (sCommand == "opcodes test") { RuntimePtc_Toggle(); continue; } // Execute the command in the r5 SQVM - IVEngineClient_CommandExecute(NULL, sCommand.c_str()); + Cbuf_AddText(Cbuf_GetCurrentPlayer(), sCommand.c_str(), cmd_source_t::kCommandSrcCode); + Cbuf_Execute(); + sCommand.clear(); ///////////////////////////////////////////////////////////////////////