This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/server/sv_user.c

540 lines
13 KiB
C
Raw Normal View History

2007-06-21 22:00:00 +02:00
/*
Copyright (C) 1997-2001 Id Software, Inc.
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// sv_user.c -- server code for moving users
#include "engine.h"
#include "server.h"
2007-09-06 22:00:00 +02:00
edict_t *sv_player;
2007-06-21 22:00:00 +02:00
/*
============================================================
USER STRINGCMD EXECUTION
sv_client and sv_player will be valid.
============================================================
*/
/*
================
SV_New_f
Sends the first message from the server to a connected client.
This will be sent on the initial connection and upon each server load.
================
*/
void SV_New_f (void)
{
2007-08-17 22:00:00 +02:00
int playernum;
2007-09-06 22:00:00 +02:00
edict_t *ent;
2007-06-21 22:00:00 +02:00
if (sv_client->state != cs_connected)
{
2007-07-28 22:00:00 +02:00
Msg ("New not valid -- already spawned\n");
2007-06-21 22:00:00 +02:00
return;
}
// send the serverdata
MSG_WriteByte (&sv_client->netchan.message, svc_serverdata);
MSG_WriteLong (&sv_client->netchan.message, PROTOCOL_VERSION);
MSG_WriteLong (&sv_client->netchan.message, svs.spawncount);
2007-11-17 22:00:00 +01:00
if(sv.state == ss_cinematic) playernum = -1;
2007-08-17 22:00:00 +02:00
else playernum = sv_client - svs.clients;
2007-09-22 22:00:00 +02:00
MSG_WriteShort (&sv_client->netchan.message, playernum );
2007-06-21 22:00:00 +02:00
// send full levelname
MSG_WriteString (&sv_client->netchan.message, sv.configstrings[CS_NAME]);
// game server
2008-05-22 22:00:00 +02:00
if (sv.state == ss_active)
2007-06-21 22:00:00 +02:00
{
// set up the entity for the client
2007-09-18 22:00:00 +02:00
ent = PRVM_EDICT_NUM(playernum + 1);
ent->priv.sv->serialnumber = playernum + 1;
2007-06-21 22:00:00 +02:00
sv_client->edict = ent;
memset (&sv_client->lastcmd, 0, sizeof(sv_client->lastcmd));
// begin fetching configstrings
MSG_WriteByte (&sv_client->netchan.message, svc_stufftext);
MSG_WriteString (&sv_client->netchan.message, va("cmd configstrings %i 0\n",svs.spawncount) );
}
}
/*
==================
SV_Configstrings_f
==================
*/
void SV_Configstrings_f (void)
{
int start;
if (sv_client->state != cs_connected)
{
2007-07-28 22:00:00 +02:00
Msg ("configstrings not valid -- already spawned\n");
2007-06-21 22:00:00 +02:00
return;
}
// handle the case of a level changing while a client was connecting
if ( atoi(Cmd_Argv(1)) != svs.spawncount )
{
2007-07-28 22:00:00 +02:00
Msg ("SV_Configstrings_f from different level\n");
2007-06-21 22:00:00 +02:00
SV_New_f ();
return;
}
start = atoi(Cmd_Argv(2));
// write a packet full of data
2007-11-14 22:00:00 +01:00
while ( sv_client->netchan.message.cursize < MAX_MSGLEN/2 && start < MAX_CONFIGSTRINGS)
2007-06-21 22:00:00 +02:00
{
if (sv.configstrings[start][0])
{
MSG_WriteByte (&sv_client->netchan.message, svc_configstring);
MSG_WriteShort (&sv_client->netchan.message, start);
MSG_WriteString (&sv_client->netchan.message, sv.configstrings[start]);
}
start++;
}
// send next command
if (start == MAX_CONFIGSTRINGS)
{
MSG_WriteByte (&sv_client->netchan.message, svc_stufftext);
MSG_WriteString (&sv_client->netchan.message, va("cmd baselines %i 0\n",svs.spawncount) );
}
else
{
MSG_WriteByte (&sv_client->netchan.message, svc_stufftext);
MSG_WriteString (&sv_client->netchan.message, va("cmd configstrings %i %i\n",svs.spawncount, start) );
}
}
/*
==================
SV_Baselines_f
==================
*/
void SV_Baselines_f (void)
{
int start;
entity_state_t nullstate;
entity_state_t *base;
if (sv_client->state != cs_connected)
{
2007-07-28 22:00:00 +02:00
Msg ("baselines not valid -- already spawned\n");
2007-06-21 22:00:00 +02:00
return;
}
// handle the case of a level changing while a client was connecting
if ( atoi(Cmd_Argv(1)) != svs.spawncount )
{
2007-07-28 22:00:00 +02:00
Msg ("SV_Baselines_f from different level\n");
2007-06-21 22:00:00 +02:00
SV_New_f ();
return;
}
start = atoi(Cmd_Argv(2));
memset (&nullstate, 0, sizeof(nullstate));
// write a packet full of data
2007-09-06 22:00:00 +02:00
while ( sv_client->netchan.message.cursize < MAX_MSGLEN/2 && start < MAX_EDICTS)
2007-06-21 22:00:00 +02:00
{
base = &sv.baselines[start];
2007-09-18 22:00:00 +02:00
if (base->modelindex || base->soundindex || base->effects)
2007-06-21 22:00:00 +02:00
{
MSG_WriteByte (&sv_client->netchan.message, svc_spawnbaseline);
MSG_WriteDeltaEntity (&nullstate, base, &sv_client->netchan.message, true, true);
}
start++;
}
// send next command
if (start == MAX_EDICTS)
{
MSG_WriteByte (&sv_client->netchan.message, svc_stufftext);
MSG_WriteString (&sv_client->netchan.message, va("precache %i\n", svs.spawncount) );
}
else
{
MSG_WriteByte (&sv_client->netchan.message, svc_stufftext);
MSG_WriteString (&sv_client->netchan.message, va("cmd baselines %i %i\n",svs.spawncount, start) );
}
}
/*
==================
SV_Begin_f
==================
*/
void SV_Begin_f (void)
{
// handle the case of a level changing while a client was connecting
2007-11-04 22:00:00 +01:00
if( atoi(Cmd_Argv(1)) != svs.spawncount )
2007-06-21 22:00:00 +02:00
{
2007-07-28 22:00:00 +02:00
Msg ("SV_Begin_f from different level\n");
2007-06-21 22:00:00 +02:00
SV_New_f ();
return;
}
sv_client->state = cs_spawned;
2007-09-06 22:00:00 +02:00
// call the game begin function
SV_ClientBegin(sv_player);
2007-06-21 22:00:00 +02:00
}
//=============================================================================
/*
==================
SV_NextDownload_f
==================
*/
void SV_NextDownload_f (void)
{
int r;
int percent;
int size;
if (!sv_client->download)
return;
r = sv_client->downloadsize - sv_client->downloadcount;
if (r > 1024)
r = 1024;
MSG_WriteByte (&sv_client->netchan.message, svc_download);
MSG_WriteShort (&sv_client->netchan.message, r);
sv_client->downloadcount += r;
size = sv_client->downloadsize;
if (!size)
size = 1;
percent = sv_client->downloadcount*100/size;
MSG_WriteByte (&sv_client->netchan.message, percent);
SZ_Write (&sv_client->netchan.message,
sv_client->download + sv_client->downloadcount - r, r);
if (sv_client->downloadcount != sv_client->downloadsize)
return;
sv_client->download = NULL;
}
/*
==================
SV_BeginDownload_f
==================
*/
void SV_BeginDownload_f(void)
{
char *name;
extern cvar_t *allow_download;
extern cvar_t *allow_download_players;
extern cvar_t *allow_download_models;
extern cvar_t *allow_download_sounds;
extern cvar_t *allow_download_maps;
int offset = 0;
name = Cmd_Argv(1);
if (Cmd_Argc() > 2)
offset = atoi(Cmd_Argv(2)); // downloaded offset
// hacked by zoid to allow more conrol over download
// first off, no .. or global allow check
if (strstr (name, "..") || !allow_download->value
// leading dot is no good
|| *name == '.'
// leading slash bad as well, must be in subdir
|| *name == '/'
// next up, skin check
|| (strncmp(name, "players/", 6) == 0 && !allow_download_players->value)
// now models
|| (strncmp(name, "models/", 6) == 0 && !allow_download_models->value)
// now sounds
|| (strncmp(name, "sound/", 6) == 0 && !allow_download_sounds->value)
// now maps (note special case for maps, must not be in pak)
|| (strncmp(name, "maps/", 6) == 0 && !allow_download_maps->value)
// MUST be in a subdirectory
|| !strstr (name, "/") )
{ // don't allow anything with .. path
MSG_WriteByte (&sv_client->netchan.message, svc_download);
MSG_WriteShort (&sv_client->netchan.message, -1);
MSG_WriteByte (&sv_client->netchan.message, 0);
return;
}
sv_client->download = FS_LoadFile (name, &sv_client->downloadsize);
sv_client->downloadcount = offset;
if (offset > sv_client->downloadsize)
sv_client->downloadcount = sv_client->downloadsize;
if (!sv_client->download)
{
2007-08-17 22:00:00 +02:00
MsgWarn("SV_BeginDownload_f: couldn't download %s to %s\n", name, sv_client->name);
2007-06-21 22:00:00 +02:00
if (sv_client->download)
{
sv_client->download = NULL;
}
MSG_WriteByte (&sv_client->netchan.message, svc_download);
MSG_WriteShort (&sv_client->netchan.message, -1);
MSG_WriteByte (&sv_client->netchan.message, 0);
return;
}
SV_NextDownload_f ();
2007-09-06 22:00:00 +02:00
MsgDev (D_INFO, "Downloading %s to %s\n", name, sv_client->name);
2007-06-21 22:00:00 +02:00
}
//============================================================================
/*
=================
SV_Disconnect_f
The client is going to disconnect, so remove the connection immediately
=================
*/
void SV_Disconnect_f (void)
{
// SV_EndRedirect ();
SV_DropClient (sv_client);
}
/*
==================
SV_ShowServerinfo_f
Dumps the serverinfo info string
==================
*/
void SV_ShowServerinfo_f (void)
{
Info_Print (Cvar_Serverinfo());
}
typedef struct
{
char *name;
void (*func) (void);
} ucmd_t;
ucmd_t ucmds[] =
{
// auto issued
{"new", SV_New_f},
{"configstrings", SV_Configstrings_f},
{"baselines", SV_Baselines_f},
{"begin", SV_Begin_f},
{"disconnect", SV_Disconnect_f},
{"info", SV_ShowServerinfo_f},
{"download", SV_BeginDownload_f},
{"nextdl", SV_NextDownload_f},
{NULL, NULL}
};
/*
==================
SV_ExecuteUserCommand
==================
*/
void SV_ExecuteUserCommand (char *s)
{
ucmd_t *u;
2007-11-04 22:00:00 +01:00
Cmd_TokenizeString(s);
2007-06-21 22:00:00 +02:00
sv_player = sv_client->edict;
2007-08-17 22:00:00 +02:00
for (u = ucmds; u->name; u++)
{
2007-06-21 22:00:00 +02:00
if (!strcmp (Cmd_Argv(0), u->name) )
{
2007-08-17 22:00:00 +02:00
u->func();
2007-06-21 22:00:00 +02:00
break;
}
2007-08-17 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
2008-05-22 22:00:00 +02:00
if (!u->name && sv.state == ss_active)
2007-09-29 22:00:00 +02:00
{
// custom client commands
2007-09-30 22:00:00 +02:00
prog->globals.sv->pev = PRVM_EDICT_TO_PROG(sv_player);
prog->globals.sv->time = sv.time;
prog->globals.sv->frametime = sv.frametime;
PRVM_ExecuteProgram (prog->globals.sv->ClientCommand, "QC function ClientCommand is missing");
2007-09-29 22:00:00 +02:00
}
2007-06-21 22:00:00 +02:00
}
/*
===========================================================================
USER CMD EXECUTION
===========================================================================
*/
2007-09-04 22:00:00 +02:00
2007-11-04 22:00:00 +01:00
void SV_ClientThink (client_state_t *cl, usercmd_t *cmd)
2007-06-21 22:00:00 +02:00
{
2007-09-04 22:00:00 +02:00
cl->commandMsec -= cmd->msec;
2007-06-21 22:00:00 +02:00
if (cl->commandMsec < 0 && sv_enforcetime->value )
{
2007-08-17 22:00:00 +02:00
MsgWarn("SV_ClientThink: commandMsec underflow from %s\n", cl->name);
2007-06-21 22:00:00 +02:00
return;
}
2007-09-06 22:00:00 +02:00
ClientThink (cl->edict, cmd);
2007-06-21 22:00:00 +02:00
}
/*
===================
SV_ExecuteClientMessage
The current net_message is parsed for the given client
===================
*/
2007-08-17 22:00:00 +02:00
#define MAX_STRINGCMDS 8
2007-11-04 22:00:00 +01:00
void SV_ExecuteClientMessage (client_state_t *cl)
2007-06-21 22:00:00 +02:00
{
int c;
2007-08-17 22:00:00 +02:00
char *s;
2007-06-21 22:00:00 +02:00
2007-09-06 22:00:00 +02:00
usercmd_t nullcmd;
usercmd_t oldest, oldcmd, newcmd;
2007-06-21 22:00:00 +02:00
int net_drop;
int stringCmdCount;
int checksum, calculatedChecksum;
int checksumIndex;
2007-09-06 22:00:00 +02:00
bool move_issued;
2007-06-21 22:00:00 +02:00
int lastframe;
sv_client = cl;
sv_player = sv_client->edict;
// only allow one move command
move_issued = false;
stringCmdCount = 0;
while (1)
{
if (net_message.readcount > net_message.cursize)
{
2007-08-17 22:00:00 +02:00
MsgWarn("SV_ReadClientMessage: bad read\n");
2007-06-21 22:00:00 +02:00
SV_DropClient (cl);
return;
}
c = MSG_ReadByte (&net_message);
2007-08-17 22:00:00 +02:00
if (c == -1) break;
2007-06-21 22:00:00 +02:00
switch (c)
{
default:
2007-08-17 22:00:00 +02:00
MsgWarn("SV_ReadClientMessage: unknown command char\n");
2007-06-21 22:00:00 +02:00
SV_DropClient (cl);
return;
2007-08-17 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
case clc_nop:
break;
case clc_userinfo:
strncpy (cl->userinfo, MSG_ReadString (&net_message), sizeof(cl->userinfo)-1);
SV_UserinfoChanged (cl);
break;
case clc_move:
2007-08-17 22:00:00 +02:00
if (move_issued) return; // someone is trying to cheat...
2007-06-21 22:00:00 +02:00
move_issued = true;
checksumIndex = net_message.readcount;
checksum = MSG_ReadByte (&net_message);
lastframe = MSG_ReadLong (&net_message);
2007-08-17 22:00:00 +02:00
if (lastframe != cl->lastframe)
{
2007-06-21 22:00:00 +02:00
cl->lastframe = lastframe;
2007-08-17 22:00:00 +02:00
if (cl->lastframe > 0)
{
cl->frame_latency[cl->lastframe&(LATENCY_COUNTS-1)] = svs.realtime - cl->frames[cl->lastframe & UPDATE_MASK].senttime;
2007-06-21 22:00:00 +02:00
}
}
memset (&nullcmd, 0, sizeof(nullcmd));
MSG_ReadDeltaUsercmd (&net_message, &nullcmd, &oldest);
MSG_ReadDeltaUsercmd (&net_message, &oldest, &oldcmd);
MSG_ReadDeltaUsercmd (&net_message, &oldcmd, &newcmd);
if ( cl->state != cs_spawned )
{
cl->lastframe = -1;
break;
}
// if the checksum fails, ignore the rest of the packet
2007-11-14 22:00:00 +01:00
calculatedChecksum = CRC_Sequence(net_message.data + checksumIndex + 1, net_message.readcount - checksumIndex - 1, cl->netchan.incoming_sequence);
2007-06-21 22:00:00 +02:00
if (calculatedChecksum != checksum)
{
2007-08-17 22:00:00 +02:00
MsgWarn("SV_ExecuteClientMessage: failed command checksum for %s (%d != %d)/%d\n", cl->name, calculatedChecksum, checksum, cl->netchan.incoming_sequence);
2007-06-21 22:00:00 +02:00
return;
}
2007-09-06 22:00:00 +02:00
2007-06-21 22:00:00 +02:00
if (!sv_paused->value)
{
net_drop = cl->netchan.dropped;
if (net_drop < 20)
{
while (net_drop > 2)
{
2007-09-06 22:00:00 +02:00
SV_ClientThink (cl, &cl->lastcmd);
2007-06-21 22:00:00 +02:00
net_drop--;
}
2007-09-06 22:00:00 +02:00
if (net_drop > 1) SV_ClientThink (cl, &oldest);
if (net_drop > 0) SV_ClientThink (cl, &oldcmd);
2007-06-21 22:00:00 +02:00
}
2007-09-06 22:00:00 +02:00
SV_ClientThink (cl, &newcmd);
2007-06-21 22:00:00 +02:00
}
cl->lastcmd = newcmd;
break;
case clc_stringcmd:
s = MSG_ReadString (&net_message);
// malicious users may try using too many string commands
2007-08-17 22:00:00 +02:00
if (++stringCmdCount < MAX_STRINGCMDS) SV_ExecuteUserCommand (s);
if (cl->state == cs_zombie) return; // disconnect command
2007-06-21 22:00:00 +02:00
break;
}
}
2007-09-05 22:00:00 +02:00
}