Skip to content

Commit

Permalink
merged "List maps" by Learath2 and cinaera. skipped redundant "maps" …
Browse files Browse the repository at this point in the history
…command. closes #1086
  • Loading branch information
oy committed Jan 12, 2019
1 parent 981cba1 commit bc38f67
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 6 deletions.
4 changes: 3 additions & 1 deletion scripts/cmd5.py
Expand Up @@ -29,5 +29,7 @@ def cstrip(lines):
f += cstrip([l.strip() for l in open(filename, "rb")])

hash = hashlib.md5(f).hexdigest().lower()[16:]
#TODO 0.7: improve nethash creation
#TODO 0.8: improve nethash creation
if hash == "0a027ded5791b521":
hash = "802f1be60a05665f"
print('#define GAME_NETVERSION_HASH "%s"' % hash)
14 changes: 14 additions & 0 deletions src/engine/client/client.cpp
Expand Up @@ -509,6 +509,7 @@ void CClient::Connect(const char *pAddress)
}

m_RconAuthed = 0;
m_UseTempRconCommands = 0;
if(m_ServerAddress.port == 0)
m_ServerAddress.port = Port;
m_NetClient.Connect(&m_ServerAddress);
Expand Down Expand Up @@ -1175,6 +1176,18 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
if(Unpacker.Error() == 0)
m_pConsole->DeregisterTemp(pName);
}
else if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_MAPLIST_ENTRY_ADD)
{
const char *pName = Unpacker.GetString(CUnpacker::SANITIZE_CC);
if(Unpacker.Error() == 0)
m_pConsole->RegisterTempMap(pName);
}
else if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_MAPLIST_ENTRY_REM)
{
const char *pName = Unpacker.GetString(CUnpacker::SANITIZE_CC);
if(Unpacker.Error() == 0)
m_pConsole->DeregisterTempMap(pName);
}
else if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_RCON_AUTH_ON)
{
m_RconAuthed = 1;
Expand All @@ -1186,6 +1199,7 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket)
if(m_UseTempRconCommands)
m_pConsole->DeregisterTempAll();
m_UseTempRconCommands = 0;
m_pConsole->DeregisterTempMapAll();
}
else if((pPacket->m_Flags&NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_RCON_LINE)
{
Expand Down
6 changes: 6 additions & 0 deletions src/engine/console.h
Expand Up @@ -24,6 +24,8 @@ class IConsole : public IInterface
TEMPCMD_HELP_LENGTH=96,
TEMPCMD_PARAMS_LENGTH=16,

TEMPMAP_NAME_LENGTH = 32,

MAX_PRINT_CB=4,
};

Expand Down Expand Up @@ -67,12 +69,16 @@ class IConsole : public IInterface
virtual const CCommandInfo *FirstCommandInfo(int AccessLevel, int Flagmask) const = 0;
virtual const CCommandInfo *GetCommandInfo(const char *pName, int FlagMask, bool Temp) = 0;
virtual void PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser) = 0;
virtual void PossibleMaps(const char *pStr, FPossibleCallback pfnCallback, void *pUser) = 0;
virtual void ParseArguments(int NumArgs, const char **ppArguments) = 0;

virtual void Register(const char *pName, const char *pParams, int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp) = 0;
virtual void RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp) = 0;
virtual void DeregisterTemp(const char *pName) = 0;
virtual void DeregisterTempAll() = 0;
virtual void RegisterTempMap(const char *pName) = 0;
virtual void DeregisterTempMap(const char *pName) = 0;
virtual void DeregisterTempMapAll() = 0;
virtual void Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser) = 0;
virtual void StoreCommands(bool Store) = 0;

Expand Down
97 changes: 96 additions & 1 deletion src/engine/server/server.cpp
Expand Up @@ -280,6 +280,10 @@ CServer::CServer() : m_DemoRecorder(&m_SnapshotDelta)
m_pCurrentMapData = 0;
m_CurrentMapSize = 0;

m_NumMapEntries = 0;
m_pFirstMapEntry = 0;
m_pLastMapEntry = 0;

m_MapReload = 0;

m_RconClientID = IServer::RCON_CID_SERV;
Expand Down Expand Up @@ -664,6 +668,7 @@ int CServer::NewClientCallback(int ClientID, void *pUser)
pThis->m_aClients[ClientID].m_Authed = AUTHED_NO;
pThis->m_aClients[ClientID].m_AuthTries = 0;
pThis->m_aClients[ClientID].m_pRconCmdToSend = 0;
pThis->m_aClients[ClientID].m_pMapListEntryToSend = 0;
pThis->m_aClients[ClientID].m_NoRconNote = false;
pThis->m_aClients[ClientID].m_Quitting = false;
pThis->m_aClients[ClientID].Reset();
Expand Down Expand Up @@ -694,6 +699,7 @@ int CServer::DelClientCallback(int ClientID, const char *pReason, void *pUser)
pThis->m_aClients[ClientID].m_Authed = AUTHED_NO;
pThis->m_aClients[ClientID].m_AuthTries = 0;
pThis->m_aClients[ClientID].m_pRconCmdToSend = 0;
pThis->m_aClients[ClientID].m_pMapListEntryToSend = 0;
pThis->m_aClients[ClientID].m_NoRconNote = false;
pThis->m_aClients[ClientID].m_Quitting = false;
pThis->m_aClients[ClientID].m_Snapshots.PurgeAll();
Expand Down Expand Up @@ -774,6 +780,36 @@ void CServer::UpdateClientRconCommands()
}
}

void CServer::SendMapListEntryAdd(const CMapListEntry *pMapListEntry, int ClientID)
{
CMsgPacker Msg(NETMSG_MAPLIST_ENTRY_ADD, true);
Msg.AddString(pMapListEntry->m_aName, 256);
SendMsg(&Msg, MSGFLAG_VITAL, ClientID);
}

void CServer::SendMapListEntryRem(const CMapListEntry *pMapListEntry, int ClientID)
{
CMsgPacker Msg(NETMSG_MAPLIST_ENTRY_REM, true);
Msg.AddString(pMapListEntry->m_aName, 256);
SendMsg(&Msg, MSGFLAG_VITAL, ClientID);
}


void CServer::UpdateClientMapListEntries()
{
for(int ClientID = Tick() % MAX_RCONCMD_RATIO; ClientID < MaxClients(); ClientID += MAX_RCONCMD_RATIO)
{
if(m_aClients[ClientID].m_State != CClient::STATE_EMPTY && m_aClients[ClientID].m_Authed)
{
for(int i = 0; i < MAX_MAPLISTENTRY_SEND && m_aClients[ClientID].m_pMapListEntryToSend; ++i)
{
SendMapListEntryAdd(m_aClients[ClientID].m_pMapListEntryToSend, ClientID);
m_aClients[ClientID].m_pMapListEntryToSend = m_aClients[ClientID].m_pMapListEntryToSend->m_pNext;
}
}
}
}

void CServer::ProcessClientPacket(CNetChunk *pPacket)
{
int ClientID = pPacket->m_ClientID;
Expand Down Expand Up @@ -979,6 +1015,7 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)

m_aClients[ClientID].m_Authed = AUTHED_ADMIN;
m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_ADMIN, CFGFLAG_SERVER);
m_aClients[ClientID].m_pMapListEntryToSend = m_pFirstMapEntry;
SendRconLine(ClientID, "Admin authentication successful. Full remote console access granted.");
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (admin)", ClientID);
Expand All @@ -992,6 +1029,9 @@ void CServer::ProcessClientPacket(CNetChunk *pPacket)
m_aClients[ClientID].m_Authed = AUTHED_MOD;
m_aClients[ClientID].m_pRconCmdToSend = Console()->FirstCommandInfo(IConsole::ACCESS_LEVEL_MOD, CFGFLAG_SERVER);
SendRconLine(ClientID, "Moderator authentication successful. Limited remote console access granted.");
const IConsole::CCommandInfo *pInfo = Console()->GetCommandInfo("sv_map", CFGFLAG_SERVER, false);
if(pInfo && pInfo->GetAccessLevel() == IConsole::ACCESS_LEVEL_MOD)
m_aClients[ClientID].m_pMapListEntryToSend = m_pFirstMapEntry;
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "ClientID=%d authed (moderator)", ClientID);
Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
Expand Down Expand Up @@ -1232,6 +1272,13 @@ int CServer::Run()
//
m_PrintCBIndex = Console()->RegisterPrintCallback(g_Config.m_ConsoleOutputLevel, SendRconLineAuthed, this);

// list maps
m_pMapListHeap = new CHeap();
CSubdirCallbackUserdata Userdata;
Userdata.m_pServer = this;
str_copy(Userdata.m_aName, "", sizeof(Userdata.m_aName));
m_pStorage->ListDirectory(IStorage::TYPE_ALL, "maps/", MapListEntryCallback, &Userdata);

// load map
if(!LoadMap(g_Config.m_SvMap))
{
Expand Down Expand Up @@ -1361,6 +1408,7 @@ int CServer::Run()
DoSnapshot();

UpdateClientRconCommands();
UpdateClientMapListEntries();
}

// master server stuff
Expand Down Expand Up @@ -1414,6 +1462,52 @@ int CServer::Run()
return 0;
}

int CServer::MapListEntryCallback(const char *pFilename, int IsDir, int DirType, void *pUser)
{
CSubdirCallbackUserdata *pUserdata = (CSubdirCallbackUserdata *)pUser;
CServer *pThis = pUserdata->m_pServer;

if(pFilename[0] == '.') // hidden files
return 0;

char aFilename[512];
if(pUserdata->m_aName[0])
str_format(aFilename, sizeof(aFilename), "%s/%s", pUserdata->m_aName, pFilename);
else
str_format(aFilename, sizeof(aFilename), "%s", pFilename);

if(IsDir)
{
CSubdirCallbackUserdata Userdata;
Userdata.m_pServer = pThis;
str_copy(Userdata.m_aName, aFilename, sizeof(Userdata.m_aName));
char FindPath[512];
str_format(FindPath, sizeof(FindPath), "maps/%s/", aFilename);
pThis->m_pStorage->ListDirectory(IStorage::TYPE_ALL, FindPath, MapListEntryCallback, &Userdata);
return 0;
}

const char *pSuffix = str_endswith(aFilename, ".map");
if(!pSuffix) // not ending with .map
{
return 0;
}

CMapListEntry *pEntry = (CMapListEntry *)pThis->m_pMapListHeap->Allocate(sizeof(CMapListEntry));
pThis->m_NumMapEntries++;
pEntry->m_pNext = 0;
pEntry->m_pPrev = pThis->m_pLastMapEntry;
if(pEntry->m_pPrev)
pEntry->m_pPrev->m_pNext = pEntry;
pThis->m_pLastMapEntry = pEntry;
if(!pThis->m_pFirstMapEntry)
pThis->m_pFirstMapEntry = pEntry;

str_truncate(pEntry->m_aName, sizeof(pEntry->m_aName), aFilename, pSuffix-aFilename);

return 0;
}

void CServer::ConKick(IConsole::IResult *pResult, void *pUser)
{
if(pResult->NumArguments() > 1)
Expand Down Expand Up @@ -1446,7 +1540,7 @@ void CServer::ConStatus(IConsole::IResult *pResult, void *pUser)
}
else
str_format(aBuf, sizeof(aBuf), "id=%d addr=%s connecting", i, aAddrStr);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "Server", aBuf);
pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "server", aBuf);
}
}
}
Expand Down Expand Up @@ -1518,6 +1612,7 @@ void CServer::ConLogout(IConsole::IResult *pResult, void *pUser)
pServer->m_aClients[pServer->m_RconClientID].m_Authed = AUTHED_NO;
pServer->m_aClients[pServer->m_RconClientID].m_AuthTries = 0;
pServer->m_aClients[pServer->m_RconClientID].m_pRconCmdToSend = 0;
pServer->m_aClients[pServer->m_RconClientID].m_pMapListEntryToSend = 0;
pServer->SendRconLine(pServer->m_RconClientID, "Logout successful.");
char aBuf[32];
str_format(aBuf, sizeof(aBuf), "ClientID=%d logged out", pServer->m_RconClientID);
Expand Down
30 changes: 29 additions & 1 deletion src/engine/server/server.h
Expand Up @@ -4,7 +4,7 @@
#define ENGINE_SERVER_SERVER_H

#include <engine/server.h>

#include <engine/shared/memheap.h>

class CSnapIDPool
{
Expand Down Expand Up @@ -76,9 +76,12 @@ class CServer : public IServer
AUTHED_ADMIN,

MAX_RCONCMD_SEND=16,
MAX_MAPLISTENTRY_SEND = 32,
MAX_RCONCMD_RATIO=8,
};

class CMapListEntry;

class CClient
{
public:
Expand Down Expand Up @@ -128,6 +131,7 @@ class CServer : public IServer
bool m_NoRconNote;
bool m_Quitting;
const IConsole::CCommandInfo *m_pRconCmdToSend;
const CMapListEntry *m_pMapListEntryToSend;

void Reset();
};
Expand Down Expand Up @@ -163,6 +167,25 @@ class CServer : public IServer
int m_CurrentMapSize;
int m_MapChunksPerRequest;

//maplist
struct CMapListEntry
{
CMapListEntry *m_pPrev;
CMapListEntry *m_pNext;
char m_aName[IConsole::TEMPMAP_NAME_LENGTH];
};

struct CSubdirCallbackUserdata
{
CServer *m_pServer;
char m_aName[IConsole::TEMPMAP_NAME_LENGTH];
};

CHeap *m_pMapListHeap;
CMapListEntry *m_pLastMapEntry;
CMapListEntry *m_pFirstMapEntry;
int m_NumMapEntries;

int m_RconPasswordSet;
int m_GeneratedRconPassword;

Expand Down Expand Up @@ -214,6 +237,9 @@ class CServer : public IServer
void SendRconCmdAdd(const IConsole::CCommandInfo *pCommandInfo, int ClientID);
void SendRconCmdRem(const IConsole::CCommandInfo *pCommandInfo, int ClientID);
void UpdateClientRconCommands();
void SendMapListEntryAdd(const CMapListEntry *pMapListEntry, int ClientID);
void SendMapListEntryRem(const CMapListEntry *pMapListEntry, int ClientID);
void UpdateClientMapListEntries();

void ProcessClientPacket(CNetChunk *pPacket);

Expand All @@ -228,6 +254,8 @@ class CServer : public IServer
void InitRegister(CNetServer *pNetServer, IEngineMasterServer *pMasterServer, IConsole *pConsole);
int Run();

static int MapListEntryCallback(const char *pFilename, int IsDir, int DirType, void *pUser);

static void ConKick(IConsole::IResult *pResult, void *pUser);
static void ConStatus(IConsole::IResult *pResult, void *pUser);
static void ConShutdown(IConsole::IResult *pResult, void *pUser);
Expand Down

0 comments on commit bc38f67

Please sign in to comment.