From 9b6699d3b80d785e20f55af84916226965d15c53 Mon Sep 17 00:00:00 2001 From: eeeee Date: Mon, 5 Jun 2017 20:51:12 -0700 Subject: [PATCH] remove casts to CServer improving encapsulation by going through the interface instead of including engine/server/server.h --- src/engine/server.h | 11 +++++++++++ src/engine/server/server.cpp | 15 ++++++++++++++- src/engine/server/server.h | 12 ++++++------ src/game/server/ddracechat.cpp | 14 ++++---------- src/game/server/ddracecommands.cpp | 1 - src/game/server/entities/character.cpp | 1 - src/game/server/gamecontext.cpp | 14 ++++++-------- src/game/server/player.cpp | 8 +++----- src/game/server/save.cpp | 1 - src/game/server/teams.cpp | 1 - 10 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/engine/server.h b/src/engine/server.h index 3d21ddf7c..1784e6de3 100644 --- a/src/engine/server.h +++ b/src/engine/server.h @@ -145,6 +145,11 @@ public: enum { + AUTHED_NO=0, + AUTHED_HELPER, + AUTHED_MOD, + AUTHED_ADMIN, + RCON_CID_SERV=-1, RCON_CID_VOTE=-2, }; @@ -167,6 +172,12 @@ public: virtual int* GetIdMap(int ClientID) = 0; virtual bool DnsblWhite(int ClientID) = 0; + virtual const char *GetAnnouncementLine(char const *FileName) = 0; + virtual bool ClientPrevIngame(int ClientID) = 0; + virtual const char *GetNetErrorString(int ClientID) = 0; + virtual void ResetNetErrorString(int ClientID) = 0; + virtual bool SetTimedOut(int ClientID, int OrigID) = 0; + virtual void SetTimeoutProtected(int ClientID) = 0; }; class IGameServer : public IInterface diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 87e1182b6..e39d8bf86 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -2791,7 +2791,7 @@ void CServer::GetClientAddr(int ClientID, NETADDR *pAddr) } } -char *CServer::GetAnnouncementLine(char const *pFileName) +const char *CServer::GetAnnouncementLine(char const *pFileName) { IOHANDLE File = m_pStorage->OpenFile(pFileName, IOFLAG_READ, IStorage::TYPE_ALL); if(File) @@ -2831,3 +2831,16 @@ int* CServer::GetIdMap(int ClientID) { return (int*)(IdMap + VANILLA_MAX_CLIENTS * ClientID); } + +bool CServer::SetTimedOut(int ClientID, int OrigID) { + if (!m_NetServer.SetTimedOut(ClientID, OrigID)) + { + return false; + } + CGameContext *GameServer = (CGameContext *) m_pGameServer; + DelClientCallback(OrigID, "Timeout Protection used", this); + m_aClients[ClientID].m_Authed = IServer::AUTHED_NO; + if (GameServer->m_apPlayers[ClientID]->GetCharacter()) + GameServer->SendTuningParams(ClientID, GameServer->m_apPlayers[ClientID]->GetCharacter()->m_TuneZone); + return true; +} diff --git a/src/engine/server/server.h b/src/engine/server/server.h index e86d18e41..794412e25 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -98,11 +98,6 @@ public: enum { - AUTHED_NO=0, - AUTHED_HELPER, - AUTHED_MOD, - AUTHED_ADMIN, - MAX_RCONCMD_SEND=16, }; @@ -337,7 +332,7 @@ public: void GetClientAddr(int ClientID, NETADDR *pAddr); int m_aPrevStates[MAX_CLIENTS]; - char *GetAnnouncementLine(char const *FileName); + const char *GetAnnouncementLine(char const *FileName); unsigned m_AnnouncementLastLine; void RestrictRconOutput(int ClientID) { m_RconRestrict = ClientID; } @@ -351,6 +346,11 @@ public: } void AuthRemoveKey(int KeySlot); + bool ClientPrevIngame(int ClientID) { return m_aPrevStates[ClientID] == CClient::STATE_INGAME; }; + const char *GetNetErrorString(int ClientID) { return m_NetServer.ErrorString(ClientID); }; + void ResetNetErrorString(int ClientID) { m_NetServer.ResetErrorString(ClientID); }; + bool SetTimedOut(int ClientID, int OrigID); + void SetTimeoutProtected(int ClientID) { m_NetServer.SetTimeoutProtected(ClientID); }; }; #endif diff --git a/src/game/server/ddracechat.cpp b/src/game/server/ddracechat.cpp index 1fccda6bd..83cd41b5a 100644 --- a/src/game/server/ddracechat.cpp +++ b/src/game/server/ddracechat.cpp @@ -2,7 +2,6 @@ #include "gamecontext.h" #include #include -#include #include #include #include @@ -279,7 +278,7 @@ void CGameContext::ConToggleSpec(IConsole::IResult *pResult, void *pUserData) } CGameContext *pSelf = (CGameContext *) pUserData; - CServer* pServ = (CServer*)pSelf->Server(); + IServer* pServ = pSelf->Server(); CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientID]; if(!pPlayer) return; @@ -307,7 +306,7 @@ void CGameContext::ConTogglePause(IConsole::IResult *pResult, void *pUserData) return; CGameContext *pSelf = (CGameContext *) pUserData; - CServer* pServ = (CServer*)pSelf->Server(); + IServer* pServ = pSelf->Server(); CPlayer *pPlayer = pSelf->m_apPlayers[pResult->m_ClientID]; if(!pPlayer) return; @@ -539,17 +538,12 @@ void CGameContext::ConTimeout(IConsole::IResult *pResult, void *pUserData) if (i == pResult->m_ClientID) continue; if (!pSelf->m_apPlayers[i]) continue; if (str_comp(pSelf->m_apPlayers[i]->m_TimeoutCode, pResult->GetString(0))) continue; - if (((CServer *)pSelf->Server())->m_NetServer.SetTimedOut(i, pResult->m_ClientID)) - { - ((CServer *)pSelf->Server())->DelClientCallback(pResult->m_ClientID, "Timeout Protection used", ((CServer *)pSelf->Server())); - ((CServer *)pSelf->Server())->m_aClients[i].m_Authed = CServer::AUTHED_NO; - if (pSelf->m_apPlayers[i]->GetCharacter()) - ((CGameContext *)(((CServer *)pSelf->Server())->GameServer()))->SendTuningParams(i, pSelf->m_apPlayers[i]->GetCharacter()->m_TuneZone); + if (pSelf->Server()->SetTimedOut(i, pResult->m_ClientID)) { return; } } - ((CServer *)pSelf->Server())->m_NetServer.SetTimeoutProtected(pResult->m_ClientID); + pSelf->Server()->SetTimeoutProtected(pResult->m_ClientID); str_copy(pPlayer->m_TimeoutCode, pResult->GetString(0), sizeof(pPlayer->m_TimeoutCode)); } diff --git a/src/game/server/ddracecommands.cpp b/src/game/server/ddracecommands.cpp index b2fabcc71..1354ed99f 100644 --- a/src/game/server/ddracecommands.cpp +++ b/src/game/server/ddracecommands.cpp @@ -1,7 +1,6 @@ /* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */ #include "gamecontext.h" #include -#include #include #include #include diff --git a/src/game/server/entities/character.cpp b/src/game/server/entities/character.cpp index 32da9ef55..9dc3ded11 100644 --- a/src/game/server/entities/character.cpp +++ b/src/game/server/entities/character.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include "light.h" diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 26cefa655..7e20f060d 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -21,7 +21,6 @@ #include #include -#include #include "gamemodes/DDRace.h" #include "score.h" #include "score/file_score.h" @@ -767,7 +766,7 @@ void CGameContext::OnTick() if(Server()->Tick() % (g_Config.m_SvAnnouncementInterval * Server()->TickSpeed() * 60) == 0) { - char *Line = ((CServer *) Server())->GetAnnouncementLine(g_Config.m_SvAnnouncementFileName); + const char *Line = Server()->GetAnnouncementLine(g_Config.m_SvAnnouncementFileName); if(Line) SendChat(-1, CGameContext::CHAT_ALL, Line); } @@ -917,7 +916,7 @@ void CGameContext::OnClientEnter(int ClientID) Score()->LoadScore(ClientID); Score()->CheckBirthday(ClientID); - if(((CServer *) Server())->m_aPrevStates[ClientID] < CServer::CClient::STATE_INGAME) + if(!Server()->ClientPrevIngame(ClientID)) { char aBuf[512]; str_format(aBuf, sizeof(aBuf), "'%s' entered and joined the %s", Server()->ClientName(ClientID), m_pController->GetTeamName(m_apPlayers[ClientID]->GetTeam())); @@ -946,7 +945,7 @@ void CGameContext::OnClientEnter(int ClientID) if(m_VoteCloseTime) SendVoteSet(ClientID); - m_apPlayers[ClientID]->m_Authed = ((CServer*)Server())->m_aClients[ClientID].m_Authed; + m_apPlayers[ClientID]->m_Authed = Server()->IsAuthed(ClientID); } void CGameContext::OnClientConnected(int ClientID) @@ -1131,7 +1130,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) Console()->SetFlagMask(CFGFLAG_CHAT); if (pPlayer->m_Authed) - Console()->SetAccessLevel(pPlayer->m_Authed == CServer::AUTHED_ADMIN ? IConsole::ACCESS_LEVEL_ADMIN : pPlayer->m_Authed == CServer::AUTHED_MOD ? IConsole::ACCESS_LEVEL_MOD : IConsole::ACCESS_LEVEL_HELPER); + Console()->SetAccessLevel(pPlayer->m_Authed == IServer::AUTHED_ADMIN ? IConsole::ACCESS_LEVEL_ADMIN : pPlayer->m_Authed == IServer::AUTHED_MOD ? IConsole::ACCESS_LEVEL_MOD : IConsole::ACCESS_LEVEL_HELPER); else Console()->SetAccessLevel(IConsole::ACCESS_LEVEL_USER); Console()->SetPrintOutputLevel(m_ChatPrintCBIndex, 0); @@ -1312,7 +1311,7 @@ void CGameContext::OnMessage(int MsgID, CUnpacker *pUnpacker, int ClientID) if(!pOption) { - if (pPlayer->m_Authed != CServer::AUTHED_ADMIN) // allow admins to call any vote they want + if (pPlayer->m_Authed != IServer::AUTHED_ADMIN) // allow admins to call any vote they want { str_format(aChatmsg, sizeof(aChatmsg), "'%s' isn't an option on this server", pMsg->m_Value); SendChatTarget(ClientID, aChatmsg); @@ -2952,12 +2951,11 @@ float CGameContext::PlayerJetpack() void CGameContext::OnSetAuthed(int ClientID, int Level) { - CServer *pServ = (CServer*)Server(); if(m_apPlayers[ClientID]) { m_apPlayers[ClientID]->m_Authed = Level; char aBuf[512], aIP[NETADDR_MAXSTRSIZE]; - pServ->GetClientAddr(ClientID, aIP, sizeof(aIP)); + Server()->GetClientAddr(ClientID, aIP, sizeof(aIP)); str_format(aBuf, sizeof(aBuf), "ban %s %d Banned by vote", aIP, g_Config.m_SvVoteKickBantime); if(!str_comp_nocase(m_aVoteCommand, aBuf) && Level > 0) { diff --git a/src/game/server/player.cpp b/src/game/server/player.cpp index 8da584881..cc156fb76 100644 --- a/src/game/server/player.cpp +++ b/src/game/server/player.cpp @@ -5,7 +5,6 @@ #include "player.h" #include -#include #include "gamecontext.h" #include #include @@ -181,12 +180,12 @@ void CPlayer::Tick() } } - if(((CServer *)Server())->m_NetServer.ErrorString(m_ClientID)[0]) + if(Server()->GetNetErrorString(m_ClientID)[0]) { char aBuf[512]; str_format(aBuf, sizeof(aBuf), "'%s' would have timed out, but can use timeout protection now", Server()->ClientName(m_ClientID)); GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf); - ((CServer *)(Server()))->m_NetServer.ResetErrorString(m_ClientID); + Server()->ResetNetErrorString(m_ClientID); } if(!GameServer()->m_World.m_Paused) @@ -618,8 +617,7 @@ bool CPlayer::AfkTimer(int NewTargetX, int NewTargetY) } else if(m_LastPlaytime < time_get()-time_freq()*g_Config.m_SvMaxAfkTime) { - CServer* serv = (CServer*)m_pGameServer->Server(); - serv->Kick(m_ClientID,"Away from keyboard"); + m_pGameServer->Server()->Kick(m_ClientID, "Away from keyboard"); return true; } } diff --git a/src/game/server/save.cpp b/src/game/server/save.cpp index 449325be6..d23d4f465 100644 --- a/src/game/server/save.cpp +++ b/src/game/server/save.cpp @@ -3,7 +3,6 @@ #include "save.h" #include "teams.h" -#include #include "./gamemodes/DDRace.h" #include diff --git a/src/game/server/teams.cpp b/src/game/server/teams.cpp index b2815c6c5..70f06afe4 100644 --- a/src/game/server/teams.cpp +++ b/src/game/server/teams.cpp @@ -1,7 +1,6 @@ /* (c) Shereef Marzouk. See "licence DDRace.txt" and the readme.txt in the root of the distribution for more information. */ #include "teams.h" #include -#include CGameTeams::CGameTeams(CGameContext *pGameContext) : m_pGameContext(pGameContext)