/
p_saveio.c
742 lines (643 loc) · 16.6 KB
/
p_saveio.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
/**\file p_saveio.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*
* 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
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "common.h"
#include "dmu_lib.h"
#include "p_mapsetup.h"
#include "p_saveg.h"
#include "p_saveio.h"
#include "p_savedef.h"
#include "materialarchive.h"
#define MAX_HUB_MAPS 99
static boolean inited;
static LZFILE* savefile;
static ddstring_t savePath; // e.g., "savegame/"
#if !__JHEXEN__
static ddstring_t clientSavePath; // e.g., "savegame/client/"
#endif
static gamesaveinfo_t* gameSaveInfo;
#if __JHEXEN__
static saveptr_t saveptr;
#endif
static void errorIfNotInited(const char* callerName)
{
if(inited) return;
Con_Error("%s: Savegame I/O is not presently initialized.", callerName);
// Unreachable. Prevents static analysers from getting rather confused, poor things.
exit(1);
}
static int removeFile(const ddstring_t* path)
{
if(!path) return 1;
return remove(Str_Text(path));
}
static void copyFile(const ddstring_t* srcPath, const ddstring_t* destPath)
{
size_t length;
char* buffer;
LZFILE* outf;
if(!srcPath || !destPath) return;
#if __JHEXEN__
if(!SV_ExistingFile(Str_Text(srcPath))) return;
#endif
length = M_ReadFile(Str_Text(srcPath), &buffer);
if(0 == length)
{
Con_Message("Warning: copyFile: Failed opening \"%s\" for reading.\n", Str_Text(srcPath));
return;
}
outf = lzOpen((char*)Str_Text(destPath), "wp");
if(outf)
{
lzWrite(buffer, length, outf);
lzClose(outf);
}
Z_Free(buffer);
}
/// @return Possibly relative saved game directory. Does not need to be free'd.
static AutoStr* composeSaveDir(void)
{
AutoStr* dir = AutoStr_NewStd();
if(CommandLine_CheckWith("-savedir", 1))
{
Str_Set(dir, CommandLine_Next());
// Add a trailing backslash is necessary.
if(Str_RAt(dir, 0) != '/')
Str_AppendChar(dir, '/');
return dir;
}
// Use the default path.
{ GameInfo gameInfo;
if(DD_GameInfo(&gameInfo))
{
Str_Appendf(dir, SAVEGAME_DEFAULT_DIR "/%s/", gameInfo.identityKey);
return dir;
}}
Con_Error("composeSaveDir: Error, failed retrieving GameInfo.");
exit(1); // Unreachable.
}
/**
* 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 @c NULL). Does not need to be free'd.
*/
static AutoStr* composeGameSavePathForSlot2(int slot, int map)
{
AutoStr* path;
assert(inited);
// A valid slot?
if(!SV_IsValidSlot(slot)) return NULL;
// Do we have a valid path?
if(!F_MakePath(SV_SavePath())) return NULL;
// Compose the full game-save path and filename.
path = AutoStr_NewStd();
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);
}
void SV_InitIO(void)
{
Str_Init(&savePath);
#if !__JHEXEN__
Str_Init(&clientSavePath);
#endif
gameSaveInfo = NULL;
inited = true;
savefile = 0;
}
void SV_ShutdownIO(void)
{
inited = false;
SV_CloseFile();
if(gameSaveInfo)
{
int i;
for(i = 0; i < NUMSAVESLOTS; ++i)
{
gamesaveinfo_t* info = &gameSaveInfo[i];
Str_Free(&info->filePath);
Str_Free(&info->name);
}
free(gameSaveInfo); gameSaveInfo = NULL;
}
Str_Free(&savePath);
#if !__JHEXEN__
Str_Free(&clientSavePath);
#endif
}
const char* SV_SavePath(void)
{
return Str_Text(&savePath);
}
#ifdef __JHEXEN__
saveptr_t* SV_HxSavePtr(void)
{
return &saveptr;
}
#endif
#if !__JHEXEN__
const char* SV_ClientSavePath(void)
{
return Str_Text(&clientSavePath);
}
#endif
// Compose and create the saved game directories.
void SV_ConfigureSavePaths(void)
{
assert(inited);
{
AutoStr* saveDir = composeSaveDir();
boolean savePathExists;
Str_Set(&savePath, Str_Text(saveDir));
#if !__JHEXEN__
Str_Clear(&clientSavePath); Str_Appendf(&clientSavePath, "%sclient/", Str_Text(saveDir));
#endif
// Ensure that these paths exist.
savePathExists = F_MakePath(Str_Text(&savePath));
#if !__JHEXEN__
if(!F_MakePath(Str_Text(&clientSavePath)))
savePathExists = false;
#endif
if(!savePathExists)
Con_Message("Warning:configureSavePaths: Failed to locate \"%s\"\nPerhaps it could "
"not be created (insufficent permissions?). Saving will not be possible.\n",
Str_Text(&savePath));
}
}
LZFILE* SV_File(void)
{
return savefile;
}
LZFILE* SV_OpenFile(const char* fileName, const char* mode)
{
assert(savefile == 0);
savefile = lzOpen((char*)fileName, (char*)mode);
return savefile;
}
void SV_CloseFile(void)
{
if(savefile)
{
lzClose(savefile);
savefile = 0;
}
}
#if __JHEXEN__
boolean SV_ExistingFile(char* name)
{
FILE* fp;
if((fp = fopen(name, "rb")) != NULL)
{
fclose(fp);
return true;
}
else
{
return false;
}
}
#endif
void SV_ClearSaveSlot(int slot)
{
AutoStr* path;
errorIfNotInited("SV_ClearSaveSlot");
if(!SV_IsValidSlot(slot)) return;
{ int i;
for(i = 0; i < MAX_HUB_MAPS; ++i)
{
path = composeGameSavePathForSlot2(slot, i);
removeFile(path);
}}
path = composeGameSavePathForSlot(slot);
removeFile(path);
}
boolean SV_IsValidSlot(int slot)
{
#if __JHEXEN__
if(slot == REBORN_SLOT) return true;
#endif
return (slot >= 0 && slot < NUMSAVESLOTS);
}
static boolean readGameSaveInfoFromFile(const ddstring_t* savePath, ddstring_t* name)
{
boolean found = false;
#if __JHEXEN__
LZFILE* fp;
#endif
assert(inited && savePath && name);
if(Str_IsEmpty(savePath)) return false;
#if __JHEXEN__
fp = lzOpen(Str_Text(savePath), "rp");
if(fp)
{
// Read the header.
char versionText[HXS_VERSION_TEXT_LENGTH];
char nameBuffer[SAVESTRINGSIZE];
lzRead(nameBuffer, SAVESTRINGSIZE, fp);
lzRead(versionText, HXS_VERSION_TEXT_LENGTH, fp);
lzClose(fp); fp = NULL;
if(!strncmp(versionText, HXS_VERSION_TEXT, 8))
{
SV_SetSaveVersion(atoi(&versionText[8]));
if(SV_SaveVersion() <= MY_SAVE_VERSION)
{
Str_Set(name, nameBuffer);
found = true;
}
}
}
#else
if(SV_OpenFile(Str_Text(savePath), "rp"))
{
saveheader_t* hdr = SV_SaveHeader();
// Read the header.
lzRead(hdr, sizeof(*hdr), SV_File());
SV_CloseFile();
if(MY_SAVE_MAGIC == hdr->magic)
{
Str_Set(name, hdr->name);
found = true;
}
}
// If not found or not recognized try other supported formats.
#if !__JDOOM64__
if(!found)
{
// Perhaps a DOOM(2).EXE v19 saved game?
if(SV_OpenFile(Str_Text(savePath), "r"))
{
char nameBuffer[SAVESTRINGSIZE];
lzRead(nameBuffer, SAVESTRINGSIZE, SV_File());
nameBuffer[SAVESTRINGSIZE - 1] = 0;
Str_Set(name, nameBuffer);
SV_CloseFile();
found = true;
}
}
# endif
#endif
// Ensure we have a non-empty name.
if(found && Str_IsEmpty(name))
{
Str_Set(name, "UNNAMED");
}
return found;
}
/// Re-build game-save info by re-scanning the save paths and populating the list.
static void buildGameSaveInfo(void)
{
int i;
assert(inited);
if(!gameSaveInfo)
{
// Not yet been here. We need to allocate and initialize the game-save info list.
gameSaveInfo = (gamesaveinfo_t*) malloc(NUMSAVESLOTS * sizeof(*gameSaveInfo));
if(!gameSaveInfo)
Con_Error("buildGameSaveInfo: Failed on allocation of %lu bytes for game-save info list.",
(unsigned long) (NUMSAVESLOTS * sizeof(*gameSaveInfo)));
// Initialize.
for(i = 0; i < NUMSAVESLOTS; ++i)
{
gamesaveinfo_t* info = &gameSaveInfo[i];
Str_Init(&info->filePath);
Str_Init(&info->name);
info->slot = i;
}
}
/// 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(i = 0; i < NUMSAVESLOTS; ++i)
{
gamesaveinfo_t* info = &gameSaveInfo[i];
Str_CopyOrClear(&info->filePath, composeGameSavePathForSlot(i));
if(Str_IsEmpty(&info->filePath))
{
// The save path cannot be accessed for some reason. Perhaps its a
// network path? Clear the info for this slot.
Str_Clear(&info->name);
continue;
}
if(!readGameSaveInfoFromFile(&info->filePath, &info->name))
{
// Not a valid save file.
Str_Clear(&info->filePath);
}
}
}
/// Given a logical save slot identifier retrieve the assciated game-save info.
static gamesaveinfo_t* findGameSaveInfoForSlot(int slot)
{
static gamesaveinfo_t invalidInfo = { { "" }, { "" }, -1 };
assert(inited);
if(slot >= 0 && slot < NUMSAVESLOTS)
{
// On first call - automatically build and populate game-save info.
if(!gameSaveInfo)
buildGameSaveInfo();
// Retrieve the info for this slot.
return &gameSaveInfo[slot];
}
return &invalidInfo;
}
boolean SV_IsGameSaveSlotUsed(int slot)
{
const gamesaveinfo_t* info;
errorIfNotInited("SV_IsGameSaveSlotUsed");
info = findGameSaveInfoForSlot(slot);
return !Str_IsEmpty(&info->filePath);
}
const gamesaveinfo_t* SV_GetGameSaveInfoForSlot(int slot)
{
errorIfNotInited("SV_GetGameSaveInfoForSlot");
return findGameSaveInfoForSlot(slot);
}
void SV_UpdateGameSaveInfo(void)
{
errorIfNotInited("SV_UpdateGameSaveInfo");
buildGameSaveInfo();
}
int SV_ParseGameSaveSlot(const char* str)
{
int slot;
// Try game-save name match.
slot = SV_FindGameSaveSlotForName(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");
}
// Try logical slot identifier.
if(M_IsStringValidInt(str))
{
return atoi(str);
}
// Unknown/not found.
return -1;
}
int SV_FindGameSaveSlotForName(const char* name)
{
int saveSlot = -1;
errorIfNotInited("SV_FindGameSaveSlotForName");
if(name && name[0])
{
int i = 0;
// On first call - automatically build and populate game-save info.
if(!gameSaveInfo)
{
buildGameSaveInfo();
}
do
{
const gamesaveinfo_t* info = &gameSaveInfo[i];
if(!Str_CompareIgnoreCase(&info->name, name))
{
// This is the one!
saveSlot = i;
}
} while(-1 == saveSlot && ++i < NUMSAVESLOTS);
}
return saveSlot;
}
boolean SV_GetGameSavePathForSlot(int slot, ddstring_t* path)
{
errorIfNotInited("SV_GetGameSavePathForSlot");
if(!path) return false;
Str_CopyOrClear(path, composeGameSavePathForSlot(slot));
return !Str_IsEmpty(path);
}
#if __JHEXEN__
boolean SV_GetGameSavePathForMapSlot(uint map, int slot, ddstring_t* path)
{
errorIfNotInited("SV_GetGameSavePathForMapSlot");
if(!path) return false;
Str_CopyOrClear(path, composeGameSavePathForSlot2(slot, (int)map));
return !Str_IsEmpty(path);
}
#endif
void SV_CopySaveSlot(int sourceSlot, int destSlot)
{
AutoStr* src, *dst;
errorIfNotInited("SV_CopySaveSlot");
if(!SV_IsValidSlot(sourceSlot))
{
#if _DEBUG
Con_Message("Warning: SV_CopySaveSlot: Source slot %i invalid, save game not copied.\n", sourceSlot);
#endif
return;
}
if(!SV_IsValidSlot(destSlot))
{
#if _DEBUG
Con_Message("Warning: SV_CopySaveSlot: Dest slot %i invalid, save game not copied.\n", destSlot);
#endif
return;
}
{ int i;
for(i = 0; i < MAX_HUB_MAPS; ++i)
{
src = composeGameSavePathForSlot2(sourceSlot, i);
dst = composeGameSavePathForSlot2(destSlot, i);
copyFile(src, dst);
}}
src = composeGameSavePathForSlot(sourceSlot);
dst = composeGameSavePathForSlot(destSlot);
copyFile(src, dst);
}
void SV_Write(const void* data, int len)
{
errorIfNotInited("SV_Write");
lzWrite((void*)data, len, savefile);
}
void SV_WriteByte(byte val)
{
errorIfNotInited("SV_WriteByte");
lzPutC(val, savefile);
}
#if __JHEXEN__
void SV_WriteShort(unsigned short val)
#else
void SV_WriteShort(short val)
#endif
{
errorIfNotInited("SV_WriteShort");
lzPutW(val, savefile);
}
#if __JHEXEN__
void SV_WriteLong(unsigned int val)
#else
void SV_WriteLong(long val)
#endif
{
errorIfNotInited("SV_WriteLong");
lzPutL(val, savefile);
}
void SV_WriteFloat(float val)
{
int32_t temp = 0;
assert(sizeof(val) == 4);
errorIfNotInited("SV_WriteFloat");
memcpy(&temp, &val, 4);
lzPutL(temp, savefile);
}
void SV_Read(void *data, int len)
{
errorIfNotInited("SV_Read");
#if __JHEXEN__
memcpy(data, saveptr.b, len);
saveptr.b += len;
#else
lzRead(data, len, savefile);
#endif
}
byte SV_ReadByte(void)
{
errorIfNotInited("SV_ReadByte");
#if __JHEXEN__
return (*saveptr.b++);
#else
return lzGetC(savefile);
#endif
}
short SV_ReadShort(void)
{
errorIfNotInited("SV_ReadShort");
#if __JHEXEN__
return (SHORT(*saveptr.w++));
#else
return lzGetW(savefile);
#endif
}
long SV_ReadLong(void)
{
errorIfNotInited("SV_ReadLong");
#if __JHEXEN__
return (LONG(*saveptr.l++));
#else
return lzGetL(savefile);
#endif
}
float SV_ReadFloat(void)
{
#if !__JHEXEN__
float returnValue = 0;
int32_t val;
#endif
errorIfNotInited("SV_ReadFloat");
#if __JHEXEN__
return (FLOAT(*saveptr.f++));
#else
val = lzGetL(savefile);
returnValue = 0;
assert(sizeof(float) == 4);
memcpy(&returnValue, &val, 4);
return returnValue;
#endif
}
static void swi8(Writer* w, char i)
{
if(!w) return;
SV_WriteByte(i);
}
static void swi16(Writer* w, short i)
{
if(!w) return;
SV_WriteShort(i);
}
static void swi32(Writer* w, int i)
{
if(!w) return;
SV_WriteLong(i);
}
static void swf(Writer* w, float i)
{
if(!w) return;
SV_WriteFloat(i);
}
static void swd(Writer* w, const char* data, int len)
{
if(!w) return;
SV_Write(data, len);
}
void SV_MaterialArchive_Write(MaterialArchive* arc)
{
Writer* svWriter = Writer_NewWithCallbacks(swi8, swi16, swi32, swf, swd);
MaterialArchive_Write(arc, svWriter);
Writer_Delete(svWriter);
}
static char sri8(Reader* r)
{
if(!r) return 0;
return SV_ReadByte();
}
static short sri16(Reader* r)
{
if(!r) return 0;
return SV_ReadShort();
}
static int sri32(Reader* r)
{
if(!r) return 0;
return SV_ReadLong();
}
static float srf(Reader* r)
{
if(!r) return 0;
return SV_ReadFloat();
}
static void srd(Reader* r, char* data, int len)
{
if(!r) return;
SV_Read(data, len);
}
void SV_MaterialArchive_Read(MaterialArchive* arc, int version)
{
Reader* svReader = Reader_NewWithCallbacks(sri8, sri16, sri32, srf, srd);
MaterialArchive_Read(arc, version, svReader);
Reader_Delete(svReader);
}