Skip to content

Commit 5498e64

Browse files
authored
Merge pull request ddnet#9097 from Robyt3/Maplist
List available maps as console arguments for `sv_map`/`change_map`, refactoring
2 parents 9e376be + a8f3c5f commit 5498e64

File tree

8 files changed

+329
-40
lines changed

8 files changed

+329
-40
lines changed

src/engine/client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ class IClient : public IInterface
214214
virtual void Rcon(const char *pLine) = 0;
215215
virtual bool ReceivingRconCommands() const = 0;
216216
virtual float GotRconCommandsPercentage() const = 0;
217+
virtual bool ReceivingMaplist() const = 0;
218+
virtual float GotMaplistPercentage() const = 0;
219+
virtual const std::vector<std::string> &MaplistEntries() const = 0;
217220

218221
// server info
219222
virtual void GetServerInfo(class CServerInfo *pServerInfo) const = 0;

src/engine/client/client.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,24 @@ void CClient::Rcon(const char *pCmd)
296296

297297
float CClient::GotRconCommandsPercentage() const
298298
{
299-
if(m_ExpectedRconCommands < 1)
299+
if(m_ExpectedRconCommands <= 0)
300300
return -1.0f;
301301
if(m_GotRconCommands > m_ExpectedRconCommands)
302302
return -1.0f;
303303

304304
return (float)m_GotRconCommands / (float)m_ExpectedRconCommands;
305305
}
306306

307+
float CClient::GotMaplistPercentage() const
308+
{
309+
if(m_ExpectedMaplistEntries <= 0)
310+
return -1.0f;
311+
if(m_vMaplistEntries.size() > (size_t)m_ExpectedMaplistEntries)
312+
return -1.0f;
313+
314+
return (float)m_vMaplistEntries.size() / (float)m_ExpectedMaplistEntries;
315+
}
316+
307317
bool CClient::ConnectionProblems() const
308318
{
309319
return m_aNetClient[g_Config.m_ClDummy].GotProblems(MaxLatencyTicks() * time_freq() / GameTickSpeed()) != 0;
@@ -667,6 +677,8 @@ void CClient::DisconnectWithReason(const char *pReason)
667677
m_ExpectedRconCommands = -1;
668678
m_GotRconCommands = 0;
669679
m_pConsole->DeregisterTempAll();
680+
m_ExpectedMaplistEntries = -1;
681+
m_vMaplistEntries.clear();
670682
m_aNetClient[CONN_MAIN].Disconnect(pReason);
671683
SetState(IClient::STATE_OFFLINE);
672684
m_pMap->Unload();
@@ -1846,6 +1858,8 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
18461858
{
18471859
m_pConsole->DeregisterTempAll();
18481860
m_ExpectedRconCommands = -1;
1861+
m_vMaplistEntries.clear();
1862+
m_ExpectedMaplistEntries = -1;
18491863
}
18501864
}
18511865
}
@@ -2214,8 +2228,8 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
22142228
}
22152229
else if(Conn == CONN_MAIN && (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_RCON_CMD_GROUP_START)
22162230
{
2217-
int ExpectedRconCommands = Unpacker.GetInt();
2218-
if(Unpacker.Error())
2231+
const int ExpectedRconCommands = Unpacker.GetInt();
2232+
if(Unpacker.Error() || ExpectedRconCommands < 0)
22192233
return;
22202234

22212235
m_ExpectedRconCommands = ExpectedRconCommands;
@@ -2225,6 +2239,34 @@ void CClient::ProcessServerPacket(CNetChunk *pPacket, int Conn, bool Dummy)
22252239
{
22262240
m_ExpectedRconCommands = -1;
22272241
}
2242+
else if(Conn == CONN_MAIN && (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_MAPLIST_ADD)
2243+
{
2244+
while(true)
2245+
{
2246+
const char *pMapName = Unpacker.GetString(CUnpacker::SANITIZE_CC | CUnpacker::SKIP_START_WHITESPACES);
2247+
if(Unpacker.Error())
2248+
{
2249+
return;
2250+
}
2251+
if(pMapName[0] != '\0')
2252+
{
2253+
m_vMaplistEntries.emplace_back(pMapName);
2254+
}
2255+
}
2256+
}
2257+
else if(Conn == CONN_MAIN && (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_MAPLIST_GROUP_START)
2258+
{
2259+
const int ExpectedMaplistEntries = Unpacker.GetInt();
2260+
if(Unpacker.Error() || ExpectedMaplistEntries < 0)
2261+
return;
2262+
2263+
m_vMaplistEntries.clear();
2264+
m_ExpectedMaplistEntries = ExpectedMaplistEntries;
2265+
}
2266+
else if(Conn == CONN_MAIN && (pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0 && Msg == NETMSG_MAPLIST_GROUP_END)
2267+
{
2268+
m_ExpectedMaplistEntries = -1;
2269+
}
22282270
}
22292271
else if((pPacket->m_Flags & NET_CHUNKFLAG_VITAL) != 0)
22302272
{

src/engine/client/client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class CClient : public IClient, public CDemoPlayer::IListener
124124
char m_aPassword[sizeof(g_Config.m_Password)] = "";
125125
bool m_SendPassword = false;
126126

127+
int m_ExpectedMaplistEntries = -1;
128+
std::vector<std::string> m_vMaplistEntries;
129+
127130
// version-checking
128131
char m_aVersionStr[10] = "0";
129132

@@ -299,6 +302,9 @@ class CClient : public IClient, public CDemoPlayer::IListener
299302
void Rcon(const char *pCmd) override;
300303
bool ReceivingRconCommands() const override { return m_ExpectedRconCommands > 0; }
301304
float GotRconCommandsPercentage() const override;
305+
bool ReceivingMaplist() const override { return m_ExpectedMaplistEntries > 0; }
306+
float GotMaplistPercentage() const override;
307+
const std::vector<std::string> &MaplistEntries() const override { return m_vMaplistEntries; }
302308

303309
bool ConnectionProblems() const override;
304310

0 commit comments

Comments
 (0)