/
p_saveg.cpp
5847 lines (4902 loc) · 151 KB
/
p_saveg.cpp
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
/** @file p_saveg.cpp Common game-save state management.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <lzss.h>
#include <cstdio>
#include <cstring>
#include "common.h"
#include <de/memory.h>
#include "g_common.h"
#include "p_saveg.h"
#include "d_net.h"
#include "dmu_lib.h"
#include "fi_lib.h"
#include "p_map.h"
#include "p_mapsetup.h"
#include "p_player.h"
#include "p_inventory.h"
#include "am_map.h"
#include "p_tick.h"
#include "p_actor.h"
#include "p_ceiling.h"
#include "p_door.h"
#include "p_floor.h"
#include "p_plat.h"
#include "p_scroll.h"
#include "p_switch.h"
#include "hu_log.h"
#if __JHERETIC__ || __JHEXEN__
#include "hu_inventory.h"
#endif
#include "r_common.h"
#include "api_materialarchive.h"
#include "p_savedef.h"
#define MAX_HUB_MAPS 99
#define FF_FULLBRIGHT 0x8000 ///< Used to be a flag in thing->frame.
#define FF_FRAMEMASK 0x7fff
typedef struct playerheader_s {
int numPowers;
int numKeys;
int numFrags;
int numWeapons;
int numAmmoTypes;
int numPSprites;
#if __JDOOM64__ || __JHERETIC__ || __JHEXEN__
int numInvItemTypes;
#endif
#if __JHEXEN__
int numArmorTypes;
#endif
} playerheader_t;
// Thinker Save flags
#define TSF_SERVERONLY 0x01 ///< Only saved by servers.
typedef struct thinkerinfo_s {
thinkerclass_t thinkclass;
thinkfunc_t function;
int flags;
WriteThinkerFunc writeFunc;
ReadThinkerFunc readFunc;
size_t size;
} thinkerinfo_t;
typedef enum sectorclass_e {
sc_normal,
sc_ploff, ///< plane offset
#if !__JHEXEN__
sc_xg1,
#endif
NUM_SECTORCLASSES
} sectorclass_t;
typedef enum lineclass_e {
lc_normal,
#if !__JHEXEN__
lc_xg1,
#endif
NUM_LINECLASSES
} lineclass_t;
static bool recogniseGameState(Str const *path, SaveInfo *info);
static void SV_WriteMobj(mobj_t const *mobj);
static int SV_ReadMobj(thinker_t *th);
static void SV_WriteCeiling(ceiling_t const *ceiling);
static int SV_ReadCeiling(ceiling_t *ceiling);
static void SV_WriteDoor(door_t const *door);
static int SV_ReadDoor(door_t *door);
static void SV_WriteFloor(floor_t const *floor);
static int SV_ReadFloor(floor_t *floor);
static void SV_WritePlat(plat_t const *plat);
static int SV_ReadPlat(plat_t *plat);
static void SV_WriteMaterialChanger(materialchanger_t const *mchanger);
static int SV_ReadMaterialChanger(materialchanger_t *mchanger);
#if __JHEXEN__
static void SV_WriteLight(light_t const *light);
static int SV_ReadLight(light_t *light);
static void SV_WritePhase(phase_t const *phase);
static int SV_ReadPhase(phase_t *phase);
static void SV_WriteScript(acs_t const *script);
static int SV_ReadScript(acs_t *script);
static void SV_WriteDoorPoly(polydoor_t const *polydoor);
static int SV_ReadDoorPoly(polydoor_t *polydoor);
static void SV_WriteMovePoly(polyevent_t const *movepoly);
static int SV_ReadMovePoly(polyevent_t *movepoly);
static void SV_WriteRotatePoly(polyevent_t const *rotatepoly);
static int SV_ReadRotatePoly(polyevent_t *rotatepoly);
static void SV_WritePillar(pillar_t const *pillar);
static int SV_ReadPillar(pillar_t *pillar);
static void SV_WriteFloorWaggle(waggle_t const *floorwaggle);
static int SV_ReadFloorWaggle(waggle_t *floorwaggle);
#else
static void SV_WriteFlash(lightflash_t const *flash);
static int SV_ReadFlash(lightflash_t *flash);
static void SV_WriteStrobe(strobe_t const *strobe);
static int SV_ReadStrobe(strobe_t *strobe);
static void SV_WriteGlow(glow_t const *glow);
static int SV_ReadGlow(glow_t *glow);
# if __JDOOM__ || __JDOOM64__
static void SV_WriteFlicker(fireflicker_t const *flicker);
static int SV_ReadFlicker(fireflicker_t *flicker);
# endif
# if __JDOOM64__
static void SV_WriteBlink(lightblink_t const *flicker);
static int SV_ReadBlink(lightblink_t *flicker);
# endif
#endif
static void SV_WriteScroll(scroll_t const *scroll);
static int SV_ReadScroll(scroll_t *scroll);
#if __JHEXEN__
static void readMapState(Str const *path);
#else
static void readMapState();
#endif
static bool inited = false;
static int cvarLastSlot; // -1 = Not yet loaded/saved in this game session.
static int cvarQuickSlot; // -1 = Not yet chosen/determined.
static SaveInfo **saveInfo;
static SaveInfo *autoSaveInfo;
#if __JHEXEN__
static SaveInfo *baseSaveInfo;
#endif
static SaveInfo *nullSaveInfo;
#if __JHEXEN__
static int mapVersion;
#endif
static saveheader_t const *hdr;
static playerheader_t playerHeader;
static boolean playerHeaderOK;
static mobj_t **thingArchive;
static uint thingArchiveSize;
static bool thingArchiveExcludePlayers;
static int saveToRealPlayerNum[MAXPLAYERS];
#if __JHEXEN__
static targetplraddress_t *targetPlayerAddrs;
static byte *saveBuffer;
#else
static int numSoundTargets;
#endif
static MaterialArchive *materialArchive;
static thinkerinfo_t thinkerInfo[] = {
{
TC_MOBJ,
(thinkfunc_t) P_MobjThinker,
TSF_SERVERONLY,
(WriteThinkerFunc) SV_WriteMobj,
(ReadThinkerFunc) SV_ReadMobj,
sizeof(mobj_t)
},
#if !__JHEXEN__
{
TC_XGMOVER,
(thinkfunc_t) XS_PlaneMover,
0,
(WriteThinkerFunc) SV_WriteXGPlaneMover,
(ReadThinkerFunc) SV_ReadXGPlaneMover,
sizeof(xgplanemover_t)
},
#endif
{
TC_CEILING,
T_MoveCeiling,
0,
(WriteThinkerFunc) SV_WriteCeiling,
(ReadThinkerFunc) SV_ReadCeiling,
sizeof(ceiling_t)
},
{
TC_DOOR,
T_Door,
0,
(WriteThinkerFunc) SV_WriteDoor,
(ReadThinkerFunc) SV_ReadDoor,
sizeof(door_t)
},
{
TC_FLOOR,
T_MoveFloor,
0,
(WriteThinkerFunc) SV_WriteFloor,
(ReadThinkerFunc) SV_ReadFloor,
sizeof(floor_t)
},
{
TC_PLAT,
T_PlatRaise,
0,
(WriteThinkerFunc) SV_WritePlat,
(ReadThinkerFunc) SV_ReadPlat,
sizeof(plat_t)
},
#if __JHEXEN__
{
TC_INTERPRET_ACS,
(thinkfunc_t) T_InterpretACS,
0,
(WriteThinkerFunc)SV_WriteScript,
(ReadThinkerFunc)SV_ReadScript,
sizeof(acs_t)
},
{
TC_FLOOR_WAGGLE,
(thinkfunc_t) T_FloorWaggle,
0,
(WriteThinkerFunc)SV_WriteFloorWaggle,
(ReadThinkerFunc)SV_ReadFloorWaggle,
sizeof(waggle_t)
},
{
TC_LIGHT,
(thinkfunc_t) T_Light,
0,
(WriteThinkerFunc)SV_WriteLight,
(ReadThinkerFunc)SV_ReadLight,
sizeof(light_t)
},
{
TC_PHASE,
(thinkfunc_t) T_Phase,
0,
(WriteThinkerFunc)SV_WritePhase,
(ReadThinkerFunc)SV_ReadPhase,
sizeof(phase_t)
},
{
TC_BUILD_PILLAR,
(thinkfunc_t) T_BuildPillar,
0,
(WriteThinkerFunc)SV_WritePillar,
(ReadThinkerFunc)SV_ReadPillar,
sizeof(pillar_t)
},
{
TC_ROTATE_POLY,
T_RotatePoly,
0,
(WriteThinkerFunc)SV_WriteRotatePoly,
(ReadThinkerFunc)SV_ReadRotatePoly,
sizeof(polyevent_t)
},
{
TC_MOVE_POLY,
T_MovePoly,
0,
(WriteThinkerFunc)SV_WriteMovePoly,
(ReadThinkerFunc)SV_ReadMovePoly,
sizeof(polyevent_t)
},
{
TC_POLY_DOOR,
T_PolyDoor,
0,
(WriteThinkerFunc)SV_WriteDoorPoly,
(ReadThinkerFunc)SV_ReadDoorPoly,
sizeof(polydoor_t)
},
#else
{
TC_FLASH,
(thinkfunc_t) T_LightFlash,
0,
(WriteThinkerFunc) SV_WriteFlash,
(ReadThinkerFunc) SV_ReadFlash,
sizeof(lightflash_t)
},
{
TC_STROBE,
(thinkfunc_t) T_StrobeFlash,
0,
(WriteThinkerFunc) SV_WriteStrobe,
(ReadThinkerFunc) SV_ReadStrobe,
sizeof(strobe_t)
},
{
TC_GLOW,
(thinkfunc_t) T_Glow,
0,
(WriteThinkerFunc) SV_WriteGlow,
(ReadThinkerFunc) SV_ReadGlow,
sizeof(glow_t)
},
# if __JDOOM__ || __JDOOM64__
{
TC_FLICKER,
(thinkfunc_t) T_FireFlicker,
0,
(WriteThinkerFunc) SV_WriteFlicker,
(ReadThinkerFunc) SV_ReadFlicker,
sizeof(fireflicker_t)
},
# endif
# if __JDOOM64__
{
TC_BLINK,
(thinkfunc_t) T_LightBlink,
0,
(WriteThinkerFunc) SV_WriteBlink,
(ReadThinkerFunc) SV_ReadBlink,
sizeof(lightblink_t)
},
# endif
#endif
{
TC_MATERIALCHANGER,
T_MaterialChanger,
0,
(WriteThinkerFunc) SV_WriteMaterialChanger,
(ReadThinkerFunc) SV_ReadMaterialChanger,
sizeof(materialchanger_t)
},
{
TC_SCROLL,
(thinkfunc_t) T_Scroll,
0,
(WriteThinkerFunc) SV_WriteScroll,
(ReadThinkerFunc) SV_ReadScroll,
sizeof(scroll_t)
},
{ TC_NULL, NULL, 0, NULL, NULL, 0 }
};
void SV_Register()
{
#if !__JHEXEN__
C_VAR_BYTE("game-save-auto-loadonreborn", &cfg.loadAutoSaveOnReborn, 0, 0, 1);
#endif
C_VAR_BYTE("game-save-confirm", &cfg.confirmQuickGameSave, 0, 0, 1);
C_VAR_BYTE("game-save-confirm-loadonreborn",&cfg.confirmRebornLoad, 0, 0, 1);
C_VAR_BYTE("game-save-last-loadonreborn", &cfg.loadLastSaveOnReborn, 0, 0, 1);
C_VAR_INT ("game-save-last-slot", &cvarLastSlot, CVF_NO_MIN|CVF_NO_MAX|CVF_NO_ARCHIVE|CVF_READ_ONLY, 0, 0);
C_VAR_INT ("game-save-quick-slot", &cvarQuickSlot, CVF_NO_MAX|CVF_NO_ARCHIVE, -1, 0);
// Aliases for obsolete cvars:
C_VAR_BYTE("menu-quick-ask", &cfg.confirmQuickGameSave, 0, 0, 1);
}
/**
* Compose the (possibly relative) path to the game-save associated
* with the logical save @a slot.
*
* @param slot Logical save slot identifier.
* @param map If @c >= 0 include this logical map index in the composed path.
* @return The composed path if reachable (else a zero-length string).
*/
static AutoStr *composeGameSavePathForSlot2(int slot, int map)
{
DENG_ASSERT(inited);
AutoStr *path = AutoStr_NewStd();
// A valid slot?
if(!SV_IsValidSlot(slot)) return path;
// Do we have a valid path?
if(!F_MakePath(SV_SavePath())) return path;
// Compose the full game-save path and filename.
if(map >= 0)
{
Str_Appendf(path, "%s" SAVEGAMENAME "%i%02i." SAVEGAMEEXTENSION, SV_SavePath(), slot, map);
}
else
{
Str_Appendf(path, "%s" SAVEGAMENAME "%i." SAVEGAMEEXTENSION, SV_SavePath(), slot);
}
F_TranslatePath(path, path);
return path;
}
static AutoStr *composeGameSavePathForSlot(int slot)
{
return composeGameSavePathForSlot2(slot, -1);
}
#if !__JHEXEN__
/**
* Compose the (possibly relative) path to the game-save associated
* with @a gameId.
*
* @param gameId Unique game identifier.
* @return File path to the reachable save directory. If the game-save path
* is unreachable then a zero-length string is returned instead.
*/
static AutoStr *composeGameSavePathForClientGameId(uint gameId)
{
AutoStr *path = AutoStr_NewStd();
// Do we have a valid path?
if(!F_MakePath(SV_ClientSavePath())) return path; // return zero-length string.
// Compose the full game-save path and filename.
Str_Appendf(path, "%s" CLIENTSAVEGAMENAME "%08X." SAVEGAMEEXTENSION, SV_ClientSavePath(), gameId);
F_TranslatePath(path, path);
return path;
}
#endif
static void clearSaveInfo()
{
if(saveInfo)
{
for(int i = 0; i < NUMSAVESLOTS; ++i)
{
SaveInfo_Delete(saveInfo[i]);
}
M_Free(saveInfo); saveInfo = 0;
}
if(autoSaveInfo)
{
SaveInfo_Delete(autoSaveInfo); autoSaveInfo = 0;
}
#if __JHEXEN__
if(baseSaveInfo)
{
SaveInfo_Delete(baseSaveInfo); baseSaveInfo = 0;
}
#endif
if(nullSaveInfo)
{
SaveInfo_Delete(nullSaveInfo); nullSaveInfo = 0;
}
}
static void updateSaveInfo(Str const *path, SaveInfo *info)
{
if(!info) return;
if(!path || Str_IsEmpty(path))
{
// The save path cannot be accessed for some reason. Perhaps its a
// network path? Clear the info for this slot.
SaveInfo_SetName(info, 0);
SaveInfo_SetGameId(info, 0);
return;
}
// Is this a recognisable save state?
if(!recogniseGameState(path, info))
{
// Clear the info for this slot.
SaveInfo_SetName(info, 0);
SaveInfo_SetGameId(info, 0);
return;
}
// Ensure we have a valid name.
if(Str_IsEmpty(SaveInfo_Name(info)))
{
SaveInfo_SetName(info, AutoStr_FromText("UNNAMED"));
}
}
/// Re-build game-save info by re-scanning the save paths and populating the list.
static void buildSaveInfo()
{
DENG_ASSERT(inited);
if(!saveInfo)
{
// Not yet been here. We need to allocate and initialize the game-save info list.
saveInfo = reinterpret_cast<SaveInfo **>(M_Malloc(NUMSAVESLOTS * sizeof(*saveInfo)));
// Initialize.
for(int i = 0; i < NUMSAVESLOTS; ++i)
{
saveInfo[i] = SaveInfo_New();
}
autoSaveInfo = SaveInfo_New();
#if __JHEXEN__
baseSaveInfo = SaveInfo_New();
#endif
nullSaveInfo = SaveInfo_New();
}
/// Scan the save paths and populate the list.
/// @todo We should look at all files on the save path and not just those
/// which match the default game-save file naming convention.
for(int i = 0; i < NUMSAVESLOTS; ++i)
{
SaveInfo *info = saveInfo[i];
updateSaveInfo(composeGameSavePathForSlot(i), info);
}
updateSaveInfo(composeGameSavePathForSlot(AUTO_SLOT), autoSaveInfo);
#if __JHEXEN__
updateSaveInfo(composeGameSavePathForSlot(BASE_SLOT), baseSaveInfo);
#endif
}
/// Given a logical save slot identifier retrieve the assciated game-save info.
static SaveInfo *findSaveInfoForSlot(int slot)
{
DENG_ASSERT(inited);
if(!SV_IsValidSlot(slot)) return nullSaveInfo;
// On first call - automatically build and populate game-save info.
if(!saveInfo)
{
buildSaveInfo();
}
// Retrieve the info for this slot.
if(slot == AUTO_SLOT) return autoSaveInfo;
#if __JHEXEN__
if(slot == BASE_SLOT) return baseSaveInfo;
#endif
return saveInfo[slot];
}
static void replaceSaveInfo(int slot, SaveInfo *newInfo)
{
DENG_ASSERT(SV_IsValidSlot(slot));
SaveInfo **destAdr;
if(slot == AUTO_SLOT)
{
destAdr = &autoSaveInfo;
}
#if __JHEXEN__
else if(slot == BASE_SLOT)
{
destAdr = &baseSaveInfo;
}
#endif
else
{
destAdr = &saveInfo[slot];
}
if(*destAdr) SaveInfo_Delete(*destAdr);
*destAdr = newInfo;
}
AutoStr *SV_ComposeSlotIdentifier(int slot)
{
AutoStr *str = AutoStr_NewStd();
if(slot < 0) return Str_Set(str, "(invalid slot)");
if(slot == AUTO_SLOT) return Str_Set(str, "<auto>");
#if __JHEXEN__
if(slot == BASE_SLOT) return Str_Set(str, "<base>");
#endif
return Str_Appendf(str, "%i", slot);
}
/**
* Determines whether to announce when the specified @a slot is cleared.
*/
static bool announceOnClearingSlot(int slot)
{
#if _DEBUG
return true; // Always.
#endif
#if __JHEXEN__
return (slot != AUTO_SLOT && slot != BASE_SLOT);
#else
return (slot != AUTO_SLOT);
#endif
}
void SV_ClearSlot(int slot)
{
DENG_ASSERT(inited);
if(!SV_IsValidSlot(slot)) return;
if(announceOnClearingSlot(slot))
{
AutoStr *ident = SV_ComposeSlotIdentifier(slot);
Con_Message("Clearing save slot %s", Str_Text(ident));
}
for(int i = 0; i < MAX_HUB_MAPS; ++i)
{
AutoStr *path = composeGameSavePathForSlot2(slot, i);
SV_RemoveFile(path);
}
AutoStr *path = composeGameSavePathForSlot(slot);
SV_RemoveFile(path);
// Update save info for this slot.
updateSaveInfo(path, findSaveInfoForSlot(slot));
}
boolean SV_IsValidSlot(int slot)
{
if(slot == AUTO_SLOT) return true;
#if __JHEXEN__
if(slot == BASE_SLOT) return true;
#endif
return (slot >= 0 && slot < NUMSAVESLOTS);
}
boolean SV_IsUserWritableSlot(int slot)
{
if(slot == AUTO_SLOT) return false;
#if __JHEXEN__
if(slot == BASE_SLOT) return false;
#endif
return SV_IsValidSlot(slot);
}
static void SV_SaveInfo_Read(SaveInfo *info)
{
Reader *svReader = SV_NewReader();
#if __JHEXEN__
// Read the magic byte to determine the high-level format.
int magic = Reader_ReadInt32(svReader);
SV_HxSavePtr()->b -= 4; // Rewind the stream.
if((!IS_NETWORK_CLIENT && magic != MY_SAVE_MAGIC) ||
( IS_NETWORK_CLIENT && magic != MY_CLIENT_SAVE_MAGIC))
{
// Perhaps the old v9 format?
SaveInfo_Read_Hx_v9(info, svReader);
}
else
#endif
{
SaveInfo_Read(info, svReader);
}
Reader_Delete(svReader);
}
static bool recogniseNativeState(Str const *path, SaveInfo *info)
{
DENG_ASSERT(path != 0 && info != 0);
if(!SV_ExistingFile(path)) return false;
#if __JHEXEN__
/// @todo Do not buffer the whole file.
byte *saveBuffer;
if(!M_ReadFile(Str_Text(path), (char**)&saveBuffer))
return false;
// Set the save pointer.
SV_HxSavePtr()->b = saveBuffer;
#else
if(!SV_OpenFile(path, "rp"))
return false;
#endif
SV_SaveInfo_Read(info);
#if __JHEXEN__
Z_Free(saveBuffer);
#else
SV_CloseFile();
#endif
// Magic must match.
if(info->header.magic != MY_SAVE_MAGIC &&
info->header.magic != MY_CLIENT_SAVE_MAGIC) return false;
/*
* Check for unsupported versions.
*/
// A future version?
if(info->header.version > MY_SAVE_VERSION) return false;
#if __JHEXEN__
// We are incompatible with v3 saves due to an invalid test used to determine
// present sides (ver3 format's sides contain chunks of junk data).
if(info->header.version == 3) return false;
#endif
return true;
}
static bool recogniseGameState(Str const *path, SaveInfo *info)
{
if(path && info)
{
if(recogniseNativeState(path, info))
return true;
// Perhaps an original game state?
#if __JDOOM__
if(SV_RecogniseState_Dm_v19(Str_Text(path), info))
return true;
#endif
#if __JHERETIC__
if(SV_RecogniseState_Hr_v13(Str_Text(path), info))
return true;
#endif
}
return false;
}
SaveInfo *SV_SaveInfoForSlot(int slot)
{
DENG_ASSERT(inited);
return findSaveInfoForSlot(slot);
}
void SV_UpdateAllSaveInfo()
{
DENG_ASSERT(inited);
buildSaveInfo();
}
int SV_ParseSlotIdentifier(char const *str)
{
// Try game-save name match.
int slot = SV_SlotForSaveName(str);
if(slot >= 0) return slot;
// Try keyword identifiers.
if(!stricmp(str, "last") || !stricmp(str, "<last>"))
{
return Con_GetInteger("game-save-last-slot");
}
if(!stricmp(str, "quick") || !stricmp(str, "<quick>"))
{
return Con_GetInteger("game-save-quick-slot");
}
if(!stricmp(str, "auto") || !stricmp(str, "<auto>"))
{
return AUTO_SLOT;
}
// Try logical slot identifier.
if(M_IsStringValidInt(str))
{
return atoi(str);
}
// Unknown/not found.
return -1;
}
int SV_SlotForSaveName(char const *name)
{
DENG_ASSERT(inited);
int saveSlot = -1;
if(name && name[0])
{
// On first call - automatically build and populate game-save info.
if(!saveInfo)
{
buildSaveInfo();
}
int i = 0;
do
{
SaveInfo *info = saveInfo[i];
if(!Str_CompareIgnoreCase(SaveInfo_Name(info), name))
{
// This is the one!
saveSlot = i;
}
} while(-1 == saveSlot && ++i < NUMSAVESLOTS);
}
return saveSlot;
}
boolean SV_IsSlotUsed(int slot)
{
DENG_ASSERT(inited);
if(SV_ExistingFile(composeGameSavePathForSlot(slot)))
{
SaveInfo *info = SV_SaveInfoForSlot(slot);
return SaveInfo_IsLoadable(info);
}
return false;
}
#if __JHEXEN__
boolean SV_HxHaveMapSaveForSlot(int slot, uint map)
{
AutoStr *path = composeGameSavePathForSlot2(slot, (int)map+1);
if(!path || Str_IsEmpty(path)) return false;
return SV_ExistingFile(path);
}
#endif
void SV_CopySlot(int sourceSlot, int destSlot)
{
DENG_ASSERT(inited);
if(!SV_IsValidSlot(sourceSlot))
{
#if _DEBUG
Con_Message("Warning: SV_CopySlot: Source slot %i invalid, save game not copied.", sourceSlot);
#endif
return;
}
if(!SV_IsValidSlot(destSlot))
{
#if _DEBUG
Con_Message("Warning: SV_CopySlot: Dest slot %i invalid, save game not copied.", destSlot);
#endif
return;
}
// Clear all save files at destination slot.
SV_ClearSlot(destSlot);
AutoStr *src, *dst;
for(int i = 0; i < MAX_HUB_MAPS; ++i)
{
src = composeGameSavePathForSlot2(sourceSlot, i);
dst = composeGameSavePathForSlot2(destSlot, i);
SV_CopyFile(src, dst);
}
src = composeGameSavePathForSlot(sourceSlot);
dst = composeGameSavePathForSlot(destSlot);
SV_CopyFile(src, dst);
// Copy saveinfo too.
replaceSaveInfo(destSlot, SaveInfo_NewCopy(findSaveInfoForSlot(sourceSlot)));
}
#if __JHEXEN__
void SV_HxInitBaseSlot()
{
SV_ClearSlot(BASE_SLOT);
}
#endif
uint SV_GenerateGameId()
{
return Timer_RealMilliseconds() + (mapTime << 24);
}
/**
* @return Thinker info for the given thinker.
*/
static thinkerinfo_t *infoForThinker(thinker_t *th)
{
if(!th) return 0;
thinkerinfo_t *thInfo = thinkerInfo;
while(thInfo->thinkclass != TC_NULL)
{
if(thInfo->function == th->function)
return thInfo;
thInfo++;
}
return 0;
}
static void initThingArchiveForLoad(uint size)
{
thingArchiveSize = size;
thingArchive = reinterpret_cast<mobj_t **>(M_Calloc(thingArchiveSize * sizeof(*thingArchive)));
}
struct countMobjThinkersToArchiveParms
{
uint count;
bool excludePlayers;
};
static int countMobjThinkersToArchive(thinker_t *th, void *context)
{
countMobjThinkersToArchiveParms *parms = (countMobjThinkersToArchiveParms *) context;
mobj_t *mo = (mobj_t *) th;
if(!(mo->player && parms->excludePlayers))
{
parms->count++;
}
return false; // Continue iteration.
}
static void initThingArchiveForSave(bool excludePlayers = false)
{
// Count the number of things we'll be writing.
countMobjThinkersToArchiveParms parms;
parms.count = 0;
parms.excludePlayers = !excludePlayers;
Thinker_Iterate((thinkfunc_t) P_MobjThinker, countMobjThinkersToArchive, &parms);
thingArchiveSize = parms.count;
thingArchive = reinterpret_cast<mobj_t **>(M_Calloc(thingArchiveSize * sizeof(*thingArchive)));
thingArchiveExcludePlayers = excludePlayers;
}
/**
* Used by the read code when mobjs are read.
*/
static void insertThingInArchive(mobj_t *mo, int num)
{
#if __JHEXEN__
if(mapVersion >= 4)
#endif
{
num -= 1;
}
if(num < 0) return; // Does this ever occur?
DENG_ASSERT(thingArchive != 0);
DENG_ASSERT(num >= 0 && (unsigned)num < thingArchiveSize);
thingArchive[num] = mo;
}
/**
* Free the thing archive. Called when load is complete.
*/
static void clearThingArchive()
{
if(thingArchive)
{
M_Free(thingArchive); thingArchive = 0;
thingArchiveSize = 0;
}
}
/**
* Called by the write code to get archive numbers.
* If the mobj is already archived, the existing number is returned.
* Number zero is not used.
*/
#if __JHEXEN__
int SV_ThingArchiveNum(mobj_t *mo)
#else
unsigned short SV_ThingArchiveNum(mobj_t* mo)
#endif
{
DENG_ASSERT(inited);
// We only archive valid mobj thinkers.
if(!mo || ((thinker_t *) mo)->function != (thinkfunc_t) P_MobjThinker)
return 0;
#if __JHEXEN__
if(mo->player && thingArchiveExcludePlayers)
return MOBJ_XX_PLAYER;
#endif
DENG_ASSERT(thingArchive != 0);
uint firstEmpty = 0;
boolean found = false;
for(uint i = 0; i < thingArchiveSize; ++i)
{
if(!thingArchive[i] && !found)
{
firstEmpty = i;