UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cl_parse.cpp
Go to the documentation of this file.
1 
6 /*
7 All original material Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "../client.h"
27 #include "cl_localentity.h"
28 #include "cl_parse.h"
29 #include "cl_hud.h"
30 #include "../cgame/cl_game.h"
31 #include "events/e_parse.h"
32 
38 static char const* const svc_strings[] =
39 {
40  "svc_bad",
41 
42  "svc_nop",
43  "svc_ping",
44  "svc_disconnect",
45  "svc_reconnect",
46  "svc_print",
47  "svc_stufftext",
48  "svc_serverdata",
49  "svc_configstring",
50  "svc_event"
51 };
52 
53 /*
54 =====================================================================
55 SERVER CONNECTING MESSAGES
56 =====================================================================
57 */
58 
62 static void CL_ParseServerData (dbuffer* msg)
63 {
64  Com_DPrintf(DEBUG_CLIENT, "Serverdata packet received.\n");
65 
67 
68  /* parse protocol version number */
69  const int i = NET_ReadLong(msg);
70  /* compare versions */
71  if (i != PROTOCOL_VERSION)
72  Com_Error(ERR_DROP, "Server returned version %i, not %i", i, PROTOCOL_VERSION);
73 
74  /* parse player entity number */
75  cl.pnum = NET_ReadShort(msg);
76 
77  /* get the full level name */
78  char str[1024];
79  NET_ReadString(msg, str, sizeof(str));
80 
81  Com_DPrintf(DEBUG_CLIENT, "serverdata: pnum %d, level %s\n", cl.pnum, str);
82 
83  if (cl.pnum >= 0) {
84  /* need to prep refresh at next opportunity */
85  refdef.ready = false;
86  }
87 }
88 
94 static void CL_ParseClientinfo (unsigned int player)
95 {
96  clientinfo_t* ci = &cl.clientinfo[player];
97  const char* s = CL_GetConfigString(CS_PLAYERNAMES + player);
98 
99  /* isolate the player's name */
100  Q_strncpyz(ci->name, s, sizeof(ci->name));
101 }
102 
103 int CL_GetPlayerNum (void)
104 {
105  return cls.team;
106 }
107 
113 const char* CL_PlayerGetName (unsigned int player)
114 {
115  const clientinfo_t* ci = &cl.clientinfo[player];
116  return ci->name;
117 }
118 
122 static void CL_ParseConfigString (dbuffer* msg)
123 {
124  const int i = NET_ReadShort(msg);
125  const char* s = CL_SetConfigString(i, msg);
126 
127  Com_DPrintf(DEBUG_CLIENT, "configstring %d: %s\n", i, s);
128 
129  /* do something appropriate */
130  if (i >= CS_MODELS && i < CS_MODELS + MAX_MODELS) {
131  if (refdef.ready) {
132  const unsigned int index = i - CS_MODELS;
133  assert(index != 0);
135  /* inline models are marked with * as first char followed by the number */
136  if (s[0] == '*')
138  else
139  cl.model_clip[index] = nullptr;
140  }
141  } else if (i >= CS_PLAYERNAMES && i < CS_PLAYERNAMES + MAX_CLIENTS) {
142  const unsigned int index = i - CS_PLAYERNAMES;
143  CL_ParseClientinfo(index);
144  }
145 }
146 
147 
148 /*
149 =====================================================================
150 ACTION MESSAGES
151 =====================================================================
152 */
153 
161 {
162  static svc_ops_t lastCmd;
163  static event_t eType;
164 
165  /* parse the message */
166  if (cmd < svc_bad || cmd >= svc_oob)
167  return;
168 
169  Com_DPrintf(DEBUG_CLIENT, "command: %s\n", svc_strings[cmd]);
170 
171  /* commands */
172  switch (cmd) {
173  case svc_nop:
174 /* Com_Printf("svc_nop\n"); */
175  break;
176 
177  case svc_ping: {
178  dbuffer ack;
179  NET_WriteByte(&ack, clc_ack);
180  NET_WriteMsg(cls.netStream, ack);
181  break;
182  }
183 
184  case svc_disconnect: {
185  char s[MAX_SVC_DISCONNECT];
186  NET_ReadString(msg, s, sizeof(s));
187  Com_Printf("%s\n", s);
188  CL_Drop(); /* ensure the right menu cvars are set */
189  break;
190  }
191 
192  case svc_reconnect: {
193  char s[MAX_SVC_RECONNECT];
194  NET_ReadString(msg, s, sizeof(s));
195  Com_Printf("%s\n", s);
196  cls.reconnectTime = CL_Milliseconds() + 4000;
197  break;
198  }
199 
200  case svc_print: {
201  const int i = NET_ReadByte(msg);
202  char s[MAX_SVC_PRINT];
203  NET_ReadString(msg, s, sizeof(s));
204  switch (i) {
205  case PRINT_HUD:
206  /* all game lib messages or server messages should be printed
207  * untranslated with BroadcastPrintf or PlayerPrintf */
208  /* see src/po/OTHER_STRINGS */
209  HUD_DisplayMessage(_(s));
210  Com_Printf("%s\n", s);
211  break;
212  case PRINT_CHAT:
214  /* skip format strings */
215  if (s[0] == '^')
216  memmove(s, &s[2], sizeof(s) - 2);
217  /* also print to console */
218  break;
219  default:
220  Com_Printf("%s", s);
221  break;
222  }
223  Com_DPrintf(DEBUG_CLIENT, "svc_print(%d): %s", i, s);
224  break;
225  }
226 
227  case svc_stufftext: {
228  char s[MAX_SVC_STUFFTEXT];
229  NET_ReadString(msg, s, sizeof(s));
230  Com_DPrintf(DEBUG_CLIENT, "stufftext: %s\n", s);
231  Cbuf_AddText("%s\n", s);
232  break;
233  }
234 
235  case svc_serverdata:
236  Cbuf_Execute(); /* make sure any stuffed commands are done */
237  CL_ParseServerData(msg);
238  break;
239 
240  case svc_configstring:
242  break;
243 
244  case svc_event:
245  eType = CL_ParseEvent(msg);
246  break;
247 
248  case svc_bad:
249  Com_Printf("CL_ParseServerMessage: bad server message %d\n", cmd);
250  break;
251 
252  default:
253  Com_Error(ERR_DROP, "CL_ParseServerMessage: Illegal server message %d (last cmd was: %d, eType: %i)",
254  cmd, lastCmd, eType);
255  }
256 
257  lastCmd = cmd;
258 }
void GAME_AddChatMessage(const char *format,...)
Definition: cl_game.cpp:370
#define PRINT_CHAT
Definition: defines.h:106
#define _(String)
Definition: cl_shared.h:43
char * CL_GetConfigString(int index)
void Cbuf_AddText(const char *format,...)
Adds command text at the end of the buffer.
Definition: cmd.cpp:126
#define MAX_SVC_PRINT
Definition: common.h:160
int NET_ReadLong(dbuffer *buf)
Definition: netpack.cpp:282
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
void CL_Drop(void)
Ensures the right menu cvars are set after error drop or map change.
Definition: cl_main.cpp:167
#define MAX_CLIENTS
Definition: q_shared.h:299
void Com_Error(int code, const char *fmt,...)
Definition: common.cpp:417
#define CS_PLAYERNAMES
Definition: q_shared.h:328
mapTiles_t * mapTiles
client_static_t cls
Definition: cl_main.cpp:83
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
static void CL_ParseConfigString(dbuffer *msg)
Definition: cl_parse.cpp:122
#define CS_MODELS
Definition: q_shared.h:327
#define ERR_DROP
Definition: common.h:211
static char const *const svc_strings[]
see also svc_ops_e in common.h
Definition: cl_parse.cpp:38
#define DEBUG_CLIENT
Definition: defines.h:59
int NET_ReadString(dbuffer *buf, char *string, size_t length)
Definition: netpack.cpp:302
rendererData_t refdef
Definition: r_main.cpp:45
model_t * model_draw[MAX_MODELS]
int reconnectTime
Definition: client.h:71
event_t
Possible event values.
Definition: q_shared.h:79
int NET_ReadShort(dbuffer *buf)
Definition: netpack.cpp:242
clientBattleScape_t cl
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Definition: common.cpp:398
struct net_stream * netStream
Definition: client.h:74
model_t * R_FindModel(const char *name)
Tries to load a model.
Definition: r_model.cpp:203
const char * CL_PlayerGetName(unsigned int player)
Get the player name.
Definition: cl_parse.cpp:113
const struct cBspModel_s * model_clip[MAX_MODELS]
int32_t svc_ops_t
Definition: common.h:165
#define MAX_SVC_DISCONNECT
Definition: common.h:162
#define MAX_SVC_RECONNECT
Definition: common.h:161
QGL_EXTERN GLuint index
Definition: r_gl.h:110
cBspModel_t * CM_InlineModel(const mapTiles_t *mapTiles, const char *name)
Searches all inline models and return the cBspModel_t pointer for the given modelnumber or -name...
Definition: bsp.cpp:929
clientinfo_t clientinfo[MAX_CLIENTS]
#define MAX_MODELS
Definition: defines.h:100
#define MAX_SVC_STUFFTEXT
Definition: common.h:159
QGL_EXTERN GLint i
Definition: r_gl.h:113
void CL_SetClientState(connstate_t state)
Sets the client state.
Definition: cl_main.cpp:1016
static void CL_ParseServerData(dbuffer *msg)
Written by SV_New_f in sv_user.c.
Definition: cl_parse.cpp:62
void NET_WriteByte(dbuffer *buf, byte c)
Definition: netpack.cpp:39
HUD related routines.
int CL_GetPlayerNum(void)
Definition: cl_parse.cpp:103
static void CL_ParseClientinfo(unsigned int player)
Parses client names that are displayed on the targeting box for multiplayer games.
Definition: cl_parse.cpp:94
event_t CL_ParseEvent(dbuffer *msg)
Called in case a svc_event was send via the network buffer.
Definition: e_parse.cpp:261
char * CL_SetConfigString(int index, dbuffer *msg)
char name[MAX_VAR]
void Cbuf_Execute(void)
Pulls off terminated lines of text from the command buffer and sends them through Cmd_ExecuteString...
Definition: cmd.cpp:214
void CL_ParseServerMessage(svc_ops_t cmd, dbuffer *msg)
Parses the server sent data from the given buffer.
Definition: cl_parse.cpp:160
#define PROTOCOL_VERSION
Definition: common.h:134
int NET_ReadByte(dbuffer *buf)
Reads a byte from the netchannel.
Definition: netpack.cpp:234
void HUD_DisplayMessage(const char *text)
Displays a message on the hud.
Definition: cl_hud.cpp:138
int CL_Milliseconds(void)
Definition: cl_main.cpp:1208
#define PRINT_HUD
Definition: defines.h:107
void NET_WriteMsg(struct net_stream *s, dbuffer &buf)
Enqueue the buffer in the net stream for ONE client.
Definition: netpack.cpp:569