Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions TheForceEngine/TFE_FileSystem/filestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ bool FileStream::exists(const char* filename)
return res;
}

bool FileStream::renameFile(const char* oldName, const char* newName)
{
remove(newName);
int status = rename(oldName, newName);
if (status < 0) { return false; }
return true;
}

bool FileStream::open(const char* filename, AccessMode mode)
{
const char* modeStrings[] = { "rb", "wb", "rb+" };
Expand Down
1 change: 1 addition & 0 deletions TheForceEngine/TFE_FileSystem/filestream.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FileStream : public Stream
~FileStream();

bool exists(const char* filename);
bool renameFile(const char* oldName, const char* newName);
bool open(const char* filename, AccessMode mode);
bool open(const FilePath* filePath, AccessMode mode);
void close();
Expand Down
12 changes: 11 additions & 1 deletion TheForceEngine/TFE_Game/saveSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,27 @@ namespace TFE_SaveSystem

bool saveGame(const char* filename, const char* saveName)
{
// We first write the new save to a temporary file, then rename the temporary file.
// This way, if we are saving into an existing slot and something goes wrong during
// the save process, we won't overwrite a valid save with a corrupted save.

char tempFilePath[TFE_MAX_PATH];
char filePath[TFE_MAX_PATH];
string tempFilename = string(filename) + ".tmp";
sprintf(tempFilePath, "%s%s", s_gameSavePath, tempFilename.c_str());
sprintf(filePath, "%s%s", s_gameSavePath, filename);

bool ret = false;
FileStream stream;
if (stream.open(filePath, Stream::MODE_WRITE))
if (stream.open(tempFilePath, Stream::MODE_WRITE))
{
saveHeader(&stream, saveName);
ret = s_game->serializeGameState(&stream, filename, true);
stream.close();
}

stream.renameFile(tempFilePath, filePath);

return ret;
}

Expand Down