- use FileReader for savegame loading in the menu.

- simplify the check for existing files here, since a function for doing just that already existed.
This commit is contained in:
Christoph Oelckers 2017-12-02 14:10:42 +01:00
parent 2ba029ec8d
commit 4003e7ca11
3 changed files with 9 additions and 21 deletions

View file

@ -456,12 +456,6 @@ PNGHandle *M_VerifyPNG (FileReader *filer, bool takereader)
return NULL; return NULL;
} }
PNGHandle *M_VerifyPNG(FILE *file)
{
FileReader *fr = new FileReader(file);
return M_VerifyPNG(fr, true);
}
//========================================================================== //==========================================================================
// //
// M_FreePNG // M_FreePNG

View file

@ -90,7 +90,6 @@ struct PNGHandle
// each chunk is not done. If it is valid, you get a PNGHandle to pass to // each chunk is not done. If it is valid, you get a PNGHandle to pass to
// the following functions. // the following functions.
PNGHandle *M_VerifyPNG (FileReader *file, bool takereader = false); PNGHandle *M_VerifyPNG (FileReader *file, bool takereader = false);
PNGHandle *M_VerifyPNG (FILE *file);
// Finds a chunk in a PNG file. The file pointer will be positioned at the // Finds a chunk in a PNG file. The file pointer will be positioned at the
// beginning of the chunk data, and its length will be returned. A return // beginning of the chunk data, and its length will be returned. A return

View file

@ -236,8 +236,8 @@ void FSavegameManager::ReadSaveStrings()
} }
else // check for old formats. else // check for old formats.
{ {
FILE *file = fopen(filepath, "rb"); FileReader file;
if (file != nullptr) if (file.Open(filepath))
{ {
PNGHandle *png; PNGHandle *png;
char sig[16]; char sig[16];
@ -255,8 +255,7 @@ void FSavegameManager::ReadSaveStrings()
title[OLDSAVESTRINGSIZE] = 0; title[OLDSAVESTRINGSIZE] = 0;
if (nullptr != (png = M_VerifyPNG(&file, false)))
if (nullptr != (png = M_VerifyPNG(file)))
{ {
char *ver = M_GetPNGText(png, "ZDoom Save Version"); char *ver = M_GetPNGText(png, "ZDoom Save Version");
if (ver != nullptr) if (ver != nullptr)
@ -273,13 +272,13 @@ void FSavegameManager::ReadSaveStrings()
} }
else else
{ {
fseek(file, 0, SEEK_SET); file.Seek(0, SEEK_SET);
if (fread(sig, 1, 16, file) == 16) if (file.Read(sig, 16) == 16)
{ {
if (strncmp(sig, "ZDOOMSAVE", 9) == 0) if (strncmp(sig, "ZDOOMSAVE", 9) == 0)
{ {
if (fread(title, 1, OLDSAVESTRINGSIZE, file) == OLDSAVESTRINGSIZE) if (file.Read(title, OLDSAVESTRINGSIZE) == OLDSAVESTRINGSIZE)
{ {
addIt = true; addIt = true;
} }
@ -287,8 +286,8 @@ void FSavegameManager::ReadSaveStrings()
else else
{ {
memcpy(title, sig, 16); memcpy(title, sig, 16);
if (fread(title + 16, 1, OLDSAVESTRINGSIZE - 16, file) == OLDSAVESTRINGSIZE - 16 && if (file.Read(title + 16, OLDSAVESTRINGSIZE - 16) == OLDSAVESTRINGSIZE - 16 &&
fread(sig, 1, 16, file) == 16 && file.Read(sig, 16) == 16 &&
strncmp(sig, "ZDOOMSAVE", 9) == 0) strncmp(sig, "ZDOOMSAVE", 9) == 0)
{ {
addIt = true; addIt = true;
@ -306,7 +305,6 @@ void FSavegameManager::ReadSaveStrings()
node->SaveTitle = title; node->SaveTitle = title;
InsertSaveNode(node); InsertSaveNode(node);
} }
fclose(file);
} }
} }
} while (I_FindNext(filefirst, &c_file) == 0); } while (I_FindNext(filefirst, &c_file) == 0);
@ -418,17 +416,14 @@ void FSavegameManager::DoSave(int Selected, const char *savegamestring)
// Find an unused filename and save as that // Find an unused filename and save as that
FString filename; FString filename;
int i; int i;
FILE *test;
for (i = 0;; ++i) for (i = 0;; ++i)
{ {
filename = G_BuildSaveName("save", i); filename = G_BuildSaveName("save", i);
test = fopen(filename, "rb"); if (!FileExists(filename))
if (test == nullptr)
{ {
break; break;
} }
fclose(test);
} }
G_SaveGame(filename, savegamestring); G_SaveGame(filename, savegamestring);
} }