From 27ee32470b463e1bde6417f013dc5de512d2a85f Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Wed, 17 Jan 2024 17:31:59 +0800 Subject: [PATCH] Reload command chains on map reload closed #7821 --- src/engine/console.h | 1 + src/engine/server.h | 1 + src/engine/server/register.cpp | 6 ++++++ src/engine/server/register.h | 2 ++ src/engine/server/server.cpp | 17 ++++++++++++----- src/engine/server/server.h | 1 + src/engine/shared/console.cpp | 19 +++++++++++++++++++ src/engine/shared/console.h | 1 + src/game/server/gamecontext.cpp | 11 ++++++++--- src/game/server/gamecontext.h | 1 + 10 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/engine/console.h b/src/engine/console.h index 774e06358..556d4a428 100644 --- a/src/engine/console.h +++ b/src/engine/console.h @@ -103,6 +103,7 @@ public: virtual void DeregisterTemp(const char *pName) = 0; virtual void DeregisterTempAll() = 0; virtual void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) = 0; + virtual void UnchainAll() = 0; virtual void StoreCommands(bool Store) = 0; virtual bool LineIsValid(const char *pStr) = 0; diff --git a/src/engine/server.h b/src/engine/server.h index d839200d8..7cc5ee7b8 100644 --- a/src/engine/server.h +++ b/src/engine/server.h @@ -299,6 +299,7 @@ public: // is instantiated. virtual void OnInit(const void *pPersistentData) = 0; virtual void OnConsoleInit() = 0; + virtual void RegisterChains() = 0; virtual void OnMapChange(char *pNewMapName, int MapNameSize) = 0; // `pPersistentData` may be null if this is the last time `IGameServer` // is destroyed. diff --git a/src/engine/server/register.cpp b/src/engine/server/register.cpp index c049741a0..0e795addd 100644 --- a/src/engine/server/register.cpp +++ b/src/engine/server/register.cpp @@ -135,6 +135,7 @@ class CRegister : public IRegister public: CRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, IHttp *pHttp, int ServerPort, unsigned SixupSecurityToken); + void RegisterChains() override; void Update() override; void OnConfigChange() override; bool OnPacket(const CNetChunk *pPacket) override; @@ -499,6 +500,11 @@ CRegister::CRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, IHt mem_copy(aTokenBytes, &SixupSecurityToken, sizeof(aTokenBytes)); str_format(m_aConnlessTokenHex, sizeof(m_aConnlessTokenHex), "%08x", bytes_be_to_uint(aTokenBytes)); + RegisterChains(); +} + +void CRegister::RegisterChains() +{ m_pConsole->Chain("sv_register", ConchainOnConfigChange, this); m_pConsole->Chain("sv_register_extra", ConchainOnConfigChange, this); m_pConsole->Chain("sv_register_url", ConchainOnConfigChange, this); diff --git a/src/engine/server/register.h b/src/engine/server/register.h index 1fbfc3c56..358525b61 100644 --- a/src/engine/server/register.h +++ b/src/engine/server/register.h @@ -22,6 +22,8 @@ public: // `pInfo` must be an encoded JSON object. virtual void OnNewInfo(const char *pInfo) = 0; virtual void OnShutdown() = 0; + + virtual void RegisterChains() = 0; }; IRegister *CreateRegister(CConfig *pConfig, IConsole *pConsole, IEngine *pEngine, IHttp *pHttp, int ServerPort, unsigned SixupSecurityToken); diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 570905008..147c9a92e 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -2808,6 +2808,9 @@ int CServer::Run() m_ServerInfoFirstRequest = 0; Kernel()->ReregisterInterface(GameServer()); GameServer()->OnInit(m_pPersistentData); + GameServer()->RegisterChains(); + m_pRegister->RegisterChains(); + RegisterChains(); if(ErrorShutdown()) { break; @@ -3772,7 +3775,16 @@ void CServer::RegisterCommands() Console()->Register("auth_list", "", CFGFLAG_SERVER, ConAuthList, this, "List all rcon keys"); RustVersionRegister(*Console()); + RegisterChains(); + // register console commands in sub parts + m_ServerBan.InitServerBan(Console(), Storage(), this); + m_NameBans.InitConsole(Console()); + m_pGameServer->OnConsoleInit(); +} + +void CServer::RegisterChains() +{ Console()->Chain("sv_name", ConchainSpecialInfoupdate, this); Console()->Chain("password", ConchainSpecialInfoupdate, this); Console()->Chain("sv_spectator_slots", ConchainSpecialInfoupdate, this); @@ -3792,11 +3804,6 @@ void CServer::RegisterCommands() #if defined(CONF_FAMILY_UNIX) Console()->Chain("sv_conn_logging_server", ConchainConnLoggingServerChange, this); #endif - - // register console commands in sub parts - m_ServerBan.InitServerBan(Console(), Storage(), this); - m_NameBans.InitConsole(Console()); - m_pGameServer->OnConsoleInit(); } int CServer::SnapNewID() diff --git a/src/engine/server/server.h b/src/engine/server/server.h index c4fca2094..d17e18695 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -428,6 +428,7 @@ public: #endif void RegisterCommands(); + void RegisterChains(); int SnapNewID() override; void SnapFreeID(int ID) override; diff --git a/src/engine/shared/console.cpp b/src/engine/shared/console.cpp index 1f1df48ab..7ddd14bab 100644 --- a/src/engine/shared/console.cpp +++ b/src/engine/shared/console.cpp @@ -1003,6 +1003,25 @@ void CConsole::Chain(const char *pName, FChainCommandCallback pfnChainFunc, void pCommand->m_pUserData = pChainInfo; } +void CConsole::UnchainAll() +{ + for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->m_pNext) + { + FCommandCallback pfnCallback = pCommand->m_pfnCallback; + void *pUserData = pCommand->m_pUserData; + CChain *pChain = nullptr; + while(pfnCallback == Con_Chain) + { + pChain = static_cast(pUserData); + pfnCallback = pChain->m_pfnCallback; + pUserData = pChain->m_pCallbackUserData; + delete pChain; + } + pCommand->m_pfnCallback = pfnCallback; + pCommand->m_pUserData = pUserData; + } +} + void CConsole::StoreCommands(bool Store) { if(!Store) diff --git a/src/engine/shared/console.h b/src/engine/shared/console.h index 339b521ce..15b8cae1b 100644 --- a/src/engine/shared/console.h +++ b/src/engine/shared/console.h @@ -204,6 +204,7 @@ public: void DeregisterTemp(const char *pName) override; void DeregisterTempAll() override; void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) override; + void UnchainAll() override; void StoreCommands(bool Store) override; bool LineIsValid(const char *pStr) override; diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 6c55177e2..33a942847 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -3518,6 +3518,13 @@ void CGameContext::OnConsoleInit() Console()->Register("dump_antibot", "", CFGFLAG_SERVER, ConDumpAntibot, this, "Dumps the antibot status"); Console()->Register("antibot", "r[command]", CFGFLAG_SERVER, ConAntibot, this, "Sends a command to the antibot"); + RegisterDDRaceCommands(); + RegisterChatCommands(); + RegisterChains(); +} + +void CGameContext::RegisterChains() +{ Console()->Chain("sv_motd", ConchainSpecialMotdupdate, this); Console()->Chain("sv_vote_kick", ConchainSettingUpdate, this); @@ -3525,9 +3532,6 @@ void CGameContext::OnConsoleInit() Console()->Chain("sv_vote_spectate", ConchainSettingUpdate, this); Console()->Chain("sv_spectator_slots", ConchainSettingUpdate, this); Console()->Chain("sv_max_clients", ConchainSettingUpdate, this); - - RegisterDDRaceCommands(); - RegisterChatCommands(); } void CGameContext::RegisterDDRaceCommands() @@ -4151,6 +4155,7 @@ void CGameContext::OnShutdown(void *pPersistentData) DeleteTempfile(); ConfigManager()->ResetGameSettings(); Collision()->Dest(); + Console()->UnchainAll(); delete m_pController; m_pController = 0; Clear(); diff --git a/src/game/server/gamecontext.h b/src/game/server/gamecontext.h index 147943d14..d4d7f35ad 100644 --- a/src/game/server/gamecontext.h +++ b/src/game/server/gamecontext.h @@ -285,6 +285,7 @@ public: void OnConsoleInit() override; void RegisterDDRaceCommands(); void RegisterChatCommands(); + void RegisterChains() override; void OnMapChange(char *pNewMapName, int MapNameSize) override; void OnShutdown(void *pPersistentData) override;