-
Notifications
You must be signed in to change notification settings - Fork 2
/
g_save.c
1208 lines (1024 loc) · 23.7 KB
/
g_save.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) 1997-2001 Id Software, Inc.
* Copyright (C) 2011 Knightmare
* Copyright (C) 2011 Yamagi Burmeister
*
* 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.
*
* =======================================================================
*
* The savegame system.
*
* =======================================================================
*/
/*
* This is the Quake 2 savegame system, fixed by Yamagi
* based on an idea by Knightmare of kmquake2. This major
* rewrite of the original g_save.c is much more robust
* and portable since it doesn't use any function pointers.
*
* Inner workings:
* When the game is saved all function pointers are
* translated into human readable function definition strings.
* The same way all mmove_t pointers are translated. This
* human readable strings are then written into the file.
* At game load the human readable strings are retranslated
* into the actual function pointers and struct pointers. The
* pointers are generated at each compilation / start of the
* client, thus the pointers are always correct.
*
* Limitations:
* While savegames survive recompilations of the game source
* and bigger changes in the source, there are some limitation
* which a nearly impossible to fix without a object orientated
* rewrite of the game.
* - If functions or mmove_t structs that a referencenced
* inside savegames are added or removed (e.g. the files
* in tables/ are altered) the load functions cannot
* reconnect all pointers and thus not restore the game.
* - If the operating system is changed internal structures
* may change in an unrepairable way.
* - If the architecture is changed pointer length and
* other internal datastructures change in an
* incompatible way.
* - If the edict_t struct is changed, savegames
* will break.
* This is not so bad as it looks since functions and
* struct won't be added and edict_t won't be changed
* if no big, sweeping changes are done. The operating
* system and architecture are in the hands of the user.
*/
#include "g_local.h"
/*
* When ever the savegame version is changed, q2 will refuse to
* load older savegames. This should be bumped if the files
* in tables/ are changed, otherwise strange things may happen.
*/
#define SAVEGAMEVER "YQ2-3"
#ifndef BUILD_DATE
#define BUILD_DATE __DATE__
#endif
/* Backporting. */
#define Q_fopen fopen
/*
* This macros are used to prohibit loading of savegames
* created on other systems or architectures. This will
* crash q2 in spectacular ways
*/
#ifndef YQ2OSTYPE
#error YQ2OSTYPE should be defined by the build system
#endif
#ifndef YQ2ARCH
#error YQ2ARCH should be defined by the build system
#endif
/*
* Older operating systen and architecture detection
* macros, implemented by savegame version YQ2-1.
*/
#if defined(__APPLE__)
#define OSTYPE_1 "MacOS X"
#elif defined(__FreeBSD__)
#define OSTYPE_1 "FreeBSD"
#elif defined(__OpenBSD__)
#define OSTYPE_1 "OpenBSD"
#elif defined(__linux__)
#define OSTYPE_1 "Linux"
#elif defined(_WIN32)
#define OSTYPE_1 "Windows"
#else
#define OSTYPE_1 "Unknown"
#endif
#if defined(__i386__)
#define ARCH_1 "i386"
#elif defined(__x86_64__)
#define ARCH_1 "amd64"
#elif defined(__sparc__)
#define ARCH_1 "sparc64"
#elif defined(__ia64__)
#define ARCH_1 "ia64"
#else
#define ARCH_1 "unknown"
#endif
/*
* Connects a human readable
* function signature with
* the corresponding pointer
*/
typedef struct
{
char *funcStr;
byte *funcPtr;
} functionList_t;
/*
* Connects a human readable
* mmove_t string with the
* corresponding pointer
* */
typedef struct
{
char *mmoveStr;
mmove_t *mmovePtr;
} mmoveList_t;
/* ========================================================= */
/*
* Prototypes for forward
* declaration for all game
* functions.
*/
#include "tables/gamefunc_decs.h"
/*
* List with function pointer
* to each of the functions
* prototyped above.
*/
functionList_t functionList[] = {
#include "tables/gamefunc_list.h"
};
/*
* Prototypes for forward
* declaration for all game
* mmove_t functions.
*/
#include "tables/gamemmove_decs.h"
/*
* List with pointers to
* each of the mmove_t
* functions prototyped
* above.
*/
mmoveList_t mmoveList[] = {
#include "tables/gamemmove_list.h"
};
/*
* Fields to be saved
*/
field_t fields[] = {
#include "tables/fields.h"
};
/*
* Level fields to
* be saved
*/
field_t levelfields[] = {
#include "tables/levelfields.h"
};
/*
* Client fields to
* be saved
*/
field_t clientfields[] = {
#include "tables/clientfields.h"
};
/* ========================================================= */
/*
* This will be called when the dll is first loaded,
* which only happens when a new game is started or
* a save game is loaded.
*/
void
InitGame(void)
{
gi.dprintf("Game is starting up.\n");
gi.dprintf("Game is %s built on %s.\n", GAMEVERSION, BUILD_DATE);
gun_x = gi.cvar("gun_x", "0", 0);
gun_y = gi.cvar("gun_y", "0", 0);
gun_z = gi.cvar("gun_z", "0", 0);
sv_rollspeed = gi.cvar("sv_rollspeed", "200", 0);
sv_rollangle = gi.cvar("sv_rollangle", "2", 0);
sv_maxvelocity = gi.cvar("sv_maxvelocity", "2500", 0);
sv_gravity = gi.cvar("sv_gravity", "800", 0);
/* noset vars */
dedicated = gi.cvar("dedicated", "0", CVAR_NOSET);
gamedir = gi.cvar ("gamedir", "", CVAR_NOSET);
/* latched vars */
sv_cheats = gi.cvar("cheats", "0", CVAR_SERVERINFO | CVAR_LATCH);
gi.cvar("gamename", GAMEVERSION, CVAR_SERVERINFO | CVAR_LATCH);
gi.cvar("gamedate", BUILD_DATE, CVAR_SERVERINFO | CVAR_LATCH);
maxclients = gi.cvar("maxclients", "4", CVAR_SERVERINFO | CVAR_LATCH);
maxspectators = gi.cvar("maxspectators", "4", CVAR_SERVERINFO);
deathmatch = gi.cvar("deathmatch", "0", CVAR_LATCH);
coop = gi.cvar("coop", "0", CVAR_LATCH);
skill = gi.cvar("skill", "1", CVAR_LATCH);
maxentities = gi.cvar("maxentities", "1024", CVAR_LATCH);
maplist = gi.cvar ("maplist", "0", CVAR_SERVERINFO);
maplistfile = gi.cvar ("maplistfile", "maplist.txt", 0);
motdfile = gi.cvar ("motdfile", "motd.txt", 0);
/* change anytime vars */
dmflags = gi.cvar("dmflags", "0", CVAR_SERVERINFO);
fraglimit = gi.cvar("fraglimit", "0", CVAR_SERVERINFO);
timelimit = gi.cvar("timelimit", "0", CVAR_SERVERINFO);
password = gi.cvar("password", "", CVAR_USERINFO);
spectator_password = gi.cvar("spectator_password", "", CVAR_USERINFO);
needpass = gi.cvar("needpass", "0", CVAR_SERVERINFO);
filterban = gi.cvar("filterban", "1", 0);
idledetect = gi.cvar ("idledetect", "0", 0);
sv_autodark = gi.cvar ("autodark", "", 0);
g_select_empty = gi.cvar("g_select_empty", "0", CVAR_ARCHIVE);
run_pitch = gi.cvar("run_pitch", "0.002", 0);
run_roll = gi.cvar("run_roll", "0.005", 0);
bob_up = gi.cvar("bob_up", "0.005", 0);
bob_pitch = gi.cvar("bob_pitch", "0.002", 0);
bob_roll = gi.cvar("bob_roll", "0.002", 0);
/* flood control */
flood_msgs = gi.cvar("flood_msgs", "4", 0);
flood_persecond = gi.cvar("flood_persecond", "4", 0);
flood_waitdelay = gi.cvar("flood_waitdelay", "10", 0);
/* weapon/feature banning */
weaponban = gi.cvar ("weaponban", "0", CVAR_SERVERINFO);
featureban = gi.cvar ("featureban", "0", CVAR_SERVERINFO);
fragban = gi.cvar ("fragban", "0", CVAR_SERVERINFO);
/* items */
InitItems();
game.helpmessage1[0] = 0;
game.helpmessage2[0] = 0;
/* initialize all entities for this game */
game.maxentities = maxentities->value;
g_edicts = gi.TagMalloc(game.maxentities * sizeof(g_edicts[0]), TAG_GAME);
globals.edicts = g_edicts;
globals.max_edicts = game.maxentities;
/* initialize all clients for this game */
game.maxclients = maxclients->value;
game.clients = gi.TagMalloc(game.maxclients * sizeof(game.clients[0]), TAG_GAME);
globals.num_edicts = game.maxclients + 1;
//ZOID
CTFInit();
//ZOID
}
/* ========================================================= */
/*
* Helper function to get
* the human readable function
* definition by an address.
* Called by WriteField1 and
* WriteField2.
*/
static functionList_t *
GetFunctionByAddress(byte *adr)
{
int i;
for (i = 0; functionList[i].funcStr; i++)
{
if (functionList[i].funcPtr == adr)
{
return &functionList[i];
}
}
return NULL;
}
/*
* Helper function to get the
* pointer to a function by
* it's human readable name.
* Called by WriteField1 and
* WriteField2.
*/
static byte *
FindFunctionByName(char *name)
{
int i;
for (i = 0; functionList[i].funcStr; i++)
{
if (!strcmp(name, functionList[i].funcStr))
{
return functionList[i].funcPtr;
}
}
return NULL;
}
/*
* Helper function to get the
* human readable definition of
* a mmove_t struct by a pointer.
*/
static mmoveList_t *
GetMmoveByAddress(mmove_t *adr)
{
int i;
for (i = 0; mmoveList[i].mmoveStr; i++)
{
if (mmoveList[i].mmovePtr == adr)
{
return &mmoveList[i];
}
}
return NULL;
}
/*
* Helper function to get the
* pointer to a mmove_t struct
* by a human readable definition.
*/
static mmove_t *
FindMmoveByName(char *name)
{
int i;
for (i = 0; mmoveList[i].mmoveStr; i++)
{
if (!strcmp(name, mmoveList[i].mmoveStr))
{
return mmoveList[i].mmovePtr;
}
}
return NULL;
}
/* ========================================================= */
/*
* The following two functions are
* doing the dirty work to write the
* data generated by the functions
* below this block into files.
*/
static void
WriteField1(FILE *f, field_t *field, byte *base, char const *caller, char const *extra)
{
void *p;
int len;
int index;
functionList_t *func;
mmoveList_t *mmove;
if (field->flags & FFL_SPAWNTEMP)
{
return;
}
p = (void *)(base + field->ofs);
switch (field->type)
{
case F_INT:
case F_FLOAT:
case F_ANGLEHACK:
case F_VECTOR:
case F_IGNORE:
break;
case F_LSTRING:
case F_GSTRING:
if (*(char **)p)
{
len = strlen(*(char **)p) + 1;
}
else
{
len = 0;
}
*(int *)p = len;
break;
case F_EDICT:
if (*(edict_t **)p == NULL)
{
index = -1;
}
else
{
index = *(edict_t **)p - g_edicts;
}
*(int *)p = index;
break;
case F_CLIENT:
if (*(gclient_t **)p == NULL)
{
index = -1;
}
else
{
index = *(gclient_t **)p - game.clients;
}
*(int *)p = index;
break;
case F_ITEM:
if (*(edict_t **)p == NULL)
{
index = -1;
}
else
{
index = GetIndexByItem(*(gitem_t **)p);
}
*(int *)p = index;
break;
case F_FUNCTION:
if (*(byte **)p == NULL)
{
len = 0;
}
else
{
func = GetFunctionByAddress (*(byte **)p);
if (!func)
{
//gi.error ("WriteField1: function not in list, can't save game");
char aszMessage[1024];
sprintf(aszMessage, "WriteField1 (%s %s): function \"%s\" not in list, can't save game", caller, extra, field->name);
gi.error(aszMessage);
}
len = strlen(func->funcStr)+1;
}
*(int *)p = len;
break;
case F_MMOVE:
if (*(byte **)p == NULL)
{
len = 0;
}
else
{
mmove = GetMmoveByAddress (*(mmove_t **)p);
if (!mmove)
{
gi.error ("WriteField1: mmove not in list, can't save game");
}
len = strlen(mmove->mmoveStr)+1;
}
*(int *)p = len;
break;
default:
gi.error("WriteEdict: unknown field type");
}
}
static void
WriteField2(FILE *f, field_t *field, byte *base)
{
int len;
void *p;
functionList_t *func;
mmoveList_t *mmove;
if (field->flags & FFL_SPAWNTEMP)
{
return;
}
p = (void *)(base + field->ofs);
switch (field->type)
{
case F_LSTRING:
if (*(char **)p)
{
len = strlen(*(char **)p) + 1;
fwrite(*(char **)p, len, 1, f);
}
break;
case F_FUNCTION:
if (*(byte **)p)
{
func = GetFunctionByAddress (*(byte **)p);
if (!func)
{
gi.error ("WriteField2: function not in list, can't save game");
}
len = strlen(func->funcStr)+1;
fwrite (func->funcStr, len, 1, f);
}
break;
case F_MMOVE:
if (*(byte **)p)
{
mmove = GetMmoveByAddress (*(mmove_t **)p);
if (!mmove)
{
gi.error ("WriteField2: mmove not in list, can't save game");
}
len = strlen(mmove->mmoveStr)+1;
fwrite (mmove->mmoveStr, len, 1, f);
}
break;
default:
break;
}
}
/* ========================================================= */
/*
* This function does the dirty
* work to read the data from a
* file. The processing of the
* data is done in the functions
* below
*/
static void
ReadField(FILE *f, field_t *field, byte *base)
{
void *p;
int len;
int index;
char funcStr[2048];
if (field->flags & FFL_SPAWNTEMP)
{
return;
}
p = (void *)(base + field->ofs);
switch (field->type)
{
case F_INT:
case F_FLOAT:
case F_ANGLEHACK:
case F_VECTOR:
case F_IGNORE:
break;
case F_LSTRING:
len = *(int *)p;
if (!len)
{
*(char **)p = NULL;
}
else
{
*(char **)p = gi.TagMalloc(32 + len, TAG_LEVEL);
fread(*(char **)p, len, 1, f);
}
break;
case F_EDICT:
index = *(int *)p;
if (index == -1)
{
*(edict_t **)p = NULL;
}
else
{
*(edict_t **)p = &g_edicts[index];
}
break;
case F_CLIENT:
index = *(int *)p;
if (index == -1)
{
*(gclient_t **)p = NULL;
}
else
{
*(gclient_t **)p = &game.clients[index];
}
break;
case F_ITEM:
index = *(int *)p;
if (index == -1)
{
*(gitem_t **)p = NULL;
}
else
{
*(gitem_t **)p = itemlist[index];
}
break;
case F_FUNCTION:
len = *(int *)p;
if (!len)
{
*(byte **)p = NULL;
}
else
{
if (len > sizeof(funcStr))
{
gi.error ("ReadField: function name is longer than buffer (%i chars)",
(int)sizeof(funcStr));
}
fread (funcStr, len, 1, f);
if ( !(*(byte **)p = FindFunctionByName (funcStr)) )
{
gi.error ("ReadField: function %s not found in table, can't load game", funcStr);
}
}
break;
case F_MMOVE:
len = *(int *)p;
if (!len)
{
*(byte **)p = NULL;
}
else
{
if (len > sizeof(funcStr))
{
gi.error ("ReadField: mmove name is longer than buffer (%i chars)",
(int)sizeof(funcStr));
}
fread (funcStr, len, 1, f);
if ( !(*(mmove_t **)p = FindMmoveByName (funcStr)) )
{
gi.error ("ReadField: mmove %s not found in table, can't load game", funcStr);
}
}
break;
default:
gi.error("ReadEdict: unknown field type");
}
}
/* ========================================================= */
/*
* Write the client struct into a file.
*/
static void
WriteClient(FILE *f, gclient_t *client)
{
field_t *field;
gclient_t temp;
/* all of the ints, floats, and vectors stay as they are */
temp = *client;
/* change the pointers to indexes */
for (field = clientfields; field->name; field++)
{
WriteField1(f, field, (byte *)&temp, "WriteClient", "");
}
/* write the block */
fwrite(&temp, sizeof(temp), 1, f);
/* now write any allocated data following the edict */
for (field = clientfields; field->name; field++)
{
WriteField2(f, field, (byte *)client);
}
}
/*
* Read the client struct from a file
*/
static void
ReadClient(FILE *f, gclient_t *client, short save_ver)
{
field_t *field;
fread(client, sizeof(*client), 1, f);
for (field = clientfields; field->name; field++)
{
if (field->save_ver <= save_ver)
{
ReadField(f, field, (byte *)client);
}
}
if (save_ver < 3)
{
InitClientResp(client);
}
}
/* ========================================================= */
/*
* Writes the game struct into
* a file. This is called whenever
* the game goes to a new level or
* the user saves the game. The saved
* information consists of:
* - cross level data
* - client states
* - help computer info
*/
void
WriteGame(const char *filename, qboolean autosave)
{
FILE *f;
int i;
char str_ver[32];
char str_game[32];
char str_os[32];
char str_arch[32];
if (!autosave)
{
SaveClientData();
}
f = Q_fopen(filename, "wb");
if (!f)
{
gi.error("Couldn't open %s", filename);
}
/* Savegame identification */
memset(str_ver, 0, sizeof(str_ver));
memset(str_game, 0, sizeof(str_game));
memset(str_os, 0, sizeof(str_os));
memset(str_arch, 0, sizeof(str_arch));
Q_strlcpy(str_ver, SAVEGAMEVER, sizeof(str_ver) - 1);
Q_strlcpy(str_game, GAMEVERSION, sizeof(str_game) - 1);
Q_strlcpy(str_os, YQ2OSTYPE, sizeof(str_os) - 1);
Q_strlcpy(str_arch, YQ2ARCH, sizeof(str_arch) - 1);
fwrite(str_ver, sizeof(str_ver), 1, f);
fwrite(str_game, sizeof(str_game), 1, f);
fwrite(str_os, sizeof(str_os), 1, f);
fwrite(str_arch, sizeof(str_arch), 1, f);
game.autosaved = autosave;
fwrite(&game, sizeof(game), 1, f);
game.autosaved = false;
for (i = 0; i < game.maxclients; i++)
{
WriteClient(f, &game.clients[i]);
}
fclose(f);
}
/*
* Read the game structs from
* a file. Called when ever a
* savegames is loaded.
*/
void
ReadGame(const char *filename)
{
FILE *f;
int i;
char str_ver[32];
char str_game[32];
char str_os[32];
char str_arch[32];
short save_ver = 0;
gi.FreeTags(TAG_GAME);
f = Q_fopen(filename, "rb");
if (!f)
{
gi.error("Couldn't open %s", filename);
}
/* Sanity checks */
fread(str_ver, sizeof(str_ver), 1, f);
fread(str_game, sizeof(str_game), 1, f);
fread(str_os, sizeof(str_os), 1, f);
fread(str_arch, sizeof(str_arch), 1, f);
if (!strcmp(str_ver, SAVEGAMEVER))
{
save_ver = 3;
if (strcmp(str_game, GAMEVERSION))
{
fclose(f);
gi.error("Savegame from another game.so.\n");
}
else if (strcmp(str_os, YQ2OSTYPE))
{
fclose(f);
gi.error("Savegame from another os.\n");
}
else if (strcmp(str_arch, YQ2ARCH))
{
fclose(f);
gi.error("Savegame from another architecture.\n");
}
}
else if (!strcmp(str_ver, "YQ2-2"))
{
save_ver = 2;
if (strcmp(str_game, GAMEVERSION))
{
fclose(f);
gi.error("Savegame from another game.so.\n");
}
else if (strcmp(str_os, YQ2OSTYPE))
{
fclose(f);
gi.error("Savegame from another os.\n");
}
else if (strcmp(str_arch, YQ2ARCH))
{
fclose(f);
gi.error("Savegame from another architecture.\n");
}
}
else if (!strcmp(str_ver, "YQ2-1"))
{
save_ver = 1;
if (strcmp(str_game, GAMEVERSION))
{
fclose(f);
gi.error("Savegame from another game.so.\n");
}
else if (strcmp(str_os, OSTYPE_1))
{
fclose(f);
gi.error("Savegame from another os.\n");
}
if (!strcmp(str_os, "Windows"))
{
/* Windows was forced to i386 */
if (strcmp(str_arch, "i386"))
{
fclose(f);
gi.error("Savegame from another architecture.\n");
}
}
else
{
if (strcmp(str_arch, ARCH_1))
{
fclose(f);
gi.error("Savegame from another architecture.\n");
}
}
}
else
{
fclose(f);
gi.error("Savegame from an incompatible version.\n");
}
g_edicts = gi.TagMalloc(game.maxentities * sizeof(g_edicts[0]), TAG_GAME);
globals.edicts = g_edicts;
fread(&game, sizeof(game), 1, f);
game.clients = gi.TagMalloc(game.maxclients * sizeof(game.clients[0]),
TAG_GAME);
for (i = 0; i < game.maxclients; i++)
{
ReadClient(f, &game.clients[i], save_ver);
}
fclose(f);
}
/* ========================================================== */
/*
* Helper function to write the
* edict into a file. Called by
* WriteLevel.
*/
static void
WriteEdict(FILE *f, edict_t *ent)
{
field_t *field;
edict_t temp;
/* all of the ints, floats, and vectors stay as they are */
temp = *ent;
/* change the pointers to lengths or indexes */
for (field = fields; field->name; field++)
{
WriteField1(f, field, (byte *)&temp, "WriteEdict", ent->classname);
}
/* write the block */
fwrite(&temp, sizeof(temp), 1, f);
/* now write any allocated data following the edict */
for (field = fields; field->name; field++)
{
WriteField2(f, field, (byte *)ent);
}
}
/*
* Helper function to write the
* level local data into a file.
* Called by WriteLevel.
*/
static void
WriteLevelLocals(FILE *f)
{