-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_cmd.c
2997 lines (2616 loc) · 80 KB
/
host_cmd.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1996-1997 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.
*/
#include "quakedef.h"
#include "sv_demo.h"
#include "image.h"
#include "utf8lib.h"
// for secure rcon authentication
#include "hmac.h"
#include "mdfour.h"
#include <time.h>
int current_skill;
cvar_t sv_cheats = {0, "sv_cheats", "0", "enables cheat commands in any game, and cheat impulses in dpmod"};
cvar_t sv_adminnick = {CVAR_SAVE, "sv_adminnick", "", "nick name to use for admin messages instead of host name"};
cvar_t sv_status_privacy = {CVAR_SAVE, "sv_status_privacy", "0", "do not show IP addresses in 'status' replies to clients"};
cvar_t sv_status_show_qcstatus = {CVAR_SAVE, "sv_status_show_qcstatus", "0", "show the 'qcstatus' field in status replies, not the 'frags' field. Turn this on if your mod uses this field, and the 'frags' field on the other hand has no meaningful value."};
cvar_t rcon_password = {CVAR_PRIVATE, "rcon_password", "", "password to authenticate rcon commands; NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password; may be set to a string of the form user1:pass1 user2:pass2 user3:pass3 to allow multiple user accounts - the client then has to specify ONE of these combinations"};
cvar_t rcon_secure = {CVAR_NQUSERINFOHACK, "rcon_secure", "0", "force secure rcon authentication (1 = time based, 2 = challenge based); NOTE: changing rcon_secure clears rcon_password, so set rcon_secure always before rcon_password"};
cvar_t rcon_secure_challengetimeout = {0, "rcon_secure_challengetimeout", "5", "challenge-based secure rcon: time out requests if no challenge came within this time interval"};
cvar_t rcon_address = {0, "rcon_address", "", "server address to send rcon commands to (when not connected to a server)"};
cvar_t team = {CVAR_USERINFO | CVAR_SAVE, "team", "none", "QW team (4 character limit, example: blue)"};
cvar_t skin = {CVAR_USERINFO | CVAR_SAVE, "skin", "", "QW player skin name (example: base)"};
cvar_t noaim = {CVAR_USERINFO | CVAR_SAVE, "noaim", "1", "QW option to disable vertical autoaim"};
cvar_t r_fixtrans_auto = {0, "r_fixtrans_auto", "0", "automatically fixtrans textures (when set to 2, it also saves the fixed versions to a fixtrans directory)"};
qboolean allowcheats = false;
extern qboolean host_shuttingdown;
extern cvar_t developer_entityparsing;
/*
==================
Host_Quit_f
==================
*/
void Host_Quit_f (void)
{
if(host_shuttingdown)
Con_Printf("shutting down already!\n");
else
Sys_Quit (0);
}
/*
==================
Host_Status_f
==================
*/
void Host_Status_f (void)
{
char qcstatus[256];
client_t *client;
int seconds = 0, minutes = 0, hours = 0, i, j, k, in, players, ping = 0, packetloss = 0;
void (*print) (const char *fmt, ...);
char ip[22];
int frags;
if (cmd_source == src_command)
{
// if running a client, try to send over network so the client's status report parser will see the report
if (cls.state == ca_connected)
{
Cmd_ForwardToServer ();
return;
}
print = Con_Printf;
}
else
print = SV_ClientPrintf;
if (!sv.active)
return;
if(cmd_source == src_command)
SV_VM_Begin();
in = 0;
if (Cmd_Argc() == 2)
{
if (strcmp(Cmd_Argv(1), "1") == 0)
in = 1;
else if (strcmp(Cmd_Argv(1), "2") == 0)
in = 2;
}
for (players = 0, i = 0;i < svs.maxclients;i++)
if (svs.clients[i].active)
players++;
print ("host: %s\n", Cvar_VariableString ("hostname"));
print ("version: %s build %s\n", gamename, buildstring);
print ("protocol: %i (%s)\n", Protocol_NumberForEnum(sv.protocol), Protocol_NameForEnum(sv.protocol));
print ("map: %s\n", sv.name);
print ("timing: %s\n", Host_TimingReport());
print ("players: %i active (%i max)\n\n", players, svs.maxclients);
if (in == 1)
print ("^2IP %%pl ping time frags no name\n");
else if (in == 2)
print ("^5IP no name\n");
for (i = 0, k = 0, client = svs.clients;i < svs.maxclients;i++, client++)
{
if (!client->active)
continue;
++k;
if (in == 0 || in == 1)
{
seconds = (int)(realtime - client->connecttime);
minutes = seconds / 60;
if (minutes)
{
seconds -= (minutes * 60);
hours = minutes / 60;
if (hours)
minutes -= (hours * 60);
}
else
hours = 0;
packetloss = 0;
if (client->netconnection)
for (j = 0;j < NETGRAPH_PACKETS;j++)
if (client->netconnection->incoming_netgraph[j].unreliablebytes == NETGRAPH_LOSTPACKET)
packetloss++;
packetloss = (packetloss * 100 + NETGRAPH_PACKETS - 1) / NETGRAPH_PACKETS;
ping = bound(0, (int)floor(client->ping*1000+0.5), 9999);
}
if(sv_status_privacy.integer && cmd_source != src_command)
strlcpy(ip, client->netconnection ? "hidden" : "botclient" , 22);
else
strlcpy(ip, (client->netconnection && client->netconnection->address) ? client->netconnection->address : "botclient", 22);
frags = client->frags;
if(sv_status_show_qcstatus.integer && prog->fieldoffsets.clientstatus >= 0)
{
const char *str = PRVM_E_STRING(PRVM_EDICT_NUM(i + 1), prog->fieldoffsets.clientstatus);
if(str && *str)
{
char *p;
const char *q;
p = qcstatus;
for(q = str; *q && p != qcstatus + sizeof(qcstatus) - 1; ++q)
if(*q != '\\' && *q != '"' && !ISWHITESPACE(*q))
*p++ = *q;
*p = 0;
if(*qcstatus)
frags = atoi(qcstatus);
}
}
if (in == 0) // default layout
{
// LordHavoc: we must use multiple prints for ProQuake compatibility
print ("#%-3u ", i+1);
print ("%-16.16s ", client->name);
print ("%4i ", frags);
print ("%2i:%02i:%02i\n ", hours, minutes, seconds);
print ("%s\n", ip);
// print ("#%-3u %-16.16s %3i %2i:%02i:%02i\n", i+1, client->name, frags, hours, minutes, seconds);
// print (" %s\n", ip);
}
else if (in == 1) // extended layout
{
print ("%s%-21s %2i %4i %2i:%02i:%02i %4i #%-3u ^7%s\n", k%2 ? "^3" : "^7", ip, packetloss, ping, hours, minutes, seconds, frags, i+1, client->name);
}
else if (in == 2) // reduced layout
{
print ("%s%-21s #%-3u ^7%s\n", k%2 ? "^3" : "^7", ip, i+1, client->name);
}
}
if(cmd_source == src_command)
SV_VM_End();
}
/*
==================
Host_God_f
Sets client to godmode
==================
*/
void Host_God_f (void)
{
if (!allowcheats)
{
SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
return;
}
host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_GODMODE;
if (!((int)host_client->edict->fields.server->flags & FL_GODMODE) )
SV_ClientPrint("godmode OFF\n");
else
SV_ClientPrint("godmode ON\n");
}
void Host_Notarget_f (void)
{
if (!allowcheats)
{
SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
return;
}
host_client->edict->fields.server->flags = (int)host_client->edict->fields.server->flags ^ FL_NOTARGET;
if (!((int)host_client->edict->fields.server->flags & FL_NOTARGET) )
SV_ClientPrint("notarget OFF\n");
else
SV_ClientPrint("notarget ON\n");
}
qboolean noclip_anglehack;
void Host_Noclip_f (void)
{
if (!allowcheats)
{
SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
return;
}
if (host_client->edict->fields.server->movetype != MOVETYPE_NOCLIP)
{
noclip_anglehack = true;
host_client->edict->fields.server->movetype = MOVETYPE_NOCLIP;
SV_ClientPrint("noclip ON\n");
}
else
{
noclip_anglehack = false;
host_client->edict->fields.server->movetype = MOVETYPE_WALK;
SV_ClientPrint("noclip OFF\n");
}
}
/*
==================
Host_Fly_f
Sets client to flymode
==================
*/
void Host_Fly_f (void)
{
if (!allowcheats)
{
SV_ClientPrint("No cheats allowed, use sv_cheats 1 and restart level to enable.\n");
return;
}
if (host_client->edict->fields.server->movetype != MOVETYPE_FLY)
{
host_client->edict->fields.server->movetype = MOVETYPE_FLY;
SV_ClientPrint("flymode ON\n");
}
else
{
host_client->edict->fields.server->movetype = MOVETYPE_WALK;
SV_ClientPrint("flymode OFF\n");
}
}
/*
==================
Host_Ping_f
==================
*/
void Host_Pings_f (void); // called by Host_Ping_f
void Host_Ping_f (void)
{
int i;
client_t *client;
void (*print) (const char *fmt, ...);
if (cmd_source == src_command)
{
// if running a client, try to send over network so the client's ping report parser will see the report
if (cls.state == ca_connected)
{
Cmd_ForwardToServer ();
return;
}
print = Con_Printf;
}
else
print = SV_ClientPrintf;
if (!sv.active)
return;
print("Client ping times:\n");
for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
{
if (!client->active)
continue;
print("%4i %s\n", bound(0, (int)floor(client->ping*1000+0.5), 9999), client->name);
}
// now call the Pings command also, which will send a report that contains packet loss for the scoreboard (as well as a simpler ping report)
// actually, don't, it confuses old clients (resulting in "unknown command pingplreport" flooding the console)
//Host_Pings_f();
}
/*
===============================================================================
SERVER TRANSITIONS
===============================================================================
*/
/*
======================
Host_Map_f
handle a
map <servername>
command from the console. Active clients are kicked off.
======================
*/
void Host_Map_f (void)
{
char level[MAX_QPATH];
if (Cmd_Argc() != 2)
{
Con_Print("map <levelname> : start a new game (kicks off all players)\n");
return;
}
// GAME_DELUXEQUAKE - clear warpmark (used by QC)
if (gamemode == GAME_DELUXEQUAKE)
Cvar_Set("warpmark", "");
cls.demonum = -1; // stop demo loop in case this fails
CL_Disconnect ();
Host_ShutdownServer();
if(svs.maxclients != svs.maxclients_next)
{
svs.maxclients = svs.maxclients_next;
if (svs.clients)
Mem_Free(svs.clients);
svs.clients = (client_t *)Mem_Alloc(sv_mempool, sizeof(client_t) * svs.maxclients);
}
// remove menu
key_dest = key_game;
svs.serverflags = 0; // haven't completed an episode yet
allowcheats = sv_cheats.integer != 0;
strlcpy(level, Cmd_Argv(1), sizeof(level));
SV_SpawnServer(level);
if (sv.active && cls.state == ca_disconnected)
CL_EstablishConnection("local:1");
}
/*
==================
Host_Changelevel_f
Goes to a new map, taking all clients along
==================
*/
void Host_Changelevel_f (void)
{
char level[MAX_QPATH];
if (Cmd_Argc() != 2)
{
Con_Print("changelevel <levelname> : continue game on a new level\n");
return;
}
// HACKHACKHACK
if (!sv.active) {
Host_Map_f();
return;
}
// remove menu
key_dest = key_game;
SV_VM_Begin();
SV_SaveSpawnparms ();
SV_VM_End();
allowcheats = sv_cheats.integer != 0;
strlcpy(level, Cmd_Argv(1), sizeof(level));
SV_SpawnServer(level);
if (sv.active && cls.state == ca_disconnected)
CL_EstablishConnection("local:1");
}
/*
==================
Host_Restart_f
Restarts the current server for a dead player
==================
*/
void Host_Restart_f (void)
{
char mapname[MAX_QPATH];
if (Cmd_Argc() != 1)
{
Con_Print("restart : restart current level\n");
return;
}
if (!sv.active)
{
Con_Print("Only the server may restart\n");
return;
}
// remove menu
key_dest = key_game;
allowcheats = sv_cheats.integer != 0;
strlcpy(mapname, sv.name, sizeof(mapname));
SV_SpawnServer(mapname);
if (sv.active && cls.state == ca_disconnected)
CL_EstablishConnection("local:1");
}
/*
==================
Host_Reconnect_f
This command causes the client to wait for the signon messages again.
This is sent just before a server changes levels
==================
*/
void Host_Reconnect_f (void)
{
char temp[128];
// if not connected, reconnect to the most recent server
if (!cls.netcon)
{
// if we have connected to a server recently, the userinfo
// will still contain its IP address, so get the address...
InfoString_GetValue(cls.userinfo, "*ip", temp, sizeof(temp));
if (temp[0])
CL_EstablishConnection(temp);
else
Con_Printf("Reconnect to what server? (you have not connected to a server yet)\n");
return;
}
// if connected, do something based on protocol
if (cls.protocol == PROTOCOL_QUAKEWORLD)
{
// quakeworld can just re-login
if (cls.qw_downloadmemory) // don't change when downloading
return;
S_StopAllSounds();
if (cls.state == ca_connected && cls.signon < SIGNONS)
{
Con_Printf("reconnecting...\n");
MSG_WriteChar(&cls.netcon->message, qw_clc_stringcmd);
MSG_WriteString(&cls.netcon->message, "new");
}
}
else
{
// netquake uses reconnect on level changes (silly)
if (Cmd_Argc() != 1)
{
Con_Print("reconnect : wait for signon messages again\n");
return;
}
if (!cls.signon)
{
Con_Print("reconnect: no signon, ignoring reconnect\n");
return;
}
cls.signon = 0; // need new connection messages
}
}
/*
=====================
Host_Connect_f
User command to connect to server
=====================
*/
void Host_Connect_f (void)
{
if (Cmd_Argc() != 2)
{
Con_Print("connect <serveraddress> : connect to a multiplayer game\n");
return;
}
// clear the rcon password, to prevent vulnerability by stuffcmd-ing a connect command
if(rcon_secure.integer <= 0)
Cvar_SetQuick(&rcon_password, "");
CL_EstablishConnection(Cmd_Argv(1));
}
/*
===============================================================================
LOAD / SAVE GAME
===============================================================================
*/
#define SAVEGAME_VERSION 5
void Host_Savegame_to (const char *name)
{
qfile_t *f;
int i, k, l, lightstyles = 64;
char comment[SAVEGAME_COMMENT_LENGTH+1];
char line[MAX_INPUTLINE];
qboolean isserver;
char *s;
// first we have to figure out if this can be saved in 64 lightstyles
// (for Quake compatibility)
for (i=64 ; i<MAX_LIGHTSTYLES ; i++)
if (sv.lightstyles[i][0])
lightstyles = i+1;
isserver = !strcmp(PRVM_NAME, "server");
Con_Printf("Saving game to %s...\n", name);
f = FS_OpenRealFile(name, "wb", false);
if (!f)
{
Con_Print("ERROR: couldn't open.\n");
return;
}
FS_Printf(f, "%i\n", SAVEGAME_VERSION);
memset(comment, 0, sizeof(comment));
if(isserver)
dpsnprintf(comment, sizeof(comment), "%-21.21s kills:%3i/%3i", PRVM_GetString(prog->edicts->fields.server->message), (int)prog->globals.server->killed_monsters, (int)prog->globals.server->total_monsters);
else
dpsnprintf(comment, sizeof(comment), "(crash dump of %s progs)", PRVM_NAME);
// convert space to _ to make stdio happy
// LordHavoc: convert control characters to _ as well
for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
if (ISWHITESPACEORCONTROL(comment[i]))
comment[i] = '_';
comment[SAVEGAME_COMMENT_LENGTH] = '\0';
FS_Printf(f, "%s\n", comment);
if(isserver)
{
for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
FS_Printf(f, "%f\n", svs.clients[0].spawn_parms[i]);
FS_Printf(f, "%d\n", current_skill);
FS_Printf(f, "%s\n", sv.name);
FS_Printf(f, "%f\n",sv.time);
}
else
{
for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
FS_Printf(f, "(dummy)\n");
FS_Printf(f, "%d\n", 0);
FS_Printf(f, "%s\n", "(dummy)");
FS_Printf(f, "%f\n", realtime);
}
// write the light styles
for (i=0 ; i<lightstyles ; i++)
{
if (isserver && sv.lightstyles[i][0])
FS_Printf(f, "%s\n", sv.lightstyles[i]);
else
FS_Print(f,"m\n");
}
PRVM_ED_WriteGlobals (f);
for (i=0 ; i<prog->num_edicts ; i++)
{
FS_Printf(f,"// edict %d\n", i);
//Con_Printf("edict %d...\n", i);
PRVM_ED_Write (f, PRVM_EDICT_NUM(i));
}
#if 1
FS_Printf(f,"/*\n");
FS_Printf(f,"// DarkPlaces extended savegame\n");
// darkplaces extension - extra lightstyles, support for color lightstyles
for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
if (isserver && sv.lightstyles[i][0])
FS_Printf(f, "sv.lightstyles %i %s\n", i, sv.lightstyles[i]);
// darkplaces extension - model precaches
for (i=1 ; i<MAX_MODELS ; i++)
if (sv.model_precache[i][0])
FS_Printf(f,"sv.model_precache %i %s\n", i, sv.model_precache[i]);
// darkplaces extension - sound precaches
for (i=1 ; i<MAX_SOUNDS ; i++)
if (sv.sound_precache[i][0])
FS_Printf(f,"sv.sound_precache %i %s\n", i, sv.sound_precache[i]);
// darkplaces extension - save buffers
for (i = 0; i < (int)Mem_ExpandableArray_IndexRange(&prog->stringbuffersarray); i++)
{
prvm_stringbuffer_t *stringbuffer = (prvm_stringbuffer_t*) Mem_ExpandableArray_RecordAtIndex(&prog->stringbuffersarray, i);
if(stringbuffer && (stringbuffer->flags & STRINGBUFFER_SAVED))
{
for(k = 0; k < stringbuffer->num_strings; k++)
{
if (!stringbuffer->strings[k])
continue;
// Parse the string a bit to turn special characters
// (like newline, specifically) into escape codes
s = stringbuffer->strings[k];
for (l = 0;l < (int)sizeof(line) - 2 && *s;)
{
if (*s == '\n')
{
line[l++] = '\\';
line[l++] = 'n';
}
else if (*s == '\r')
{
line[l++] = '\\';
line[l++] = 'r';
}
else if (*s == '\\')
{
line[l++] = '\\';
line[l++] = '\\';
}
else if (*s == '"')
{
line[l++] = '\\';
line[l++] = '"';
}
else
line[l++] = *s;
s++;
}
line[l] = '\0';
FS_Printf(f,"sv.bufstr %i %i \"%s\"\n", i, k, line);
}
}
}
FS_Printf(f,"*/\n");
#endif
FS_Close (f);
Con_Print("done.\n");
}
/*
===============
Host_Savegame_f
===============
*/
void Host_Savegame_f (void)
{
char name[MAX_QPATH];
if (!sv.active)
{
Con_Print("Can't save - no server running.\n");
return;
}
if (cl.islocalgame)
{
// singleplayer checks
if (cl.intermission)
{
Con_Print("Can't save in intermission.\n");
return;
}
if (svs.clients[0].active && svs.clients[0].edict->fields.server->deadflag)
{
Con_Print("Can't savegame with a dead player\n");
return;
}
}
else
Con_Print("Warning: saving a multiplayer game may have strange results when restored (to properly resume, all players must join in the same player slots and then the game can be reloaded).\n");
if (Cmd_Argc() != 2)
{
Con_Print("save <savename> : save a game\n");
return;
}
if (strstr(Cmd_Argv(1), ".."))
{
Con_Print("Relative pathnames are not allowed.\n");
return;
}
strlcpy (name, Cmd_Argv(1), sizeof (name));
FS_DefaultExtension (name, ".sav", sizeof (name));
SV_VM_Begin();
Host_Savegame_to(name);
SV_VM_End();
}
/*
===============
Host_Loadgame_f
===============
*/
void Host_Loadgame_f (void)
{
char filename[MAX_QPATH];
char mapname[MAX_QPATH];
float time;
const char *start;
const char *end;
const char *t;
char *text;
prvm_edict_t *ent;
int i, k;
int entnum;
int version;
float spawn_parms[NUM_SPAWN_PARMS];
prvm_stringbuffer_t *stringbuffer;
size_t alloclen;
if (Cmd_Argc() != 2)
{
Con_Print("load <savename> : load a game\n");
return;
}
strlcpy (filename, Cmd_Argv(1), sizeof(filename));
FS_DefaultExtension (filename, ".sav", sizeof (filename));
Con_Printf("Loading game from %s...\n", filename);
// stop playing demos
if (cls.demoplayback)
CL_Disconnect ();
// remove menu
key_dest = key_game;
cls.demonum = -1; // stop demo loop in case this fails
t = text = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
if (!text)
{
Con_Print("ERROR: couldn't open.\n");
return;
}
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading version\n");
// version
COM_ParseToken_Simple(&t, false, false);
version = atoi(com_token);
if (version != SAVEGAME_VERSION)
{
Mem_Free(text);
Con_Printf("Savegame is version %i, not %i\n", version, SAVEGAME_VERSION);
return;
}
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading description\n");
// description
COM_ParseToken_Simple(&t, false, false);
for (i = 0;i < NUM_SPAWN_PARMS;i++)
{
COM_ParseToken_Simple(&t, false, false);
spawn_parms[i] = atof(com_token);
}
// skill
COM_ParseToken_Simple(&t, false, false);
// this silliness is so we can load 1.06 save files, which have float skill values
current_skill = (int)(atof(com_token) + 0.5);
Cvar_SetValue ("skill", (float)current_skill);
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading mapname\n");
// mapname
COM_ParseToken_Simple(&t, false, false);
strlcpy (mapname, com_token, sizeof(mapname));
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading time\n");
// time
COM_ParseToken_Simple(&t, false, false);
time = atof(com_token);
allowcheats = sv_cheats.integer != 0;
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: spawning server\n");
SV_SpawnServer (mapname);
if (!sv.active)
{
Mem_Free(text);
Con_Print("Couldn't load map\n");
return;
}
sv.paused = true; // pause until all clients connect
sv.loadgame = true;
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading light styles\n");
// load the light styles
SV_VM_Begin();
// -1 is the globals
entnum = -1;
for (i = 0;i < MAX_LIGHTSTYLES;i++)
{
// light style
start = t;
COM_ParseToken_Simple(&t, false, false);
// if this is a 64 lightstyle savegame produced by Quake, stop now
// we have to check this because darkplaces may save more than 64
if (com_token[0] == '{')
{
t = start;
break;
}
strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
}
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: skipping until globals\n");
// now skip everything before the first opening brace
// (this is for forward compatibility, so that older versions (at
// least ones with this fix) can load savegames with extra data before the
// first brace, as might be produced by a later engine version)
for (;;)
{
start = t;
if (!COM_ParseToken_Simple(&t, false, false))
break;
if (com_token[0] == '{')
{
t = start;
break;
}
}
// unlink all entities
World_UnlinkAll(&sv.world);
// load the edicts out of the savegame file
end = t;
for (;;)
{
start = t;
while (COM_ParseToken_Simple(&t, false, false))
if (!strcmp(com_token, "}"))
break;
if (!COM_ParseToken_Simple(&start, false, false))
{
// end of file
break;
}
if (strcmp(com_token,"{"))
{
Mem_Free(text);
Host_Error ("First token isn't a brace");
}
if (entnum == -1)
{
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading globals\n");
// parse the global vars
PRVM_ED_ParseGlobals (start);
}
else
{
// parse an edict
if (entnum >= MAX_EDICTS)
{
Mem_Free(text);
Host_Error("Host_PerformLoadGame: too many edicts in save file (reached MAX_EDICTS %i)", MAX_EDICTS);
}
while (entnum >= prog->max_edicts)
PRVM_MEM_IncreaseEdicts();
ent = PRVM_EDICT_NUM(entnum);
memset (ent->fields.server, 0, prog->progs->entityfields * 4);
ent->priv.server->free = false;
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading edict %d\n", entnum);
PRVM_ED_ParseEdict (start, ent);
// link it into the bsp tree
if (!ent->priv.server->free)
SV_LinkEdict(ent);
}
end = t;
entnum++;
}
prog->num_edicts = entnum;
sv.time = time;
for (i = 0;i < NUM_SPAWN_PARMS;i++)
svs.clients[0].spawn_parms[i] = spawn_parms[i];
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: skipping until extended data\n");
// read extended data if present
// the extended data is stored inside a /* */ comment block, which the
// parser intentionally skips, so we have to check for it manually here
if(end)
{
while (*end == '\r' || *end == '\n')
end++;
if (end[0] == '/' && end[1] == '*' && (end[2] == '\r' || end[2] == '\n'))
{
if(developer_entityparsing.integer)
Con_Printf("Host_Loadgame_f: loading extended data\n");
Con_Printf("Loading extended DarkPlaces savegame\n");
t = end + 2;
memset(sv.lightstyles[0], 0, sizeof(sv.lightstyles));
memset(sv.model_precache[0], 0, sizeof(sv.model_precache));
memset(sv.sound_precache[0], 0, sizeof(sv.sound_precache));
while (COM_ParseToken_Simple(&t, false, false))
{
if (!strcmp(com_token, "sv.lightstyles"))
{
COM_ParseToken_Simple(&t, false, false);
i = atoi(com_token);
COM_ParseToken_Simple(&t, false, false);
if (i >= 0 && i < MAX_LIGHTSTYLES)
strlcpy(sv.lightstyles[i], com_token, sizeof(sv.lightstyles[i]));
else
Con_Printf("unsupported lightstyle %i \"%s\"\n", i, com_token);
}
else if (!strcmp(com_token, "sv.model_precache"))
{
COM_ParseToken_Simple(&t, false, false);
i = atoi(com_token);
COM_ParseToken_Simple(&t, false, false);
if (i >= 0 && i < MAX_MODELS)
{
strlcpy(sv.model_precache[i], com_token, sizeof(sv.model_precache[i]));
sv.models[i] = Mod_ForName (sv.model_precache[i], true, false, sv.model_precache[i][0] == '*' ? sv.modelname : NULL);
}
else
Con_Printf("unsupported model %i \"%s\"\n", i, com_token);
}
else if (!strcmp(com_token, "sv.sound_precache"))
{