From 5f56c23af2e3b338c341418da3018ca87a2f98bc Mon Sep 17 00:00:00 2001
From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com>
Date: Sun, 29 Jan 2023 16:07:02 +0100
Subject: [PATCH] Compile CRConServer for gamesdk

RCON can now be used on the host game as well (previously dedicated exclusive).
---
 r5dev/engine/client/cl_rcon.cpp     | 23 -----------------------
 r5dev/engine/client/cl_rcon.h       |  1 -
 r5dev/engine/host_state.cpp         | 12 +++++++-----
 r5dev/engine/server/sv_rcon.cpp     |  2 +-
 r5dev/squirrel/sqvm.cpp             | 23 ++++++++++++-----------
 r5dev/tier0/dbg.cpp                 | 29 +++++++++++++++--------------
 r5dev/tier1/IConVar.cpp             |  5 ++---
 r5dev/tier1/cvar.cpp                |  4 ++--
 r5dev/tier1/cvar.h                  |  4 ++--
 r5dev/vproj/gamesdk.vcxproj         |  2 ++
 r5dev/vproj/gamesdk.vcxproj.filters |  6 ++++++
 r5dev/vstdlib/callback.cpp          | 13 ++++++-------
 12 files changed, 55 insertions(+), 69 deletions(-)

diff --git a/r5dev/engine/client/cl_rcon.cpp b/r5dev/engine/client/cl_rcon.cpp
index a3f5e874..21872a85 100644
--- a/r5dev/engine/client/cl_rcon.cpp
+++ b/r5dev/engine/client/cl_rcon.cpp
@@ -37,10 +37,6 @@ CRConClient::~CRConClient(void)
 //-----------------------------------------------------------------------------
 void CRConClient::Init(void)
 {
-	if (!m_bInitialized)
-	{
-		this->SetPassword(rcon_password->GetString());
-	}
 	m_bInitialized = true;
 }
 
@@ -55,25 +51,6 @@ void CRConClient::Shutdown(void)
 	}
 }
 
-//-----------------------------------------------------------------------------
-// Purpose: changes the password
-// Input  : *pszPassword - 
-// Output : true on success, false otherwise
-//-----------------------------------------------------------------------------
-bool CRConClient::SetPassword(const char* pszPassword)
-{
-	const size_t nLen = std::strlen(pszPassword);
-	if (nLen < 8)
-	{
-		if (nLen > 0)
-		{
-			DevMsg(eDLL_T::CLIENT, "Remote server access requires a password of at least 8 characters\n");
-		}
-		return false;
-	}
-	return true;
-}
-
 //-----------------------------------------------------------------------------
 // Purpose: client rcon main processing loop
 //-----------------------------------------------------------------------------
diff --git a/r5dev/engine/client/cl_rcon.h b/r5dev/engine/client/cl_rcon.h
index 2d796a02..442037e8 100644
--- a/r5dev/engine/client/cl_rcon.h
+++ b/r5dev/engine/client/cl_rcon.h
@@ -13,7 +13,6 @@ public:
 	void Init(void);
 	void Shutdown(void);
 
-	bool SetPassword(const char* pszPassword);
 	void RunFrame(void);
 
 	bool Connect(void);
diff --git a/r5dev/engine/host_state.cpp b/r5dev/engine/host_state.cpp
index 050c92ba..74f1011d 100644
--- a/r5dev/engine/host_state.cpp
+++ b/r5dev/engine/host_state.cpp
@@ -16,9 +16,10 @@
 #include "tier2/socketcreator.h"
 #include "vpc/keyvalues.h"
 #include "datacache/mdlcache.h"
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 #include "engine/server/sv_rcon.h"
-#else // 
+#endif // !CLIENT_DLL
+#ifndef DEDICATED
 #include "engine/client/cl_rcon.h"
 #include "engine/client/cl_main.h"
 #include "engine/client/clientstate.h"
@@ -66,11 +67,12 @@ FORCEINLINE void CHostState::FrameUpdate(CHostState* pHostState, double flCurren
 	}
 
 	g_pHostState->Think();
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 	RCONServer()->RunFrame();
-#else // 
+#endif // !CLIENT_DLL
+#ifndef DEDICATED
 	RCONClient()->RunFrame();
-#endif // DEDICATED
+#endif // !DEDICATED
 
 	HostStates_t oldState{};
 	if (setjmp(*host_abortserver))
diff --git a/r5dev/engine/server/sv_rcon.cpp b/r5dev/engine/server/sv_rcon.cpp
index dec2aaa8..74e68818 100644
--- a/r5dev/engine/server/sv_rcon.cpp
+++ b/r5dev/engine/server/sv_rcon.cpp
@@ -52,7 +52,7 @@ void CRConServer::Init(void)
 
 	m_Socket.CreateListenSocket(m_Address, false);
 
-	DevMsg(eDLL_T::SERVER, "Remote server access initialized\n");
+	DevMsg(eDLL_T::SERVER, "Remote server access initialized ('%s')\n", m_Address.ToString());
 	m_bInitialized = true;
 }
 
diff --git a/r5dev/squirrel/sqvm.cpp b/r5dev/squirrel/sqvm.cpp
index 002315b8..768d67f3 100644
--- a/r5dev/squirrel/sqvm.cpp
+++ b/r5dev/squirrel/sqvm.cpp
@@ -10,13 +10,14 @@
 #include "tier0/commandline.h"
 #include "tier1/cvar.h"
 #include "tier1/IConVar.h"
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 #include "engine/server/sv_rcon.h"
-#else // DEDICATED
+#endif // CLIENT_DLL
+#ifndef DEDICATED
 #include "client/cdll_engine_int.h"
 #include "vgui/vgui_debugpanel.h"
 #include "gameui/IConsole.h"
-#endif
+#endif // !DEDICATED
 #include "squirrel/sqtype.h"
 #include "squirrel/sqvm.h"
 #include "squirrel/sqinit.h"
@@ -133,9 +134,9 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...)
 		if (!g_bSpdLog_UseAnsiClr)
 		{
 			wconsole->debug(vmStr);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 			RCONServer()->Send(vmStr, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, nResponseId);
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 		}
 		else // Use ANSI escape codes for the external console.
 		{
@@ -155,9 +156,9 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...)
 			}
 			vmStrAnsi.append(buf);
 			wconsole->debug(vmStrAnsi);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 			RCONServer()->Send(vmStrAnsi, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, nResponseId);
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 		}
 
 #ifndef DEDICATED
@@ -270,9 +271,9 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger*
 		if (!g_bSpdLog_UseAnsiClr)
 		{
 			wconsole->debug(vmStr);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 			RCONServer()->Send(vmStr, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, nResponseId);
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 		}
 		else
 		{
@@ -280,9 +281,9 @@ SQRESULT SQVM_WarningFunc(HSQUIRRELVM v, SQInteger a2, SQInteger a3, SQInteger*
 			vmStrAnsi.append(SQVM_WARNING_ANSI_LOG_T[static_cast<int>(context)]);
 			vmStrAnsi.append(svConstructor);
 			wconsole->debug(vmStrAnsi);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 			RCONServer()->Send(vmStrAnsi, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, nResponseId);
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 		}
 
 #ifndef DEDICATED
diff --git a/r5dev/tier0/dbg.cpp b/r5dev/tier0/dbg.cpp
index 8b3a9eb1..002113fe 100644
--- a/r5dev/tier0/dbg.cpp
+++ b/r5dev/tier0/dbg.cpp
@@ -15,9 +15,10 @@
 #ifndef DEDICATED
 #include "vgui/vgui_debugpanel.h"
 #include "gameui/IConsole.h"
-#else
+#endif // !DEDICATED
+#ifndef CLIENT_DLL
 #include "engine/server/sv_rcon.h"
-#endif
+#endif // !CLIENT_DLL
 
 #if defined( _X360 )
 #include "xbox/xbox_console.h"
@@ -332,9 +333,9 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
 	if (!g_bSpdLog_UseAnsiClr)
 	{
 		wconsole->debug(svOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 	else
 	{
@@ -347,9 +348,9 @@ void DevMsg(eDLL_T context, const char* fmt, ...)
 			svAnsiOut.append("\n");
 		}
 		wconsole->debug(svAnsiOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svAnsiOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 
 	sqlogger->debug(svOut);
@@ -454,9 +455,9 @@ void Warning(eDLL_T context, const char* fmt, ...)
 	if (!g_bSpdLog_UseAnsiClr)
 	{
 		wconsole->debug(svOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 	else
 	{
@@ -470,9 +471,9 @@ void Warning(eDLL_T context, const char* fmt, ...)
 			svAnsiOut.append("\n");
 		}
 		wconsole->debug(svAnsiOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svAnsiOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 
 	sqlogger->debug(svOut);
@@ -534,9 +535,9 @@ void Error(eDLL_T context, const UINT code, const char* fmt, ...)
 	if (!g_bSpdLog_UseAnsiClr)
 	{
 		wconsole->debug(svOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 	else
 	{
@@ -550,9 +551,9 @@ void Error(eDLL_T context, const UINT code, const char* fmt, ...)
 			svAnsiOut.append("\n");
 		}
 		wconsole->debug(svAnsiOut);
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 		RCONServer()->Send(svAnsiOut, "", sv_rcon::response_t::SERVERDATA_RESPONSE_CONSOLE_LOG, static_cast<int>(context));
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 
 	sqlogger->debug(svOut);
diff --git a/r5dev/tier1/IConVar.cpp b/r5dev/tier1/IConVar.cpp
index 3e181510..d46ec345 100644
--- a/r5dev/tier1/IConVar.cpp
+++ b/r5dev/tier1/IConVar.cpp
@@ -110,15 +110,14 @@ void ConVar::Init(void)
 	sv_autoReloadRate     = ConVar::Create("sv_autoReloadRate"    , "0"  , FCVAR_RELEASE, "Time in seconds between each server auto-reload (disabled if null). ", true, 0.f, false, 0.f, nullptr, nullptr);
 	sv_quota_stringCmdsPerSecond = ConVar::Create("sv_quota_stringCmdsPerSecond", "16", FCVAR_RELEASE, "How many string commands per second clients are allowed to submit, 0 to disallow all string commands.", true, 0.f, false, 0.f, nullptr, nullptr);
 	sv_simulateBots = ConVar::Create("sv_simulateBots", "1", FCVAR_RELEASE, "Simulate user commands for bots on the server.", true, 0.f, false, 0.f, nullptr, nullptr);
-#ifdef DEDICATED
+
 	sv_rcon_debug       = ConVar::Create("sv_rcon_debug"      , "0" , FCVAR_RELEASE, "Show rcon debug information ( !slower! ).", false, 0.f, false, 0.f, nullptr, nullptr);
 	sv_rcon_sendlogs    = ConVar::Create("sv_rcon_sendlogs"   , "0" , FCVAR_RELEASE, "Network console logs to connected and authenticated sockets.", false, 0.f, false, 0.f, nullptr, nullptr);
 	sv_rcon_banpenalty  = ConVar::Create("sv_rcon_banpenalty" , "10", FCVAR_RELEASE, "Number of minutes to ban users who fail rcon authentication.", false, 0.f, false, 0.f, nullptr, nullptr);
 	sv_rcon_maxfailures = ConVar::Create("sv_rcon_maxfailures", "10", FCVAR_RELEASE, "Max number of times a user can fail rcon authentication before being banned.", true, 1.f, false, 0.f, nullptr, nullptr);
 	sv_rcon_maxignores  = ConVar::Create("sv_rcon_maxignores" , "15", FCVAR_RELEASE, "Max number of times a user can ignore the no-auth message before being banned.", true, 1.f, false, 0.f, nullptr, nullptr);
 	sv_rcon_maxsockets  = ConVar::Create("sv_rcon_maxsockets" , "32", FCVAR_RELEASE, "Max number of accepted sockets before the server starts closing redundant sockets.", true, 1.f, false, 0.f, nullptr, nullptr);
-	sv_rcon_whitelist_address = ConVar::Create("sv_rcon_whitelist_address", "", FCVAR_RELEASE, "This address is not considered a 'redundant' socket and will never be banned for failed authentication attempts.", false, 0.f, false, 0.f, nullptr, "Format: '::ffff:127.0.0.1'.");
-#endif // DEDICATED
+	sv_rcon_whitelist_address = ConVar::Create("sv_rcon_whitelist_address", "", FCVAR_RELEASE, "This address is not considered a 'redundant' socket and will never be banned for failed authentication attempts.", false, 0.f, false, 0.f, nullptr, "Format: '::ffff:127.0.0.1'");
 #endif // !CLIENT_DLL
 #if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1)
 	bhit_depth_test = ConVar::Create("bhit_depth_test", "0", FCVAR_DEVELOPMENTONLY | FCVAR_REPLICATED, "Use depth test for bullet ray trace overlay.", false, 0.f, false, 0.f, nullptr, nullptr);
diff --git a/r5dev/tier1/cvar.cpp b/r5dev/tier1/cvar.cpp
index bf4c65b3..bd78b724 100644
--- a/r5dev/tier1/cvar.cpp
+++ b/r5dev/tier1/cvar.cpp
@@ -85,7 +85,7 @@ ConVar* sv_quota_stringCmdsPerSecond       = nullptr;
 ConVar* sv_simulateBots                    = nullptr;
 ConVar* sv_showhitboxes                    = nullptr;
 
-#ifdef DEDICATED
+//#ifdef DEDICATED
 ConVar* sv_rcon_debug                      = nullptr;
 ConVar* sv_rcon_sendlogs                   = nullptr;
 ConVar* sv_rcon_banpenalty                 = nullptr; // TODO
@@ -93,7 +93,7 @@ ConVar* sv_rcon_maxfailures                = nullptr;
 ConVar* sv_rcon_maxignores                 = nullptr;
 ConVar* sv_rcon_maxsockets                 = nullptr;
 ConVar* sv_rcon_whitelist_address          = nullptr;
-#endif // DEDICATED
+//#endif // DEDICATED
 #endif // !CLIENT_DLL
 ConVar* sv_visualizetraces                 = nullptr;
 ConVar* sv_visualizetraces_duration        = nullptr;
diff --git a/r5dev/tier1/cvar.h b/r5dev/tier1/cvar.h
index 5315a078..1550a66a 100644
--- a/r5dev/tier1/cvar.h
+++ b/r5dev/tier1/cvar.h
@@ -81,7 +81,7 @@ extern ConVar* sv_quota_stringCmdsPerSecond;
 extern ConVar* sv_simulateBots;
 extern ConVar* sv_showhitboxes;
 
-#ifdef DEDICATED
+//#ifdef DEDICATED
 extern ConVar* sv_rcon_debug;
 extern ConVar* sv_rcon_sendlogs;
 extern ConVar* sv_rcon_banpenalty;
@@ -89,7 +89,7 @@ extern ConVar* sv_rcon_maxfailures;
 extern ConVar* sv_rcon_maxignores;
 extern ConVar* sv_rcon_maxsockets;
 extern ConVar* sv_rcon_whitelist_address;
-#endif // DEDICATED
+//#endif // DEDICATED
 #endif // CLIENT_DLL
 extern ConVar* sv_visualizetraces;
 extern ConVar* sv_visualizetraces_duration;
diff --git a/r5dev/vproj/gamesdk.vcxproj b/r5dev/vproj/gamesdk.vcxproj
index a9de1505..9521b544 100644
--- a/r5dev/vproj/gamesdk.vcxproj
+++ b/r5dev/vproj/gamesdk.vcxproj
@@ -53,6 +53,7 @@
     <ClCompile Include="..\engine\sdk_dll.cpp" />
     <ClCompile Include="..\engine\server\server.cpp" />
     <ClCompile Include="..\engine\server\sv_main.cpp" />
+    <ClCompile Include="..\engine\server\sv_rcon.cpp" />
     <ClCompile Include="..\engine\sys_dll.cpp" />
     <ClCompile Include="..\engine\sys_dll2.cpp" />
     <ClCompile Include="..\engine\sys_engine.cpp" />
@@ -244,6 +245,7 @@
     <ClInclude Include="..\engine\sdk_dll.h" />
     <ClInclude Include="..\engine\server\server.h" />
     <ClInclude Include="..\engine\server\sv_main.h" />
+    <ClInclude Include="..\engine\server\sv_rcon.h" />
     <ClInclude Include="..\engine\sys_dll.h" />
     <ClInclude Include="..\engine\sys_dll2.h" />
     <ClInclude Include="..\engine\sys_engine.h" />
diff --git a/r5dev/vproj/gamesdk.vcxproj.filters b/r5dev/vproj/gamesdk.vcxproj.filters
index 564f5c89..99aa7734 100644
--- a/r5dev/vproj/gamesdk.vcxproj.filters
+++ b/r5dev/vproj/gamesdk.vcxproj.filters
@@ -738,6 +738,9 @@
     <ClCompile Include="..\tier1\NetAdr.cpp">
       <Filter>sdk\tier1</Filter>
     </ClCompile>
+    <ClCompile Include="..\engine\server\sv_rcon.cpp">
+      <Filter>sdk\engine\server</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\client\cdll_engine_int.h">
@@ -2150,6 +2153,9 @@
     <ClInclude Include="..\tier1\NetAdr.h">
       <Filter>sdk\tier1</Filter>
     </ClInclude>
+    <ClInclude Include="..\engine\server\sv_rcon.h">
+      <Filter>sdk\engine\server</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Image Include="..\shared\resource\lockedserver.png">
diff --git a/r5dev/vstdlib/callback.cpp b/r5dev/vstdlib/callback.cpp
index b0244258..da388a31 100644
--- a/r5dev/vstdlib/callback.cpp
+++ b/r5dev/vstdlib/callback.cpp
@@ -9,9 +9,9 @@
 #include "tier0/fasttimer.h"
 #include "tier1/cvar.h"
 #include "tier1/IConVar.h"
-#ifdef DEDICATED
+#ifndef CLIENT_DLL
 #include "engine/server/sv_rcon.h"
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 #ifndef DEDICATED
 #include "engine/client/cl_rcon.h"
 #endif // !DEDICATED
@@ -926,16 +926,15 @@ void RCON_PasswordChanged_f(IConVar* pConVar, const char* pOldString, float flOl
 			return; // Same password.
 
 #ifndef DEDICATED
-		if (RCONClient()->IsInitialized())
-			RCONClient()->SetPassword(pConVarRef->GetString());
-		else
+		if (!RCONClient()->IsInitialized())
 			RCONClient()->Init(); // Initialize first.
-#elif DEDICATED
+#endif // !DEDICATED
+#ifndef CLIENT_DLL
 		if (RCONServer()->IsInitialized())
 			RCONServer()->SetPassword(pConVarRef->GetString());
 		else
 			RCONServer()->Init(); // Initialize first.
-#endif // DEDICATED
+#endif // !CLIENT_DLL
 	}
 }