forked from DaemonEngine/Daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsv_ccmds.cpp
More file actions
439 lines (366 loc) · 11.6 KB
/
sv_ccmds.cpp
File metadata and controls
439 lines (366 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
===========================================================================
Daemon GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Daemon GPL Source Code (Daemon Source Code).
Daemon Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daemon Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daemon Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code. If not, please request a copy in writing from id Software at the address
below.
If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.
===========================================================================
*/
#include "server.h"
#include "framework/CvarSystem.h"
/*
===============================================================================
OPERATOR CONSOLE ONLY COMMANDS
These commands can only be entered from stdin or by a remote operator datagram
===============================================================================
*/
class MapCmd: public Cmd::StaticCmd {
public:
MapCmd(Str::StringRef name, Str::StringRef description, bool cheat):
Cmd::StaticCmd(name, Cmd::SERVER, description), cheat(cheat) {
}
void Run(const Cmd::Args& args) const override {
if (args.Argc() < 2) {
PrintUsage(args, "<mapname> (layoutname)", "loads a new map");
return;
}
// An immediate shutdown occurs if the time hits 0x7F000000. Check for a smaller time
// here to try to shut down a server only at the end of a game. This will happen
// after 21.7 days uptime
if (Sys::Milliseconds() > 0x70000000) {
Sys::Error("Shutting down to prevent time overflow");
}
const std::string& mapName = args.Argv(1);
// For non-legacy paks, a map named "foo" must be in the pak "map-foo"
std::string pakName;
//Make sure the map exists to avoid typos that would kill the game
// The map searching algorithms here should be in sync with GetAvailableMaps()
if (FS::FindPak("map-" + mapName) != nullptr) {
pakName = "map-" + mapName;
} else if (FS::UseLegacyPaks()) {
// Load all legacy paks
for (const FS::PakInfo& pak : FS::GetAvailablePaks()) {
if (pak.version.empty()) {
std::error_code ignored;
FS::PakPath::LoadPakPrefix(pak, "maps/", ignored);
}
}
if (const FS::PakInfo* pak = FS::PakPath::LocateFile(Str::Format("maps/%s.bsp", mapName))) {
pakName = pak->name;
}
}
if (pakName.empty()) {
Print("Can't find map %s", mapName);
return;
}
//Gets the layout list from the command but do not override if there is nothing
std::string layouts = args.ConcatArgs(2);
if (not layouts.empty()) {
Cvar::SetValue("g_layouts", layouts);
}
Cvar::SetValueForce("sv_cheats", cheat ? "1" : "0");
SV_SpawnServer(pakName, mapName);
}
Cmd::CompletionResult Complete(int argNum, const Cmd::Args& args, Str::StringRef prefix) const override {
if (argNum == 1) {
Cmd::CompletionResult out;
for (auto& map: FS::GetAvailableMaps(true)) {
if (Str::IsPrefix(prefix, map))
out.push_back({map, ""});
}
return out;
} else if (argNum > 1) {
// FIXME: a layout could also be in a pak?
return FS::HomePath::CompleteFilename(prefix, "game/layouts/" + args.Argv(1), ".dat", false, true);
}
return {};
}
private:
bool cheat;
};
static MapCmd MapCmdRegistration("map", "starts a new map", false);
static MapCmd DevmapCmdRegistration("devmap", "starts a new map with cheats enabled", true);
void MSG_PrioritiseEntitystateFields();
void MSG_PrioritisePlayerStateFields();
static void SV_FieldInfo_f()
{
MSG_PrioritiseEntitystateFields();
MSG_PrioritisePlayerStateFields();
}
/*
================
SV_MapRestart_f
Completely restarts a level, but doesn't send a new gamestate to the clients.
This allows fair starts with variable load times.
================
*/
static void SV_MapRestart_f()
{
int i;
client_t *client;
bool denied;
char reason[ MAX_STRING_CHARS ];
bool isBot;
// make sure we aren't restarting twice in the same frame
if ( com_frameTime == sv.serverId )
{
return;
}
// make sure server is running
if ( !com_sv_running.Get() )
{
Log::Notice( "Server is not running." );
return;
}
// toggle the server bit so clients can detect that a
// map_restart has happened
svs.snapFlagServerBit ^= SNAPFLAG_SERVERCOUNT;
// generate a new serverid
// TTimo - don't update restartedserverId there, otherwise we won't deal correctly with multiple map_restart
sv.serverId = com_frameTime;
Cvar::SetValueForce( "sv_serverid", va( "%i", sv.serverId ) );
// reset all the VM data in place without changing memory allocation
// note that we do NOT set sv.state = SS_LOADING, so configstrings that
// had been changed from their default values will generate broadcast updates
sv.state = serverState_t::SS_LOADING;
sv.restarting = true;
SV_RestartGameProgs();
// run a few frames to allow everything to settle
for ( i = 0; i < GAME_INIT_FRAMES; i++ )
{
gvm.GameRunFrame( sv.time );
svs.time += FRAMETIME;
sv.time += FRAMETIME;
}
// create a baseline for more efficient communications
// Gordon: meh, this won't work here as the client doesn't know it has happened
// SV_CreateBaseline ();
sv.state = serverState_t::SS_GAME;
sv.restarting = false;
// connect and begin all the clients
for ( i = 0; i < sv_maxClients.Get(); i++ )
{
client = &svs.clients[ i ];
// send the new gamestate to all connected clients
if ( client->state < clientState_t::CS_CONNECTED )
{
continue;
}
isBot = SV_IsBot(client);
// add the map_restart command
SV_AddServerCommand( client, "map_restart\n" );
// connect the client again, without the firstTime flag
denied = gvm.GameClientConnect( reason, sizeof( reason ), i, false, isBot );
if ( denied )
{
// this generally shouldn't happen, because the client
// was connected before the level change
SV_DropClient( client, reason );
if ( !isBot )
{
Log::Notice( "SV_MapRestart_f: dropped client %i: denied!", i );
}
continue;
}
client->state = clientState_t::CS_ACTIVE;
SV_ClientEnterWorld( client, &client->lastUsercmd );
}
// run another frame to allow things to look at all the players
gvm.GameRunFrame( sv.time );
svs.time += FRAMETIME;
sv.time += FRAMETIME;
}
class StatusCmd: public Cmd::StaticCmd
{
public:
StatusCmd():
StaticCmd("status", Cmd::SERVER, "Shows a table with server and player information")
{}
void Run(const Cmd::Args&) const override
{
// make sure server is running
if ( !com_sv_running.Get() )
{
Log::Notice( "Server is not running." );
return;
}
double cpu = ( svs.stats.latched_active + svs.stats.latched_idle );
if ( cpu )
{
cpu = 100 * svs.stats.latched_active / cpu;
}
auto players = std::count_if(
svs.clients,
svs.clients+sv_maxClients.Get(),
[](const client_t& cl) {
return cl.state != clientState_t::CS_FREE;
}
);
std::string time_string;
auto seconds = svs.time / 1000 % 60;
auto minutes = svs.time / 1000 / 60 % 60;
if ( auto hours = svs.time / 1000 / 60 / 60 / 60 )
{
time_string = Str::Format("%02d:", hours);
}
time_string += Str::Format("%02d:%02d", minutes, seconds);
Print(
"(begin server status)\n"
"hostname: %s\n"
"version: %s\n"
"protocol: %d\n"
"cpu: %.0f%%\n"
"time: %s\n"
"map: %s\n"
"players: %d / %d\n"
"num score connection address port name\n"
"--- ----- ---------- ---------------------- ------ ----",
sv_hostname.Get(),
Q3_VERSION " on " Q3_ENGINE,
PROTOCOL_VERSION,
cpu,
time_string,
sv_mapname.Get(),
players,
sv_maxClients.Get()
);
for ( int i = 0; i < sv_maxClients.Get(); i++ )
{
const client_t& cl = svs.clients[i];
if ( cl.state == clientState_t::CS_FREE )
{
continue;
}
std::string connection;
if ( cl.state == clientState_t::CS_CONNECTED )
{
connection = "CONNECTED";
}
else if ( cl.state == clientState_t::CS_ZOMBIE )
{
connection = "ZOMBIE";
}
else
{
connection = std::to_string(cl.ping);
if ( connection.size() > 10 )
{
connection = "ERROR";
}
}
OpaquePlayerState* ps = SV_GameClientNum( i );
const char *address = NET_AdrToString( cl.netchan.remoteAddress );
Print(
"%3i %5i %10s %-22s %-6i %s",
i,
ps->persistant[ PERS_SCORE ],
connection,
address,
cl.netchan.qport,
cl.name
);
}
Print( "(end server status)" );
}
};
static StatusCmd StatusCmdRegistration;
/*
===========
SV_Serverinfo_f
Examine the serverinfo string
===========
*/
static void SV_Serverinfo_f()
{
// make sure server is running
if ( !com_sv_running.Get() )
{
Log::Notice( "Server is not running." );
return;
}
Log::Notice( "Server info settings:" );
Info_Print( Cvar_InfoString( CVAR_SERVERINFO, false ) );
}
/*
===========
SV_Systeminfo_f
Examine the systeminfo string
===========
*/
static void SV_Systeminfo_f()
{
// make sure server is running
if ( !com_sv_running.Get() )
{
Log::Notice( "Server is not running." );
return;
}
Log::Notice( "System info settings:" );
Info_Print( Cvar_InfoString( CVAR_SYSTEMINFO, false ) );
}
class ListMapsCmd: public Cmd::StaticCmd
{
public:
ListMapsCmd():
StaticCmd("listmaps", Cmd::SERVER, "Lists all maps available to the server")
{}
void Run(const Cmd::Args&) const override
{
auto maps = FS::GetAvailableMaps(true);
Print("Listing %d maps:", maps.size());
for ( const auto& map: maps )
{
Print(map.c_str());
}
}
};
#if BUILD_SERVER
static ListMapsCmd ListMapsCmdRegistration;
#endif
/*
==================
SV_AddOperatorCommands
==================
*/
void SV_AddOperatorCommands()
{
if ( com_sv_running.Get() )
{
// These commands should only be available while the server is running.
Cmd_AddCommand( "fieldinfo", SV_FieldInfo_f );
Cmd_AddCommand( "heartbeat", SV_Heartbeat_f );
Cmd_AddCommand( "map_restart", SV_MapRestart_f );
Cmd_AddCommand( "serverinfo", SV_Serverinfo_f );
Cmd_AddCommand( "systeminfo", SV_Systeminfo_f );
}
}
/*
==================
SV_RemoveOperatorCommands
==================
*/
void SV_RemoveOperatorCommands()
{
Cmd_RemoveCommand( "fieldinfo" );
Cmd_RemoveCommand( "heartbeat" );
Cmd_RemoveCommand( "map_restart" );
Cmd_RemoveCommand( "serverinfo" );
Cmd_RemoveCommand( "systeminfo" );
}