-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathqtv.c
547 lines (421 loc) · 11.6 KB
/
qtv.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
Copyright (C) 2011 qqshka
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, see <http://www.gnu.org/licenses/>.
*/
/*
Support for FTE QuakeTV
$Id: qtv.c,v 1.20 2007-10-28 02:45:19 qqshka Exp $
*/
#include "quakedef.h"
#include "qtv.h"
#include "input.h"
#include "teamplay.h"
#include "fs.h"
#include "utils.h"
cvar_t qtv_buffertime = {"qtv_buffertime", "0.5"};
cvar_t qtv_chatprefix = {"qtv_chatprefix", "$[{QTV}$] "};
cvar_t qtv_gamechatprefix = {"qtv_gamechatprefix", "$[{QTV>game}$] "};
cvar_t qtv_skipchained = {"qtv_skipchained", "1"};
cvar_t qtv_adjustbuffer = {"qtv_adjustbuffer", "1"};
cvar_t qtv_adjustminspeed = {"qtv_adjustminspeed", "0"};
cvar_t qtv_adjustmaxspeed = {"qtv_adjustmaxspeed", "999"};
cvar_t qtv_adjustlowstart = {"qtv_adjustlowstart", "0.3"};
cvar_t qtv_adjusthighstart = {"qtv_adjusthighstart", "1"};
cvar_t qtv_say_team = {"qtv_say_team", "0"};
cvar_t qtv_allow_pause = {"qtv_allow_pause", "0"}; // ignore cl_demospeed during QTV playback by default
cvar_t qtv_event_join = {"qtv_event_join", " &c2F2joined&r"};
cvar_t qtv_event_leave = {"qtv_event_leave", " &cF22left&r"};
cvar_t qtv_event_changename = {"qtv_event_changename", " &cFF0changed name to&r "};
void Qtvusers_f (void);
void QTV_Init(void)
{
Cvar_SetCurrentGroup(CVAR_GROUP_QTV);
Cvar_Register(&qtv_buffertime);
Cvar_Register(&qtv_chatprefix);
Cvar_Register(&qtv_gamechatprefix);
Cvar_Register(&qtv_skipchained);
Cvar_Register(&qtv_adjustbuffer);
Cvar_Register(&qtv_adjustminspeed);
Cvar_Register(&qtv_adjustmaxspeed);
Cvar_Register(&qtv_adjustlowstart);
Cvar_Register(&qtv_adjusthighstart);
Cvar_Register(&qtv_say_team);
Cvar_Register(&qtv_allow_pause);
Cvar_Register(&qtv_event_join);
Cvar_Register(&qtv_event_leave);
Cvar_Register(&qtv_event_changename);
Cvar_ResetCurrentGroup();
Cmd_AddCommand ("qtvusers", Qtvusers_f);
}
//=================================================
char *QTV_CL_HEADER(float qtv_ver, int qtv_ezquake_ext)
{
static char header[1024];
snprintf(header, sizeof(header), "QTV\n" "VERSION: %g\n" QTV_EZQUAKE_EXT ": %d\n", qtv_ver, qtv_ezquake_ext);
return header;
}
//=================================================
// ripped from FTEQTV, original name is SV_ConsistantMVDData
// return non zero if we have at least one message
// ms - will contain ms
int ConsistantMVDDataEx(unsigned char *buffer, int remaining, int *ms)
{
qbool warn = true;
int lengthofs;
int length;
int available = 0;
if (ms)
ms[0] = 0;
while( 1 )
{
if (remaining < 2)
{
return available;
}
//buffer[0] is time
switch (buffer[1]&dem_mask)
{
case dem_set:
length = 10;
goto gottotallength;
case dem_multiple:
lengthofs = 6;
break;
default:
lengthofs = 2;
break;
}
if (lengthofs+4 > remaining)
{
return available;
}
length = (buffer[lengthofs]<<0) + (buffer[lengthofs+1]<<8) + (buffer[lengthofs+2]<<16) + (buffer[lengthofs+3]<<24);
if (length > MAX_MVD_SIZE && warn)
{
Com_Printf("Corrupt mvd, length: %d\n", length);
warn = false;
}
length += lengthofs+4;
gottotallength:
if (remaining < length)
{
return available;
}
if (ms)
ms[0] += buffer[0];
remaining -= length;
available += length;
buffer += length;
}
}
int ConsistantMVDData(unsigned char *buffer, int remaining)
{
return ConsistantMVDDataEx(buffer, remaining, NULL);
}
//=================================================
extern vfsfile_t *playbackfile;
void QTV_ForwardToServerEx (qbool skip_if_no_params, qbool use_first_argument)
{
char data[1024 + 100] = {0}, text[1024], *s;
sizebuf_t buf;
if ( cls.mvdplayback != QTV_PLAYBACK
|| !playbackfile /* || cls.qtv_ezquake_ext & QTV_EZQUAKE_EXT_CLC_STRINGCMD ???*/
)
return;
if (skip_if_no_params)
if (Cmd_Argc() < 2)
return;
// lowercase command
for (s = Cmd_Argv(0); *s; s++)
*s = (char) tolower(*s);
if (cls.state == ca_disconnected) {
Com_Printf ("Can't \"%s\", not connected\n", Cmd_Argv(0));
return;
}
if (strcmp(Cmd_Argv(0), "say_team") == 0 && !qtv_say_team.integer) {
Com_Printf("Cannot send team messages. Use qtv_say_team 1 to override.\n");
return;
}
SZ_Init(&buf, (byte*) data, sizeof(data));
s = TP_ParseMacroString (Cmd_Args());
s = TP_ParseFunChars (s, true);
text[0] = 0; // *cat is dangerous, ensure we empty buffer before use it
if (use_first_argument)
strlcat(text, Cmd_Argv(0), sizeof(text));
if (s[0])
{
strlcat(text, " ", sizeof(text));
strlcat(text, s, sizeof(text));
}
MSG_WriteShort (&buf, 2 + 1 + strlen(text) + 1); // short + byte + null terminated string
MSG_WriteByte (&buf, qtv_clc_stringcmd);
MSG_WriteString (&buf, text);
VFS_WRITE(playbackfile, buf.data, buf.cursize);
}
void QTV_Say_f (void)
{
tokenizecontext_t tmpcontext;
// save context, so we can later restore it
Cmd_SaveContext(&tmpcontext);
// in our last tests, this check was not necessary
// and even lead to issues
// so we are disabling it temporarily to see if everything works ok without it
#if 0
// get rid of quotes, if any
char *s = Cmd_Args();
if (0 && s[0] == '\"' && s[(len = strlen(s))-1] == '\"' && len > 2)
{
int len;
char text[1024] = {0};
snprintf(text, sizeof(text), "%s %s", Cmd_Argv(0), s + 1);
if ((len = strlen(text)))
text[len - 1] = 0;
Cmd_TokenizeString(text);
}
#endif
QTV_ForwardToServerEx (true, true);
// restore
Cmd_RestoreContext(&tmpcontext);
}
void QTV_Cmd_ForwardToServer (void)
{
QTV_ForwardToServerEx (false, true);
}
// don't forward the first argument
void QTV_Cl_ForwardToServer_f (void)
{
QTV_ForwardToServerEx (false, false);
}
void QTV_Cmd_Printf(int qtv_ext, char *fmt, ...)
{
va_list argptr;
char msg[1024] = {0};
tokenizecontext_t tmpcontext;
if (cls.mvdplayback != QTV_PLAYBACK || (qtv_ext & cls.qtv_ezquake_ext) != qtv_ext)
return; // no point for this, since it not qtv playback or qtv server do not support it
va_start (argptr, fmt);
vsnprintf (msg, sizeof(msg), fmt, argptr);
va_end (argptr);
// save context, so we can later restore it
Cmd_SaveContext(&tmpcontext);
Cmd_TokenizeString(msg);
QTV_Cmd_ForwardToServer ();
// restore
Cmd_RestoreContext(&tmpcontext);
}
//=================================================
static qtvuser_t *qtvuserlist = NULL;
static qtvuser_t *QTV_UserById(int id)
{
qtvuser_t *current;
for (current = qtvuserlist; current; current = current->next)
if (current->id == id)
return current;
return NULL;
}
static void QTV_SetUser(qtvuser_t *to, qtvuser_t *from)
{
*to = *from;
}
// allocate data and set fields, perform linkage to qtvuserlist
// Well, instead of QTV_NewUser(int id, char *name, ...) I pass params with single qtvuser_t *user struct, well its OK for current struct.
static qtvuser_t *QTV_NewUser(qtvuser_t *user)
{
// check, may be user alredy exist, so reuse it
qtvuser_t *newuser = QTV_UserById(user->id);
if (!newuser)
{
// user does't exist, alloc data
newuser = Q_malloc(sizeof(*newuser));
QTV_SetUser(newuser, user);
// perform linkage
newuser->next = qtvuserlist;
qtvuserlist = newuser;
}
else
{
// we do not need linkage, just save current
qtvuser_t *oldnext = newuser->next; // we need save this before assign all fields
QTV_SetUser(newuser, user);
newuser->next = oldnext;
}
return newuser;
}
// free data, perform unlink if requested
static void QTV_FreeUser(qtvuser_t *user, qbool unlink)
{
if (!user)
return;
if (unlink)
{
qtvuser_t *next, *prev, *current;
prev = NULL;
current = qtvuserlist;
for ( ; current; )
{
next = current->next;
if (user == current)
{
if (prev)
prev->next = next;
else
qtvuserlist = next;
break;
}
prev = current;
current = next;
}
}
Q_free(user);
}
// free whole qtvuserlist
void QTV_FreeUserList(void)
{
qtvuser_t *next, *current;
current = qtvuserlist;
for ( ; current; current = next)
{
next = current->next;
QTV_FreeUser(current, false);
}
qtvuserlist = NULL;
}
#define QTV_EVENT_PREFIX "QTV: "
// user join qtv
void QTV_JoinEvent(qtvuser_t *user)
{
// make it optional message
if (!qtv_event_join.string[0])
return;
// do not show "user joined" at moment of connection to QTV, it mostly QTV just spammed userlist to us.
if (cls.state <= ca_demostart)
return;
if (QTV_UserById(user->id))
{
// we alredy have this user, do not double trigger
return;
}
Com_Printf("%s%s%s\n", QTV_EVENT_PREFIX, user->name, qtv_event_join.string);
}
// user leaved/left qtv
void QTV_LeaveEvent(qtvuser_t *user)
{
qtvuser_t *olduser;
// make it optional message
if (!qtv_event_leave.string[0])
return;
if (!(olduser = QTV_UserById(user->id)))
{
// we do not have this user
return;
}
Com_Printf("%s%s%s\n", QTV_EVENT_PREFIX, olduser->name, qtv_event_leave.string);
}
// user changed name on qtv
void QTV_ChangeEvent(qtvuser_t *user)
{
qtvuser_t *olduser;
// well, too spammy, make it as option
if (!qtv_event_changename.string[0])
return;
if (!(olduser = QTV_UserById(user->id)))
{
// we do not have this user yet
Com_DPrintf("qtv: change event without olduser\n");
return;
}
Com_Printf("%s%s%s%s\n", QTV_EVENT_PREFIX, olduser->name, qtv_event_changename.string, user->name);
}
void Parse_QtvUserList(char *s)
{
qtvuser_t tmpuser;
qtvuserlist_t action;
int cnt = 1;
memset(&tmpuser, 0, sizeof(tmpuser));
// action id [\"name\"]
Cmd_TokenizeString( s );
action = atoi( Cmd_Argv( cnt++ ) );
tmpuser.id = atoi( Cmd_Argv( cnt++ ) );
strlcpy(tmpuser.name, Cmd_Argv( cnt++ ), sizeof(tmpuser.name)); // name is optional in some cases
switch ( action )
{
case QUL_ADD:
QTV_JoinEvent(&tmpuser);
QTV_NewUser(&tmpuser);
break;
case QUL_CHANGE:
QTV_ChangeEvent(&tmpuser);
QTV_NewUser(&tmpuser);
break;
case QUL_DEL:
QTV_LeaveEvent(&tmpuser);
QTV_FreeUser(QTV_UserById(tmpuser.id), true);
break;
default:
Com_Printf("Parse_QtvUserList: unknown action %d\n", action);
return;
}
}
// FIXME: make sexy GUI instead!
void Qtvusers_f (void)
{
qtvuser_t *current;
int c;
if (cls.state == ca_disconnected)
{
Com_Printf ("Can't \"%s\", not connected\n", Cmd_Argv(0));
return;
}
if (cls.mvdplayback != QTV_PLAYBACK)
{
CL_SendClientCommand(true, "%s %s", Cmd_Argv(0), Cmd_Args());
return;
}
c = 0;
Com_Printf ("userid name\n");
Com_Printf ("------ ----\n");
for (current = qtvuserlist; current; current = current->next)
{
Com_Printf ("%6i %s\n", current->id, current->name);
c++;
}
Com_Printf ("%i total users\n", c);
}
qbool QTV_FindBestNick (const char *nick, char *result, size_t result_len)
{
int best = 999999;
char name[128], *match;
qtvuser_t *current = NULL, *bestplayer = NULL;
result[0] = 0;
for (current = qtvuserlist; current; current = current->next)
{
if (!current->name[0])
continue;
strlcpy(name, current->name, sizeof(name));
RemoveColors(name, sizeof (name));
for (match = name; match[0]; match++)
match[0] = tolower(match[0]);
if (!name[0])
continue;
if ((match = strstr(name, nick)) && match - name < best)
{
best = match - name;
bestplayer = current;
}
}
if (bestplayer)
{
strlcpy(result, bestplayer->name, result_len);
return true;
}
return false;
}